From 6d7bb127e6fab4e8d80b44826eca28f274139eed Mon Sep 17 00:00:00 2001 From: clubby789 Date: Wed, 31 Jul 2024 19:39:39 +0000 Subject: [PATCH] Emit an error if `#[optimize]` is applied to an incompatible item --- compiler/rustc_passes/messages.ftl | 4 ++++ compiler/rustc_passes/src/check_attr.rs | 22 +++++++++++++++++++ compiler/rustc_passes/src/errors.rs | 4 ++++ tests/ui/attributes/optimize.rs | 28 +++++++++++++++++++++++++ tests/ui/attributes/optimize.stderr | 20 ++++++++++++++++++ 5 files changed, 78 insertions(+) create mode 100644 tests/ui/attributes/optimize.rs create mode 100644 tests/ui/attributes/optimize.stderr diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl index bfe0d54e645..1747307a1b2 100644 --- a/compiler/rustc_passes/messages.ftl +++ b/compiler/rustc_passes/messages.ftl @@ -542,6 +542,10 @@ passes_only_has_effect_on = *[unspecified] (unspecified--this is a compiler bug) } +passes_optimize_not_fn_or_closure = + attribute should be applied to function or closure + .label = not a function or closure + passes_outer_crate_level_attr = crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]` diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs index 3a62ba66b54..86b18570f37 100644 --- a/compiler/rustc_passes/src/check_attr.rs +++ b/compiler/rustc_passes/src/check_attr.rs @@ -124,6 +124,7 @@ fn check_attributes( } [sym::inline] => self.check_inline(hir_id, attr, span, target), [sym::coverage] => self.check_coverage(attr, span, target), + [sym::optimize] => self.check_optimize(hir_id, attr, target), [sym::non_exhaustive] => self.check_non_exhaustive(hir_id, attr, span, target), [sym::marker] => self.check_marker(hir_id, attr, span, target), [sym::target_feature] => { @@ -373,6 +374,27 @@ fn check_coverage(&self, attr: &Attribute, span: Span, target: Target) { } } + /// Checks that `#[optimize(..)]` is applied to a function/closure/method, + /// or to an impl block or module. + fn check_optimize(&self, hir_id: HirId, attr: &Attribute, target: Target) { + match target { + Target::Fn + | Target::Closure + | Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) + | Target::Impl + | Target::Mod => {} + + _ => { + self.tcx.emit_node_span_lint( + UNUSED_ATTRIBUTES, + hir_id, + attr.span, + errors::OptimizeNotFnOrClosure, + ); + } + } + } + fn check_generic_attr( &self, hir_id: HirId, diff --git a/compiler/rustc_passes/src/errors.rs b/compiler/rustc_passes/src/errors.rs index ff85be109d4..c4f3c8a0d6c 100644 --- a/compiler/rustc_passes/src/errors.rs +++ b/compiler/rustc_passes/src/errors.rs @@ -68,6 +68,10 @@ pub struct CoverageNotFnOrClosure { pub defn_span: Span, } +#[derive(LintDiagnostic)] +#[diag(passes_optimize_not_fn_or_closure)] +pub struct OptimizeNotFnOrClosure; + #[derive(Diagnostic)] #[diag(passes_should_be_applied_to_fn)] pub struct AttrShouldBeAppliedToFn { diff --git a/tests/ui/attributes/optimize.rs b/tests/ui/attributes/optimize.rs new file mode 100644 index 00000000000..b01806165c1 --- /dev/null +++ b/tests/ui/attributes/optimize.rs @@ -0,0 +1,28 @@ +#![feature(optimize_attribute)] +#![feature(stmt_expr_attributes)] +#![deny(unused_attributes)] +#![allow(dead_code)] + +#[optimize(speed)] //~ ERROR attribute should be applied to function or closure +struct F; + +fn invalid() { + #[optimize(speed)] //~ ERROR attribute should be applied to function or closure + { + 1 + }; +} + +#[optimize(speed)] +fn valid() {} + +#[optimize(speed)] +mod valid_module {} + +#[optimize(speed)] +impl F {} + +fn main() { + let _ = #[optimize(speed)] + (|| 1); +} diff --git a/tests/ui/attributes/optimize.stderr b/tests/ui/attributes/optimize.stderr new file mode 100644 index 00000000000..3c445d73c2e --- /dev/null +++ b/tests/ui/attributes/optimize.stderr @@ -0,0 +1,20 @@ +error: attribute should be applied to function or closure + --> $DIR/optimize.rs:6:1 + | +LL | #[optimize(speed)] + | ^^^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/optimize.rs:3:9 + | +LL | #![deny(unused_attributes)] + | ^^^^^^^^^^^^^^^^^ + +error: attribute should be applied to function or closure + --> $DIR/optimize.rs:10:5 + | +LL | #[optimize(speed)] + | ^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors +