@@ -12,35 +12,23 @@ use borrow_check::nll::constraints::OutlivesConstraint;
12
12
use borrow_check:: nll:: type_check:: { BorrowCheckContext , Locations } ;
13
13
use rustc:: infer:: nll_relate:: { TypeRelating , TypeRelatingDelegate } ;
14
14
use rustc:: infer:: { InferCtxt , NLLRegionVariableOrigin } ;
15
- use rustc:: mir:: { ConstraintCategory , UserTypeAnnotation } ;
15
+ use rustc:: mir:: ConstraintCategory ;
16
16
use rustc:: traits:: query:: Fallible ;
17
17
use rustc:: ty:: relate:: TypeRelation ;
18
- use rustc:: ty:: subst:: { Subst , UserSelfTy , UserSubsts } ;
19
- use rustc:: ty:: { self , Ty , TypeFoldable } ;
20
- use syntax_pos:: DUMMY_SP ;
21
-
22
- /// Adds sufficient constraints to ensure that `a <: b`.
23
- pub ( super ) fn sub_types < ' tcx > (
24
- infcx : & InferCtxt < ' _ , ' _ , ' tcx > ,
25
- a : Ty < ' tcx > ,
26
- b : Ty < ' tcx > ,
27
- locations : Locations ,
28
- category : ConstraintCategory ,
29
- borrowck_context : Option < & mut BorrowCheckContext < ' _ , ' tcx > > ,
30
- ) -> Fallible < ( ) > {
31
- debug ! ( "sub_types(a={:?}, b={:?}, locations={:?})" , a, b, locations) ;
32
- TypeRelating :: new (
33
- infcx,
34
- NllTypeRelatingDelegate :: new ( infcx, borrowck_context, locations, category) ,
35
- ty:: Variance :: Covariant ,
36
- ) . relate ( & a, & b) ?;
37
- Ok ( ( ) )
38
- }
39
-
40
- /// Adds sufficient constraints to ensure that `a == b`.
41
- pub ( super ) fn eq_types < ' tcx > (
18
+ use rustc:: ty:: { self , Ty } ;
19
+
20
+ /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
21
+ ///
22
+ /// - "Covariant" `a <: b`
23
+ /// - "Invariant" `a == b`
24
+ /// - "Contravariant" `a :> b`
25
+ ///
26
+ /// NB. The type `a` is permitted to have unresolved inference
27
+ /// variables, but not the type `b`.
28
+ pub ( super ) fn relate_types < ' tcx > (
42
29
infcx : & InferCtxt < ' _ , ' _ , ' tcx > ,
43
30
a : Ty < ' tcx > ,
31
+ v : ty:: Variance ,
44
32
b : Ty < ' tcx > ,
45
33
locations : Locations ,
46
34
category : ConstraintCategory ,
@@ -50,91 +38,11 @@ pub(super) fn eq_types<'tcx>(
50
38
TypeRelating :: new (
51
39
infcx,
52
40
NllTypeRelatingDelegate :: new ( infcx, borrowck_context, locations, category) ,
53
- ty :: Variance :: Invariant ,
41
+ v ,
54
42
) . relate ( & a, & b) ?;
55
43
Ok ( ( ) )
56
44
}
57
45
58
- /// Adds sufficient constraints to ensure that `a <: b`, where `b` is
59
- /// a user-given type (which means it may have canonical variables
60
- /// encoding things like `_`).
61
- pub ( super ) fn relate_type_and_user_type < ' tcx > (
62
- infcx : & InferCtxt < ' _ , ' _ , ' tcx > ,
63
- a : Ty < ' tcx > ,
64
- v : ty:: Variance ,
65
- user_ty : UserTypeAnnotation < ' tcx > ,
66
- locations : Locations ,
67
- category : ConstraintCategory ,
68
- borrowck_context : Option < & mut BorrowCheckContext < ' _ , ' tcx > > ,
69
- ) -> Fallible < Ty < ' tcx > > {
70
- debug ! (
71
- "relate_type_and_user_type(a={:?}, v={:?}, b={:?}, locations={:?})" ,
72
- a, v, user_ty, locations
73
- ) ;
74
-
75
- // The `TypeRelating` code assumes that the "canonical variables"
76
- // appear in the "a" side, so flip `Contravariant` ambient
77
- // variance to get the right relationship.
78
- let v1 = ty:: Contravariant . xform ( v) ;
79
-
80
- let mut type_relating = TypeRelating :: new (
81
- infcx,
82
- NllTypeRelatingDelegate :: new ( infcx, borrowck_context, locations, category) ,
83
- v1,
84
- ) ;
85
-
86
- match user_ty {
87
- UserTypeAnnotation :: Ty ( canonical_ty) => {
88
- let ( ty, _) =
89
- infcx. instantiate_canonical_with_fresh_inference_vars ( DUMMY_SP , & canonical_ty) ;
90
- type_relating. relate ( & ty, & a) ?;
91
- Ok ( ty)
92
- }
93
- UserTypeAnnotation :: TypeOf ( def_id, canonical_substs) => {
94
- let (
95
- UserSubsts {
96
- substs,
97
- user_self_ty,
98
- } ,
99
- _,
100
- ) = infcx. instantiate_canonical_with_fresh_inference_vars ( DUMMY_SP , & canonical_substs) ;
101
-
102
- let ty = infcx. tcx . type_of ( def_id) ;
103
- let ty = ty. subst ( infcx. tcx , substs) ;
104
-
105
- type_relating. relate ( & ty, & a) ?;
106
-
107
- if let Some ( UserSelfTy {
108
- impl_def_id,
109
- self_ty,
110
- } ) = user_self_ty
111
- {
112
- let impl_self_ty = infcx. tcx . type_of ( impl_def_id) ;
113
- let impl_self_ty = impl_self_ty. subst ( infcx. tcx , & substs) ;
114
-
115
- // There may be type variables in `substs` and hence
116
- // in `impl_self_ty`, but they should all have been
117
- // resolved to some fixed value during the first call
118
- // to `relate`, above. Therefore, if we use
119
- // `resolve_type_vars_if_possible` we should get to
120
- // something without type variables. This is important
121
- // because the `b` type in `relate_with_variance`
122
- // below is not permitted to have inference variables.
123
- let impl_self_ty = infcx. resolve_type_vars_if_possible ( & impl_self_ty) ;
124
- assert ! ( !impl_self_ty. has_infer_types( ) ) ;
125
-
126
- type_relating. relate_with_variance (
127
- ty:: Variance :: Invariant ,
128
- & self_ty,
129
- & impl_self_ty,
130
- ) ?;
131
- }
132
-
133
- Ok ( ty)
134
- }
135
- }
136
- }
137
-
138
46
struct NllTypeRelatingDelegate < ' me , ' bccx : ' me , ' gcx : ' tcx , ' tcx : ' bccx > {
139
47
infcx : & ' me InferCtxt < ' me , ' gcx , ' tcx > ,
140
48
borrowck_context : Option < & ' me mut BorrowCheckContext < ' bccx , ' tcx > > ,
0 commit comments