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

@ -432,21 +432,34 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
/// Checks that `#[coverage(..)]` is applied to a function/closure/method,
/// 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 {
Target::Fn
| Target::Closure
| Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
| 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 {
attr_span: attr.span,
defn_span: span,
});
not_fn_impl_mod = Some(target_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,

View file

@ -71,13 +71,21 @@ pub(crate) struct InlineNotFnOrClosure {
pub defn_span: Span,
}
/// "coverage attribute not allowed here"
#[derive(Diagnostic)]
#[diag(passes_coverage_not_fn_or_closure, code = E0788)]
pub(crate) struct CoverageNotFnOrClosure {
#[diag(passes_coverage_attribute_not_allowed, code = E0788)]
pub(crate) struct CoverageAttributeNotAllowed {
#[primary_span]
pub attr_span: Span,
#[label]
pub defn_span: Span,
/// "not a function, impl block, or module"
#[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)]