Skip to content

Commit 4d97fb7

Browse files
committed
Assert that all attributes are actually checked via CheckAttrVisitor and aren't accidentally usable on completely unrelated HIR nodes
1 parent 67a08b5 commit 4d97fb7

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

Diff for: compiler/rustc_feature/src/builtin_attrs.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -703,21 +703,21 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
703703
EncodeCrossCrate::No, allocator_internals, experimental!(needs_allocator),
704704
),
705705
gated!(
706-
panic_runtime, Normal, template!(Word), WarnFollowing,
706+
panic_runtime, CrateLevel, template!(Word), WarnFollowing,
707707
EncodeCrossCrate::No, experimental!(panic_runtime)
708708
),
709709
gated!(
710-
needs_panic_runtime, Normal, template!(Word), WarnFollowing,
710+
needs_panic_runtime, CrateLevel, template!(Word), WarnFollowing,
711711
EncodeCrossCrate::No, experimental!(needs_panic_runtime)
712712
),
713713
gated!(
714-
compiler_builtins, Normal, template!(Word), WarnFollowing,
714+
compiler_builtins, CrateLevel, template!(Word), WarnFollowing,
715715
EncodeCrossCrate::No,
716716
"the `#[compiler_builtins]` attribute is used to identify the `compiler_builtins` crate \
717717
which contains compiler-rt intrinsics and will never be stable",
718718
),
719719
gated!(
720-
profiler_runtime, Normal, template!(Word), WarnFollowing,
720+
profiler_runtime, CrateLevel, template!(Word), WarnFollowing,
721721
EncodeCrossCrate::No,
722722
"the `#[profiler_runtime]` attribute is used to identify the `profiler_builtins` crate \
723723
which contains the profiler runtime and will never be stable",

Diff for: compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ passes_continue_labeled_block =
100100
.label = labeled blocks cannot be `continue`'d
101101
.block_label = labeled block the `continue` points to
102102
103+
passes_coroutine_on_non_closure =
104+
attribute should be applied to closures
105+
.label = not a closure
106+
103107
passes_coverage_not_fn_or_closure =
104108
attribute should be applied to a function definition or closure
105109
.label = not a function or closure

Diff for: compiler/rustc_passes/src/check_attr.rs

+64-2
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ use rustc_hir::{
2020
TraitItem, CRATE_HIR_ID, CRATE_OWNER_ID,
2121
};
2222
use rustc_macros::LintDiagnostic;
23-
use rustc_middle::bug;
2423
use rustc_middle::hir::nested_filter;
2524
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
2625
use rustc_middle::query::Providers;
2726
use rustc_middle::traits::ObligationCause;
2827
use rustc_middle::ty::error::{ExpectedFound, TypeError};
2928
use rustc_middle::ty::{self, TyCtxt};
29+
use rustc_middle::{bug, span_bug};
3030
use rustc_session::lint::builtin::{
3131
CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, INVALID_MACRO_EXPORT_ARGUMENTS,
3232
UNKNOWN_OR_MALFORMED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
@@ -238,7 +238,60 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
238238
self.check_generic_attr(hir_id, attr, target, Target::Fn);
239239
self.check_proc_macro(hir_id, target, ProcMacroKind::Derive)
240240
}
241-
_ => {}
241+
[sym::coroutine] => {
242+
self.check_coroutine(attr, target);
243+
}
244+
[
245+
// ok
246+
sym::allow
247+
| sym::expect
248+
| sym::warn
249+
| sym::deny
250+
| sym::forbid
251+
| sym::cfg
252+
// need to be fixed
253+
| sym::cfi_encoding // FIXME(cfi_encoding)
254+
| sym::may_dangle // FIXME(dropck_eyepatch)
255+
| sym::pointee // FIXME(derive_smart_pointer)
256+
| sym::linkage // FIXME(linkage)
257+
| sym::no_sanitize // FIXME(no_sanitize)
258+
| sym::omit_gdb_pretty_printer_section // FIXME(omit_gdb_pretty_printer_section)
259+
| sym::used // handled elsewhere to restrict to static items
260+
| sym::repr // handled elsewhere to restrict to type decls items
261+
| sym::optimize // FIXME(optimize_attribute)
262+
| sym::instruction_set // broken on stable!!!
263+
| sym::windows_subsystem // broken on stable!!!
264+
| sym::patchable_function_entry // FIXME(patchable_function_entry)
265+
| sym::deprecated_safe // FIXME(deprecated_safe)
266+
// internal
267+
| sym::prelude_import
268+
| sym::panic_handler
269+
| sym::allow_internal_unsafe
270+
| sym::fundamental
271+
| sym::lang
272+
| sym::needs_allocator
273+
| sym::default_lib_allocator
274+
| sym::start
275+
| sym::custom_mir,
276+
] => {}
277+
[name, ..] => {
278+
match BUILTIN_ATTRIBUTE_MAP.get(name) {
279+
// checked below
280+
Some(BuiltinAttribute { type_: AttributeType::CrateLevel, .. }) => {}
281+
Some(_) => {
282+
// FIXME: differentiate between unstable and internal attributes just like we do with features instead
283+
// of just accepting `rustc_` attributes by name. That should allow trimming the above list, too.
284+
if !name.as_str().starts_with("rustc_") {
285+
span_bug!(
286+
attr.span,
287+
"builtin attribute {name:?} not handled by `CheckAttrVisitor`"
288+
)
289+
}
290+
}
291+
None => (),
292+
}
293+
}
294+
[] => unreachable!(),
242295
}
243296

244297
let builtin = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
@@ -2251,6 +2304,15 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
22512304
self.abort.set(true);
22522305
}
22532306
}
2307+
2308+
fn check_coroutine(&self, attr: &Attribute, target: Target) {
2309+
match target {
2310+
Target::Closure => return,
2311+
_ => {
2312+
self.dcx().emit_err(errors::CoroutineOnNonClosure { span: attr.span });
2313+
}
2314+
}
2315+
}
22542316
}
22552317

22562318
impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {

Diff for: compiler/rustc_passes/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,13 @@ pub struct Confusables {
632632
pub attr_span: Span,
633633
}
634634

635+
#[derive(Diagnostic)]
636+
#[diag(passes_coroutine_on_non_closure)]
637+
pub struct CoroutineOnNonClosure {
638+
#[primary_span]
639+
pub span: Span,
640+
}
641+
635642
#[derive(Diagnostic)]
636643
#[diag(passes_empty_confusables)]
637644
pub(crate) struct EmptyConfusables {

0 commit comments

Comments
 (0)