Skip to content

Commit

Permalink
Report the note when specified in diagnostic::on_unimplemented
Browse files Browse the repository at this point in the history
Signed-off-by: FedericoBruzzone <federico.bruzzone.i@gmail.com>
  • Loading branch information
FedericoBruzzone committed Sep 8, 2024
1 parent adf8d16 commit 7ffb12d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 5 deletions.
16 changes: 11 additions & 5 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let actual_prefix = rcvr_ty.prefix_string(self.tcx);
info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
let mut long_ty_file = None;
let (primary_message, label) = if unimplemented_traits.len() == 1
let (primary_message, label, notes) = if unimplemented_traits.len() == 1
&& unimplemented_traits_only
{
unimplemented_traits
Expand All @@ -1327,16 +1327,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if trait_ref.self_ty().references_error() || rcvr_ty.references_error()
{
// Avoid crashing.
return (None, None);
return (None, None, Vec::new());
}
let OnUnimplementedNote { message, label, .. } = self
let OnUnimplementedNote { message, label, notes, .. } = self
.err_ctxt()
.on_unimplemented_note(trait_ref, &obligation, &mut long_ty_file);
(message, label)
(message, label, notes)
})
.unwrap()
} else {
(None, None)
(None, None, Vec::new())
};
let primary_message = primary_message.unwrap_or_else(|| {
format!(
Expand All @@ -1363,6 +1363,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
"the following trait bounds were not satisfied:\n{bound_list}"
));
}
if !notes.is_empty() {
notes.iter().for_each(|note| {
err.span_note(span, note.to_string());
});
}

suggested_derive = self.suggest_derive(&mut err, unsatisfied_predicates);

unsatisfied_bounds = true;
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/traits/custom-on-unimplemented-diagnostic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#[diagnostic::on_unimplemented(message = "my message", label = "my label", note = "my note")]
pub trait ProviderLt {}

pub trait ProviderExt {
fn request<R>(&self) {
todo!()
}
}

impl<T: ?Sized + ProviderLt> ProviderExt for T {}

struct B;

fn main() {
B.request(); //~ ERROR 15:7: 15:14: my message [E0599]
}
36 changes: 36 additions & 0 deletions tests/ui/traits/custom-on-unimplemented-diagnostic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error[E0599]: my message
--> $DIR/custom-on-unimplemented-diagnostic.rs:15:7
|
LL | struct B;
| -------- method `request` not found for this struct because it doesn't satisfy `B: ProviderExt` or `B: ProviderLt`
...
LL | B.request();
| ^^^^^^^ my label
|
note: trait bound `B: ProviderLt` was not satisfied
--> $DIR/custom-on-unimplemented-diagnostic.rs:10:18
|
LL | impl<T: ?Sized + ProviderLt> ProviderExt for T {}
| ^^^^^^^^^^ ----------- -
| |
| unsatisfied trait bound introduced here
note: my note
--> $DIR/custom-on-unimplemented-diagnostic.rs:15:7
|
LL | B.request();
| ^^^^^^^
note: the trait `ProviderLt` must be implemented
--> $DIR/custom-on-unimplemented-diagnostic.rs:2:1
|
LL | pub trait ProviderLt {}
| ^^^^^^^^^^^^^^^^^^^^
= help: items from traits can only be used if the trait is implemented and in scope
note: `ProviderExt` defines an item `request`, perhaps you need to implement it
--> $DIR/custom-on-unimplemented-diagnostic.rs:4:1
|
LL | pub trait ProviderExt {
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.
15 changes: 15 additions & 0 deletions tests/ui/traits/on-unimplemented-diagnostic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
pub trait ProviderLt {}

pub trait ProviderExt {
fn request<R>(&self) {
todo!()
}
}

impl<T: ?Sized + ProviderLt> ProviderExt for T {}

struct B;

fn main() {
B.request(); //~ ERROR 14:7: 14:14: the method `request` exists for struct `B`, but its trait bounds were not satisfied [E0599]
}
31 changes: 31 additions & 0 deletions tests/ui/traits/on-unimplemented-diagnostic.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
error[E0599]: the method `request` exists for struct `B`, but its trait bounds were not satisfied
--> $DIR/on-unimplemented-diagnostic.rs:14:7
|
LL | struct B;
| -------- method `request` not found for this struct because it doesn't satisfy `B: ProviderExt` or `B: ProviderLt`
...
LL | B.request();
| ^^^^^^^ method cannot be called on `B` due to unsatisfied trait bounds
|
note: trait bound `B: ProviderLt` was not satisfied
--> $DIR/on-unimplemented-diagnostic.rs:9:18
|
LL | impl<T: ?Sized + ProviderLt> ProviderExt for T {}
| ^^^^^^^^^^ ----------- -
| |
| unsatisfied trait bound introduced here
note: the trait `ProviderLt` must be implemented
--> $DIR/on-unimplemented-diagnostic.rs:1:1
|
LL | pub trait ProviderLt {}
| ^^^^^^^^^^^^^^^^^^^^
= help: items from traits can only be used if the trait is implemented and in scope
note: `ProviderExt` defines an item `request`, perhaps you need to implement it
--> $DIR/on-unimplemented-diagnostic.rs:3:1
|
LL | pub trait ProviderExt {
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0599`.

0 comments on commit 7ffb12d

Please sign in to comment.