Skip to content

Commit 37747b5

Browse files
committed
nll-relate: improve hr opaque types support
1 parent 425e142 commit 37747b5

File tree

6 files changed

+32
-26
lines changed

6 files changed

+32
-26
lines changed

compiler/rustc_borrowck/src/type_check/relate_tys.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,13 @@ impl<'a, 'b, 'tcx> NllTypeRelating<'a, 'b, 'tcx> {
124124
// by using `ty_vid rel B` and then finally and end by equating `ty_vid` to
125125
// the opaque.
126126
let mut enable_subtyping = |ty, opaque_is_expected| {
127-
let ty_vid = infcx.next_ty_var_id_in_universe(self.span(), ty::UniverseIndex::ROOT);
128-
127+
// We create the fresh inference variable in the highest universe.
128+
// In theory we could limit it to the highest universe in the args of
129+
// the opaque but that isn't really worth the effort.
130+
//
131+
// We'll make sure that the opaque type can actually name everything
132+
// in its hidden type later on.
133+
let ty_vid = infcx.next_ty_vid(self.span());
129134
let variance = if opaque_is_expected {
130135
self.ambient_variance
131136
} else {

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -743,22 +743,30 @@ impl<'tcx> InferCtxt<'tcx> {
743743
self.inner.borrow_mut().type_variables().num_vars()
744744
}
745745

746-
pub fn next_ty_var(&self, span: Span) -> Ty<'tcx> {
747-
self.next_ty_var_with_origin(TypeVariableOrigin { span, param_def_id: None })
746+
pub fn next_ty_vid(&self, span: Span) -> TyVid {
747+
self.next_ty_vid_with_origin(TypeVariableOrigin { span, param_def_id: None })
748748
}
749749

750-
pub fn next_ty_var_with_origin(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
751-
let vid = self.inner.borrow_mut().type_variables().new_var(self.universe(), origin);
752-
Ty::new_var(self.tcx, vid)
750+
pub fn next_ty_vid_with_origin(&self, origin: TypeVariableOrigin) -> TyVid {
751+
self.inner.borrow_mut().type_variables().new_var(self.universe(), origin)
753752
}
754753

755-
pub fn next_ty_var_id_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> TyVid {
754+
pub fn next_ty_vid_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> TyVid {
756755
let origin = TypeVariableOrigin { span, param_def_id: None };
757756
self.inner.borrow_mut().type_variables().new_var(universe, origin)
758757
}
759758

759+
pub fn next_ty_var(&self, span: Span) -> Ty<'tcx> {
760+
self.next_ty_var_with_origin(TypeVariableOrigin { span, param_def_id: None })
761+
}
762+
763+
pub fn next_ty_var_with_origin(&self, origin: TypeVariableOrigin) -> Ty<'tcx> {
764+
let vid = self.next_ty_vid_with_origin(origin);
765+
Ty::new_var(self.tcx, vid)
766+
}
767+
760768
pub fn next_ty_var_in_universe(&self, span: Span, universe: ty::UniverseIndex) -> Ty<'tcx> {
761-
let vid = self.next_ty_var_id_in_universe(span, universe);
769+
let vid = self.next_ty_vid_in_universe(span, universe);
762770
Ty::new_var(self.tcx, vid)
763771
}
764772

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ type Successors<'a> = impl std::fmt::Debug + 'a;
2424
impl Terminator {
2525
#[define_opaque(Successors, Tait)]
2626
fn successors(&self, mut f: for<'x> fn(&'x ()) -> <&'x A as B>::C) -> Successors<'_> {
27-
f = g;
28-
//~^ ERROR mismatched types
27+
f = g; //~ ERROR expected generic lifetime parameter, found `'x`
2928
}
3029
}
3130

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
error[E0308]: mismatched types
1+
error[E0792]: expected generic lifetime parameter, found `'x`
22
--> $DIR/higher_kinded_params3.rs:27:9
33
|
44
LL | type Tait<'a> = impl std::fmt::Debug + 'a;
5-
| ------------------------- the expected opaque type
5+
| -- this generic parameter must be used with a generic lifetime parameter
66
...
77
LL | f = g;
8-
| ^^^^^ one type is more general than the other
9-
|
10-
= note: expected fn pointer `for<'x> fn(&'x ()) -> Tait<'x>`
11-
found fn pointer `for<'a> fn(&'a ()) -> &'a ()`
8+
| ^^^^^
129

1310
error: aborting due to 1 previous error
1411

15-
For more information about this error, try `rustc --explain E0308`.
12+
For more information about this error, try `rustc --explain E0792`.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn foo<'a>(x: &'a ()) -> &'a () {
88

99
#[define_opaque(Opaque)]
1010
fn test() -> for<'a> fn(&'a ()) -> Opaque<'a> {
11-
foo //~ ERROR: mismatched types
11+
foo //~ ERROR: expected generic lifetime parameter, found `'a`
1212
}
1313

1414
fn main() {}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
error[E0308]: mismatched types
1+
error[E0792]: expected generic lifetime parameter, found `'a`
22
--> $DIR/hkl_forbidden3.rs:11:5
33
|
44
LL | type Opaque<'a> = impl Sized + 'a;
5-
| --------------- the expected opaque type
5+
| -- this generic parameter must be used with a generic lifetime parameter
66
...
77
LL | foo
8-
| ^^^ one type is more general than the other
9-
|
10-
= note: expected fn pointer `for<'a> fn(&'a ()) -> Opaque<'a>`
11-
found fn pointer `for<'a> fn(&'a ()) -> &'a ()`
8+
| ^^^
129

1310
error: aborting due to 1 previous error
1411

15-
For more information about this error, try `rustc --explain E0308`.
12+
For more information about this error, try `rustc --explain E0792`.

0 commit comments

Comments
 (0)