Rollup merge of #152054 - lapla-cogito:diag_const_trait_impl, r=estebank

Distinguish error message for `#[diagnostic::on_const]` on const trait impls

context: https://github.com/rust-lang/rust/pull/149627#discussion_r2589712535

Sorry for the delay between receiving the review and submitting this patch. I'll ask the original proposer to review it.

r? estebank
This commit is contained in:
Jonathan Brouwer 2026-02-03 23:29:57 +01:00 committed by GitHub
commit 9ff335cb8f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 4 deletions

View file

@ -87,6 +87,10 @@ passes_deprecated_annotation_has_no_effect =
passes_deprecated_attribute =
deprecated attribute must be paired with either stable or unstable attribute
passes_diagnostic_diagnostic_on_const_only_for_non_const_trait_impls =
`#[diagnostic::on_const]` can only be applied to non-const trait impls
.label = this is a const trait impl
passes_diagnostic_diagnostic_on_const_only_for_trait_impls =
`#[diagnostic::on_const]` can only be applied to trait impls
.label = not a trait impl

View file

@ -67,6 +67,13 @@ struct DiagnosticOnConstOnlyForTraitImpls {
item_span: Span,
}
#[derive(LintDiagnostic)]
#[diag(passes_diagnostic_diagnostic_on_const_only_for_non_const_trait_impls)]
struct DiagnosticOnConstOnlyForNonConstTraitImpls {
#[label]
item_span: Span,
}
fn target_from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem<'_>) -> Target {
match impl_item.kind {
hir::ImplItemKind::Const(..) => Target::AssocConst,
@ -630,7 +637,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
if target == (Target::Impl { of_trait: true }) {
match item.unwrap() {
ItemLike::Item(it) => match it.expect_impl().constness {
Constness::Const => {}
Constness::Const => {
let item_span = self.tcx.hir_span(hir_id);
self.tcx.emit_node_span_lint(
MISPLACED_DIAGNOSTIC_ATTRIBUTES,
hir_id,
attr_span,
DiagnosticOnConstOnlyForNonConstTraitImpls { item_span },
);
return;
}
Constness::NotConst => return,
},
ItemLike::ForeignItem => {}

View file

@ -6,7 +6,7 @@
pub struct Foo;
#[diagnostic::on_const(message = "tadaa", note = "boing")]
//~^ ERROR: `#[diagnostic::on_const]` can only be applied to trait impls
//~^ ERROR: `#[diagnostic::on_const]` can only be applied to non-const trait impls
impl const PartialEq for Foo {
fn eq(&self, _other: &Foo) -> bool {
true

View file

@ -13,14 +13,14 @@ note: the lint level is defined here
LL | #![deny(misplaced_diagnostic_attributes)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: `#[diagnostic::on_const]` can only be applied to trait impls
error: `#[diagnostic::on_const]` can only be applied to non-const trait impls
--> $DIR/misplaced_attr.rs:8:1
|
LL | #[diagnostic::on_const(message = "tadaa", note = "boing")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | impl const PartialEq for Foo {
| ---------------------------- not a trait impl
| ---------------------------- this is a const trait impl
error: `#[diagnostic::on_const]` can only be applied to trait impls
--> $DIR/misplaced_attr.rs:16:1