Skip to content

Commit 47bdda2

Browse files
Rollup merge of #114828 - compiler-errors:next-solver-probe-upcasting, r=lcnr
Probe when assembling upcast candidates so they don't step on eachother's toes in new solver Lack of a probe causes one candidate to disqualify the other due to inference side-effects. r? lcnr
2 parents e4b9e72 + ab126c2 commit 47bdda2

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

Diff for: compiler/rustc_trait_selection/src/solve/trait_goals.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -552,16 +552,18 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
552552
self.walk_vtable(
553553
a_principal.with_self_ty(tcx, a_ty),
554554
|ecx, new_a_principal, _, vtable_vptr_slot| {
555-
if let Ok(resp) = ecx.consider_builtin_upcast_to_principal(
556-
goal,
557-
a_data,
558-
a_region,
559-
b_data,
560-
b_region,
561-
Some(new_a_principal.map_bound(|trait_ref| {
562-
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
563-
})),
564-
) {
555+
if let Ok(resp) = ecx.probe_candidate("dyn upcast").enter(|ecx| {
556+
ecx.consider_builtin_upcast_to_principal(
557+
goal,
558+
a_data,
559+
a_region,
560+
b_data,
561+
b_region,
562+
Some(new_a_principal.map_bound(|trait_ref| {
563+
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
564+
})),
565+
)
566+
}) {
565567
responses
566568
.push((resp, BuiltinImplSource::TraitUpcasting { vtable_vptr_slot }));
567569
}

Diff for: tests/ui/traits/trait-upcasting/type-checking-test-1.stderr renamed to tests/ui/traits/trait-upcasting/type-checking-test-1.current.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>`
2-
--> $DIR/type-checking-test-1.rs:16:13
2+
--> $DIR/type-checking-test-1.rs:19:13
33
|
44
LL | let _ = x as &dyn Bar<_>; // Ambiguous
55
| ^^^^^^^^^^^^^^^^ invalid cast
@@ -10,7 +10,7 @@ LL | let _ = &x as &dyn Bar<_>; // Ambiguous
1010
| +
1111

1212
error[E0277]: the trait bound `&dyn Foo: Bar<_>` is not satisfied
13-
--> $DIR/type-checking-test-1.rs:16:13
13+
--> $DIR/type-checking-test-1.rs:19:13
1414
|
1515
LL | let _ = x as &dyn Bar<_>; // Ambiguous
1616
| ^ the trait `Bar<_>` is not implemented for `&dyn Foo`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0605]: non-primitive cast: `&dyn Foo` as `&dyn Bar<_>`
2+
--> $DIR/type-checking-test-1.rs:19:13
3+
|
4+
LL | let _ = x as &dyn Bar<_>; // Ambiguous
5+
| ^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0605`.

Diff for: tests/ui/traits/trait-upcasting/type-checking-test-1.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: current next
2+
//[next] compile-flags: -Ztrait-solver=next
3+
14
#![feature(trait_upcasting)]
25

36
trait Foo: Bar<i32> + Bar<u32> {}
@@ -15,7 +18,7 @@ fn test_specific(x: &dyn Foo) {
1518
fn test_unknown_version(x: &dyn Foo) {
1619
let _ = x as &dyn Bar<_>; // Ambiguous
1720
//~^ ERROR non-primitive cast
18-
//~^^ ERROR the trait bound `&dyn Foo: Bar<_>` is not satisfied
21+
//[current]~^^ ERROR the trait bound `&dyn Foo: Bar<_>` is not satisfied
1922
}
2023

2124
fn test_infer_version(x: &dyn Foo) {

0 commit comments

Comments
 (0)