Skip to content

Commit 1140d36

Browse files
authored
Unrolled build for rust-lang#131814
Rollup merge of rust-lang#131814 - Borgerr:misapplied-optimize-attribute, r=jieyouxu `optimize` attribute applied to things other than methods/functions/c… …losures gives an error (rust-lang#128488) Duplicate of rust-lang#128943, which I had accidentally closed when rebasing. cc. `@jieyouxu` `@compiler-errors` `@nikomatsakis` `@traviscross` `@pnkfelix.`
2 parents 662180b + 080103f commit 1140d36

File tree

8 files changed

+115
-69
lines changed

8 files changed

+115
-69
lines changed

compiler/rustc_passes/messages.ftl

+3-3
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,9 @@ passes_only_has_effect_on =
553553
*[unspecified] (unspecified--this is a compiler bug)
554554
}
555555
556-
passes_optimize_not_fn_or_closure =
557-
attribute should be applied to function or closure
558-
.label = not a function or closure
556+
passes_optimize_invalid_target =
557+
attribute applied to an invalid target
558+
.label = invalid target
559559
560560
passes_outer_crate_level_attr =
561561
crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`

compiler/rustc_passes/src/check_attr.rs

+13-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,19 @@ 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+
);
443+
if !is_valid {
444+
self.dcx().emit_err(errors::OptimizeInvalidTarget {
445+
attr_span: attr.span,
446+
defn_span: span,
447+
on_crate: hir_id == CRATE_HIR_ID,
448+
});
453449
}
454450
}
455451

compiler/rustc_passes/src/errors.rs

+9-3
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)]
80-
#[diag(passes_optimize_not_fn_or_closure)]
81-
pub(crate) struct OptimizeNotFnOrClosure;
79+
#[derive(Diagnostic)]
80+
#[diag(passes_optimize_invalid_target)]
81+
pub(crate) struct OptimizeInvalidTarget {
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.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
#![deny(unused_attributes)]
44
#![allow(dead_code)]
55

6-
#[optimize(speed)] //~ ERROR attribute should be applied to function or closure
6+
//@ edition: 2018
7+
8+
#[optimize(speed)] //~ ERROR attribute applied to an invalid target
79
struct F;
810

911
fn invalid() {
10-
#[optimize(speed)] //~ ERROR attribute should be applied to function or closure
12+
#[optimize(speed)] //~ ERROR attribute applied to an invalid target
1113
{
1214
1
1315
};
@@ -16,13 +18,25 @@ fn invalid() {
1618
#[optimize(speed)]
1719
fn valid() {}
1820

19-
#[optimize(speed)]
21+
#[optimize(speed)] //~ ERROR attribute applied to an invalid target
2022
mod valid_module {}
2123

22-
#[optimize(speed)]
24+
#[optimize(speed)] //~ ERROR attribute applied to an invalid target
2325
impl F {}
2426

2527
fn main() {
2628
let _ = #[optimize(speed)]
2729
(|| 1);
2830
}
31+
32+
use std::future::Future;
33+
34+
fn async_block() -> impl Future<Output = ()> {
35+
#[optimize(speed)]
36+
async { }
37+
}
38+
39+
#[optimize(speed)]
40+
async fn async_fn() {
41+
()
42+
}

tests/ui/attributes/optimize.stderr

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,36 @@
1-
error: attribute should be applied to function or closure
2-
--> $DIR/optimize.rs:6:1
1+
error: attribute applied to an invalid target
2+
--> $DIR/optimize.rs:8:1
33
|
44
LL | #[optimize(speed)]
55
| ^^^^^^^^^^^^^^^^^^
6+
LL | struct F;
7+
| --------- invalid target
8+
9+
error: attribute applied to an invalid target
10+
--> $DIR/optimize.rs:12:5
611
|
7-
note: the lint level is defined here
8-
--> $DIR/optimize.rs:3:9
12+
LL | #[optimize(speed)]
13+
| ^^^^^^^^^^^^^^^^^^
14+
LL | / {
15+
LL | | 1
16+
LL | | };
17+
| |_____- invalid target
18+
19+
error: attribute applied to an invalid target
20+
--> $DIR/optimize.rs:21:1
921
|
10-
LL | #![deny(unused_attributes)]
11-
| ^^^^^^^^^^^^^^^^^
22+
LL | #[optimize(speed)]
23+
| ^^^^^^^^^^^^^^^^^^
24+
LL | mod valid_module {}
25+
| ------------------- invalid target
1226

13-
error: attribute should be applied to function or closure
14-
--> $DIR/optimize.rs:10:5
27+
error: attribute applied to an invalid target
28+
--> $DIR/optimize.rs:24:1
1529
|
16-
LL | #[optimize(speed)]
17-
| ^^^^^^^^^^^^^^^^^^
30+
LL | #[optimize(speed)]
31+
| ^^^^^^^^^^^^^^^^^^
32+
LL | impl F {}
33+
| --------- invalid target
1834

19-
error: aborting due to 2 previous errors
35+
error: aborting due to 4 previous errors
2036

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//@ edition: 2024
2+
//@ compile-flags: -Zunstable-options
3+
//@ run-pass
4+
#![feature(gen_blocks)]
5+
#![feature(optimize_attribute)]
6+
#![feature(stmt_expr_attributes)]
7+
#![feature(async_iterator)]
8+
#![allow(dead_code)]
9+
10+
// make sure that other attributes e.g. `optimize` can be applied to gen blocks and functions
11+
12+
fn main() { }
13+
14+
fn optimize_gen_block() -> impl Iterator<Item = ()> {
15+
#[optimize(speed)]
16+
gen { yield (); }
17+
}
18+
19+
#[optimize(speed)]
20+
gen fn optimize_gen_fn() -> i32 {
21+
yield 1;
22+
yield 2;
23+
yield 3;
24+
}
25+
26+
#[optimize(speed)]
27+
async gen fn optimize_async_gen_fn() -> i32 {
28+
yield 1;
29+
yield 2;
30+
yield 3;
31+
}
32+
33+
use std::async_iter::AsyncIterator;
34+
35+
pub fn deduce() -> impl AsyncIterator<Item = ()> {
36+
#[optimize(size)]
37+
async gen {
38+
yield ();
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
#![crate_type="rlib"]
2-
#![optimize(speed)] //~ ERROR the `#[optimize]` attribute is an experimental feature
3-
4-
#[optimize(size)] //~ ERROR the `#[optimize]` attribute is an experimental feature
5-
mod module {
62

73
#[optimize(size)] //~ ERROR the `#[optimize]` attribute is an experimental feature
84
fn size() {}
@@ -14,5 +10,3 @@ fn speed() {}
1410
//~^ ERROR the `#[optimize]` attribute is an experimental feature
1511
//~| ERROR E0722
1612
fn not_known() {}
17-
18-
}
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
error[E0658]: the `#[optimize]` attribute is an experimental feature
2-
--> $DIR/feature-gate-optimize_attribute.rs:2:1
3-
|
4-
LL | #![optimize(speed)]
5-
| ^^^^^^^^^^^^^^^^^^^
6-
|
7-
= note: see issue #54882 <https://github.com/rust-lang/rust/issues/54882> for more information
8-
= help: add `#![feature(optimize_attribute)]` to the crate attributes to enable
9-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
10-
11-
error[E0658]: the `#[optimize]` attribute is an experimental feature
12-
--> $DIR/feature-gate-optimize_attribute.rs:4:1
13-
|
14-
LL | #[optimize(size)]
15-
| ^^^^^^^^^^^^^^^^^
16-
|
17-
= note: see issue #54882 <https://github.com/rust-lang/rust/issues/54882> for more information
18-
= help: add `#![feature(optimize_attribute)]` to the crate attributes to enable
19-
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
20-
21-
error[E0658]: the `#[optimize]` attribute is an experimental feature
22-
--> $DIR/feature-gate-optimize_attribute.rs:7:1
2+
--> $DIR/feature-gate-optimize_attribute.rs:3:1
233
|
244
LL | #[optimize(size)]
255
| ^^^^^^^^^^^^^^^^^
@@ -29,7 +9,7 @@ LL | #[optimize(size)]
299
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
3010

