11use rustc_infer:: infer:: nll_relate:: { NormalizationStrategy , TypeRelating , TypeRelatingDelegate } ;
2- use rustc_infer:: infer:: { InferCtxt , NllRegionVariableOrigin } ;
2+ use rustc_infer:: infer:: NllRegionVariableOrigin ;
33use rustc_middle:: mir:: ConstraintCategory ;
44use rustc_middle:: ty:: relate:: TypeRelation ;
55use rustc_middle:: ty:: { self , Const , Ty } ;
66use rustc_trait_selection:: traits:: query:: Fallible ;
77
88use crate :: constraints:: OutlivesConstraint ;
99use crate :: diagnostics:: UniverseInfo ;
10- use crate :: type_check:: { BorrowCheckContext , Locations } ;
11-
12- /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
13- ///
14- /// - "Covariant" `a <: b`
15- /// - "Invariant" `a == b`
16- /// - "Contravariant" `a :> b`
17- ///
18- /// N.B., the type `a` is permitted to have unresolved inference
19- /// variables, but not the type `b`.
20- #[ instrument( skip( infcx, param_env, borrowck_context) , level = "debug" ) ]
21- pub ( super ) fn relate_types < ' tcx > (
22- infcx : & InferCtxt < ' _ , ' tcx > ,
23- param_env : ty:: ParamEnv < ' tcx > ,
24- a : Ty < ' tcx > ,
25- v : ty:: Variance ,
26- b : Ty < ' tcx > ,
27- locations : Locations ,
28- category : ConstraintCategory ,
29- borrowck_context : & mut BorrowCheckContext < ' _ , ' tcx > ,
30- ) -> Fallible < ( ) > {
31- TypeRelating :: new (
32- infcx,
33- NllTypeRelatingDelegate :: new (
34- infcx,
35- borrowck_context,
36- param_env,
37- locations,
38- category,
39- UniverseInfo :: relate ( a, b) ,
40- ) ,
41- v,
42- )
43- . relate ( a, b) ?;
44- Ok ( ( ) )
10+ use crate :: type_check:: { Locations , TypeChecker } ;
11+
12+ impl < ' a , ' tcx > TypeChecker < ' a , ' tcx > {
13+ /// Adds sufficient constraints to ensure that `a R b` where `R` depends on `v`:
14+ ///
15+ /// - "Covariant" `a <: b`
16+ /// - "Invariant" `a == b`
17+ /// - "Contravariant" `a :> b`
18+ ///
19+ /// N.B., the type `a` is permitted to have unresolved inference
20+ /// variables, but not the type `b`.
21+ #[ instrument( skip( self ) , level = "debug" ) ]
22+ pub ( super ) fn relate_types (
23+ & mut self ,
24+ a : Ty < ' tcx > ,
25+ v : ty:: Variance ,
26+ b : Ty < ' tcx > ,
27+ locations : Locations ,
28+ category : ConstraintCategory ,
29+ ) -> Fallible < ( ) > {
30+ TypeRelating :: new (
31+ self . infcx ,
32+ NllTypeRelatingDelegate :: new ( self , locations, category, UniverseInfo :: relate ( a, b) ) ,
33+ v,
34+ )
35+ . relate ( a, b) ?;
36+ Ok ( ( ) )
37+ }
4538}
4639
4740struct NllTypeRelatingDelegate < ' me , ' bccx , ' tcx > {
48- infcx : & ' me InferCtxt < ' me , ' tcx > ,
49- borrowck_context : & ' me mut BorrowCheckContext < ' bccx , ' tcx > ,
50-
51- param_env : ty:: ParamEnv < ' tcx > ,
41+ type_checker : & ' me mut TypeChecker < ' bccx , ' tcx > ,
5242
5343 /// Where (and why) is this relation taking place?
5444 locations : Locations ,
@@ -63,25 +53,24 @@ struct NllTypeRelatingDelegate<'me, 'bccx, 'tcx> {
6353
6454impl NllTypeRelatingDelegate < ' me , ' bccx , ' tcx > {
6555 fn new (
66- infcx : & ' me InferCtxt < ' me , ' tcx > ,
67- borrowck_context : & ' me mut BorrowCheckContext < ' bccx , ' tcx > ,
68- param_env : ty:: ParamEnv < ' tcx > ,
56+ type_checker : & ' me mut TypeChecker < ' bccx , ' tcx > ,
6957 locations : Locations ,
7058 category : ConstraintCategory ,
7159 universe_info : UniverseInfo < ' tcx > ,
7260 ) -> Self {
73- Self { infcx , borrowck_context , param_env , locations, category, universe_info }
61+ Self { type_checker , locations, category, universe_info }
7462 }
7563}
7664
7765impl TypeRelatingDelegate < ' tcx > for NllTypeRelatingDelegate < ' _ , ' _ , ' tcx > {
7866 fn param_env ( & self ) -> ty:: ParamEnv < ' tcx > {
79- self . param_env
67+ self . type_checker . param_env
8068 }
8169
8270 fn create_next_universe ( & mut self ) -> ty:: UniverseIndex {
83- let universe = self . infcx . create_next_universe ( ) ;
84- self . borrowck_context
71+ let universe = self . type_checker . infcx . create_next_universe ( ) ;
72+ self . type_checker
73+ . borrowck_context
8574 . constraints
8675 . universe_causes
8776 . insert ( universe, self . universe_info . clone ( ) ) ;
@@ -90,15 +79,18 @@ impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
9079
9180 fn next_existential_region_var ( & mut self , from_forall : bool ) -> ty:: Region < ' tcx > {
9281 let origin = NllRegionVariableOrigin :: Existential { from_forall } ;
93- self . infcx . next_nll_region_var ( origin)
82+ self . type_checker . infcx . next_nll_region_var ( origin)
9483 }
9584
9685 fn next_placeholder_region ( & mut self , placeholder : ty:: PlaceholderRegion ) -> ty:: Region < ' tcx > {
97- self . borrowck_context . constraints . placeholder_region ( self . infcx , placeholder)
86+ self . type_checker
87+ . borrowck_context
88+ . constraints
89+ . placeholder_region ( self . type_checker . infcx , placeholder)
9890 }
9991
10092 fn generalize_existential ( & mut self , universe : ty:: UniverseIndex ) -> ty:: Region < ' tcx > {
101- self . infcx . next_nll_region_var_in_universe (
93+ self . type_checker . infcx . next_nll_region_var_in_universe (
10294 NllRegionVariableOrigin :: Existential { from_forall : false } ,
10395 universe,
10496 )
@@ -110,15 +102,17 @@ impl TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> {
110102 sub : ty:: Region < ' tcx > ,
111103 info : ty:: VarianceDiagInfo < ' tcx > ,
112104 ) {
113- let sub = self . borrowck_context . universal_regions . to_region_vid ( sub) ;
114- let sup = self . borrowck_context . universal_regions . to_region_vid ( sup) ;
115- self . borrowck_context . constraints . outlives_constraints . push ( OutlivesConstraint {
116- sup,
117- sub,
118- locations : self . locations ,
119- category : self . category ,
120- variance_info : info,
121- } ) ;
105+ let sub = self . type_checker . borrowck_context . universal_regions . to_region_vid ( sub) ;
106+ let sup = self . type_checker . borrowck_context . universal_regions . to_region_vid ( sup) ;
107+ self . type_checker . borrowck_context . constraints . outlives_constraints . push (
108+ OutlivesConstraint {
109+ sup,
110+ sub,
111+ locations : self . locations ,
112+ category : self . category ,
113+ variance_info : info,
114+ } ,
115+ ) ;
122116 }
123117
124118 // We don't have to worry about the equality of consts during borrow checking
0 commit comments