1
Fork 0

Overhaul error messages for disallowed coverage attributes

This commit is contained in:
Zalathar 2024-12-25 14:57:21 +11:00
parent 9124662da3
commit 3996209398
6 changed files with 160 additions and 75 deletions

View file

@ -112,9 +112,11 @@ passes_coroutine_on_non_closure =
attribute should be applied to closures attribute should be applied to closures
.label = not a closure .label = not a closure
passes_coverage_not_fn_or_closure = passes_coverage_attribute_not_allowed =
attribute should be applied to a function definition or closure coverage attribute not allowed here
.label = not a function or closure .not_fn_impl_mod = not a function, impl block, or module
.no_body = function has no body
.help = coverage attribute can be applied to a function (with body), impl block, or module
passes_dead_codes = passes_dead_codes =
{ $multiple -> { $multiple ->

View file

@ -432,21 +432,34 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
/// Checks that `#[coverage(..)]` is applied to a function/closure/method, /// Checks that `#[coverage(..)]` is applied to a function/closure/method,
/// or to an impl block or module. /// or to an impl block or module.
fn check_coverage(&self, attr: &Attribute, span: Span, target: Target) { fn check_coverage(&self, attr: &Attribute, target_span: Span, target: Target) {
let mut not_fn_impl_mod = None;
let mut no_body = None;
match target { match 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::Impl
| Target::Mod => {} | Target::Mod => return,
// These are "functions", but they aren't allowed because they don't
// have a body, so the usual explanation would be confusing.
Target::Method(MethodKind::Trait { body: false }) | Target::ForeignFn => {
no_body = Some(target_span);
}
_ => { _ => {
self.dcx().emit_err(errors::CoverageNotFnOrClosure { not_fn_impl_mod = Some(target_span);
attr_span: attr.span,
defn_span: span,
});
} }
} }
self.dcx().emit_err(errors::CoverageAttributeNotAllowed {
attr_span: attr.span,
not_fn_impl_mod,
no_body,
help: (),
});
} }
/// Checks that `#[optimize(..)]` is applied to a function/closure/method, /// Checks that `#[optimize(..)]` is applied to a function/closure/method,

View file

@ -71,13 +71,21 @@ pub(crate) struct InlineNotFnOrClosure {
pub defn_span: Span, pub defn_span: Span,
} }
/// "coverage attribute not allowed here"
#[derive(Diagnostic)] #[derive(Diagnostic)]
#[diag(passes_coverage_not_fn_or_closure, code = E0788)] #[diag(passes_coverage_attribute_not_allowed, code = E0788)]
pub(crate) struct CoverageNotFnOrClosure { pub(crate) struct CoverageAttributeNotAllowed {
#[primary_span] #[primary_span]
pub attr_span: Span, pub attr_span: Span,
#[label] /// "not a function, impl block, or module"
pub defn_span: Span, #[label(passes_not_fn_impl_mod)]
pub not_fn_impl_mod: Option<Span>,
/// "function has no body"
#[label(passes_no_body)]
pub no_body: Option<Span>,
/// "coverage attribute can be applied to a function (with body), impl block, or module"
#[help]
pub help: (),
} }
#[derive(Diagnostic)] #[derive(Diagnostic)]

View file

@ -8,15 +8,17 @@ LL | let _closure_expr = #[coverage(off)] || ();
= help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable = help: add `#![feature(stmt_expr_attributes)]` to the crate attributes to enable
= 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[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:14:1 --> $DIR/allowed-positions.rs:14:1
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | type MyTypeAlias = (); LL | type MyTypeAlias = ();
| ---------------------- not a function or closure | ---------------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:17:1 --> $DIR/allowed-positions.rs:17:1
| |
LL | #[coverage(off)] LL | #[coverage(off)]
@ -27,9 +29,11 @@ LL | | const TRAIT_ASSOC_CONST: u32;
... | ... |
LL | | fn trait_assoc_fn(); LL | | fn trait_assoc_fn();
LL | | } LL | | }
| |_- not a function or closure | |_- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:61:1 --> $DIR/allowed-positions.rs:61:1
| |
LL | #[coverage(off)] LL | #[coverage(off)]
@ -38,119 +42,149 @@ LL | / struct MyStruct {
LL | | #[coverage(off)] LL | | #[coverage(off)]
LL | | field: u32, LL | | field: u32,
LL | | } LL | | }
| |_- not a function or closure | |_- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:63:5 --> $DIR/allowed-positions.rs:63:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | field: u32, LL | field: u32,
| ---------- not a function or closure | ---------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:88:5 --> $DIR/allowed-positions.rs:88:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | let _ = (); LL | let _ = ();
| ----------- not a function or closure | ----------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:94:5 --> $DIR/allowed-positions.rs:94:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | let _let_closure = || (); LL | let _let_closure = || ();
| ------------------------- not a function or closure | ------------------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:110:9 --> $DIR/allowed-positions.rs:110:9
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | () => (), LL | () => (),
| -------- not a function or closure | -------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:114:5 --> $DIR/allowed-positions.rs:114:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | return (); LL | return ();
| --------- not a function or closure | --------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:19:5 --> $DIR/allowed-positions.rs:19:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | const TRAIT_ASSOC_CONST: u32; LL | const TRAIT_ASSOC_CONST: u32;
| ----------------------------- not a function or closure | ----------------------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:22:5 --> $DIR/allowed-positions.rs:22:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | type TraitAssocType; LL | type TraitAssocType;
| -------------------- not a function or closure | -------------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:25:5 --> $DIR/allowed-positions.rs:25:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | fn trait_method(&self); LL | fn trait_method(&self);
| ----------------------- not a function or closure | ----------------------- function has no body
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:31:5 --> $DIR/allowed-positions.rs:31:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | fn trait_assoc_fn(); LL | fn trait_assoc_fn();
| -------------------- not a function or closure | -------------------- function has no body
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:39:5 --> $DIR/allowed-positions.rs:39:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | type TraitAssocType = Self; LL | type TraitAssocType = Self;
| --------------------------- not a function or closure | --------------------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:56:5 --> $DIR/allowed-positions.rs:56:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | type T = impl Copy; LL | type T = impl Copy;
| ------------------- not a function or closure | ------------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:76:5 --> $DIR/allowed-positions.rs:76:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | static X: u32; LL | static X: u32;
| -------------- not a function or closure | -------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:79:5 --> $DIR/allowed-positions.rs:79:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | type T; LL | type T;
| ------- not a function or closure | ------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/allowed-positions.rs:82:5 --> $DIR/allowed-positions.rs:82:5
| |
LL | #[coverage(off)] LL | #[coverage(off)]
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
LL | fn foreign_fn(); LL | fn foreign_fn();
| ---------------- not a function or closure | ---------------- function has no body
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error: aborting due to 18 previous errors error: aborting due to 18 previous errors

