Skip to content

Port #[allow_internal_unsafe] to the new attribute system #144857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,11 @@ impl<S: Stage> AttributeParser<S> for MacroUseParser {
Some(AttributeKind::MacroUse { span: self.first_span?, arguments: self.state })
}
}

pub(crate) struct AllowInternalUnsafeParser;

impl<S: Stage> NoArgsAttributeParser<S> for AllowInternalUnsafeParser {
const PATH: &[Symbol] = &[sym::allow_internal_unsafe];
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
const CREATE: fn(Span) -> AttributeKind = |span| AttributeKind::AllowInternalUnsafe(span);
}
5 changes: 4 additions & 1 deletion compiler/rustc_attr_parsing/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ use crate::attributes::lint_helpers::{
AsPtrParser, AutomaticallyDerivedParser, PassByValueParser, PubTransparentParser,
};
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
use crate::attributes::macro_attrs::{MacroEscapeParser, MacroUseParser};
use crate::attributes::macro_attrs::{
AllowInternalUnsafeParser, MacroEscapeParser, MacroUseParser,
};
use crate::attributes::must_use::MustUseParser;
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
use crate::attributes::non_exhaustive::NonExhaustiveParser;
Expand Down Expand Up @@ -177,6 +179,7 @@ attribute_parsers!(
Single<SkipDuringMethodDispatchParser>,
Single<TransparencyParser>,
Single<WithoutArgs<AllowIncoherentImplParser>>,
Single<WithoutArgs<AllowInternalUnsafeParser>>,
Single<WithoutArgs<AsPtrParser>>,
Single<WithoutArgs<AutomaticallyDerivedParser>>,
Single<WithoutArgs<CoherenceIsCoreParser>>,
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,10 +905,7 @@ impl SyntaxExtension {
find_attr!(attrs, AttributeKind::AllowInternalUnstable(i, _) => i)
.map(|i| i.as_slice())
.unwrap_or_default();
// FIXME(jdonszelman): allow_internal_unsafe isn't yet new-style
// let allow_internal_unsafe = find_attr!(attrs, AttributeKind::AllowInternalUnsafe);
let allow_internal_unsafe =
ast::attr::find_by_name(attrs, sym::allow_internal_unsafe).is_some();
let allow_internal_unsafe = find_attr!(attrs, AttributeKind::AllowInternalUnsafe(_));

let local_inner_macros = ast::attr::find_by_name(attrs, sym::macro_export)
.and_then(|macro_export| macro_export.meta_item_list())
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_hir/src/attrs/data_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ pub enum AttributeKind {
/// Represents `#[rustc_allow_incoherent_impl]`.
AllowIncoherentImpl(Span),

/// Represents `#[allow_internal_unsafe]`.
AllowInternalUnsafe(Span),

/// Represents `#[allow_internal_unstable]`.
AllowInternalUnstable(ThinVec<(Symbol, Span)>, Span),

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_hir/src/attrs/encode_cross_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ impl AttributeKind {
Align { .. } => No,
AllowConstFnUnstable(..) => No,
AllowIncoherentImpl(..) => No,
AllowInternalUnsafe(..) => Yes,
AllowInternalUnstable(..) => Yes,
AsPtr(..) => Yes,
AutomaticallyDerived(..) => Yes,
Expand Down
13 changes: 12 additions & 1 deletion compiler/rustc_lint/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
//! [`crate::late_lint_methods!`] invocation in `lib.rs`.
use std::fmt::Write;
use std::slice;

use ast::token::TokenKind;
use rustc_abi::BackendRepr;
use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast::visit::{FnCtxt, FnKind};
use rustc_ast::{self as ast, *};
use rustc_ast_pretty::pprust::expr_to_string;
use rustc_attr_parsing::AttributeParser;
use rustc_errors::{Applicability, LintDiagnostic};
use rustc_feature::GateIssue;
use rustc_hir as hir;
Expand Down Expand Up @@ -249,7 +251,16 @@ impl UnsafeCode {

impl EarlyLintPass for UnsafeCode {
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
if attr.has_name(sym::allow_internal_unsafe) {
if AttributeParser::parse_limited(
cx.builder.sess(),
slice::from_ref(attr),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know this function existed

sym::allow_internal_unsafe,
attr.span,
DUMMY_NODE_ID,
Some(cx.builder.features()),
)
.is_some()
{
self.report_unsafe(cx, attr.span, BuiltinUnsafe::AllowInternalUnsafe);
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ passes_allow_incoherent_impl =
`rustc_allow_incoherent_impl` attribute should be applied to impl items
.label = the only currently supported targets are inherent methods
passes_allow_internal_unstable =
passes_macro_only_attribute =
attribute should be applied to a macro
.label = not a macro
Expand Down
49 changes: 41 additions & 8 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
Attribute::Parsed(AttributeKind::ConstContinue(attr_span)) => {
self.check_const_continue(hir_id, *attr_span, target)
}
Attribute::Parsed(AttributeKind::AllowInternalUnsafe(attr_span)) => {
self.check_allow_internal_unsafe(hir_id, *attr_span, span, target, attrs)
}
Attribute::Parsed(AttributeKind::AllowInternalUnstable(_, first_span)) => {
self.check_allow_internal_unstable(hir_id, *first_span, span, target, attrs)
}
Expand Down Expand Up @@ -415,7 +418,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
// internal
| sym::prelude_import
| sym::panic_handler
| sym::allow_internal_unsafe
| sym::lang
| sym::needs_allocator
| sym::default_lib_allocator
Expand Down Expand Up @@ -2214,14 +2216,49 @@ impl<'tcx> CheckAttrVisitor<'tcx> {

/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
/// (Allows proc_macro functions)
// FIXME(jdonszelmann): if possible, move to attr parsing
fn check_allow_internal_unstable(
&self,
hir_id: HirId,
attr_span: Span,
span: Span,
target: Target,
attrs: &[Attribute],
) {
self.check_macro_only_attr(
hir_id,
attr_span,
span,
target,
attrs,
"allow_internal_unstable",
)
}

/// Outputs an error for `#[allow_internal_unsafe]` which can only be applied to macros.
/// (Allows proc_macro functions)
fn check_allow_internal_unsafe(
&self,
hir_id: HirId,
attr_span: Span,
span: Span,
target: Target,
attrs: &[Attribute],
) {
self.check_macro_only_attr(hir_id, attr_span, span, target, attrs, "allow_internal_unsafe")
}

/// Outputs an error for attributes that can only be applied to macros, such as
/// `#[allow_internal_unsafe]` and `#[allow_internal_unstable]`.
/// (Allows proc_macro functions)
// FIXME(jdonszelmann): if possible, move to attr parsing
fn check_macro_only_attr(
&self,
hir_id: HirId,
attr_span: Span,
span: Span,
target: Target,
attrs: &[Attribute],
attr_name: &str,
) {
match target {
Target::Fn => {
Expand All @@ -2240,18 +2277,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
// erroneously allowed it and some crates used it accidentally, to be compatible
// with crates depending on them, we can't throw an error here.
Target::Field | Target::Arm => {
self.inline_attr_str_error_without_macro_def(
hir_id,
attr_span,
"allow_internal_unstable",
);
self.inline_attr_str_error_without_macro_def(hir_id, attr_span, attr_name);
return;
}
// otherwise continue out of the match
_ => {}
}

self.tcx.dcx().emit_err(errors::AllowInternalUnstable { attr_span, span });
self.tcx.dcx().emit_err(errors::MacroOnlyAttribute { attr_span, span });
}

/// Checks if the items on the `#[debugger_visualizer]` attribute are valid.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,8 +643,8 @@ pub(crate) struct UsedStatic {
}

#[derive(Diagnostic)]
#[diag(passes_allow_internal_unstable)]
pub(crate) struct AllowInternalUnstable {
#[diag(passes_macro_only_attribute)]
pub(crate) struct MacroOnlyAttribute {
#[primary_span]
pub attr_span: Span,
#[label]
Expand Down
15 changes: 9 additions & 6 deletions tests/ui/attributes/malformed-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,6 @@ LL - #[macro_export = 18]
LL + #[macro_export]
|

error: malformed `allow_internal_unsafe` attribute input
--> $DIR/malformed-attrs.rs:217:1
|
LL | #[allow_internal_unsafe = 1]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[allow_internal_unsafe]`

error: the `#[proc_macro]` attribute is only usable with crates of the `proc-macro` crate type
--> $DIR/malformed-attrs.rs:100:1
|
Expand Down Expand Up @@ -567,6 +561,15 @@ error: valid forms for the attribute are `#[macro_use(name1, name2, ...)]` and `
LL | #[macro_use = 1]
| ^^^^^^^^^^^^^^^^

error[E0565]: malformed `allow_internal_unsafe` attribute input
--> $DIR/malformed-attrs.rs:217:1
|
LL | #[allow_internal_unsafe = 1]
| ^^^^^^^^^^^^^^^^^^^^^^^^---^
| | |
| | didn't expect any arguments here
| help: must be of the form: `#[allow_internal_unsafe]`

error[E0565]: malformed `type_const` attribute input
--> $DIR/malformed-attrs.rs:144:5
|
Expand Down
Loading