misapplied optimize attribute throws a compilation error (#128488)
This commit is contained in:
parent
798fb83f7d
commit
080103f1ed
8 changed files with 117 additions and 71 deletions
|
@ -553,9 +553,9 @@ 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_optimize_invalid_target =
|
||||
attribute applied to an invalid target
|
||||
.label = invalid target
|
||||
|
||||
passes_outer_crate_level_attr =
|
||||
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::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, ..] => {
|
||||
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,
|
||||
/// 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, target: Target) {
|
||||
match target {
|
||||
fn check_optimize(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
|
||||
let is_valid = matches!(
|
||||
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,
|
||||
);
|
||||
}
|
||||
| Target::Closure
|
||||
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
|
||||
);
|
||||
if !is_valid {
|
||||
self.dcx().emit_err(errors::OptimizeInvalidTarget {
|
||||
attr_span: attr.span,
|
||||
defn_span: span,
|
||||
on_crate: hir_id == CRATE_HIR_ID,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -76,9 +76,15 @@ pub(crate) struct CoverageNotFnOrClosure {
|
|||
pub defn_span: Span,
|
||||
}
|
||||
|
||||
#[derive(LintDiagnostic)]
|
||||
#[diag(passes_optimize_not_fn_or_closure)]
|
||||
pub(crate) struct OptimizeNotFnOrClosure;
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(passes_optimize_invalid_target)]
|
||||
pub(crate) struct OptimizeInvalidTarget {
|
||||
#[primary_span]
|
||||
pub attr_span: Span,
|
||||
#[label]
|
||||
pub defn_span: Span,
|
||||
pub on_crate: bool,
|
||||
}
|
||||
|
||||
#[derive(Diagnostic)]
|
||||
#[diag(passes_should_be_applied_to_fn)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue