Skip to content

Commit 9bf55a2

Browse files
authored
Unrolled build for rust-lang#128458
Rollup merge of rust-lang#128458 - clubby789:optimize-unused-attr, r=compiler-errors Emit an error if `#[optimize]` is applied to an incompatible item rust-lang#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.
2 parents 70591dc + 6d7bb12 commit 9bf55a2

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

compiler/rustc_passes/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ passes_only_has_effect_on =
542542
*[unspecified] (unspecified--this is a compiler bug)
543543
}
544544
545+
passes_optimize_not_fn_or_closure =
546+
attribute should be applied to function or closure
547+
.label = not a function or closure
548+
545549
passes_outer_crate_level_attr =
546550
crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
547551

compiler/rustc_passes/src/check_attr.rs

+22
Original file line numberDiff line numberDiff line change
@@ -124,6 +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),
127128
[sym::non_exhaustive] => self.check_non_exhaustive(hir_id, attr, span, target),
128129
[sym::marker] => self.check_marker(hir_id, attr, span, target),
129130
[sym::target_feature] => {
@@ -373,6 +374,27 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
373374
}
374375
}
375376

377+
/// Checks that `#[optimize(..)]` is applied to a function/closure/method,
378+
/// or to an impl block or module.
379+
fn check_optimize(&self, hir_id: HirId, attr: &Attribute, target: Target) {
380+
match target {
381+
Target::Fn
382+
| Target::Closure
383+
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
384+
| Target::Impl
385+
| Target::Mod => {}
386+
387+
_ => {
388+
self.tcx.emit_node_span_lint(
389+
UNUSED_ATTRIBUTES,
390+
hir_id,
391+
attr.span,
392+
errors::OptimizeNotFnOrClosure,
393+
);
394+
}
395+
}
396+
}
397+
376398
fn check_generic_attr(
377399
&self,
378400
hir_id: HirId,

compiler/rustc_passes/src/errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ pub struct CoverageNotFnOrClosure {
6868
pub defn_span: Span,
6969
}
7070

71+
#[derive(LintDiagnostic)]
72+
#[diag(passes_optimize_not_fn_or_closure)]
73+
pub struct OptimizeNotFnOrClosure;
74+
7175
#[derive(Diagnostic)]
7276
#[diag(passes_should_be_applied_to_fn)]
7377
pub struct AttrShouldBeAppliedToFn {

tests/ui/attributes/optimize.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(optimize_attribute)]
2+
#![feature(stmt_expr_attributes)]
3+
#![deny(unused_attributes)]
4+
#![allow(dead_code)]
5+
6+
#[optimize(speed)] //~ ERROR attribute should be applied to function or closure
7+
struct F;
8+
9+
fn invalid() {
10+
#[optimize(speed)] //~ ERROR attribute should be applied to function or closure
11+
{
12+
1
13+
};
14+
}
15+
16+
#[optimize(speed)]
17+
fn valid() {}
18+
19+
#[optimize(speed)]
20+
mod valid_module {}
21+
22+
#[optimize(speed)]
23+
impl F {}
24+
25+
fn main() {
26+
let _ = #[optimize(speed)]
27+
(|| 1);
28+
}

tests/ui/attributes/optimize.stderr

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: attribute should be applied to function or closure
2+
--> $DIR/optimize.rs:6:1
3+
|
4+
LL | #[optimize(speed)]
5+
| ^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/optimize.rs:3:9
9+
|
10+
LL | #![deny(unused_attributes)]
11+
| ^^^^^^^^^^^^^^^^^
12+
13+
error: attribute should be applied to function or closure
14+
--> $DIR/optimize.rs:10:5
15+
|
16+
LL | #[optimize(speed)]
17+
| ^^^^^^^^^^^^^^^^^^
18+
19+
error: aborting due to 2 previous errors
20+

0 commit comments

Comments
 (0)