Skip to content

Commit f2892e4

Browse files
committed
Allow constraining opaque types during subtyping in the trait system
1 parent bcde0f8 commit f2892e4

File tree

5 files changed

+25
-32
lines changed

5 files changed

+25
-32
lines changed

compiler/rustc_infer/src/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -962,9 +962,9 @@ impl<'tcx> InferCtxt<'tcx> {
962962

963963
self.enter_forall(predicate, |ty::SubtypePredicate { a_is_expected, a, b }| {
964964
if a_is_expected {
965-
Ok(self.at(cause, param_env).sub(DefineOpaqueTypes::No, a, b))
965+
Ok(self.at(cause, param_env).sub(DefineOpaqueTypes::Yes, a, b))
966966
} else {
967-
Ok(self.at(cause, param_env).sup(DefineOpaqueTypes::No, b, a))
967+
Ok(self.at(cause, param_env).sup(DefineOpaqueTypes::Yes, b, a))
968968
}
969969
})
970970
}

tests/ui/impl-trait/lazy_subtyping_of_opaques.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
//! No hidden types are being constrained in the subtyping predicate, but type and
33
//! lifetime variables get subtyped in the generic parameter list of the opaque.
44
5+
//@ check-pass
6+
57
fn foo() -> impl Default + Copy {
68
if false {
79
let x = Default::default();
810
// add `Subtype(?x, ?y)` obligation
9-
let y = x; //~ ERROR: mismatched types
11+
let y = x;
1012

1113
// Make a tuple `(?x, ?y)` and equate it with `(impl Default, u32)`.
1214
// For us to try and prove a `Subtype(impl Default, u32)` obligation,

tests/ui/impl-trait/lazy_subtyping_of_opaques.stderr

-15
This file was deleted.

tests/ui/type-alias-impl-trait/lazy_subtyping_of_opaques.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
type Tait = impl FnOnce() -> ();
88

99
fn reify_as_tait() -> Thunk<Tait> {
10+
//~^ ERROR: expected a `FnOnce()` closure, found `()`
1011
Thunk::new(|cont| cont)
1112
//~^ ERROR: mismatched types
12-
//~| ERROR: mismatched types
13+
//~| ERROR: expected a `FnOnce()` closure, found `()`
1314
}
1415

1516
struct Thunk<F>(F);
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/lazy_subtyping_of_opaques.rs:10:23
1+
error[E0277]: expected a `FnOnce()` closure, found `()`
2+
--> $DIR/lazy_subtyping_of_opaques.rs:11:23
33
|
4-
LL | type Tait = impl FnOnce() -> ();
5-
| ------------------- the found opaque type
6-
...
74
LL | Thunk::new(|cont| cont)
8-
| ^^^^ expected `()`, found opaque type
5+
| ^^^^ expected an `FnOnce()` closure, found `()`
96
|
10-
= note: expected unit type `()`
11-
found opaque type `Tait`
7+
= help: the trait `FnOnce<()>` is not implemented for `()`
8+
= note: wrap the `()` in a closure with no arguments: `|| { /* code */ }`
129

13-
error[E0308]: mismatched types
14-
--> $DIR/lazy_subtyping_of_opaques.rs:10:5
10+
error[E0277]: expected a `FnOnce()` closure, found `()`
11+
--> $DIR/lazy_subtyping_of_opaques.rs:9:23
1512
|
1613
LL | fn reify_as_tait() -> Thunk<Tait> {
17-
| ----------- expected `Thunk<_>` because of return type
14+
| ^^^^^^^^^^^ expected an `FnOnce()` closure, found `()`
15+
|
16+
= help: the trait `FnOnce<()>` is not implemented for `()`
17+
= note: wrap the `()` in a closure with no arguments: `|| { /* code */ }`
18+
19+
error[E0308]: mismatched types
20+
--> $DIR/lazy_subtyping_of_opaques.rs:11:5
21+
|
1822
LL | Thunk::new(|cont| cont)
1923
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `Thunk<_>`, found `()`
2024
|
2125
= note: expected struct `Thunk<_>`
2226
found unit type `()`
2327

24-
error: aborting due to 2 previous errors
28+
error: aborting due to 3 previous errors
2529

26-
For more information about this error, try `rustc --explain E0308`.
30+
Some errors have detailed explanations: E0277, E0308.
31+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)