Skip to content

Commit 63c266f

Browse files
authored
Rollup merge of rust-lang#145155 - scrabsha:push-tkvwkolzooyq, r=jdonszelmann
Port `#[allow_internal_unsafe]` to the new attribute system (attempt 2) This is a slightly modified version of ae1487a, which caused a performance regression (reverted in rust-lang#145086 (comment)). The diff between this PR and the previous one can be seen in 027a1de. r? `@jdonszelmann` 💖
2 parents b78241d + f777067 commit 63c266f

File tree

11 files changed

+85
-28
lines changed

11 files changed

+85
-28
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;
@@ -178,6 +180,7 @@ attribute_parsers!(
178180
Single<SkipDuringMethodDispatchParser>,
179181
Single<TransparencyParser>,
180182
Single<WithoutArgs<AllowIncoherentImplParser>>,
183+
Single<WithoutArgs<AllowInternalUnsafeParser>>,
181184
Single<WithoutArgs<AsPtrParser>>,
182185
Single<WithoutArgs<AutomaticallyDerivedParser>>,
183186
Single<WithoutArgs<CoherenceIsCoreParser>>,

compiler/rustc_expand/src/base.rs

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

912909
let local_inner_macros = ast::attr::find_by_name(attrs, sym::macro_export)
913910
.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_hir/src/hir.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,7 @@ impl AttributeExt for Attribute {
13101310
Attribute::Parsed(AttributeKind::Ignore { span, .. }) => *span,
13111311
Attribute::Parsed(AttributeKind::ShouldPanic { span, .. }) => *span,
13121312
Attribute::Parsed(AttributeKind::AutomaticallyDerived(span)) => *span,
1313+
Attribute::Parsed(AttributeKind::AllowInternalUnsafe(span)) => *span,
13131314
a => panic!("can't get the span of an arbitrary parsed attribute: {a:?}"),
13141315
}
13151316
}

compiler/rustc_lint/src/builtin.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
2121
use rustc_ast::visit::{FnCtxt, FnKind};
2222
use rustc_ast::{self as ast, *};
2323
use rustc_ast_pretty::pprust::expr_to_string;
24+
use rustc_attr_parsing::AttributeParser;
2425
use rustc_errors::{Applicability, LintDiagnostic};
2526
use rustc_feature::GateIssue;
2627
use rustc_hir as hir;
@@ -248,12 +249,6 @@ impl UnsafeCode {
248249
}
249250

250251
impl EarlyLintPass for UnsafeCode {
251-
fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
252-
if attr.has_name(sym::allow_internal_unsafe) {
253-
self.report_unsafe(cx, attr.span, BuiltinUnsafe::AllowInternalUnsafe);
254-
}
255-
}
256-
257252
#[inline]
258253
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &ast::Expr) {
259254
if let ast::ExprKind::Block(ref blk, _) = e.kind {
@@ -312,6 +307,19 @@ impl EarlyLintPass for UnsafeCode {
312307
}
313308
}
314309

310+
ast::ItemKind::MacroDef(..) => {
311+
if let Some(attr) = AttributeParser::parse_limited(
312+
cx.builder.sess(),
313+
&it.attrs,
314+
sym::allow_internal_unsafe,
315+
it.span,
316+
DUMMY_NODE_ID,
317+
Some(cx.builder.features()),
318+
) {
319+
self.report_unsafe(cx, attr.span(), BuiltinUnsafe::AllowInternalUnsafe);
320+
}
321+
}
322+
315323
_ => {}
316324
}
317325
}

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
}
@@ -413,7 +416,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
413416
// internal
414417
| sym::prelude_import
415418
| sym::panic_handler
416-
| sym::allow_internal_unsafe
417419
| sym::lang
418420
| sym::needs_allocator
419421
| sym::default_lib_allocator
@@ -2212,14 +2214,49 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
22122214

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

2252-
self.tcx.dcx().emit_err(errors::AllowInternalUnstable { attr_span, span });
2285+
self.tcx.dcx().emit_err(errors::MacroOnlyAttribute { attr_span, span });
22532286
}
22542287

22552288
/// 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]

0 commit comments

Comments
 (0)