View file

@ -154,16 +154,18 @@ LL | #[coverage(off)]
LL | #[coverage(on)] LL | #[coverage(on)]
| |
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/name-value.rs:21:1 --> $DIR/name-value.rs:21:1
| |
LL | #[coverage = "off"] LL | #[coverage = "off"]
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
... ...
LL | struct MyStruct; LL | struct MyStruct;
| ---------------- not a function or closure | ---------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/name-value.rs:35:1 --> $DIR/name-value.rs:35:1
| |
LL | #[coverage = "off"] LL | #[coverage = "off"]
@ -174,52 +176,64 @@ LL | | #[coverage = "off"]
... | ... |
LL | | type T; LL | | type T;
LL | | } LL | | }
| |_- not a function or closure | |_- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/name-value.rs:39:5 --> $DIR/name-value.rs:39:5
| |
LL | #[coverage = "off"] LL | #[coverage = "off"]
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
... ...
LL | const X: u32; LL | const X: u32;
| ------------- not a function or closure | ------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/name-value.rs:44:5 --> $DIR/name-value.rs:44:5
| |
LL | #[coverage = "off"] LL | #[coverage = "off"]
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
... ...
LL | type T; LL | type T;
| ------- not a function or closure | ------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/name-value.rs:29:5 --> $DIR/name-value.rs:29:5
| |
LL | #[coverage = "off"] LL | #[coverage = "off"]
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
... ...
LL | const X: u32 = 7; LL | const X: u32 = 7;
| ----------------- not a function or closure | ----------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/name-value.rs:53:5 --> $DIR/name-value.rs:53:5
| |
LL | #[coverage = "off"] LL | #[coverage = "off"]
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
... ...
LL | const X: u32 = 8; LL | const X: u32 = 8;
| ----------------- not a function or closure | ----------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/name-value.rs:58:5 --> $DIR/name-value.rs:58:5
| |
LL | #[coverage = "off"] LL | #[coverage = "off"]
| ^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^
... ...
LL | type T = (); LL | type T = ();
| ------------ not a function or closure | ------------ not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error: aborting due to 19 previous errors error: aborting due to 19 previous errors

View file

@ -154,16 +154,18 @@ LL | #[coverage(off)]
LL | #[coverage(on)] LL | #[coverage(on)]
| |
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/word-only.rs:21:1 --> $DIR/word-only.rs:21:1
| |
LL | #[coverage] LL | #[coverage]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
... ...
LL | struct MyStruct; LL | struct MyStruct;
| ---------------- not a function or closure | ---------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/word-only.rs:35:1 --> $DIR/word-only.rs:35:1
| |
LL | #[coverage] LL | #[coverage]
@ -174,52 +176,64 @@ LL | | #[coverage]
... | ... |
LL | | type T; LL | | type T;
LL | | } LL | | }
| |_- not a function or closure | |_- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/word-only.rs:39:5 --> $DIR/word-only.rs:39:5
| |
LL | #[coverage] LL | #[coverage]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
... ...
LL | const X: u32; LL | const X: u32;
| ------------- not a function or closure | ------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/word-only.rs:44:5 --> $DIR/word-only.rs:44:5
| |
LL | #[coverage] LL | #[coverage]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
... ...
LL | type T; LL | type T;
| ------- not a function or closure | ------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/word-only.rs:29:5 --> $DIR/word-only.rs:29:5
| |
LL | #[coverage] LL | #[coverage]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
... ...
LL | const X: u32 = 7; LL | const X: u32 = 7;
| ----------------- not a function or closure | ----------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/word-only.rs:53:5 --> $DIR/word-only.rs:53:5
| |
LL | #[coverage] LL | #[coverage]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
... ...
LL | const X: u32 = 8; LL | const X: u32 = 8;
| ----------------- not a function or closure | ----------------- not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error[E0788]: attribute should be applied to a function definition or closure error[E0788]: coverage attribute not allowed here
--> $DIR/word-only.rs:58:5 --> $DIR/word-only.rs:58:5
| |
LL | #[coverage] LL | #[coverage]
| ^^^^^^^^^^^ | ^^^^^^^^^^^
... ...
LL | type T = (); LL | type T = ();
| ------------ not a function or closure | ------------ not a function, impl block, or module
|
= help: coverage attribute can be applied to a function (with body), impl block, or module
error: aborting due to 19 previous errors error: aborting due to 19 previous errors