Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ passes_deprecated_attribute =
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
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ struct DiagnosticOnUnimplementedOnlyForTraits;

#[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 {
Expand Down Expand Up @@ -541,11 +544,12 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
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 },
);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/ui/diagnostic_namespace/on_const/misplaced_attr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ error: `#[diagnostic::on_const]` can only be applied to trait impls
|
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
Expand All @@ -15,18 +18,27 @@ error: `#[diagnostic::on_const]` can only be applied to trait impls
|
LL | #[diagnostic::on_const(message = "tadaa", note = "boing")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | impl const PartialEq for Foo {
| ---------------------------- not a trait impl
Comment on lines +21 to +23
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to fix this to specify on const impls that the annotation only works on non-const ones, but that can be done in a follow up PR.


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

Loading