mirror of
https://github.com/rust-lang/rust.git
synced 2026-04-30 14:52:56 +03:00
Distinguish error message for #[diagnostic::on_const] on const trait impls
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
@@ -629,7 +636,16 @@ fn check_diagnostic_on_const(
|
||||
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 => {}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user