2525//! sometimes useful when the types of `c` and `d` are not traceable
2626//! things. (That system should probably be refactored.)
2727
28+ use relate:: lattice:: { LatticeOp , LatticeOpKind } ;
2829use rustc_middle:: bug;
2930use rustc_middle:: ty:: { Const , ImplSubject } ;
3031
3132use super :: * ;
33+ use crate :: infer:: relate:: type_relating:: TypeRelating ;
3234use crate :: infer:: relate:: { Relate , StructurallyRelateAliases , TypeRelation } ;
33- use crate :: traits:: Obligation ;
3435
3536/// Whether we should define opaque types or just treat them opaquely.
3637///
@@ -108,14 +109,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
108109 where
109110 T : ToTrace < ' tcx > ,
110111 {
111- let mut fields = CombineFields :: new (
112+ let mut op = TypeRelating :: new (
112113 self . infcx ,
113114 ToTrace :: to_trace ( self . cause , expected, actual) ,
114115 self . param_env ,
115116 define_opaque_types,
117+ StructurallyRelateAliases :: No ,
118+ ty:: Contravariant ,
116119 ) ;
117- fields . sup ( ) . relate ( expected, actual) ?;
118- Ok ( InferOk { value : ( ) , obligations : fields . into_obligations ( ) } )
120+ op . relate ( expected, actual) ?;
121+ Ok ( InferOk { value : ( ) , obligations : op . into_obligations ( ) } )
119122 }
120123
121124 /// Makes `expected <: actual`.
@@ -128,14 +131,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
128131 where
129132 T : ToTrace < ' tcx > ,
130133 {
131- let mut fields = CombineFields :: new (
134+ let mut op = TypeRelating :: new (
132135 self . infcx ,
133136 ToTrace :: to_trace ( self . cause , expected, actual) ,
134137 self . param_env ,
135138 define_opaque_types,
139+ StructurallyRelateAliases :: No ,
140+ ty:: Covariant ,
136141 ) ;
137- fields . sub ( ) . relate ( expected, actual) ?;
138- Ok ( InferOk { value : ( ) , obligations : fields . into_obligations ( ) } )
142+ op . relate ( expected, actual) ?;
143+ Ok ( InferOk { value : ( ) , obligations : op . into_obligations ( ) } )
139144 }
140145
141146 /// Makes `expected == actual`.
@@ -167,45 +172,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
167172 where
168173 T : Relate < TyCtxt < ' tcx > > ,
169174 {
170- let mut fields = CombineFields :: new ( self . infcx , trace, self . param_env , define_opaque_types) ;
171- fields. equate ( StructurallyRelateAliases :: No ) . relate ( expected, actual) ?;
172- Ok ( InferOk {
173- value : ( ) ,
174- obligations : fields
175- . goals
176- . into_iter ( )
177- . map ( |goal| {
178- Obligation :: new (
179- self . infcx . tcx ,
180- fields. trace . cause . clone ( ) ,
181- goal. param_env ,
182- goal. predicate ,
183- )
184- } )
185- . collect ( ) ,
186- } )
187- }
188-
189- /// Equates `expected` and `found` while structurally relating aliases.
190- /// This should only be used inside of the next generation trait solver
191- /// when relating rigid aliases.
192- pub fn eq_structurally_relating_aliases < T > (
193- self ,
194- expected : T ,
195- actual : T ,
196- ) -> InferResult < ' tcx , ( ) >
197- where
198- T : ToTrace < ' tcx > ,
199- {
200- assert ! ( self . infcx. next_trait_solver( ) ) ;
201- let mut fields = CombineFields :: new (
175+ let mut op = TypeRelating :: new (
202176 self . infcx ,
203- ToTrace :: to_trace ( self . cause , expected , actual ) ,
177+ trace ,
204178 self . param_env ,
205- DefineOpaqueTypes :: Yes ,
179+ define_opaque_types,
180+ StructurallyRelateAliases :: No ,
181+ ty:: Invariant ,
206182 ) ;
207- fields . equate ( StructurallyRelateAliases :: Yes ) . relate ( expected, actual) ?;
208- Ok ( InferOk { value : ( ) , obligations : fields . into_obligations ( ) } )
183+ op . relate ( expected, actual) ?;
184+ Ok ( InferOk { value : ( ) , obligations : op . into_obligations ( ) } )
209185 }
210186
211187 pub fn relate < T > (
@@ -242,19 +218,16 @@ impl<'a, 'tcx> At<'a, 'tcx> {
242218 where
243219 T : Relate < TyCtxt < ' tcx > > ,
244220 {
245- let mut fields = CombineFields :: new (
221+ let mut op = TypeRelating :: new (
246222 self . infcx ,
247223 TypeTrace :: dummy ( self . cause ) ,
248224 self . param_env ,
249225 DefineOpaqueTypes :: Yes ,
250- ) ;
251- fields. sub ( ) . relate_with_variance (
226+ StructurallyRelateAliases :: No ,
252227 variance,
253- ty:: VarianceDiagInfo :: default ( ) ,
254- expected,
255- actual,
256- ) ?;
257- Ok ( fields. goals )
228+ ) ;
229+ op. relate ( expected, actual) ?;
230+ Ok ( op. into_obligations ( ) . into_iter ( ) . map ( |o| o. into ( ) ) . collect ( ) )
258231 }
259232
260233 /// Used in the new solver since we don't care about tracking an `ObligationCause`.
@@ -266,60 +239,35 @@ impl<'a, 'tcx> At<'a, 'tcx> {
266239 where
267240 T : Relate < TyCtxt < ' tcx > > ,
268241 {
269- let mut fields = CombineFields :: new (
242+ let mut op = TypeRelating :: new (
270243 self . infcx ,
271244 TypeTrace :: dummy ( self . cause ) ,
272245 self . param_env ,
273246 DefineOpaqueTypes :: Yes ,
247+ StructurallyRelateAliases :: Yes ,
248+ ty:: Invariant ,
274249 ) ;
275- fields . equate ( StructurallyRelateAliases :: Yes ) . relate ( expected, actual) ?;
276- Ok ( fields . goals )
250+ op . relate ( expected, actual) ?;
251+ Ok ( op . into_obligations ( ) . into_iter ( ) . map ( |o| o . into ( ) ) . collect ( ) )
277252 }
278253
279254 /// Computes the least-upper-bound, or mutual supertype, of two
280255 /// values. The order of the arguments doesn't matter, but since
281256 /// this can result in an error (e.g., if asked to compute LUB of
282257 /// u32 and i32), it is meaningful to call one of them the
283258 /// "expected type".
284- pub fn lub < T > (
285- self ,
286- define_opaque_types : DefineOpaqueTypes ,
287- expected : T ,
288- actual : T ,
289- ) -> InferResult < ' tcx , T >
259+ pub fn lub < T > ( self , expected : T , actual : T ) -> InferResult < ' tcx , T >
290260 where
291261 T : ToTrace < ' tcx > ,
292262 {
293- let mut fields = CombineFields :: new (
263+ let mut op = LatticeOp :: new (
294264 self . infcx ,
295265 ToTrace :: to_trace ( self . cause , expected, actual) ,
296266 self . param_env ,
297- define_opaque_types,
298- ) ;
299- let value = fields. lub ( ) . relate ( expected, actual) ?;
300- Ok ( InferOk { value, obligations : fields. into_obligations ( ) } )
301- }
302-
303- /// Computes the greatest-lower-bound, or mutual subtype, of two
304- /// values. As with `lub` order doesn't matter, except for error
305- /// cases.
306- pub fn glb < T > (
307- self ,
308- define_opaque_types : DefineOpaqueTypes ,
309- expected : T ,
310- actual : T ,
311- ) -> InferResult < ' tcx , T >
312- where
313- T : ToTrace < ' tcx > ,
314- {
315- let mut fields = CombineFields :: new (
316- self . infcx ,
317- ToTrace :: to_trace ( self . cause , expected, actual) ,
318- self . param_env ,
319- define_opaque_types,
267+ LatticeOpKind :: Lub ,
320268 ) ;
321- let value = fields . glb ( ) . relate ( expected, actual) ?;
322- Ok ( InferOk { value, obligations : fields . into_obligations ( ) } )
269+ let value = op . relate ( expected, actual) ?;
270+ Ok ( InferOk { value, obligations : op . into_obligations ( ) } )
323271 }
324272}
325273
0 commit comments