Skip to content

Commit b8ce945

Browse files
Report the note when specified in diagnostic::on_unimplemented
Signed-off-by: FedericoBruzzone <[email protected]>
1 parent adf8d16 commit b8ce945

8 files changed

+108
-47
lines changed

Diff for: compiler/rustc_hir_typeck/src/method/suggest.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13171317
let actual_prefix = rcvr_ty.prefix_string(self.tcx);
13181318
info!("unimplemented_traits.len() == {}", unimplemented_traits.len());
13191319
let mut long_ty_file = None;
1320-
let (primary_message, label) = if unimplemented_traits.len() == 1
1320+
let (primary_message, label, notes) = if unimplemented_traits.len() == 1
13211321
&& unimplemented_traits_only
13221322
{
13231323
unimplemented_traits
@@ -1327,16 +1327,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13271327
if trait_ref.self_ty().references_error() || rcvr_ty.references_error()
13281328
{
13291329
// Avoid crashing.
1330-
return (None, None);
1330+
return (None, None, Vec::new());
13311331
}
1332-
let OnUnimplementedNote { message, label, .. } = self
1332+
let OnUnimplementedNote { message, label, notes, .. } = self
13331333
.err_ctxt()
13341334
.on_unimplemented_note(trait_ref, &obligation, &mut long_ty_file);
1335-
(message, label)
1335+
(message, label, notes)
13361336
})
13371337
.unwrap()
13381338
} else {
1339-
(None, None)
1339+
(None, None, Vec::new())
13401340
};
13411341
let primary_message = primary_message.unwrap_or_else(|| {
13421342
format!(
@@ -1363,6 +1363,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13631363
"the following trait bounds were not satisfied:\n{bound_list}"
13641364
));
13651365
}
1366+
for note in notes {
1367+
err.note(note);
1368+
}
1369+
13661370
suggested_derive = self.suggest_derive(&mut err, unsatisfied_predicates);
13671371

13681372
unsatisfied_bounds = true;

Diff for: tests/ui/methods/suggest-convert-ptr-to-ref.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ note: the method `to_string` exists on the type `&u8`
1111
= note: the following trait bounds were not satisfied:
1212
`*const u8: std::fmt::Display`
1313
which is required by `*const u8: ToString`
14+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
1415

1516
error[E0599]: `*mut u8` doesn't implement `std::fmt::Display`
1617
--> $DIR/suggest-convert-ptr-to-ref.rs:8:22
@@ -25,6 +26,7 @@ note: the method `to_string` exists on the type `&&mut u8`
2526
= note: the following trait bounds were not satisfied:
2627
`*mut u8: std::fmt::Display`
2728
which is required by `*mut u8: ToString`
29+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
2830

2931
error[E0599]: no method named `make_ascii_lowercase` found for raw pointer `*mut u8` in the current scope
3032
--> $DIR/suggest-convert-ptr-to-ref.rs:9:7
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#[diagnostic::on_unimplemented(message = "my message", label = "my label", note = "my note")]
2+
pub trait ProviderLt {}
3+
4+
pub trait ProviderExt {
5+
fn request<R>(&self) {
6+
todo!()
7+
}
8+
}
9+
10+
impl<T: ?Sized + ProviderLt> ProviderExt for T {}
11+
12+
struct B;
13+
14+
fn main() {
15+
B.request();
16+
}
17+
//~^^ my message [E0599]
18+
//~| my label
19+
//~| my note
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error[E0599]: my message
2+
--> $DIR/custom-on-unimplemented-diagnostic.rs:15:7
3+
|
4+
LL | struct B;
5+
| -------- method `request` not found for this struct because it doesn't satisfy `B: ProviderExt` or `B: ProviderLt`
6+
...
7+
LL | B.request();
8+
| ^^^^^^^ my label
9+
|
10+
note: trait bound `B: ProviderLt` was not satisfied
11+
--> $DIR/custom-on-unimplemented-diagnostic.rs:10:18
12+
|
13+
LL | impl<T: ?Sized + ProviderLt> ProviderExt for T {}
14+
| ^^^^^^^^^^ ----------- -
15+
| |
16+
| unsatisfied trait bound introduced here
17+
= note: my note
18+
note: the trait `ProviderLt` must be implemented
19+
--> $DIR/custom-on-unimplemented-diagnostic.rs:2:1
20+
|
21+
LL | pub trait ProviderLt {}
22+
| ^^^^^^^^^^^^^^^^^^^^
23+
= help: items from traits can only be used if the trait is implemented and in scope
24+
note: `ProviderExt` defines an item `request`, perhaps you need to implement it
25+
--> $DIR/custom-on-unimplemented-diagnostic.rs:4:1
26+
|
27+
LL | pub trait ProviderExt {
28+
| ^^^^^^^^^^^^^^^^^^^^^
29+
30+
error: aborting due to 1 previous error
31+
32+
For more information about this error, try `rustc --explain E0599`.

Diff for: tests/ui/traits/on-unimplemented-diagnostic.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub trait ProviderLt {}
2+
3+
pub trait ProviderExt {
4+
fn request<R>(&self) {
5+
todo!()
6+
}
7+
}
8+
9+
impl<T: ?Sized + ProviderLt> ProviderExt for T {}
10+
11+
struct B;
12+
13+
fn main() {
14+
B.request(); //~ ERROR 14:7: 14:14: the method `request` exists for struct `B`, but its trait bounds were not satisfied [E0599]
15+
}

Diff for: tests/ui/traits/on-unimplemented-diagnostic.stderr

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0599]: the method `request` exists for struct `B`, but its trait bounds were not satisfied
2+
--> $DIR/on-unimplemented-diagnostic.rs:14:7
3+
|
4+
LL | struct B;
5+
| -------- method `request` not found for this struct because it doesn't satisfy `B: ProviderExt` or `B: ProviderLt`
6+
...
7+
LL | B.request();
8+
| ^^^^^^^ method cannot be called on `B` due to unsatisfied trait bounds
9+
|
10+
note: trait bound `B: ProviderLt` was not satisfied
11+
--> $DIR/on-unimplemented-diagnostic.rs:9:18
12+
|
13+
LL | impl<T: ?Sized + ProviderLt> ProviderExt for T {}
14+
| ^^^^^^^^^^ ----------- -
15+
| |
16+
| unsatisfied trait bound introduced here
17+
note: the trait `ProviderLt` must be implemented
18+
--> $DIR/on-unimplemented-diagnostic.rs:1:1
19+
|
20+
LL | pub trait ProviderLt {}
21+
| ^^^^^^^^^^^^^^^^^^^^
22+
= help: items from traits can only be used if the trait is implemented and in scope
23+
note: `ProviderExt` defines an item `request`, perhaps you need to implement it
24+
--> $DIR/on-unimplemented-diagnostic.rs:3:1
25+
|
26+
LL | pub trait ProviderExt {
27+
| ^^^^^^^^^^^^^^^^^^^^^
28+
29+
error: aborting due to 1 previous error
30+
31+
For more information about this error, try `rustc --explain E0599`.

Diff for: tests/ui/traits/on_unimplemented_long_types.rs

-17
This file was deleted.

Diff for: tests/ui/traits/on_unimplemented_long_types.stderr

-25
This file was deleted.

0 commit comments

Comments
 (0)