Skip to content

Commit a75d7e6

Browse files
committed
optimize attribute applied to things other than methods/functions/closures give
s an error (#128488)
1 parent 798fb83 commit a75d7e6

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

compiler/rustc_passes/src/check_attr.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
124124
}
125125
[sym::inline, ..] => self.check_inline(hir_id, attr, span, target),
126126
[sym::coverage, ..] => self.check_coverage(attr, span, target),
127-
[sym::optimize, ..] => self.check_optimize(hir_id, attr, target),
127+
[sym::optimize, ..] => self.check_optimize(hir_id, attr, span, target),
128128
[sym::no_sanitize, ..] => {
129129
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
130130
}
@@ -433,23 +433,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
433433

434434
/// Checks that `#[optimize(..)]` is applied to a function/closure/method,
435435
/// or to an impl block or module.
436-
// FIXME(#128488): this should probably be elevated to an error?
437-
fn check_optimize(&self, hir_id: HirId, attr: &Attribute, target: Target) {
438-
match target {
436+
fn check_optimize(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
437+
let is_valid = matches!(
438+
target,
439439
Target::Fn
440-
| Target::Closure
441-
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
442-
| Target::Impl
443-
| Target::Mod => {}
444-
445-
_ => {
446-
self.tcx.emit_node_span_lint(
447-
UNUSED_ATTRIBUTES,
448-
hir_id,
449-
attr.span,
450-
errors::OptimizeNotFnOrClosure,
451-
);
452-
}
440+
| Target::Closure
441+
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
442+
| Target::Impl
443+
| Target::Mod
444+
);
445+
if !is_valid {
446+
self.dcx().emit_err(errors::OptimizeNotFnOrClosure {
447+
attr_span: attr.span,
448+
defn_span: span,
449+
on_crate: hir_id == CRATE_HIR_ID,
450+
});
453451
}
454452
}
455453

compiler/rustc_passes/src/errors.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,15 @@ pub(crate) struct CoverageNotFnOrClosure {
7676
pub defn_span: Span,
7777
}
7878

79-
#[derive(LintDiagnostic)]
79+
#[derive(Diagnostic)]
8080
#[diag(passes_optimize_not_fn_or_closure)]
81-
pub(crate) struct OptimizeNotFnOrClosure;
81+
pub(crate) struct OptimizeNotFnOrClosure {
82+
#[primary_span]
83+
pub attr_span: Span,
84+
#[label]
85+
pub defn_span: Span,
86+
pub on_crate: bool,
87+
}
8288

8389
#[derive(Diagnostic)]
8490
#[diag(passes_should_be_applied_to_fn)]

tests/ui/attributes/optimize.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ error: attribute should be applied to function or closure
33
|
44
LL | #[optimize(speed)]
55
| ^^^^^^^^^^^^^^^^^^
6-
|
7-
note: the lint level is defined here
8-
--> $DIR/optimize.rs:3:9
9-
|
10-
LL | #![deny(unused_attributes)]
11-
| ^^^^^^^^^^^^^^^^^
6+
LL | struct F;
7+
| --------- not a function or closure
128

139
error: attribute should be applied to function or closure
1410
--> $DIR/optimize.rs:10:5
1511
|
16-
LL | #[optimize(speed)]
17-
| ^^^^^^^^^^^^^^^^^^
12+
LL | #[optimize(speed)]
13+
| ^^^^^^^^^^^^^^^^^^
14+
LL | / {
15+
LL | | 1
16+
LL | | };
17+
| |_____- not a function or closure
1818

1919
error: aborting due to 2 previous errors
2020

0 commit comments

Comments
 (0)