Skip to content

Commit

Permalink
optimize attribute applied to things other than methods/functions/c…
Browse files Browse the repository at this point in the history
…losures give

s an error (#128488)
  • Loading branch information
Borgerr committed Aug 11, 2024
1 parent 8291d68 commit 133e486
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
32 changes: 15 additions & 17 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +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::optimize, ..] => self.check_optimize(hir_id, attr, span, target),
[sym::no_sanitize, ..] => self.check_no_sanitize(hir_id, attr, span, target),
[sym::non_exhaustive, ..] => self.check_non_exhaustive(hir_id, attr, span, target),
[sym::marker, ..] => self.check_marker(hir_id, attr, span, target),
Expand Down Expand Up @@ -431,23 +431,21 @@ impl<'tcx> CheckAttrVisitor<'tcx> {

/// Checks that `#[optimize(..)]` is applied to a function/closure/method,
/// or to an impl block or module.
// FIXME(#128488): this should probably be elevated to an error?
fn check_optimize(&self, hir_id: HirId, attr: &Attribute, target: Target) {
match target {
fn check_optimize(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
let is_valid = matches!(
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,
);
}
| Target::Closure
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
| Target::Impl
| Target::Mod
);
if !is_valid {
self.dcx().emit_err(errors::OptimizeNotFnOrClosure {
attr_span: attr.span,
defn_span: span,
on_crate: hir_id == CRATE_HIR_ID,
});
}
}

Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ pub struct CoverageNotFnOrClosure {
pub defn_span: Span,
}

#[derive(LintDiagnostic)]
#[derive(Diagnostic)]
#[diag(passes_optimize_not_fn_or_closure)]
pub struct OptimizeNotFnOrClosure;
pub struct OptimizeNotFnOrClosure {
#[primary_span]
pub attr_span: Span,
#[label]
pub defn_span: Span,
pub on_crate: bool,
}

#[derive(Diagnostic)]
#[diag(passes_should_be_applied_to_fn)]
Expand Down
16 changes: 8 additions & 8 deletions tests/ui/attributes/optimize.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ error: attribute should be applied to function or closure
|
LL | #[optimize(speed)]
| ^^^^^^^^^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/optimize.rs:3:9
|
LL | #![deny(unused_attributes)]
| ^^^^^^^^^^^^^^^^^
LL | struct F;
| --------- not a function or closure

error: attribute should be applied to function or closure
--> $DIR/optimize.rs:10:5
|
LL | #[optimize(speed)]
| ^^^^^^^^^^^^^^^^^^
LL | #[optimize(speed)]
| ^^^^^^^^^^^^^^^^^^
LL | / {
LL | | 1
LL | | };
| |_____- not a function or closure

error: aborting due to 2 previous errors

0 comments on commit 133e486

Please sign in to comment.