Skip to content

Commit 8de3078

Browse files
Check allow instantiating object trait binder when upcasting and in new solver
1 parent 0399709 commit 8de3078

File tree

8 files changed

+69
-50
lines changed

8 files changed

+69
-50
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ where
896896
&& ecx
897897
.probe(|_| ProbeKind::UpcastProjectionCompatibility)
898898
.enter(|ecx| -> Result<(), NoSolution> {
899-
ecx.eq(param_env, source_projection, target_projection)?;
899+
ecx.sub(param_env, source_projection, target_projection)?;
900900
let _ = ecx.try_evaluate_added_goals()?;
901901
Ok(())
902902
})
@@ -909,7 +909,7 @@ where
909909
// Check that a's supertrait (upcast_principal) is compatible
910910
// with the target (b_ty).
911911
ty::ExistentialPredicate::Trait(target_principal) => {
912-
ecx.eq(
912+
ecx.sub(
913913
param_env,
914914
upcast_principal.unwrap(),
915915
bound.rebind(target_principal),
@@ -934,7 +934,7 @@ where
934934
Certainty::AMBIGUOUS,
935935
);
936936
}
937-
ecx.eq(param_env, source_projection, target_projection)?;
937+
ecx.sub(param_env, source_projection, target_projection)?;
938938
}
939939
// Check that b_ty's auto traits are present in a_ty's bounds.
940940
ty::ExistentialPredicate::AutoTrait(def_id) => {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -2582,12 +2582,12 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
25822582
nested.extend(
25832583
self.infcx
25842584
.at(&obligation.cause, obligation.param_env)
2585-
.eq(
2585+
.sup(
25862586
DefineOpaqueTypes::Yes,
2587+
bound.rebind(target_principal),
25872588
upcast_principal.map_bound(|trait_ref| {
25882589
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
25892590
}),
2590-
bound.rebind(target_principal),
25912591
)
25922592
.map_err(|_| SelectionError::Unimplemented)?
25932593
.into_obligations(),
@@ -2620,7 +2620,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
26202620
nested.extend(
26212621
self.infcx
26222622
.at(&obligation.cause, obligation.param_env)
2623-
.eq(DefineOpaqueTypes::Yes, source_projection, target_projection)
2623+
.sup(DefineOpaqueTypes::Yes, target_projection, source_projection)
26242624
.map_err(|_| SelectionError::Unimplemented)?
26252625
.into_obligations(),
26262626
);

Diff for: tests/ui/coercion/sub-principals.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Verify that the unsize goal can cast a higher-ranked trait goal to
7+
// a non-higer-ranked instantiation.
8+
9+
#![feature(unsize)]
10+
11+
use std::marker::Unsize;
12+
13+
fn test<T: ?Sized, U: ?Sized>()
14+
where
15+
T: Unsize<U>,
16+
{
17+
}
18+
19+
fn main() {
20+
test::<dyn for<'a> Fn(&'a ()) -> &'a (), dyn Fn(&'static ()) -> &'static ()>();
21+
22+
trait Foo<'a, 'b> {}
23+
test::<dyn for<'a, 'b> Foo<'a, 'b>, dyn for<'a> Foo<'a, 'a>>();
24+
25+
trait Bar<'a> {}
26+
test::<dyn for<'a> Bar<'a>, dyn Bar<'_>>();
27+
}

Diff for: tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.current.stderr

-22
This file was deleted.

Diff for: tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ok.next.stderr

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
//@ revisions: current next
22
//@ ignore-compare-mode-next-solver (explicit revisions)
33
//@[next] compile-flags: -Znext-solver
4+
//@ check-pass
45

56
// We should be able to instantiate a binder during trait upcasting.
67
// This test could be `check-pass`, but we should make sure that we
78
// do so in both trait solvers.
9+
810
#![feature(trait_upcasting)]
9-
#![crate_type = "rlib"]
10-
trait Supertrait<'a, 'b> {}
1111

12+
trait Supertrait<'a, 'b> {}
1213
trait Subtrait<'a, 'b>: Supertrait<'a, 'b> {}
1314

1415
impl<'a> Supertrait<'a, 'a> for () {}
1516
impl<'a> Subtrait<'a, 'a> for () {}
1617
fn ok(x: &dyn for<'a, 'b> Subtrait<'a, 'b>) -> &dyn for<'a> Supertrait<'a, 'a> {
17-
x //~ ERROR mismatched types
18-
//[current]~^ ERROR mismatched types
18+
x
1919
}
20+
21+
fn main() {}

Diff for: tests/ui/traits/trait-upcasting/higher-ranked-upcasting-ub.current.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ error[E0308]: mismatched types
44
LL | x
55
| ^ one type is more general than the other
66
|
7-
= note: expected existential trait ref `for<'a> Supertrait<'a, 'a>`
8-
found existential trait ref `for<'a, 'b> Supertrait<'a, 'b>`
7+
= note: expected existential trait ref `for<'a, 'b> Supertrait<'a, 'b>`
8+
found existential trait ref `for<'a> Supertrait<'a, 'a>`
99

1010
error[E0308]: mismatched types
1111
--> $DIR/higher-ranked-upcasting-ub.rs:22:5
1212
|
1313
LL | x
1414
| ^ one type is more general than the other
1515
|
16-
= note: expected existential trait ref `for<'a> Supertrait<'a, 'a>`
17-
found existential trait ref `for<'a, 'b> Supertrait<'a, 'b>`
16+
= note: expected existential trait ref `for<'a, 'b> Supertrait<'a, 'b>`
17+
found existential trait ref `for<'a> Supertrait<'a, 'a>`
1818
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1919

2020
error: aborting due to 2 previous errors

Diff for: tests/ui/traits/trait-upcasting/sub.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//@ check-pass
2+
//@ revisions: current next
3+
//@ ignore-compare-mode-next-solver (explicit revisions)
4+
//@[next] compile-flags: -Znext-solver
5+
6+
// Verify that the unsize goal can cast a higher-ranked trait goal to
7+
// a non-higer-ranked instantiation.
8+
9+
#![feature(unsize)]
10+
11+
use std::marker::Unsize;
12+
13+
fn test<T: ?Sized, U: ?Sized>()
14+
where
15+
T: Unsize<U>,
16+
{
17+
}
18+
19+
fn main() {
20+
test::<dyn for<'a> Fn(&'a ()) -> &'a (), dyn FnOnce(&'static ()) -> &'static ()>();
21+
22+
trait Foo: for<'a> Bar<'a> {}
23+
trait Bar<'a> {}
24+
test::<dyn Foo, dyn Bar<'static>>();
25+
test::<dyn Foo, dyn Bar<'_>>();
26+
}

0 commit comments

Comments
 (0)