10
10
11
11
use borrow_check:: nll:: constraints:: OutlivesConstraint ;
12
12
use borrow_check:: nll:: type_check:: { BorrowCheckContext , Locations } ;
13
- use rustc:: infer:: canonical:: { Canonical , CanonicalVarInfos , CanonicalVarValues } ;
14
13
use rustc:: infer:: { InferCtxt , NLLRegionVariableOrigin } ;
15
14
use rustc:: mir:: ConstraintCategory ;
16
15
use rustc:: traits:: query:: Fallible ;
17
16
use rustc:: ty:: fold:: { TypeFoldable , TypeVisitor } ;
18
17
use rustc:: ty:: relate:: { self , Relate , RelateResult , TypeRelation } ;
19
18
use rustc:: ty:: subst:: Kind ;
20
- use rustc:: ty:: { self , CanonicalTy , CanonicalVar , Ty , TyCtxt } ;
19
+ use rustc:: ty:: { self , CanonicalTy , Ty , TyCtxt } ;
21
20
use rustc_data_structures:: fx:: FxHashMap ;
22
- use rustc_data_structures :: indexed_vec :: IndexVec ;
21
+ use syntax :: source_map :: DUMMY_SP ;
23
22
24
23
/// Adds sufficient constraints to ensure that `a <: b`.
25
24
pub ( super ) fn sub_types < ' tcx > (
@@ -35,7 +34,6 @@ pub(super) fn sub_types<'tcx>(
35
34
infcx,
36
35
NllTypeRelatingDelegate :: new ( infcx, borrowck_context, locations, category) ,
37
36
ty:: Variance :: Covariant ,
38
- ty:: List :: empty ( ) ,
39
37
) . relate ( & a, & b) ?;
40
38
Ok ( ( ) )
41
39
}
@@ -54,7 +52,6 @@ pub(super) fn eq_types<'tcx>(
54
52
infcx,
55
53
NllTypeRelatingDelegate :: new ( infcx, borrowck_context, locations, category) ,
56
54
ty:: Variance :: Invariant ,
57
- ty:: List :: empty ( ) ,
58
55
) . relate ( & a, & b) ?;
59
56
Ok ( ( ) )
60
57
}
@@ -66,19 +63,20 @@ pub(super) fn relate_type_and_user_type<'tcx>(
66
63
infcx : & InferCtxt < ' _ , ' _ , ' tcx > ,
67
64
a : Ty < ' tcx > ,
68
65
v : ty:: Variance ,
69
- b : CanonicalTy < ' tcx > ,
66
+ canonical_b : CanonicalTy < ' tcx > ,
70
67
locations : Locations ,
71
68
category : ConstraintCategory ,
72
69
borrowck_context : Option < & mut BorrowCheckContext < ' _ , ' tcx > > ,
73
70
) -> Fallible < Ty < ' tcx > > {
74
71
debug ! (
75
- "sub_type_and_user_type(a={:?}, b={:?}, locations={:?})" ,
76
- a, b, locations
72
+ "relate_type_and_user_type(a={:?}, v={:?}, b={:?}, locations={:?})" ,
73
+ a, v, canonical_b, locations
74
+ ) ;
75
+
76
+ let ( b, _values) = infcx. instantiate_canonical_with_fresh_inference_vars (
77
+ DUMMY_SP ,
78
+ & canonical_b,
77
79
) ;
78
- let Canonical {
79
- variables : b_variables,
80
- value : b_value,
81
- } = b;
82
80
83
81
// The `TypeRelating` code assumes that the "canonical variables"
84
82
// appear in the "a" side, so flip `Contravariant` ambient
@@ -89,20 +87,10 @@ pub(super) fn relate_type_and_user_type<'tcx>(
89
87
infcx,
90
88
NllTypeRelatingDelegate :: new ( infcx, borrowck_context, locations, category) ,
91
89
v1,
92
- b_variables,
93
90
) ;
94
- type_relating. relate ( & b_value, & a) ?;
95
-
96
- Ok ( b. substitute (
97
- infcx. tcx ,
98
- & CanonicalVarValues {
99
- var_values : type_relating
100
- . canonical_var_values
101
- . into_iter ( )
102
- . map ( |x| x. expect ( "unsubstituted canonical variable" ) )
103
- . collect ( ) ,
104
- } ,
105
- ) )
91
+ type_relating. relate ( & b, & a) ?;
92
+
93
+ Ok ( b)
106
94
}
107
95
108
96
struct TypeRelating < ' me , ' gcx : ' tcx , ' tcx : ' me , D >
@@ -136,19 +124,6 @@ where
136
124
137
125
/// Same as `a_scopes`, but for the `b` type.
138
126
b_scopes : Vec < BoundRegionScope < ' tcx > > ,
139
-
140
- /// As we execute, the type on the LHS *may* come from a canonical
141
- /// source. In that case, we will sometimes find a constraint like
142
- /// `?0 = B`, where `B` is a type from the RHS. The first time we
143
- /// find that, we simply record `B` (and the list of scopes that
144
- /// tells us how to *interpret* `B`). The next time we encounter
145
- /// `?0`, then, we can read this value out and use it.
146
- ///
147
- /// One problem: these variables may be in some other universe,
148
- /// how can we enforce that? I guess I could add some kind of
149
- /// "minimum universe constraint" that we can feed to the NLL checker.
150
- /// --> also, we know this doesn't happen
151
- canonical_var_values : IndexVec < CanonicalVar , Option < Kind < ' tcx > > > ,
152
127
}
153
128
154
129
trait TypeRelatingDelegate < ' tcx > {
@@ -279,14 +254,11 @@ where
279
254
infcx : & ' me InferCtxt < ' me , ' gcx , ' tcx > ,
280
255
delegate : D ,
281
256
ambient_variance : ty:: Variance ,
282
- canonical_var_infos : CanonicalVarInfos < ' tcx > ,
283
257
) -> Self {
284
- let canonical_var_values = IndexVec :: from_elem_n ( None , canonical_var_infos. len ( ) ) ;
285
258
Self {
286
259
infcx,
287
260
delegate,
288
261
ambient_variance,
289
- canonical_var_values,
290
262
a_scopes : vec ! [ ] ,
291
263
b_scopes : vec ! [ ] ,
292
264
}
@@ -400,19 +372,13 @@ where
400
372
/// equated, then equate it again.
401
373
fn relate_var (
402
374
& mut self ,
403
- var : CanonicalVar ,
404
- b_kind : Kind < ' tcx > ,
405
- ) -> RelateResult < ' tcx , Kind < ' tcx > > {
406
- debug ! ( "equate_var(var={:?}, b_kind={:?})" , var, b_kind) ;
407
-
408
- let generalized_kind = match self . canonical_var_values [ var] {
409
- Some ( v) => v,
410
- None => {
411
- let generalized_kind = self . generalize_value ( b_kind) ;
412
- self . canonical_var_values [ var] = Some ( generalized_kind) ;
413
- generalized_kind
414
- }
415
- } ;
375
+ var_ty : Ty < ' tcx > ,
376
+ value_ty : Ty < ' tcx > ,
377
+ ) -> RelateResult < ' tcx , Ty < ' tcx > > {
378
+ debug ! ( "equate_var(var_ty={:?}, value_ty={:?})" , var_ty, value_ty) ;
379
+
380
+ let generalized_ty = self . generalize_value ( value_ty) ;
381
+ self . infcx . force_instantiate_unchecked ( var_ty, generalized_ty) ;
416
382
417
383
// The generalized values we extract from `canonical_var_values` have
418
384
// been fully instantiated and hence the set of scopes we have
@@ -421,16 +387,16 @@ where
421
387
let old_a_scopes = :: std:: mem:: replace ( & mut self . a_scopes , vec ! [ ] ) ;
422
388
423
389
// Relate the generalized kind to the original one.
424
- let result = self . relate ( & generalized_kind , & b_kind ) ;
390
+ let result = self . relate ( & generalized_ty , & value_ty ) ;
425
391
426
392
// Restore the old scopes now.
427
393
self . a_scopes = old_a_scopes;
428
394
429
395
debug ! ( "equate_var: complete, result = {:?}" , result) ;
430
- return result;
396
+ result
431
397
}
432
398
433
- fn generalize_value ( & mut self , kind : Kind < ' tcx > ) -> Kind < ' tcx > {
399
+ fn generalize_value < T : Relate < ' tcx > > ( & mut self , value : T ) -> T {
434
400
TypeGeneralizer {
435
401
tcx : self . infcx . tcx ,
436
402
delegate : & mut self . delegate ,
@@ -440,7 +406,7 @@ where
440
406
// These always correspond to an `_` or `'_` written by
441
407
// user, and those are always in the root universe.
442
408
universe : ty:: UniverseIndex :: ROOT ,
443
- } . relate ( & kind , & kind )
409
+ } . relate ( & value , & value )
444
410
. unwrap ( )
445
411
}
446
412
}
@@ -490,18 +456,22 @@ where
490
456
}
491
457
492
458
fn tys ( & mut self , a : Ty < ' tcx > , b : Ty < ' tcx > ) -> RelateResult < ' tcx , Ty < ' tcx > > {
493
- // Watch out for the case that we are matching a `?T` against the
494
- // right-hand side.
495
- if let ty:: Infer ( ty:: CanonicalTy ( var) ) = a. sty {
496
- self . relate_var ( var, b. into ( ) ) ?;
497
- Ok ( a)
498
- } else {
499
- debug ! (
500
- "tys(a={:?}, b={:?}, variance={:?})" ,
501
- a, b, self . ambient_variance
502
- ) ;
459
+ let a = self . infcx . shallow_resolve ( a) ;
460
+ match a. sty {
461
+ ty:: Infer ( ty:: TyVar ( _) ) |
462
+ ty:: Infer ( ty:: IntVar ( _) ) |
463
+ ty:: Infer ( ty:: FloatVar ( _) ) => {
464
+ self . relate_var ( a. into ( ) , b. into ( ) )
465
+ }
466
+
467
+ _ => {
468
+ debug ! (
469
+ "tys(a={:?}, b={:?}, variance={:?})" ,
470
+ a, b, self . ambient_variance
471
+ ) ;
503
472
504
- relate:: super_relate_tys ( self , a, b)
473
+ relate:: super_relate_tys ( self , a, b)
474
+ }
505
475
}
506
476
}
507
477
@@ -510,11 +480,6 @@ where
510
480
a : ty:: Region < ' tcx > ,
511
481
b : ty:: Region < ' tcx > ,
512
482
) -> RelateResult < ' tcx , ty:: Region < ' tcx > > {
513
- if let ty:: ReCanonical ( var) = a {
514
- self . relate_var ( * var, b. into ( ) ) ?;
515
- return Ok ( a) ;
516
- }
517
-
518
483
debug ! (
519
484
"regions(a={:?}, b={:?}, variance={:?})" ,
520
485
a, b, self . ambient_variance
0 commit comments