@@ -282,9 +282,10 @@ impl ast_node for hir::Pat {
282
282
283
283
#[ derive( Clone ) ]
284
284
pub struct MemCategorizationContext < ' a , ' gcx : ' a +' tcx , ' tcx : ' a > {
285
- pub infcx : & ' a InferCtxt < ' a , ' gcx , ' tcx > ,
285
+ pub tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
286
286
pub region_maps : & ' a RegionMaps ,
287
287
pub tables : & ' a ty:: TypeckTables < ' tcx > ,
288
+ infcx : Option < & ' a InferCtxt < ' a , ' gcx , ' tcx > > ,
288
289
}
289
290
290
291
pub type McResult < T > = Result < T , ( ) > ;
@@ -385,27 +386,51 @@ impl MutabilityCategory {
385
386
}
386
387
}
387
388
388
- impl < ' a , ' gcx , ' tcx > MemCategorizationContext < ' a , ' gcx , ' tcx > {
389
- /// Context should be the `DefId` we use to fetch region-maps.
390
- pub fn new ( infcx : & ' a InferCtxt < ' a , ' gcx , ' tcx > ,
389
+ impl < ' a , ' tcx > MemCategorizationContext < ' a , ' tcx , ' tcx > {
390
+ pub fn new ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
391
391
region_maps : & ' a RegionMaps ,
392
392
tables : & ' a ty:: TypeckTables < ' tcx > )
393
- -> MemCategorizationContext < ' a , ' gcx , ' tcx > {
394
- MemCategorizationContext { infcx , region_maps, tables }
393
+ -> MemCategorizationContext < ' a , ' tcx , ' tcx > {
394
+ MemCategorizationContext { tcx , region_maps, tables, infcx : None }
395
395
}
396
+ }
396
397
397
- fn tcx ( & self ) -> TyCtxt < ' a , ' gcx , ' tcx > {
398
- self . infcx . tcx
398
+ impl < ' a , ' gcx , ' tcx > MemCategorizationContext < ' a , ' gcx , ' tcx > {
399
+ pub fn with_infer ( infcx : & ' a InferCtxt < ' a , ' gcx , ' tcx > ,
400
+ region_maps : & ' a RegionMaps ,
401
+ tables : & ' a ty:: TypeckTables < ' tcx > )
402
+ -> MemCategorizationContext < ' a , ' gcx , ' tcx > {
403
+ MemCategorizationContext {
404
+ tcx : infcx. tcx ,
405
+ region_maps,
406
+ tables,
407
+ infcx : Some ( infcx) ,
408
+ }
409
+ }
410
+
411
+ pub fn type_moves_by_default ( & self ,
412
+ param_env : ty:: ParamEnv < ' tcx > ,
413
+ ty : Ty < ' tcx > ,
414
+ span : Span )
415
+ -> bool {
416
+ self . infcx . map ( |infcx| infcx. type_moves_by_default ( param_env, ty, span) )
417
+ . or_else ( || {
418
+ self . tcx . lift_to_global ( & ( param_env, ty) ) . map ( |( param_env, ty) | {
419
+ ty. moves_by_default ( self . tcx . global_tcx ( ) , param_env, span)
420
+ } )
421
+ } )
422
+ . unwrap_or ( true )
399
423
}
400
424
401
425
fn resolve_type_vars_if_possible < T > ( & self , value : & T ) -> T
402
426
where T : TypeFoldable < ' tcx >
403
427
{
404
- self . infcx . resolve_type_vars_if_possible ( value)
428
+ self . infcx . map ( |infcx| infcx. resolve_type_vars_if_possible ( value) )
429
+ . unwrap_or_else ( || value. clone ( ) )
405
430
}
406
431
407
432
fn is_tainted_by_errors ( & self ) -> bool {
408
- self . infcx . is_tainted_by_errors ( )
433
+ self . infcx . map_or ( false , |infcx| infcx . is_tainted_by_errors ( ) )
409
434
}
410
435
411
436
fn resolve_type_vars_or_error ( & self ,
@@ -426,7 +451,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
426
451
None if self . is_tainted_by_errors ( ) => Err ( ( ) ) ,
427
452
None => {
428
453
bug ! ( "no type for node {}: {} in mem_categorization" ,
429
- id, self . tcx( ) . hir. node_to_string( id) ) ;
454
+ id, self . tcx. hir. node_to_string( id) ) ;
430
455
}
431
456
}
432
457
}
@@ -506,7 +531,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
506
531
adjustment:: Adjust :: Deref ( overloaded) => {
507
532
// Equivalent to *expr or something similar.
508
533
let base = if let Some ( deref) = overloaded {
509
- let ref_ty = self . tcx ( ) . mk_ref ( deref. region , ty:: TypeAndMut {
534
+ let ref_ty = self . tcx . mk_ref ( deref. region , ty:: TypeAndMut {
510
535
ty : target,
511
536
mutbl : deref. mutbl ,
512
537
} ) ;
@@ -624,17 +649,17 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
624
649
}
625
650
626
651
Def :: Upvar ( def_id, _, fn_node_id) => {
627
- let var_id = self . tcx ( ) . hir . as_local_node_id ( def_id) . unwrap ( ) ;
652
+ let var_id = self . tcx . hir . as_local_node_id ( def_id) . unwrap ( ) ;
628
653
self . cat_upvar ( id, span, var_id, fn_node_id)
629
654
}
630
655
631
656
Def :: Local ( def_id) => {
632
- let vid = self . tcx ( ) . hir . as_local_node_id ( def_id) . unwrap ( ) ;
657
+ let vid = self . tcx . hir . as_local_node_id ( def_id) . unwrap ( ) ;
633
658
Ok ( Rc :: new ( cmt_ {
634
659
id : id,
635
660
span : span,
636
661
cat : Categorization :: Local ( vid) ,
637
- mutbl : MutabilityCategory :: from_local ( self . tcx ( ) , vid) ,
662
+ mutbl : MutabilityCategory :: from_local ( self . tcx , vid) ,
638
663
ty : expr_ty,
639
664
note : NoteNone
640
665
} ) )
@@ -686,7 +711,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
686
711
let var_ty = self . node_ty ( var_id) ?;
687
712
688
713
// Mutability of original variable itself
689
- let var_mutbl = MutabilityCategory :: from_local ( self . tcx ( ) , var_id) ;
714
+ let var_mutbl = MutabilityCategory :: from_local ( self . tcx , var_id) ;
690
715
691
716
// Construct the upvar. This represents access to the field
692
717
// from the environment (perhaps we should eventually desugar
@@ -753,11 +778,11 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
753
778
-> cmt_ < ' tcx >
754
779
{
755
780
// Region of environment pointer
756
- let env_region = self . tcx ( ) . mk_region ( ty:: ReFree ( ty:: FreeRegion {
781
+ let env_region = self . tcx . mk_region ( ty:: ReFree ( ty:: FreeRegion {
757
782
// The environment of a closure is guaranteed to
758
783
// outlive any bindings introduced in the body of the
759
784
// closure itself.
760
- scope : self . tcx ( ) . hir . local_def_id ( upvar_id. closure_expr_id ) ,
785
+ scope : self . tcx . hir . local_def_id ( upvar_id. closure_expr_id ) ,
761
786
bound_region : ty:: BrEnv
762
787
} ) ) ;
763
788
@@ -774,7 +799,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
774
799
// one.
775
800
let cmt_result = cmt_ {
776
801
mutbl : McImmutable ,
777
- ty : self . tcx ( ) . types . err ,
802
+ ty : self . tcx . types . err ,
778
803
..cmt_result
779
804
} ;
780
805
@@ -806,7 +831,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
806
831
pub fn temporary_scope ( & self , id : ast:: NodeId ) -> ty:: Region < ' tcx >
807
832
{
808
833
let scope = self . region_maps . temporary_scope ( id) ;
809
- self . tcx ( ) . mk_region ( match scope {
834
+ self . tcx . mk_region ( match scope {
810
835
Some ( scope) => ty:: ReScope ( scope) ,
811
836
None => ty:: ReStatic
812
837
} )
@@ -817,20 +842,20 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
817
842
span : Span ,
818
843
expr_ty : Ty < ' tcx > )
819
844
-> cmt < ' tcx > {
820
- let promotable = self . tcx ( ) . rvalue_promotable_to_static . borrow ( ) . get ( & id) . cloned ( )
845
+ let promotable = self . tcx . rvalue_promotable_to_static . borrow ( ) . get ( & id) . cloned ( )
821
846
. unwrap_or ( false ) ;
822
847
823
848
// When the corresponding feature isn't toggled, only promote `[T; 0]`.
824
849
let promotable = match expr_ty. sty {
825
850
ty:: TyArray ( _, 0 ) => true ,
826
- _ => promotable && self . tcx ( ) . sess . features . borrow ( ) . rvalue_static_promotion ,
851
+ _ => promotable && self . tcx . sess . features . borrow ( ) . rvalue_static_promotion ,
827
852
} ;
828
853
829
854
// Compute maximum lifetime of this rvalue. This is 'static if
830
855
// we can promote to a constant, otherwise equal to enclosing temp
831
856
// lifetime.
832
857
let re = if promotable {
833
- self . tcx ( ) . types . re_static
858
+ self . tcx . types . re_static
834
859
} else {
835
860
self . temporary_scope ( id)
836
861
} ;
@@ -911,7 +936,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
911
936
span_bug ! ( expr. span, "cat_overloaded_lvalue: base is not a reference" )
912
937
}
913
938
} ;
914
- let ref_ty = self . tcx ( ) . mk_ref ( region, ty:: TypeAndMut {
939
+ let ref_ty = self . tcx . mk_ref ( region, ty:: TypeAndMut {
915
940
ty : lvalue_ty,
916
941
mutbl,
917
942
} ) ;
@@ -1098,8 +1123,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
1098
1123
Def :: Variant ( variant_did) |
1099
1124
Def :: VariantCtor ( variant_did, ..) => {
1100
1125
// univariant enums do not need downcasts
1101
- let enum_did = self . tcx ( ) . parent_def_id ( variant_did) . unwrap ( ) ;
1102
- if !self . tcx ( ) . adt_def ( enum_did) . is_univariant ( ) {
1126
+ let enum_did = self . tcx . parent_def_id ( variant_did) . unwrap ( ) ;
1127
+ if !self . tcx . adt_def ( enum_did) . is_univariant ( ) {
1103
1128
self . cat_downcast ( pat, cmt. clone ( ) , cmt. ty , variant_did)
1104
1129
} else {
1105
1130
cmt
@@ -1116,8 +1141,8 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
1116
1141
let def = self . tables . qpath_def ( qpath, pat. id ) ;
1117
1142
let expected_len = match def {
1118
1143
Def :: VariantCtor ( def_id, CtorKind :: Fn ) => {
1119
- let enum_def = self . tcx ( ) . parent_def_id ( def_id) . unwrap ( ) ;
1120
- self . tcx ( ) . adt_def ( enum_def) . variant_with_id ( def_id) . fields . len ( )
1144
+ let enum_def = self . tcx . parent_def_id ( def_id) . unwrap ( ) ;
1145
+ self . tcx . adt_def ( enum_def) . variant_with_id ( def_id) . fields . len ( )
1121
1146
}
1122
1147
Def :: StructCtor ( _, CtorKind :: Fn ) => {
1123
1148
match self . pat_ty ( & pat) ?. sty {
0 commit comments