mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 14:55:26 +00:00
misapplied optimize attribute throws a compilation error (#128488)
This commit is contained in:
parent
798fb83f7d
commit
080103f1ed
@ -553,9 +553,9 @@ passes_only_has_effect_on =
|
|||||||
*[unspecified] (unspecified--this is a compiler bug)
|
*[unspecified] (unspecified--this is a compiler bug)
|
||||||
}
|
}
|
||||||
|
|
||||||
passes_optimize_not_fn_or_closure =
|
passes_optimize_invalid_target =
|
||||||
attribute should be applied to function or closure
|
attribute applied to an invalid target
|
||||||
.label = not a function or closure
|
.label = invalid target
|
||||||
|
|
||||||
passes_outer_crate_level_attr =
|
passes_outer_crate_level_attr =
|
||||||
crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
|
crate-level attribute should be an inner attribute: add an exclamation mark: `#![foo]`
|
||||||
|
@ -124,7 +124,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
|||||||
}
|
}
|
||||||
[sym::inline, ..] => self.check_inline(hir_id, attr, span, target),
|
[sym::inline, ..] => self.check_inline(hir_id, attr, span, target),
|
||||||
[sym::coverage, ..] => self.check_coverage(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, ..] => {
|
[sym::no_sanitize, ..] => {
|
||||||
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
|
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
|
||||||
}
|
}
|
||||||
@ -433,23 +433,19 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
|
|||||||
|
|
||||||
/// Checks that `#[optimize(..)]` is applied to a function/closure/method,
|
/// Checks that `#[optimize(..)]` is applied to a function/closure/method,
|
||||||
/// or to an impl block or module.
|
/// 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, span: Span, target: Target) {
|
||||||
fn check_optimize(&self, hir_id: HirId, attr: &Attribute, target: Target) {
|
let is_valid = matches!(
|
||||||
match target {
|
target,
|
||||||
Target::Fn
|
Target::Fn
|
||||||
| Target::Closure
|
| Target::Closure
|
||||||
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
|
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
|
||||||
| Target::Impl
|
);
|
||||||
| Target::Mod => {}
|
if !is_valid {
|
||||||
|
self.dcx().emit_err(errors::OptimizeInvalidTarget {
|
||||||
_ => {
|
attr_span: attr.span,
|
||||||
self.tcx.emit_node_span_lint(
|
defn_span: span,
|
||||||
UNUSED_ATTRIBUTES,
|
on_crate: hir_id == CRATE_HIR_ID,
|
||||||
hir_id,
|
});
|
||||||
attr.span,
|
|
||||||
errors::OptimizeNotFnOrClosure,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,9 +76,15 @@ pub(crate) struct CoverageNotFnOrClosure {
|
|||||||
pub defn_span: Span,
|
pub defn_span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(LintDiagnostic)]
|
#[derive(Diagnostic)]
|
||||||
#[diag(passes_optimize_not_fn_or_closure)]
|
#[diag(passes_optimize_invalid_target)]
|
||||||
pub(crate) struct OptimizeNotFnOrClosure;
|
pub(crate) struct OptimizeInvalidTarget {
|
||||||
|
#[primary_span]
|
||||||
|
pub attr_span: Span,
|
||||||
|
#[label]
|
||||||
|
pub defn_span: Span,
|
||||||
|
pub on_crate: bool,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Diagnostic)]
|
#[derive(Diagnostic)]
|
||||||
#[diag(passes_should_be_applied_to_fn)]
|
#[diag(passes_should_be_applied_to_fn)]
|
||||||
|
@ -3,11 +3,13 @@
|
|||||||
#![deny(unused_attributes)]
|
#![deny(unused_attributes)]
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
#[optimize(speed)] //~ ERROR attribute should be applied to function or closure
|
//@ edition: 2018
|
||||||
|
|
||||||
|
#[optimize(speed)] //~ ERROR attribute applied to an invalid target
|
||||||
struct F;
|
struct F;
|
||||||
|
|
||||||
fn invalid() {
|
fn invalid() {
|
||||||
#[optimize(speed)] //~ ERROR attribute should be applied to function or closure
|
#[optimize(speed)] //~ ERROR attribute applied to an invalid target
|
||||||
{
|
{
|
||||||
1
|
1
|
||||||
};
|
};
|
||||||
@ -16,13 +18,25 @@ fn invalid() {
|
|||||||
#[optimize(speed)]
|
#[optimize(speed)]
|
||||||
fn valid() {}
|
fn valid() {}
|
||||||
|
|
||||||
#[optimize(speed)]
|
#[optimize(speed)] //~ ERROR attribute applied to an invalid target
|
||||||
mod valid_module {}
|
mod valid_module {}
|
||||||
|
|
||||||
#[optimize(speed)]
|
#[optimize(speed)] //~ ERROR attribute applied to an invalid target
|
||||||
impl F {}
|
impl F {}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = #[optimize(speed)]
|
let _ = #[optimize(speed)]
|
||||||
(|| 1);
|
(|| 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
fn async_block() -> impl Future<Output = ()> {
|
||||||
|
#[optimize(speed)]
|
||||||
|
async { }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[optimize(speed)]
|
||||||
|
async fn async_fn() {
|
||||||
|
()
|
||||||
|
}
|
||||||
|
@ -1,20 +1,36 @@
|
|||||||
error: attribute should be applied to function or closure
|
error: attribute applied to an invalid target
|
||||||
--> $DIR/optimize.rs:6:1
|
--> $DIR/optimize.rs:8:1
|
||||||
|
|
|
|
||||||
LL | #[optimize(speed)]
|
LL | #[optimize(speed)]
|
||||||
| ^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^
|
||||||
|
|
LL | struct F;
|
||||||
note: the lint level is defined here
|
| --------- invalid target
|
||||||
--> $DIR/optimize.rs:3:9
|
|
||||||
|
|
|
||||||
LL | #![deny(unused_attributes)]
|
|
||||||
| ^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
||||||
error: attribute should be applied to function or closure
|
error: attribute applied to an invalid target
|
||||||
--> $DIR/optimize.rs:10:5
|
--> $DIR/optimize.rs:12:5
|
||||||
|
|
|
|
||||||
LL | #[optimize(speed)]
|
LL | #[optimize(speed)]
|
||||||
| ^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^
|
||||||
|
LL | / {
|
||||||
|
LL | | 1
|
||||||
|
LL | | };
|
||||||
|
| |_____- invalid target
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: attribute applied to an invalid target
|
||||||
|
--> $DIR/optimize.rs:21:1
|
||||||
|
|
|
||||||
|
LL | #[optimize(speed)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^
|
||||||
|
LL | mod valid_module {}
|
||||||
|
| ------------------- invalid target
|
||||||
|
|
||||||
|
error: attribute applied to an invalid target
|
||||||
|
--> $DIR/optimize.rs:24:1
|
||||||
|
|
|
||||||
|
LL | #[optimize(speed)]
|
||||||
|
| ^^^^^^^^^^^^^^^^^^
|
||||||
|
LL | impl F {}
|
||||||
|
| --------- invalid target
|
||||||
|
|
||||||
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
|
40
tests/ui/coroutine/other-attribute-on-gen.rs
Normal file
40
tests/ui/coroutine/other-attribute-on-gen.rs
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
//@ edition: 2024
|
||||||
|
//@ compile-flags: -Zunstable-options
|
||||||
|
//@ run-pass
|
||||||
|
#![feature(gen_blocks)]
|
||||||
|
#![feature(optimize_attribute)]
|
||||||
|
#![feature(stmt_expr_attributes)]
|
||||||
|
#![feature(async_iterator)]
|
||||||
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
// make sure that other attributes e.g. `optimize` can be applied to gen blocks and functions
|
||||||
|
|
||||||
|
fn main() { }
|
||||||
|
|
||||||
|
fn optimize_gen_block() -> impl Iterator<Item = ()> {
|
||||||
|
#[optimize(speed)]
|
||||||
|
gen { yield (); }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[optimize(speed)]
|
||||||
|
gen fn optimize_gen_fn() -> i32 {
|
||||||
|
yield 1;
|
||||||
|
yield 2;
|
||||||
|
yield 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[optimize(speed)]
|
||||||
|
async gen fn optimize_async_gen_fn() -> i32 {
|
||||||
|
yield 1;
|
||||||
|
yield 2;
|
||||||
|
yield 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
use std::async_iter::AsyncIterator;
|
||||||
|
|
||||||
|
pub fn deduce() -> impl AsyncIterator<Item = ()> {
|
||||||
|
#[optimize(size)]
|
||||||
|
async gen {
|
||||||
|
yield ();
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,4 @@
|
|||||||
#![crate_type="rlib"]
|
#![crate_type="rlib"]
|
||||||
#![optimize(speed)] //~ ERROR the `#[optimize]` attribute is an experimental feature
|
|
||||||
|
|
||||||
#[optimize(size)] //~ ERROR the `#[optimize]` attribute is an experimental feature
|
|
||||||
mod module {
|
|
||||||
|
|
||||||
#[optimize(size)] //~ ERROR the `#[optimize]` attribute is an experimental feature
|
#[optimize(size)] //~ ERROR the `#[optimize]` attribute is an experimental feature
|
||||||
fn size() {}
|
fn size() {}
|
||||||
@ -14,5 +10,3 @@ fn speed() {}
|
|||||||
//~^ ERROR the `#[optimize]` attribute is an experimental feature
|
//~^ ERROR the `#[optimize]` attribute is an experimental feature
|
||||||
//~| ERROR E0722
|
//~| ERROR E0722
|
||||||
fn not_known() {}
|
fn not_known() {}
|
||||||
|
|
||||||
}
|
|
||||||
|
@ -1,15 +1,5 @@
|
|||||||
error[E0658]: the `#[optimize]` attribute is an experimental feature
|
error[E0658]: the `#[optimize]` attribute is an experimental feature
|
||||||
--> $DIR/feature-gate-optimize_attribute.rs:2:1
|
--> $DIR/feature-gate-optimize_attribute.rs:3:1
|
||||||
|
|
|
||||||
LL | #![optimize(speed)]
|
|
||||||
| ^^^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
|
||||||
= note: see issue #54882 <https://github.com/rust-lang/rust/issues/54882> for more information
|
|
||||||
= help: add `#![feature(optimize_attribute)]` to the crate attributes to enable
|
|
||||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
|
||||||
|
|
||||||
error[E0658]: the `#[optimize]` attribute is an experimental feature
|
|
||||||
--> $DIR/feature-gate-optimize_attribute.rs:4:1
|
|
||||||
|
|
|
|
||||||
LL | #[optimize(size)]
|
LL | #[optimize(size)]
|
||||||
| ^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^
|
||||||
@ -19,17 +9,7 @@ LL | #[optimize(size)]
|
|||||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||||
|
|
||||||
error[E0658]: the `#[optimize]` attribute is an experimental feature
|
error[E0658]: the `#[optimize]` attribute is an experimental feature
|
||||||
--> $DIR/feature-gate-optimize_attribute.rs:7:1
|
--> $DIR/feature-gate-optimize_attribute.rs:6:1
|
||||||
|
|
|
||||||
LL | #[optimize(size)]
|
|
||||||
| ^^^^^^^^^^^^^^^^^
|
|
||||||
|
|
|
||||||
= note: see issue #54882 <https://github.com/rust-lang/rust/issues/54882> for more information
|
|
||||||
= help: add `#![feature(optimize_attribute)]` to the crate attributes to enable
|
|
||||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
|
||||||
|
|
||||||
error[E0658]: the `#[optimize]` attribute is an experimental feature
|
|
||||||
--> $DIR/feature-gate-optimize_attribute.rs:10:1
|
|
||||||
|
|
|
|
||||||
LL | #[optimize(speed)]
|
LL | #[optimize(speed)]
|
||||||
| ^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^
|
||||||
@ -39,7 +19,7 @@ LL | #[optimize(speed)]
|
|||||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||||
|
|
||||||
error[E0658]: the `#[optimize]` attribute is an experimental feature
|
error[E0658]: the `#[optimize]` attribute is an experimental feature
|
||||||
--> $DIR/feature-gate-optimize_attribute.rs:13:1
|
--> $DIR/feature-gate-optimize_attribute.rs:9:1
|
||||||
|
|
|
|
||||||
LL | #[optimize(banana)]
|
LL | #[optimize(banana)]
|
||||||
| ^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^
|
||||||
@ -49,12 +29,12 @@ LL | #[optimize(banana)]
|
|||||||
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
|
||||||
|
|
||||||
error[E0722]: invalid argument
|
error[E0722]: invalid argument
|
||||||
--> $DIR/feature-gate-optimize_attribute.rs:13:12
|
--> $DIR/feature-gate-optimize_attribute.rs:9:12
|
||||||
|
|
|
|
||||||
LL | #[optimize(banana)]
|
LL | #[optimize(banana)]
|
||||||
| ^^^^^^
|
| ^^^^^^
|
||||||
|
|
||||||
error: aborting due to 6 previous errors
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
Some errors have detailed explanations: E0658, E0722.
|
Some errors have detailed explanations: E0658, E0722.
|
||||||
For more information about an error, try `rustc --explain E0658`.
|
For more information about an error, try `rustc --explain E0658`.
|
||||||
|
Loading…
Reference in New Issue
Block a user