Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit an error if #[optimize] is applied to an incompatible item #128458

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -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]`
Expand Down
22 changes: 22 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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] => {
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions tests/ui/attributes/optimize.rs
Original file line number Diff line number Diff line change
@@ -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);
}
20 changes: 20 additions & 0 deletions tests/ui/attributes/optimize.stderr
Original file line number Diff line number Diff line change
@@ -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

Loading