mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 16:24:46 +00:00
Emit an error if #[optimize]
is applied to an incompatible item
This commit is contained in:
parent
99322d84c4
commit
6d7bb127e6
@ -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]`
|
||||
|
||||
|
@ -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] => {
|
||||
@ -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,
|
||||
|
@ -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 {
|
||||
|
28
tests/ui/attributes/optimize.rs
Normal file
28
tests/ui/attributes/optimize.rs
Normal file
@ -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
tests/ui/attributes/optimize.stderr
Normal file
20
tests/ui/attributes/optimize.stderr
Normal file
@ -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…
Reference in New Issue
Block a user