Skip to content

Commit

Permalink
Allow constraining opaque types during subtyping in the trait system
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed May 27, 2024
1 parent bcde0f8 commit f2892e4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -962,9 +962,9 @@ impl<'tcx> InferCtxt<'tcx> {

self.enter_forall(predicate, |ty::SubtypePredicate { a_is_expected, a, b }| {
if a_is_expected {
Ok(self.at(cause, param_env).sub(DefineOpaqueTypes::No, a, b))
Ok(self.at(cause, param_env).sub(DefineOpaqueTypes::Yes, a, b))
} else {
Ok(self.at(cause, param_env).sup(DefineOpaqueTypes::No, b, a))
Ok(self.at(cause, param_env).sup(DefineOpaqueTypes::Yes, b, a))
}
})
}
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/impl-trait/lazy_subtyping_of_opaques.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
//! No hidden types are being constrained in the subtyping predicate, but type and
//! lifetime variables get subtyped in the generic parameter list of the opaque.
//@ check-pass

fn foo() -> impl Default + Copy {
if false {
let x = Default::default();
// add `Subtype(?x, ?y)` obligation
let y = x; //~ ERROR: mismatched types
let y = x;

// Make a tuple `(?x, ?y)` and equate it with `(impl Default, u32)`.
// For us to try and prove a `Subtype(impl Default, u32)` obligation,
Expand Down
15 changes: 0 additions & 15 deletions tests/ui/impl-trait/lazy_subtyping_of_opaques.stderr

This file was deleted.

3 changes: 2 additions & 1 deletion tests/ui/type-alias-impl-trait/lazy_subtyping_of_opaques.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
type Tait = impl FnOnce() -> ();

fn reify_as_tait() -> Thunk<Tait> {
//~^ ERROR: expected a `FnOnce()` closure, found `()`
Thunk::new(|cont| cont)
//~^ ERROR: mismatched types
//~| ERROR: mismatched types
//~| ERROR: expected a `FnOnce()` closure, found `()`
}

struct Thunk<F>(F);
Expand Down
31 changes: 18 additions & 13 deletions tests/ui/type-alias-impl-trait/lazy_subtyping_of_opaques.stderr
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
error[E0308]: mismatched types
--> $DIR/lazy_subtyping_of_opaques.rs:10:23
error[E0277]: expected a `FnOnce()` closure, found `()`
--> $DIR/lazy_subtyping_of_opaques.rs:11:23
|
LL | type Tait = impl FnOnce() -> ();
| ------------------- the found opaque type
...
LL | Thunk::new(|cont| cont)
| ^^^^ expected `()`, found opaque type
| ^^^^ expected an `FnOnce()` closure, found `()`
|
= note: expected unit type `()`
found opaque type `Tait`
= help: the trait `FnOnce<()>` is not implemented for `()`
= note: wrap the `()` in a closure with no arguments: `|| { /* code */ }`

error[E0308]: mismatched types
--> $DIR/lazy_subtyping_of_opaques.rs:10:5
error[E0277]: expected a `FnOnce()` closure, found `()`
--> $DIR/lazy_subtyping_of_opaques.rs:9:23
|
LL | fn reify_as_tait() -> Thunk<Tait> {
| ----------- expected `Thunk<_>` because of return type
| ^^^^^^^^^^^ expected an `FnOnce()` closure, found `()`
|
= help: the trait `FnOnce<()>` is not implemented for `()`
= note: wrap the `()` in a closure with no arguments: `|| { /* code */ }`

error[E0308]: mismatched types
--> $DIR/lazy_subtyping_of_opaques.rs:11:5
|
LL | Thunk::new(|cont| cont)
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `Thunk<_>`, found `()`
|
= note: expected struct `Thunk<_>`
found unit type `()`

error: aborting due to 2 previous errors
error: aborting due to 3 previous errors

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

0 comments on commit f2892e4

Please sign in to comment.