Skip to content

Commit da3e3f7

Browse files
committed
Part #[allow_internal_unsafe] to the new attribute system
1 parent 7cd9505 commit da3e3f7

File tree

10 files changed

+82
-23
lines changed

10 files changed

+82
-23
lines changed

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,11 @@ impl<S: Stage> AttributeParser<S> for MacroUseParser {
113113
Some(AttributeKind::MacroUse { span: self.first_span?, arguments: self.state })
114114
}
115115
}
116+
117+
pub(crate) struct AllowInternalUnsafeParser;
118+
119+
impl<S: Stage> NoArgsAttributeParser<S> for AllowInternalUnsafeParser {
120+
const PATH: &[Symbol] = &[sym::allow_internal_unsafe];
121+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::Ignore;
122+
const CREATE: fn(Span) -> AttributeKind = |span| AttributeKind::AllowInternalUnsafe(span);
123+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ use crate::attributes::lint_helpers::{
3333
AsPtrParser, AutomaticallyDerivedParser, PassByValueParser, PubTransparentParser,
3434
};
3535
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
36-
use crate::attributes::macro_attrs::{MacroEscapeParser, MacroUseParser};
36+
use crate::attributes::macro_attrs::{
37+
AllowInternalUnsafeParser, MacroEscapeParser, MacroUseParser,
38+
};
3739
use crate::attributes::must_use::MustUseParser;
3840
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
3941
use crate::attributes::non_exhaustive::NonExhaustiveParser;
@@ -177,6 +179,7 @@ attribute_parsers!(
177179
Single<SkipDuringMethodDispatchParser>,
178180
Single<TransparencyParser>,
179181
Single<WithoutArgs<AllowIncoherentImplParser>>,
182+
Single<WithoutArgs<AllowInternalUnsafeParser>>,
180183
Single<WithoutArgs<AsPtrParser>>,
181184
Single<WithoutArgs<AutomaticallyDerivedParser>>,
182185
Single<WithoutArgs<CoherenceIsCoreParser>>,

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -905,10 +905,7 @@ impl SyntaxExtension {
905905
find_attr!(attrs, AttributeKind::AllowInternalUnstable(i, _) => i)
906906
.map(|i| i.as_slice())
907907
.unwrap_or_default();
908-
// FIXME(jdonszelman): allow_internal_unsafe isn't yet new-style
909-
// let allow_internal_unsafe = find_attr!(attrs, AttributeKind::AllowInternalUnsafe);
910-
let allow_internal_unsafe =
911-
ast::attr::find_by_name(attrs, sym::allow_internal_unsafe).is_some();
908+
let allow_internal_unsafe = find_attr!(attrs, AttributeKind::AllowInternalUnsafe(_));
912909

913910
let local_inner_macros = ast::attr::find_by_name(attrs, sym::macro_export)
914911
.and_then(|macro_export| macro_export.meta_item_list())

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ pub enum AttributeKind {
249249
/// Represents `#[rustc_allow_incoherent_impl]`.
250250
AllowIncoherentImpl(Span),
251251

252+
/// Represents `#[allow_internal_unsafe]`.
253+
AllowInternalUnsafe(Span),
254+
252255
/// Represents `#[allow_internal_unstable]`.
253256
AllowInternalUnstable(ThinVec<(Symbol, Span)>, Span),
254257

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl AttributeKind {
1717
AllowConstFnUnstable(..) => No,
1818
AllowIncoherentImpl(..) => No,
1919
AllowInternalUnstable(..) => Yes,
20+
AllowInternalUnsafe(..) => Yes,
2021
AsPtr(..) => Yes,
2122
AutomaticallyDerived(..) => Yes,
2223
BodyStability { .. } => No,

compiler/rustc_lint/src/builtin.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
//! [`crate::late_lint_methods!`] invocation in `lib.rs`.
1515
1616
use std::fmt::Write;
17+
use std::slice;
1718

1819
use ast::token::TokenKind;
1920
use rustc_abi::BackendRepr;
2021
use rustc_ast::tokenstream::{TokenStream, TokenTree};
2122
use rustc_ast::visit::{FnCtxt, FnKind};
2223
use rustc_ast::{self as ast, *};
2324
use rustc_ast_pretty::pprust::expr_to_string;
25+
use rustc_attr_parsing::AttributeParser;
2426
use rustc_errors::{Applicability, LintDiagnostic};
2527
use rustc_feature::GateIssue;
2628
use rustc_hir as hir;
@@ -249,7 +251,16 @@ impl UnsafeCode {
249251

250252
impl EarlyLintPass for UnsafeCode {
251253
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
252-
if attr.has_name(sym::allow_internal_unsafe) {
254+
if AttributeParser::parse_limited(
255+
cx.builder.sess(),
256+
slice::from_ref(attr),
257+
sym::allow_internal_unsafe,
258+
attr.span,
259+
DUMMY_NODE_ID,
260+
Some(cx.builder.features()),
261+
)
262+
.is_some()
263+
{
253264
self.report_unsafe(cx, attr.span, BuiltinUnsafe::AllowInternalUnsafe);
254265
}
255266
}

compiler/rustc_passes/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ passes_allow_incoherent_impl =
2929
`rustc_allow_incoherent_impl` attribute should be applied to impl items
3030
.label = the only currently supported targets are inherent methods
3131
32-
passes_allow_internal_unstable =
32+
passes_macro_only_attribute =
3333
attribute should be applied to a macro
3434
.label = not a macro
3535

compiler/rustc_passes/src/check_attr.rs

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
207207
Attribute::Parsed(AttributeKind::ConstContinue(attr_span)) => {
208208
self.check_const_continue(hir_id, *attr_span, target)
209209
}
210+
Attribute::Parsed(AttributeKind::AllowInternalUnsafe(attr_span)) => {
211+
self.check_allow_internal_unsafe(hir_id, *attr_span, span, target, attrs)
212+
}
210213
Attribute::Parsed(AttributeKind::AllowInternalUnstable(_, first_span)) => {
211214
self.check_allow_internal_unstable(hir_id, *first_span, span, target, attrs)
212215
}
@@ -415,7 +418,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
415418
// internal
416419
| sym::prelude_import
417420
| sym::panic_handler
418-
| sym::allow_internal_unsafe
419421
| sym::lang
420422
| sym::needs_allocator
421423
| sym::default_lib_allocator
@@ -2214,14 +2216,49 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
22142216

22152217
/// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
22162218
/// (Allows proc_macro functions)
2217-
// FIXME(jdonszelmann): if possible, move to attr parsing
22182219
fn check_allow_internal_unstable(
22192220
&self,
22202221
hir_id: HirId,
22212222
attr_span: Span,
22222223
span: Span,
22232224
target: Target,
22242225
attrs: &[Attribute],
2226+
) {
2227+
self.check_macro_only_attr(
2228+
hir_id,
2229+
attr_span,
2230+
span,
2231+
target,
2232+
attrs,
2233+
"allow_internal_unstable",
2234+
)
2235+
}
2236+
2237+
/// Outputs an error for `#[allow_internal_unsafe]` which can only be applied to macros.
2238+
/// (Allows proc_macro functions)
2239+
fn check_allow_internal_unsafe(
2240+
&self,
2241+
hir_id: HirId,
2242+
attr_span: Span,
2243+
span: Span,
2244+
target: Target,
2245+
attrs: &[Attribute],
2246+
) {
2247+
self.check_macro_only_attr(hir_id, attr_span, span, target, attrs, "allow_internal_unsafe")
2248+
}
2249+
2250+
/// Outputs an error for attributes that can only be applied to macros, such as
2251+
/// `#[allow_internal_unsafe]` and `#[allow_internal_unstable]`.
2252+
/// (Allows proc_macro functions)
2253+
// FIXME(jdonszelmann): if possible, move to attr parsing
2254+
fn check_macro_only_attr(
2255+
&self,
2256+
hir_id: HirId,
2257+
attr_span: Span,
2258+
span: Span,
2259+
target: Target,
2260+
attrs: &[Attribute],
2261+
attr_name: &str,
22252262
) {
22262263
match target {
22272264
Target::Fn => {
@@ -2240,18 +2277,14 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
22402277
// erroneously allowed it and some crates used it accidentally, to be compatible
22412278
// with crates depending on them, we can't throw an error here.
22422279
Target::Field | Target::Arm => {
2243-
self.inline_attr_str_error_without_macro_def(
2244-
hir_id,
2245-
attr_span,
2246-
"allow_internal_unstable",
2247-
);
2280+
self.inline_attr_str_error_without_macro_def(hir_id, attr_span, attr_name);
22482281
return;
22492282
}
22502283
// otherwise continue out of the match
22512284
_ => {}
22522285
}
22532286

2254-
self.tcx.dcx().emit_err(errors::AllowInternalUnstable { attr_span, span });
2287+
self.tcx.dcx().emit_err(errors::MacroOnlyAttribute { attr_span, span });
22552288
}
22562289

22572290
/// Checks if the items on the `#[debugger_visualizer]` attribute are valid.

compiler/rustc_passes/src/errors.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,8 @@ pub(crate) struct UsedStatic {
643643
}
644644

645645
#[derive(Diagnostic)]
646-
#[diag(passes_allow_internal_unstable)]
647-
pub(crate) struct AllowInternalUnstable {
646+
#[diag(passes_macro_only_attribute)]
647+
pub(crate) struct MacroOnlyAttribute {
648648
#[primary_span]
649649
pub attr_span: Span,
650650
#[label]

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,6 @@ LL - #[macro_export = 18]
151151
LL + #[macro_export]
152152
|
153153

154-
error: malformed `allow_internal_unsafe` attribute input
155-
--> $DIR/malformed-attrs.rs:217:1
156-
|
157-
LL | #[allow_internal_unsafe = 1]
158-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[allow_internal_unsafe]`
159-
160154
error: the `#[proc_macro]` attribute is only usable with crates of the `proc-macro` crate type
161155
--> $DIR/malformed-attrs.rs:100:1
162156
|
@@ -567,6 +561,15 @@ error: valid forms for the attribute are `#[macro_use(name1, name2, ...)]` and `
567561
LL | #[macro_use = 1]
568562
| ^^^^^^^^^^^^^^^^
569563

564+
error[E0565]: malformed `allow_internal_unsafe` attribute input
565+
--> $DIR/malformed-attrs.rs:217:1
566+
|
567+
LL | #[allow_internal_unsafe = 1]
568+
| ^^^^^^^^^^^^^^^^^^^^^^^^---^
569+
| | |
570+
| | didn't expect any arguments here
571+
| help: must be of the form: `#[allow_internal_unsafe]`
572+
570573
error[E0565]: malformed `type_const` attribute input
571574
--> $DIR/malformed-attrs.rs:144:5
572575
|

0 commit comments

Comments
 (0)