Skip to content

Commit 4f7a6ac

Browse files
committed
Port #[allow_internal_unsafe] to the new attribute system
1 parent e1b9081 commit 4f7a6ac

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
@@ -32,7 +32,9 @@ use crate::attributes::lint_helpers::{
3232
AsPtrParser, AutomaticallyDerivedParser, PassByValueParser, PubTransparentParser,
3333
};
3434
use crate::attributes::loop_match::{ConstContinueParser, LoopMatchParser};
35-
use crate::attributes::macro_attrs::{MacroEscapeParser, MacroUseParser};
35+
use crate::attributes::macro_attrs::{
36+
AllowInternalUnsafeParser, MacroEscapeParser, MacroUseParser,
37+
};
3638
use crate::attributes::must_use::MustUseParser;
3739
use crate::attributes::no_implicit_prelude::NoImplicitPreludeParser;
3840
use crate::attributes::non_exhaustive::NonExhaustiveParser;
@@ -176,6 +178,7 @@ attribute_parsers!(
176178
Single<SkipDuringMethodDispatchParser>,
177179
Single<TransparencyParser>,
178180
Single<WithoutArgs<AllowIncoherentImplParser>>,
181+
Single<WithoutArgs<AllowInternalUnsafeParser>>,
179182
Single<WithoutArgs<AsPtrParser>>,
180183
Single<WithoutArgs<AutomaticallyDerivedParser>>,
181184
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
@@ -16,6 +16,7 @@ impl AttributeKind {
1616
Align { .. } => No,
1717
AllowConstFnUnstable(..) => No,
1818
AllowIncoherentImpl(..) => No,
19+
AllowInternalUnsafe(..) => Yes,
1920
AllowInternalUnstable(..) => Yes,
2021
AsPtr(..) => Yes,
2122
AutomaticallyDerived(..) => Yes,

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
}
@@ -414,7 +417,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
414417
// internal
415418
| sym::prelude_import
416419
| sym::panic_handler
417-
| sym::allow_internal_unsafe
418420
| sym::lang
419421
| sym::needs_allocator
420422
| sym::default_lib_allocator
@@ -2213,14 +2215,49 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
22132215

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

2253-
self.tcx.dcx().emit_err(errors::AllowInternalUnstable { attr_span, span });
2286+
self.tcx.dcx().emit_err(errors::MacroOnlyAttribute { attr_span, span });
22542287
}
22552288

22562289
/// 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:213: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:96:1
162156
|
@@ -558,6 +552,15 @@ error: valid forms for the attribute are `#[macro_use(name1, name2, ...)]` and `
558552
LL | #[macro_use = 1]
559553
| ^^^^^^^^^^^^^^^^
560554

555+
error[E0565]: malformed `allow_internal_unsafe` attribute input
556+
--> $DIR/malformed-attrs.rs:213:1
557+
|
558+
LL | #[allow_internal_unsafe = 1]
559+
| ^^^^^^^^^^^^^^^^^^^^^^^^---^
560+
| | |
561+
| | didn't expect any arguments here
562+
| help: must be of the form: `#[allow_internal_unsafe]`
563+
561564
error[E0565]: malformed `type_const` attribute input
562565
--> $DIR/malformed-attrs.rs:140:5
563566
|

0 commit comments

Comments
 (0)