1
1
use std:: iter;
2
2
use std:: rc:: Rc ;
3
3
4
- use rustc_data_structures:: frozen:: Frozen ;
5
4
use rustc_data_structures:: fx:: FxHashMap ;
6
5
use rustc_hir:: def_id:: DefId ;
7
6
use rustc_index:: IndexVec ;
7
+ use rustc_infer:: infer:: outlives:: env:: RegionBoundPairs ;
8
8
use rustc_infer:: infer:: { InferCtxt , NllRegionVariableOrigin } ;
9
+ use rustc_infer:: traits:: ObligationCause ;
9
10
use rustc_macros:: extension;
10
11
use rustc_middle:: bug;
11
- use rustc_middle:: mir:: ConstraintCategory ;
12
- use rustc_middle:: ty:: relate:: {
13
- Relate , RelateResult , TypeRelation , structurally_relate_consts, structurally_relate_tys,
14
- } ;
12
+ use rustc_middle:: mir:: { Body , ConstraintCategory } ;
15
13
use rustc_middle:: ty:: {
16
14
self , DefiningScopeKind , GenericArgsRef , OpaqueHiddenType , OpaqueTypeKey , Region , RegionVid ,
17
15
Ty , TyCtxt , TypeFoldable , TypeFolder , TypeSuperFoldable , TypeSuperVisitable , TypeVisitable ,
@@ -23,13 +21,16 @@ use rustc_trait_selection::error_reporting::infer::region::unexpected_hidden_reg
23
21
use rustc_trait_selection:: opaque_types:: {
24
22
InvalidOpaqueTypeArgs , check_opaque_type_parameter_valid,
25
23
} ;
24
+ use rustc_trait_selection:: solve:: NoSolution ;
25
+ use rustc_trait_selection:: traits:: query:: type_op:: custom:: CustomTypeOp ;
26
26
use tracing:: { debug, instrument} ;
27
27
28
28
use super :: reverse_sccs:: ReverseSccGraph ;
29
29
use super :: values:: RegionValues ;
30
30
use super :: { ConstraintSccs , RegionDefinition , RegionInferenceContext } ;
31
31
use crate :: constraints:: ConstraintSccIndex ;
32
32
use crate :: consumers:: OutlivesConstraint ;
33
+ use crate :: type_check:: canonical:: fully_perform_op_raw;
33
34
use crate :: type_check:: free_region_relations:: UniversalRegionRelations ;
34
35
use crate :: type_check:: { Locations , MirTypeckRegionConstraints } ;
35
36
use crate :: universal_regions:: { RegionClassification , UniversalRegions } ;
@@ -52,8 +53,11 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
52
53
pub ( crate ) fn handle_opaque_type_uses ( & mut self , borrowck_state : & mut BorrowckState < ' tcx > ) {
53
54
let BorrowckState {
54
55
infcx,
55
- constraints ,
56
+ body_owned ,
56
57
universal_region_relations,
58
+ region_bound_pairs,
59
+ known_type_outlives_obligations,
60
+ constraints,
57
61
location_map,
58
62
deferred_opaque_type_errors,
59
63
..
@@ -62,10 +66,13 @@ impl<'tcx> BorrowCheckRootCtxt<'tcx> {
62
66
handle_opaque_type_uses (
63
67
self ,
64
68
infcx,
65
- constraints,
66
- deferred_opaque_type_errors,
69
+ body_owned,
67
70
universal_region_relations,
71
+ region_bound_pairs,
72
+ known_type_outlives_obligations,
68
73
location_map,
74
+ constraints,
75
+ deferred_opaque_type_errors,
69
76
)
70
77
}
71
78
}
@@ -82,7 +89,7 @@ struct RegionCtxt<'a, 'tcx> {
82
89
impl < ' a , ' tcx > RegionCtxt < ' a , ' tcx > {
83
90
fn new (
84
91
infcx : & ' a BorrowckInferCtxt < ' tcx > ,
85
- universal_region_relations : & ' a Frozen < UniversalRegionRelations < ' tcx > > ,
92
+ universal_region_relations : & ' a UniversalRegionRelations < ' tcx > ,
86
93
location_map : Rc < DenseLocationMap > ,
87
94
constraints : & MirTypeckRegionConstraints < ' tcx > ,
88
95
) -> RegionCtxt < ' a , ' tcx > {
@@ -160,10 +167,13 @@ impl<'a, 'tcx> RegionCtxt<'a, 'tcx> {
160
167
pub ( crate ) fn handle_opaque_type_uses < ' tcx > (
161
168
root_cx : & mut BorrowCheckRootCtxt < ' tcx > ,
162
169
infcx : & BorrowckInferCtxt < ' tcx > ,
170
+ body : & Body < ' tcx > ,
171
+ universal_region_relations : & UniversalRegionRelations < ' tcx > ,
172
+ region_bound_pairs : & RegionBoundPairs < ' tcx > ,
173
+ known_type_outlives_obligations : & [ ty:: PolyTypeOutlivesPredicate < ' tcx > ] ,
174
+ location_map : & Rc < DenseLocationMap > ,
163
175
constraints : & mut MirTypeckRegionConstraints < ' tcx > ,
164
176
deferred_errors : & mut Vec < DeferredOpaqueTypeError < ' tcx > > ,
165
- universal_region_relations : & Frozen < UniversalRegionRelations < ' tcx > > ,
166
- location_map : & Rc < DenseLocationMap > ,
167
177
) {
168
178
let opaque_types = infcx. take_opaque_types ( ) ;
169
179
if opaque_types. is_empty ( ) {
@@ -197,7 +207,16 @@ pub(crate) fn handle_opaque_type_uses<'tcx>(
197
207
& mut opaque_types,
198
208
) ;
199
209
200
- apply_defining_uses ( root_cx, infcx, constraints, universal_region_relations, & opaque_types) ;
210
+ apply_defining_uses (
211
+ root_cx,
212
+ infcx,
213
+ body,
214
+ & universal_region_relations. universal_regions ,
215
+ region_bound_pairs,
216
+ known_type_outlives_obligations,
217
+ constraints,
218
+ & opaque_types,
219
+ ) ;
201
220
}
202
221
203
222
#[ derive( Debug ) ]
@@ -212,7 +231,7 @@ fn collect_defining_uses<'tcx>(
212
231
infcx : & BorrowckInferCtxt < ' tcx > ,
213
232
constraints : & mut MirTypeckRegionConstraints < ' tcx > ,
214
233
deferred_errors : & mut Vec < DeferredOpaqueTypeError < ' tcx > > ,
215
- universal_region_relations : & Frozen < UniversalRegionRelations < ' tcx > > ,
234
+ universal_region_relations : & UniversalRegionRelations < ' tcx > ,
216
235
location_map : & Rc < DenseLocationMap > ,
217
236
opaque_types : & [ ( OpaqueTypeKey < ' tcx > , OpaqueHiddenType < ' tcx > ) ] ,
218
237
) {
@@ -546,8 +565,11 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for HiddenTypeMeh<'_, 'tcx> {
546
565
fn apply_defining_uses < ' tcx > (
547
566
root_cx : & mut BorrowCheckRootCtxt < ' tcx > ,
548
567
infcx : & BorrowckInferCtxt < ' tcx > ,
568
+ body : & Body < ' tcx > ,
569
+ universal_regions : & UniversalRegions < ' tcx > ,
570
+ region_bound_pairs : & RegionBoundPairs < ' tcx > ,
571
+ known_type_outlives_obligations : & [ ty:: PolyTypeOutlivesPredicate < ' tcx > ] ,
549
572
constraints : & mut MirTypeckRegionConstraints < ' tcx > ,
550
- universal_region_relations : & Frozen < UniversalRegionRelations < ' tcx > > ,
551
573
opaque_types : & [ ( OpaqueTypeKey < ' tcx > , OpaqueHiddenType < ' tcx > ) ] ,
552
574
) {
553
575
let tcx = infcx. tcx ;
@@ -570,122 +592,30 @@ fn apply_defining_uses<'tcx>(
570
592
}
571
593
} ) ;
572
594
573
- let universal_regions = & universal_region_relations. universal_regions ;
574
- let mut relation =
575
- EquateRegions { infcx, span : hidden_type. span , universal_regions, constraints } ;
576
- match TypeRelation :: relate ( & mut relation, hidden_type. ty , expected. ty ) {
577
- Ok ( _) => { }
578
- Err ( _) => {
579
- let _ = hidden_type. build_mismatch_error ( & expected, tcx) . map ( |d| d. emit ( ) ) ;
580
- }
581
- }
582
-
583
- // normalize -> equate
584
- //
585
- // Do we need to normalize?
586
- // Only to support non-universal type or const args. It feels likely that we need (and want) to do so
587
- // This once again needs to be careful about cycles: normalizing and equating while defining/revealing
588
- // opaque types may end up introducing new defining uses.
589
-
590
- // How can we equate here?
591
- // If we need to normalize the answer is just "it's TypeChecker time"
592
- // without it we could manually walk over the types using a type relation and equate region vars
593
- // that way.
594
- }
595
- }
596
-
597
- fn to_region_vid < ' tcx > (
598
- constraints : & MirTypeckRegionConstraints < ' tcx > ,
599
- universal_regions : & UniversalRegions < ' tcx > ,
600
- r : Region < ' tcx > ,
601
- ) -> RegionVid {
602
- if let ty:: RePlaceholder ( placeholder) = r. kind ( ) {
603
- constraints. get_placeholder_region ( placeholder) . as_var ( )
604
- } else {
605
- universal_regions. to_region_vid ( r)
606
- }
607
- }
608
-
609
- // FUCKING SHIT NO!wegrdtfhergfsg
610
- struct EquateRegions < ' a , ' tcx > {
611
- infcx : & ' a BorrowckInferCtxt < ' tcx > ,
612
- span : Span ,
613
- universal_regions : & ' a UniversalRegions < ' tcx > ,
614
- constraints : & ' a mut MirTypeckRegionConstraints < ' tcx > ,
615
- }
616
-
617
- impl < ' tcx > TypeRelation < TyCtxt < ' tcx > > for EquateRegions < ' _ , ' tcx > {
618
- fn cx ( & self ) -> TyCtxt < ' tcx > {
619
- self . infcx . tcx
620
- }
621
-
622
- fn relate_with_variance < T : Relate < TyCtxt < ' tcx > > > (
623
- & mut self ,
624
- _variance : ty:: Variance ,
625
- _info : ty:: VarianceDiagInfo < TyCtxt < ' tcx > > ,
626
- a : T ,
627
- b : T ,
628
- ) -> RelateResult < ' tcx , T > {
629
- self . relate ( a, b)
630
- }
631
-
632
- fn tys ( & mut self , a : Ty < ' tcx > , b : Ty < ' tcx > ) -> RelateResult < ' tcx , Ty < ' tcx > > {
633
- structurally_relate_tys ( self , a, b)
634
- }
635
-
636
- fn regions (
637
- & mut self ,
638
- a : ty:: Region < ' tcx > ,
639
- b : ty:: Region < ' tcx > ,
640
- ) -> RelateResult < ' tcx , ty:: Region < ' tcx > > {
641
- if matches ! ( a. kind( ) , ty:: ReBound ( ..) ) || matches ! ( b. kind( ) , ty:: ReBound ( ..) ) {
642
- assert_eq ! ( a, b) ;
643
- return Ok ( a) ;
644
- }
645
-
646
- let a_vid = to_region_vid ( self . constraints , self . universal_regions , a) ;
647
- let b_vid = to_region_vid ( self . constraints , self . universal_regions , b) ;
648
- let locations = Locations :: All ( self . span ) ;
649
- self . constraints . outlives_constraints . push ( OutlivesConstraint {
650
- sup : a_vid,
651
- sub : b_vid,
652
- locations,
653
- span : self . span ,
654
- category : ConstraintCategory :: OpaqueType ,
655
- variance_info : ty:: VarianceDiagInfo :: None ,
656
- from_closure : false ,
657
- } ) ;
658
- self . constraints . outlives_constraints . push ( OutlivesConstraint {
659
- sup : b_vid,
660
- sub : a_vid,
595
+ let locations = Locations :: All ( hidden_type. span ) ;
596
+ fully_perform_op_raw (
597
+ infcx,
598
+ body,
599
+ universal_regions,
600
+ region_bound_pairs,
601
+ known_type_outlives_obligations,
602
+ constraints,
661
603
locations,
662
- span : self . span ,
663
- category : ConstraintCategory :: OpaqueType ,
664
- variance_info : ty:: VarianceDiagInfo :: None ,
665
- from_closure : false ,
666
- } ) ;
667
-
668
- Ok ( a)
669
- }
670
-
671
- fn consts (
672
- & mut self ,
673
- a : ty:: Const < ' tcx > ,
674
- b : ty:: Const < ' tcx > ,
675
- ) -> RelateResult < ' tcx , ty:: Const < ' tcx > > {
676
- structurally_relate_consts ( self , a, b)
677
- }
678
-
679
- fn binders < T > (
680
- & mut self ,
681
- a : ty:: Binder < ' tcx , T > ,
682
- b : ty:: Binder < ' tcx , T > ,
683
- ) -> RelateResult < ' tcx , ty:: Binder < ' tcx , T > >
684
- where
685
- T : Relate < TyCtxt < ' tcx > > ,
686
- {
687
- self . relate ( a. skip_binder ( ) , b. skip_binder ( ) ) ?;
688
- Ok ( a)
604
+ ConstraintCategory :: OpaqueType ,
605
+ CustomTypeOp :: new (
606
+ |ocx| {
607
+ let cause = ObligationCause :: misc (
608
+ hidden_type. span ,
609
+ body. source . def_id ( ) . expect_local ( ) ,
610
+ ) ;
611
+ let expected = ocx. normalize ( & cause, infcx. param_env , expected) ;
612
+ ocx. eq ( & cause, infcx. param_env , expected. ty , hidden_type. ty )
613
+ . map_err ( |_| NoSolution )
614
+ } ,
615
+ "equating opaque types" ,
616
+ ) ,
617
+ )
618
+ . expect ( "failed to relate opaque types" ) ;
689
619
}
690
620
}
691
621
0 commit comments