Skip to content

Commit 133e486

Browse files
committed
optimize attribute applied to things other than methods/functions/closures give
s an error (rust-lang#128488)
1 parent 8291d68 commit 133e486

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, ..] => self.check_no_sanitize(hir_id, attr, span, target),
129129
[sym::non_exhaustive, ..] => self.check_non_exhaustive(hir_id, attr, span, target),
130130
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
@@ -431,23 +431,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
431431

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

compiler/rustc_passes/src/errors.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,15 @@ pub struct CoverageNotFnOrClosure {
6868
pub defn_span: Span,
6969
}
7070

71-
#[derive(LintDiagnostic)]
71+
#[derive(Diagnostic)]
7272
#[diag(passes_optimize_not_fn_or_closure)]
73-
pub struct OptimizeNotFnOrClosure;
73+
pub struct OptimizeNotFnOrClosure {
74+
#[primary_span]
75+
pub attr_span: Span,
76+
#[label]
77+
pub defn_span: Span,
78+
pub on_crate: bool,
79+
}
7480

7581
#[derive(Diagnostic)]
7682
#[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)