3111
error[E0658]: the `#[optimize]` attribute is an experimental feature
32-
--> $DIR/feature-gate-optimize_attribute.rs:10:1
12+
--> $DIR/feature-gate-optimize_attribute.rs:6:1
3313
|
3414
LL | #[optimize(speed)]
3515
| ^^^^^^^^^^^^^^^^^^
@@ -39,7 +19,7 @@ LL | #[optimize(speed)]
3919
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
4020

4121
error[E0658]: the `#[optimize]` attribute is an experimental feature
42-
--> $DIR/feature-gate-optimize_attribute.rs:13:1
22+
--> $DIR/feature-gate-optimize_attribute.rs:9:1
4323
|
4424
LL | #[optimize(banana)]
4525
| ^^^^^^^^^^^^^^^^^^^
@@ -49,12 +29,12 @@ LL | #[optimize(banana)]
4929
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
5030

5131
error[E0722]: invalid argument
52-
--> $DIR/feature-gate-optimize_attribute.rs:13:12
32+
--> $DIR/feature-gate-optimize_attribute.rs:9:12
5333
|
5434
LL | #[optimize(banana)]
5535
| ^^^^^^
5636

57-
error: aborting due to 6 previous errors
37+
error: aborting due to 4 previous errors
5838

5939
Some errors have detailed explanations: E0658, E0722.
6040
For more information about an error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)