Rollup merge of #149627 - lapla-cogito:diag_onconst, r=jdonszelmann
Point to the item that is incorrectly annotated with `#[diagnostic::on_const]`
close rust-lang/rust#149606
r? estebank
diff --git a/compiler/rustc_passes/messages.ftl b/compiler/rustc_passes/messages.ftl
index 91a20a1..09ac3ae 100644
--- a/compiler/rustc_passes/messages.ftl
+++ b/compiler/rustc_passes/messages.ftl
@@ -98,6 +98,7 @@
passes_diagnostic_diagnostic_on_const_only_for_trait_impls =
`#[diagnostic::on_const]` can only be applied to trait impls
+ .label = not a trait impl
passes_diagnostic_diagnostic_on_unimplemented_only_for_traits =
`#[diagnostic::on_unimplemented]` can only be applied to trait definitions
diff --git a/compiler/rustc_passes/src/check_attr.rs b/compiler/rustc_passes/src/check_attr.rs
index e3b2a92..1289867 100644
--- a/compiler/rustc_passes/src/check_attr.rs
+++ b/compiler/rustc_passes/src/check_attr.rs
@@ -57,7 +57,10 @@
#[derive(LintDiagnostic)]
#[diag(passes_diagnostic_diagnostic_on_const_only_for_trait_impls)]
-struct DiagnosticOnConstOnlyForTraitImpls;
+struct DiagnosticOnConstOnlyForTraitImpls {
+ #[label]
+ item_span: Span,
+}
fn target_from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem<'_>) -> Target {
match impl_item.kind {
@@ -541,11 +544,12 @@ fn check_diagnostic_on_const(
ItemLike::ForeignItem => {}
}
}
+ let item_span = self.tcx.hir_span(hir_id);
self.tcx.emit_node_span_lint(
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
hir_id,
attr_span,
- DiagnosticOnConstOnlyForTraitImpls,
+ DiagnosticOnConstOnlyForTraitImpls { item_span },
);
}
diff --git a/tests/ui/diagnostic_namespace/on_const/misplaced_attr.stderr b/tests/ui/diagnostic_namespace/on_const/misplaced_attr.stderr
index a12c00c..baa0b11 100644
--- a/tests/ui/diagnostic_namespace/on_const/misplaced_attr.stderr
+++ b/tests/ui/diagnostic_namespace/on_const/misplaced_attr.stderr
@@ -3,6 +3,9 @@
|
LL | #[diagnostic::on_const(message = "tadaa", note = "boing")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL |
+LL | pub struct Foo;
+ | -------------- not a trait impl
|
note: the lint level is defined here
--> $DIR/misplaced_attr.rs:2:9
@@ -15,18 +18,27 @@
|
LL | #[diagnostic::on_const(message = "tadaa", note = "boing")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL |
+LL | impl const PartialEq for Foo {
+ | ---------------------------- not a trait impl
error: `#[diagnostic::on_const]` can only be applied to trait impls
--> $DIR/misplaced_attr.rs:16:1
|
LL | #[diagnostic::on_const(message = "tadaa", note = "boing")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL |
+LL | impl Foo {
+ | -------- not a trait impl
error: `#[diagnostic::on_const]` can only be applied to trait impls
--> $DIR/misplaced_attr.rs:25:5
|
LL | #[diagnostic::on_const(message = "tadaa", note = "boing")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+LL |
+LL | fn partial_cmp(&self, other: &Foo) -> Option<std::cmp::Ordering> {
+ | ---------------------------------------------------------------- not a trait impl
error: aborting due to 4 previous errors