Skip to content

Commit d992d9c

Browse files
committed
On E0308 involving dyn Trait, mention trait objects
When encountering a type mismatch error involving `dyn Trait`, mention the existence of boxed trait objects if the other type involved implements `Trait`. Partially addresses rust-lang#102629.
1 parent 0b77301 commit d992d9c

File tree

7 files changed

+56
-0
lines changed

7 files changed

+56
-0
lines changed

compiler/rustc_infer/src/infer/error_reporting/note_and_explain.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,55 @@ impl<T> Trait<T> for X {
294294
);
295295
}
296296
}
297+
(ty::Dynamic(t, _, ty::DynKind::Dyn), _)
298+
if let Some(def_id) = t.principal_def_id() =>
299+
{
300+
let mut impl_def_ids = vec![];
301+
tcx.for_each_relevant_impl(def_id, values.found, |did| {
302+
impl_def_ids.push(did)
303+
});
304+
if let [_] = &impl_def_ids[..] {
305+
let trait_name = tcx.item_name(def_id);
306+
diag.help(format!(
307+
"`{}` implements `{trait_name}` so you could box the found value \
308+
and coerce it to the trait object `Box<dyn {trait_name}>`, you \
309+
will have to change the expected type as well",
310+
values.found,
311+
));
312+
}
313+
}
314+
(_, ty::Dynamic(t, _, ty::DynKind::Dyn))
315+
if let Some(def_id) = t.principal_def_id() =>
316+
{
317+
let mut impl_def_ids = vec![];
318+
tcx.for_each_relevant_impl(def_id, values.expected, |did| {
319+
impl_def_ids.push(did)
320+
});
321+
if let [_] = &impl_def_ids[..] {
322+
let trait_name = tcx.item_name(def_id);
323+
diag.help(format!(
324+
"`{}` implements `{trait_name}` so you could change the expected \
325+
type to `Box<dyn {trait_name}>`",
326+
values.expected,
327+
));
328+
}
329+
}
330+
(ty::Dynamic(t, _, ty::DynKind::DynStar), _)
331+
if let Some(def_id) = t.principal_def_id() =>
332+
{
333+
let mut impl_def_ids = vec![];
334+
tcx.for_each_relevant_impl(def_id, values.found, |did| {
335+
impl_def_ids.push(did)
336+
});
337+
if let [_] = &impl_def_ids[..] {
338+
let trait_name = tcx.item_name(def_id);
339+
diag.help(format!(
340+
"`{}` implements `{trait_name}`, `#[feature(dyn_star)]` is likely \
341+
not enabled; that feature it is currently incomplete",
342+
values.found,
343+
));
344+
}
345+
}
297346
(_, ty::Alias(ty::Opaque, opaque_ty))
298347
| (ty::Alias(ty::Opaque, opaque_ty), _) => {
299348
if opaque_ty.def_id.is_local()

tests/ui/dst/dst-bad-assign-3.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LL | f5.2 = Bar1 {f: 36};
88
|
99
= note: expected trait object `dyn ToBar`
1010
found struct `Bar1`
11+
= help: `Bar1` implements `ToBar` so you could box the found value and coerce it to the trait object `Box<dyn ToBar>`, you will have to change the expected type as well
1112

1213
error[E0277]: the size for values of type `dyn ToBar` cannot be known at compilation time
1314
--> $DIR/dst-bad-assign-3.rs:33:5

tests/ui/dst/dst-bad-assign.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LL | f5.ptr = Bar1 {f: 36};
88
|
99
= note: expected trait object `dyn ToBar`
1010
found struct `Bar1`
11+
= help: `Bar1` implements `ToBar` so you could box the found value and coerce it to the trait object `Box<dyn ToBar>`, you will have to change the expected type as well
1112

1213
error[E0277]: the size for values of type `dyn ToBar` cannot be known at compilation time
1314
--> $DIR/dst-bad-assign.rs:35:5

tests/ui/dyn-star/no-implicit-dyn-star.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ LL | dyn_star_foreign::require_dyn_star_display(1usize);
88
|
99
= note: expected trait object `(dyn* std::fmt::Display + 'static)`
1010
found type `usize`
11+
= help: `usize` implements `Display`, `#[feature(dyn_star)]` is likely not enabled; that feature it is currently incomplete
1112
note: function defined here
1213
--> $DIR/auxiliary/dyn-star-foreign.rs:6:8
1314
|

tests/ui/generic-associated-types/issue-79422.extended.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ LL | type VRefCont<'a> = &'a V where Self: 'a;
2727
| ^^^^^
2828
= note: expected trait object `(dyn RefCont<'_, u8> + 'static)`
2929
found reference `&u8`
30+
= help: `&u8` implements `RefCont` so you could box the found value and coerce it to the trait object `Box<dyn RefCont>`, you will have to change the expected type as well
3031
= note: required for the cast from `Box<BTreeMap<u8, u8>>` to `Box<dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>>`
3132

3233
error: aborting due to 2 previous errors

tests/ui/impl-trait/dyn-trait-return-should-be-impl-trait.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | fn fuz() -> (usize, Trait) { (42, Struct) }
66
|
77
= note: expected trait object `(dyn Trait + 'static)`
88
found struct `Struct`
9+
= help: `Struct` implements `Trait` so you could box the found value and coerce it to the trait object `Box<dyn Trait>`, you will have to change the expected type as well
910

1011
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
1112
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:7:13
@@ -27,6 +28,7 @@ LL | fn bar() -> (usize, dyn Trait) { (42, Struct) }
2728
|
2829
= note: expected trait object `(dyn Trait + 'static)`
2930
found struct `Struct`
31+
= help: `Struct` implements `Trait` so you could box the found value and coerce it to the trait object `Box<dyn Trait>`, you will have to change the expected type as well
3032

3133
error[E0277]: the size for values of type `(dyn Trait + 'static)` cannot be known at compilation time
3234
--> $DIR/dyn-trait-return-should-be-impl-trait.rs:10:13

tests/ui/methods/issues/issue-61525.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ LL | 1.query::<dyn ToString>("")
2727
|
2828
= note: expected trait object `dyn ToString`
2929
found reference `&'static str`
30+
= help: `&'static str` implements `ToString` so you could box the found value and coerce it to the trait object `Box<dyn ToString>`, you will have to change the expected type as well
3031
note: method defined here
3132
--> $DIR/issue-61525.rs:2:8
3233
|

0 commit comments

Comments
 (0)