diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2024-08-01 08:33:29 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-08-01 08:33:29 +0200 |
| commit | 7c605258774794ddf2dae5f0c46768e32bf13bd6 (patch) | |
| tree | 091a45fd5597da54242133e7c1e324d069083632 /compiler | |
| parent | 2c3b89e4d85b7791280fe6cfcebeee389e2e3f0c (diff) | |
| parent | 6d7bb127e6fab4e8d80b44826eca28f274139eed (diff) | |
| download | rust-7c605258774794ddf2dae5f0c46768e32bf13bd6.tar.gz rust-7c605258774794ddf2dae5f0c46768e32bf13bd6.zip | |
Rollup merge of #128458 - clubby789:optimize-unused-attr, r=compiler-errors
Emit an error if `#[optimize]` is applied to an incompatible item #54882 The RFC specifies that this should emit a lint. I used the same allow logic as the `coverage` attribute (also allowing modules and impl blocks) - this should possibly be changed depending on if it's decided to allow 'propogation' of the attribute.
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_passes/messages.ftl | 4 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/check_attr.rs | 22 | ||||
| -rw-r--r-- | compiler/rustc_passes/src/errors.rs | 4 |
3 files changed, 30 insertions, 0 deletions
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 @@ impl<'tcx> CheckAttrVisitor<'tcx> { } [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 @@ impl<'tcx> CheckAttrVisitor<'tcx> { } } + /// 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 { |
