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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 4 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

0 commit comments

Comments
 (0)