Skip to content

Commit f8e0dcb

Browse files
Better error message for object type with GAT
1 parent 690bcc6 commit f8e0dcb

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
548548
obligation.cause.span,
549549
"GATs in trait object shouldn't have been considered",
550550
);
551-
return Err(SelectionError::Unimplemented);
551+
return Err(SelectionError::TraitNotObjectSafe(trait_predicate.trait_ref.def_id));
552552
}
553553

554554
// This maybe belongs in wf, but that can't (doesn't) handle

Diff for: tests/ui/impl-trait/in-trait/object-safety.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ fn main() {
1919
//~| ERROR the trait `Foo` cannot be made into an object
2020
let s = i.baz();
2121
//~^ ERROR the trait `Foo` cannot be made into an object
22-
//~| ERROR the trait bound `dyn Foo: Foo`
22+
//~| ERROR the trait `Foo` cannot be made into an object
2323
}

Diff for: tests/ui/impl-trait/in-trait/object-safety.stderr

+11-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,20 @@ LL | fn baz(&self) -> impl Debug;
1313
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
1414
= help: consider moving `baz` to another trait
1515

16-
error[E0277]: the trait bound `dyn Foo: Foo` is not satisfied
16+
error[E0038]: the trait `Foo` cannot be made into an object
1717
--> $DIR/object-safety.rs:20:15
1818
|
1919
LL | let s = i.baz();
20-
| ^^^ the trait `Foo` is not implemented for `dyn Foo`
20+
| ^^^ `Foo` cannot be made into an object
2121
|
22-
= help: the trait `Foo` is implemented for `u32`
22+
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
23+
--> $DIR/object-safety.rs:7:22
24+
|
25+
LL | trait Foo {
26+
| --- this trait cannot be made into an object...
27+
LL | fn baz(&self) -> impl Debug;
28+
| ^^^^^^^^^^ ...because method `baz` references an `impl Trait` type in its return type
29+
= help: consider moving `baz` to another trait
2330

2431
error[E0038]: the trait `Foo` cannot be made into an object
2532
--> $DIR/object-safety.rs:20:13
@@ -54,5 +61,4 @@ LL | fn baz(&self) -> impl Debug;
5461

5562
error: aborting due to 4 previous errors
5663

57-
Some errors have detailed explanations: E0038, E0277.
58-
For more information about an error, try `rustc --explain E0038`.
64+
For more information about this error, try `rustc --explain E0038`.

0 commit comments

Comments
 (0)