Skip to content

Commit be2ef3e

Browse files
Rollup merge of rust-lang#98277 - compiler-errors:issue-93596, r=estebank
Fix trait object reborrow suggestion Fixes rust-lang#93596 Slightly generalizes the logic we use to suggest fix first implemented in rust-lang#95609, specifically when we have a `Sized` obligation that comes from a struct's unsized tail.
2 parents 0bfffb7 + 25fe474 commit be2ef3e

32 files changed

+91
-56
lines changed

compiler/rustc_middle/src/traits/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ pub enum ObligationCauseCode<'tcx> {
253253
ObjectTypeBound(Ty<'tcx>, ty::Region<'tcx>),
254254

255255
/// Obligation incurred due to an object cast.
256-
ObjectCastObligation(/* Object type */ Ty<'tcx>),
256+
ObjectCastObligation(/* Concrete type */ Ty<'tcx>, /* Object type */ Ty<'tcx>),
257257

258258
/// Obligation incurred due to a coercion.
259259
Coercion {

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
484484
err.span_label(span, explanation);
485485
}
486486

487-
if let ObligationCauseCode::ObjectCastObligation(obj_ty) = obligation.cause.code().peel_derives() &&
488-
let Some(self_ty) = trait_predicate.self_ty().no_bound_vars() &&
487+
if let ObligationCauseCode::ObjectCastObligation(concrete_ty, obj_ty) = obligation.cause.code().peel_derives() &&
489488
Some(trait_ref.def_id()) == self.tcx.lang_items().sized_trait() {
490-
self.suggest_borrowing_for_object_cast(&mut err, &obligation, self_ty, *obj_ty);
489+
self.suggest_borrowing_for_object_cast(&mut err, &root_obligation, *concrete_ty, *obj_ty);
491490
}
492491

493492
if trait_predicate.is_const_if_const() && obligation.param_env.is_const() {
@@ -1560,7 +1559,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'a, 'tcx> for InferCtxt<'a, 'tcx> {
15601559
obligation.cause.code().peel_derives(),
15611560
ObligationCauseCode::ItemObligation(_)
15621561
| ObligationCauseCode::BindingObligation(_, _)
1563-
| ObligationCauseCode::ObjectCastObligation(_)
1562+
| ObligationCauseCode::ObjectCastObligation(..)
15641563
| ObligationCauseCode::OpaqueType
15651564
);
15661565
if let Err(error) = self.at(&obligation.cause, obligation.param_env).eq_exp(

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2217,9 +2217,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
22172217
err.span_note(tcx.def_span(item_def_id), &descr);
22182218
}
22192219
}
2220-
ObligationCauseCode::ObjectCastObligation(object_ty) => {
2220+
ObligationCauseCode::ObjectCastObligation(concrete_ty, object_ty) => {
22212221
err.note(&format!(
2222-
"required for the cast to the object type `{}`",
2222+
"required for the cast from `{}` to the object type `{}`",
2223+
self.ty_to_string(concrete_ty),
22232224
self.ty_to_string(object_ty)
22242225
));
22252226
}

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
813813
let cause = ObligationCause::new(
814814
obligation.cause.span,
815815
obligation.cause.body_id,
816-
ObjectCastObligation(target),
816+
ObjectCastObligation(source, target),
817817
);
818818
let outlives = ty::OutlivesPredicate(r_a, r_b);
819819
nested.push(Obligation::with_depth(
@@ -910,7 +910,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
910910
let cause = ObligationCause::new(
911911
obligation.cause.span,
912912
obligation.cause.body_id,
913-
ObjectCastObligation(target),
913+
ObjectCastObligation(source, target),
914914
);
915915
let outlives = ty::OutlivesPredicate(r_a, r_b);
916916
nested.push(Obligation::with_depth(
@@ -931,7 +931,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
931931
let cause = ObligationCause::new(
932932
obligation.cause.span,
933933
obligation.cause.body_id,
934-
ObjectCastObligation(target),
934+
ObjectCastObligation(source, target),
935935
);
936936

937937
let predicate_to_obligation = |predicate| {

src/test/ui/associated-type-bounds/assoc-type-eq-with-dyn-atb-fail.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0277]: the trait bound `String: Copy` is not satisfied
44
LL | Box::new(AssocNoCopy)
55
| ^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
66
|
7-
= note: required for the cast to the object type `dyn Bar<Assoc = <AssocNoCopy as Thing>::Out::{opaque#0}>`
7+
= note: required for the cast from `AssocNoCopy` to the object type `dyn Bar<Assoc = <AssocNoCopy as Thing>::Out::{opaque#0}>`
88

99
error: aborting due to previous error
1010

src/test/ui/associated-types/associated-types-eq-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ note: expected this to be `Bar`
4141
|
4242
LL | type A = usize;
4343
| ^^^^^
44-
= note: required for the cast to the object type `dyn Foo<A = Bar>`
44+
= note: required for the cast from `isize` to the object type `dyn Foo<A = Bar>`
4545

4646
error: aborting due to 3 previous errors
4747

src/test/ui/associated-types/associated-types-overridden-binding-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error[E0271]: type mismatch resolving `<std::vec::IntoIter<u32> as Iterator>::It
44
LL | let _: &dyn I32Iterator<Item = u32> = &vec![42].into_iter();
55
| ^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `u32`
66
|
7-
= note: required for the cast to the object type `dyn Iterator<Item = u32, Item = i32>`
7+
= note: required for the cast from `std::vec::IntoIter<u32>` to the object type `dyn Iterator<Item = u32, Item = i32>`
88

99
error: aborting due to previous error
1010

src/test/ui/associated-types/issue-65774-1.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ note: required because of the requirements on the impl of `MyDisplay` for `&mut
2323
|
2424
LL | impl<'a, T: MyDisplay> MyDisplay for &'a mut T { }
2525
| ^^^^^^^^^ ^^^^^^^^^
26-
= note: required for the cast to the object type `dyn MyDisplay`
26+
= note: required for the cast from `&mut T` to the object type `dyn MyDisplay`
2727

2828
error: aborting due to 2 previous errors
2929

src/test/ui/associated-types/issue-65774-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ LL | writer.my_write(valref)
1818
| ^^^^^^ the trait `MyDisplay` is not implemented for `T`
1919
|
2020
= help: the trait `MyDisplay` is implemented for `&'a mut T`
21-
= note: required for the cast to the object type `dyn MyDisplay`
21+
= note: required for the cast from `T` to the object type `dyn MyDisplay`
2222

2323
error: aborting due to 2 previous errors
2424

src/test/ui/async-await/async-block-control-flow-static-semantics.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ error[E0271]: type mismatch resolving `<impl Future<Output = u8> as Future>::Out
3737
LL | let _: &dyn Future<Output = ()> = &block;
3838
| ^^^^^^ expected `()`, found `u8`
3939
|
40-
= note: required for the cast to the object type `dyn Future<Output = ()>`
40+
= note: required for the cast from `impl Future<Output = u8>` to the object type `dyn Future<Output = ()>`
4141

4242
error[E0308]: mismatched types
4343
--> $DIR/async-block-control-flow-static-semantics.rs:12:43
@@ -53,7 +53,7 @@ error[E0271]: type mismatch resolving `<impl Future<Output = u8> as Future>::Out
5353
LL | let _: &dyn Future<Output = ()> = &block;
5454
| ^^^^^^ expected `()`, found `u8`
5555
|
56-
= note: required for the cast to the object type `dyn Future<Output = ()>`
56+
= note: required for the cast from `impl Future<Output = u8>` to the object type `dyn Future<Output = ()>`
5757

5858
error[E0308]: mismatched types
5959
--> $DIR/async-block-control-flow-static-semantics.rs:47:44

src/test/ui/async-await/issue-86507.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ note: captured value is not `Send` because `&` references cannot be sent unless
1313
|
1414
LL | let x = x;
1515
| ^ has type `&T` which is not `Send`, because `T` is not `Sync`
16-
= note: required for the cast to the object type `dyn Future<Output = ()> + Send`
16+
= note: required for the cast from `impl Future<Output = ()>` to the object type `dyn Future<Output = ()> + Send`
1717
help: consider further restricting this bound
1818
|
1919
LL | fn bar<'me, 'async_trait, T: Send + std::marker::Sync>(x: &'me T)

src/test/ui/coercion/coerce-issue-49593-box-never.nofallback.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ error[E0277]: the trait bound `(): std::error::Error` is not satisfied
44
LL | /* *mut $0 is coerced to Box<dyn Error> here */ Box::<_ /* ! */>::new(x)
55
| ^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`
66
|
7-
= note: required for the cast to the object type `dyn std::error::Error`
7+
= note: required for the cast from `()` to the object type `dyn std::error::Error`
88

99
error[E0277]: the trait bound `(): std::error::Error` is not satisfied
1010
--> $DIR/coerce-issue-49593-box-never.rs:23:49
1111
|
1212
LL | /* *mut $0 is coerced to *mut Error here */ raw_ptr_box::<_ /* ! */>(x)
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::error::Error` is not implemented for `()`
1414
|
15-
= note: required for the cast to the object type `(dyn std::error::Error + 'static)`
15+
= note: required for the cast from `()` to the object type `(dyn std::error::Error + 'static)`
1616

1717
error: aborting due to 2 previous errors
1818

src/test/ui/const-generics/defaults/trait_objects_fail.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | foo(&10_u32);
77
| required by a bound introduced by this call
88
|
99
= help: the trait `Trait<2_u8>` is implemented for `u32`
10-
= note: required for the cast to the object type `dyn Trait`
10+
= note: required for the cast from `u32` to the object type `dyn Trait`
1111

1212
error[E0277]: the trait bound `bool: Traitor<{_: u8}>` is not satisfied
1313
--> $DIR/trait_objects_fail.rs:28:9
@@ -18,7 +18,7 @@ LL | bar(&true);
1818
| required by a bound introduced by this call
1919
|
2020
= help: the trait `Traitor<2_u8, 3_u8>` is implemented for `bool`
21-
= note: required for the cast to the object type `dyn Traitor<{_: u8}>`
21+
= note: required for the cast from `bool` to the object type `dyn Traitor<{_: u8}>`
2222

2323
error: aborting due to 2 previous errors
2424

src/test/ui/custom_test_frameworks/mismatch.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | #[test]
66
LL | fn wrong_kind(){}
77
| ^^^^^^^^^^^^^^^^^ the trait `Testable` is not implemented for `TestDescAndFn`
88
|
9-
= note: required for the cast to the object type `dyn Testable`
9+
= note: required for the cast from `TestDescAndFn` to the object type `dyn Testable`
1010
= note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
1111

1212
error: aborting due to previous error

src/test/ui/dst/dst-bad-coerce1.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ error[E0277]: the trait bound `Foo: Bar` is not satisfied
1515
LL | let f3: &Fat<dyn Bar> = f2;
1616
| ^^ the trait `Bar` is not implemented for `Foo`
1717
|
18-
= note: required for the cast to the object type `dyn Bar`
18+
= note: required for the cast from `Foo` to the object type `dyn Bar`
1919

2020
error[E0308]: mismatched types
2121
--> $DIR/dst-bad-coerce1.rs:28:27
@@ -34,7 +34,7 @@ error[E0277]: the trait bound `Foo: Bar` is not satisfied
3434
LL | let f3: &(dyn Bar,) = f2;
3535
| ^^ the trait `Bar` is not implemented for `Foo`
3636
|
37-
= note: required for the cast to the object type `dyn Bar`
37+
= note: required for the cast from `Foo` to the object type `dyn Bar`
3838

3939
error: aborting due to 4 previous errors
4040

src/test/ui/dst/dst-object-from-unsized-type.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | fn test1<T: ?Sized + Foo>(t: &T) {
66
LL | let u: &dyn Foo = t;
77
| ^ doesn't have a size known at compile-time
88
|
9-
= note: required for the cast to the object type `dyn Foo`
9+
= note: required for the cast from `T` to the object type `dyn Foo`
1010
help: consider removing the `?Sized` bound to make the type parameter `Sized`
1111
|
1212
LL - fn test1<T: ?Sized + Foo>(t: &T) {
@@ -21,7 +21,7 @@ LL | fn test2<T: ?Sized + Foo>(t: &T) {
2121
LL | let v: &dyn Foo = t as &dyn Foo;
2222
| ^ doesn't have a size known at compile-time
2323
|
24-
= note: required for the cast to the object type `dyn Foo`
24+
= note: required for the cast from `T` to the object type `dyn Foo`
2525
help: consider removing the `?Sized` bound to make the type parameter `Sized`
2626
|
2727
LL - fn test2<T: ?Sized + Foo>(t: &T) {
@@ -35,7 +35,7 @@ LL | let _: &[&dyn Foo] = &["hi"];
3535
| ^^^^ doesn't have a size known at compile-time
3636
|
3737
= help: the trait `Sized` is not implemented for `str`
38-
= note: required for the cast to the object type `dyn Foo`
38+
= note: required for the cast from `str` to the object type `dyn Foo`
3939

4040
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
4141
--> $DIR/dst-object-from-unsized-type.rs:23:23
@@ -44,7 +44,7 @@ LL | let _: &dyn Foo = x as &dyn Foo;
4444
| ^ doesn't have a size known at compile-time
4545
|
4646
= help: the trait `Sized` is not implemented for `[u8]`
47-
= note: required for the cast to the object type `dyn Foo`
47+
= note: required for the cast from `[u8]` to the object type `dyn Foo`
4848

4949
error: aborting due to 4 previous errors
5050

src/test/ui/generic-associated-types/issue-79422.extended.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +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-
= note: required for the cast to the object type `dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>`
30+
= note: required for the cast from `BTreeMap<u8, u8>` to the object type `dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>`
3131

3232
error: aborting due to 2 previous errors
3333

src/test/ui/issues/issue-14366.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | let _x = "test" as &dyn (::std::any::Any);
55
| ^^^^^^ doesn't have a size known at compile-time
66
|
77
= help: the trait `Sized` is not implemented for `str`
8-
= note: required for the cast to the object type `dyn Any`
8+
= note: required for the cast from `str` to the object type `dyn Any`
99
help: consider borrowing the value, since `&str` can be coerced into `dyn Any`
1010
|
1111
LL | let _x = &"test" as &dyn (::std::any::Any);

src/test/ui/issues/issue-22034.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | &mut *(ptr as *mut dyn Fn())
66
|
77
= help: the trait `Fn<()>` is not implemented for `()`
88
= note: wrap the `()` in a closure with no arguments: `|| { /* code */ }`
9-
= note: required for the cast to the object type `dyn Fn()`
9+
= note: required for the cast from `()` to the object type `dyn Fn()`
1010

1111
error: aborting due to previous error
1212

src/test/ui/issues/issue-22872.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: required because of the requirements on the impl of `for<'b> Wrap<'b>` for
1010
|
1111
LL | impl<'b, P> Wrap<'b> for Wrapper<P>
1212
| ^^^^^^^^ ^^^^^^^^^^
13-
= note: required for the cast to the object type `dyn for<'b> Wrap<'b>`
13+
= note: required for the cast from `Wrapper<P>` to the object type `dyn for<'b> Wrap<'b>`
1414
help: consider further restricting the associated type
1515
|
1616
LL | fn push_process<P>(process: P) where P: Process<'static>, <P as Process<'_>>::Item: Iterator {

src/test/ui/issues/issue-7013.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ note: required because it appears within the type `B`
1111
|
1212
LL | struct B {
1313
| ^
14-
= note: required for the cast to the object type `dyn Foo + Send`
14+
= note: required for the cast from `B` to the object type `dyn Foo + Send`
1515

1616
error: aborting due to previous error
1717

src/test/ui/kindck/kindck-impl-type-params.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: required because of the requirements on the impl of `Gettable<T>` for `S<T
99
|
1010
LL | impl<T: Send + Copy + 'static> Gettable<T> for S<T> {}
1111
| ^^^^^^^^^^^ ^^^^
12-
= note: required for the cast to the object type `dyn Gettable<T>`
12+
= note: required for the cast from `S<T>` to the object type `dyn Gettable<T>`
1313
help: consider restricting type parameter `T`
1414
|
1515
LL | fn f<T: std::marker::Send>(val: T) {
@@ -26,7 +26,7 @@ note: required because of the requirements on the impl of `Gettable<T>` for `S<T
2626
|
2727
LL | impl<T: Send + Copy + 'static> Gettable<T> for S<T> {}
2828
| ^^^^^^^^^^^ ^^^^
29-
= note: required for the cast to the object type `dyn Gettable<T>`
29+
= note: required for the cast from `S<T>` to the object type `dyn Gettable<T>`
3030
help: consider restricting type parameter `T`
3131
|
3232
LL | fn f<T: std::marker::Copy>(val: T) {
@@ -43,7 +43,7 @@ note: required because of the requirements on the impl of `Gettable<T>` for `S<T
4343
|
4444
LL | impl<T: Send + Copy + 'static> Gettable<T> for S<T> {}
4545
| ^^^^^^^^^^^ ^^^^
46-
= note: required for the cast to the object type `dyn Gettable<T>`
46+
= note: required for the cast from `S<T>` to the object type `dyn Gettable<T>`
4747
help: consider restricting type parameter `T`
4848
|
4949
LL | fn g<T: std::marker::Send>(val: T) {
@@ -60,7 +60,7 @@ note: required because of the requirements on the impl of `Gettable<T>` for `S<T
6060
|
6161
LL | impl<T: Send + Copy + 'static> Gettable<T> for S<T> {}
6262
| ^^^^^^^^^^^ ^^^^
63-
= note: required for the cast to the object type `dyn Gettable<T>`
63+
= note: required for the cast from `S<T>` to the object type `dyn Gettable<T>`
6464
help: consider restricting type parameter `T`
6565
|
6666
LL | fn g<T: std::marker::Copy>(val: T) {
@@ -78,7 +78,7 @@ note: required because of the requirements on the impl of `Gettable<String>` for
7878
|
7979
LL | impl<T: Send + Copy + 'static> Gettable<T> for S<T> {}
8080
| ^^^^^^^^^^^ ^^^^
81-
= note: required for the cast to the object type `dyn Gettable<String>`
81+
= note: required for the cast from `S<String>` to the object type `dyn Gettable<String>`
8282

8383
error[E0277]: the trait bound `Foo: Copy` is not satisfied
8484
--> $DIR/kindck-impl-type-params.rs:43:37
@@ -92,7 +92,7 @@ note: required because of the requirements on the impl of `Gettable<Foo>` for `S
9292
|
9393
LL | impl<T: Send + Copy + 'static> Gettable<T> for S<T> {}
9494
| ^^^^^^^^^^^ ^^^^
95-
= note: required for the cast to the object type `dyn Gettable<Foo>`
95+
= note: required for the cast from `S<Foo>` to the object type `dyn Gettable<Foo>`
9696
help: consider annotating `Foo` with `#[derive(Copy)]`
9797
|
9898
LL | #[derive(Copy)]

src/test/ui/mismatched_types/cast-rfc0401.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ LL | let _ = fat_v as *const dyn Foo;
220220
| ^^^^^ doesn't have a size known at compile-time
221221
|
222222
= help: the trait `Sized` is not implemented for `[u8]`
223-
= note: required for the cast to the object type `dyn Foo`
223+
= note: required for the cast from `[u8]` to the object type `dyn Foo`
224224
help: consider borrowing the value, since `&[u8]` can be coerced into `dyn Foo`
225225
|
226226
LL | let _ = &fat_v as *const dyn Foo;
@@ -233,7 +233,7 @@ LL | let _ = a as *const dyn Foo;
233233
| ^ doesn't have a size known at compile-time
234234
|
235235
= help: the trait `Sized` is not implemented for `str`
236-
= note: required for the cast to the object type `dyn Foo`
236+
= note: required for the cast from `str` to the object type `dyn Foo`
237237
help: consider borrowing the value, since `&str` can be coerced into `dyn Foo`
238238
|
239239
LL | let _ = &a as *const dyn Foo;

src/test/ui/never_type/fallback-closure-wrap.fallback.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | | }) as Box<dyn FnMut()>);
1010
|
1111
= note: expected unit type `()`
1212
found type `!`
13-
= note: required for the cast to the object type `dyn FnMut()`
13+
= note: required for the cast from `[closure@$DIR/fallback-closure-wrap.rs:18:40: 21:6]` to the object type `dyn FnMut()`
1414

1515
error: aborting due to previous error
1616

0 commit comments

Comments
 (0)