@@ -12,21 +12,19 @@ use std::ops::ControlFlow;
12
12
13
13
use rustc_ast:: visit:: walk_list;
14
14
use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
15
- use rustc_data_structures:: sorted_map:: SortedMap ;
16
15
use rustc_errors:: ErrorGuaranteed ;
17
16
use rustc_hir:: def:: { DefKind , Res } ;
18
17
use rustc_hir:: intravisit:: { self , InferKind , Visitor , VisitorExt } ;
19
18
use rustc_hir:: {
20
- self as hir, AmbigArg , GenericArg , GenericParam , GenericParamKind , HirId , ItemLocalMap ,
21
- LifetimeName , Node ,
19
+ self as hir, AmbigArg , GenericArg , GenericParam , GenericParamKind , HirId , LifetimeName , Node ,
22
20
} ;
23
21
use rustc_macros:: extension;
24
22
use rustc_middle:: hir:: nested_filter;
25
23
use rustc_middle:: middle:: resolve_bound_vars:: * ;
26
24
use rustc_middle:: query:: Providers ;
27
25
use rustc_middle:: ty:: { self , TyCtxt , TypeSuperVisitable , TypeVisitor } ;
28
26
use rustc_middle:: { bug, span_bug} ;
29
- use rustc_span:: def_id:: { DefId , LocalDefId , LocalDefIdMap } ;
27
+ use rustc_span:: def_id:: { DefId , LocalDefId } ;
30
28
use rustc_span:: { Ident , Span , sym} ;
31
29
use tracing:: { debug, debug_span, instrument} ;
32
30
@@ -62,33 +60,9 @@ impl ResolvedArg {
62
60
}
63
61
}
64
62
65
- /// Maps the id of each bound variable reference to the variable decl
66
- /// that it corresponds to.
67
- ///
68
- /// FIXME. This struct gets converted to a `ResolveBoundVars` for
69
- /// actual use. It has the same data, but indexed by `LocalDefId`. This
70
- /// is silly.
71
- #[ derive( Debug , Default ) ]
72
- struct NamedVarMap {
73
- // maps from every use of a named (not anonymous) bound var to a
74
- // `ResolvedArg` describing how that variable is bound
75
- defs : ItemLocalMap < ResolvedArg > ,
76
-
77
- // Maps relevant hir items to the bound vars on them. These include:
78
- // - function defs
79
- // - function pointers
80
- // - closures
81
- // - trait refs
82
- // - bound types (like `T` in `for<'a> T<'a>: Foo`)
83
- late_bound_vars : ItemLocalMap < Vec < ty:: BoundVariableKind > > ,
84
-
85
- // List captured variables for each opaque type.
86
- opaque_captured_lifetimes : LocalDefIdMap < Vec < ( ResolvedArg , LocalDefId ) > > ,
87
- }
88
-
89
63
struct BoundVarContext < ' a , ' tcx > {
90
64
tcx : TyCtxt < ' tcx > ,
91
- map : & ' a mut NamedVarMap ,
65
+ rbv : & ' a mut ResolveBoundVars ,
92
66
scope : ScopeRef < ' a > ,
93
67
}
94
68
@@ -267,19 +241,12 @@ pub(crate) fn provide(providers: &mut Providers) {
267
241
268
242
/// Computes the `ResolveBoundVars` map that contains data for an entire `Item`.
269
243
/// You should not read the result of this query directly, but rather use
270
- /// `named_variable_map`, `is_late_bound_map `, etc.
244
+ /// `named_variable_map`, `late_bound_vars_map `, etc.
271
245
#[ instrument( level = "debug" , skip( tcx) ) ]
272
246
fn resolve_bound_vars ( tcx : TyCtxt < ' _ > , local_def_id : hir:: OwnerId ) -> ResolveBoundVars {
273
- let mut named_variable_map = NamedVarMap {
274
- defs : Default :: default ( ) ,
275
- late_bound_vars : Default :: default ( ) ,
276
- opaque_captured_lifetimes : Default :: default ( ) ,
277
- } ;
278
- let mut visitor = BoundVarContext {
279
- tcx,
280
- map : & mut named_variable_map,
281
- scope : & Scope :: Root { opt_parent_item : None } ,
282
- } ;
247
+ let mut rbv = ResolveBoundVars :: default ( ) ;
248
+ let mut visitor =
249
+ BoundVarContext { tcx, rbv : & mut rbv, scope : & Scope :: Root { opt_parent_item : None } } ;
283
250
match tcx. hir_owner_node ( local_def_id) {
284
251
hir:: OwnerNode :: Item ( item) => visitor. visit_item ( item) ,
285
252
hir:: OwnerNode :: ForeignItem ( item) => visitor. visit_foreign_item ( item) ,
@@ -299,19 +266,10 @@ fn resolve_bound_vars(tcx: TyCtxt<'_>, local_def_id: hir::OwnerId) -> ResolveBou
299
266
hir:: OwnerNode :: Synthetic => unreachable ! ( ) ,
300
267
}
301
268
302
- let defs = named_variable_map. defs . into_sorted_stable_ord ( ) ;
303
- let late_bound_vars = named_variable_map. late_bound_vars . into_sorted_stable_ord ( ) ;
304
- let opaque_captured_lifetimes = named_variable_map. opaque_captured_lifetimes ;
305
- let rl = ResolveBoundVars {
306
- defs : SortedMap :: from_presorted_elements ( defs) ,
307
- late_bound_vars : SortedMap :: from_presorted_elements ( late_bound_vars) ,
308
- opaque_captured_lifetimes,
309
- } ;
310
-
311
- debug ! ( ?rl. defs) ;
312
- debug ! ( ?rl. late_bound_vars) ;
313
- debug ! ( ?rl. opaque_captured_lifetimes) ;
314
- rl
269
+ debug ! ( ?rbv. defs) ;
270
+ debug ! ( ?rbv. late_bound_vars) ;
271
+ debug ! ( ?rbv. opaque_captured_lifetimes) ;
272
+ rbv
315
273
}
316
274
317
275
fn late_arg_as_bound_arg < ' tcx > (
@@ -404,7 +362,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
404
362
Scope :: Binder { hir_id, .. } => {
405
363
// Nested poly trait refs have the binders concatenated
406
364
let mut full_binders =
407
- self . map . late_bound_vars . entry ( hir_id. local_id ) . or_default ( ) . clone ( ) ;
365
+ self . rbv . late_bound_vars . get_mut_or_insert_default ( hir_id. local_id ) . clone ( ) ;
408
366
full_binders. extend ( supertrait_bound_vars) ;
409
367
break ( full_binders, BinderScopeType :: Concatenating ) ;
410
368
}
@@ -646,7 +604,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
646
604
647
605
let captures = captures. into_inner ( ) . into_iter ( ) . collect ( ) ;
648
606
debug ! ( ?captures) ;
649
- self . map . opaque_captured_lifetimes . insert ( opaque. def_id , captures) ;
607
+ self . rbv . opaque_captured_lifetimes . insert ( opaque. def_id , captures) ;
650
608
}
651
609
652
610
#[ instrument( level = "debug" , skip( self ) ) ]
@@ -848,7 +806,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
848
806
hir:: TyKind :: Ref ( lifetime_ref, ref mt) => {
849
807
self . visit_lifetime ( lifetime_ref) ;
850
808
let scope = Scope :: ObjectLifetimeDefault {
851
- lifetime : self . map . defs . get ( & lifetime_ref. hir_id . local_id ) . cloned ( ) ,
809
+ lifetime : self . rbv . defs . get ( & lifetime_ref. hir_id . local_id ) . cloned ( ) ,
852
810
s : self . scope ,
853
811
} ;
854
812
self . with ( scope, |this| this. visit_ty_unambig ( mt. ty ) ) ;
@@ -966,7 +924,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
966
924
let bound_vars: Vec < _ > =
967
925
self . tcx . fn_sig ( sig_id) . skip_binder ( ) . bound_vars ( ) . iter ( ) . collect ( ) ;
968
926
let hir_id = self . tcx . local_def_id_to_hir_id ( def_id) ;
969
- self . map . late_bound_vars . insert ( hir_id. local_id , bound_vars) ;
927
+ self . rbv . late_bound_vars . insert ( hir_id. local_id , bound_vars) ;
970
928
}
971
929
self . visit_fn_like_elision ( fd. inputs , output, matches ! ( fk, intravisit:: FnKind :: Closure ) ) ;
972
930
intravisit:: walk_fn_kind ( self , fk) ;
@@ -1140,8 +1098,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1140
1098
where
1141
1099
F : for < ' b > FnOnce ( & mut BoundVarContext < ' b , ' tcx > ) ,
1142
1100
{
1143
- let BoundVarContext { tcx, map , .. } = self ;
1144
- let mut this = BoundVarContext { tcx : * tcx, map , scope : & wrap_scope } ;
1101
+ let BoundVarContext { tcx, rbv , .. } = self ;
1102
+ let mut this = BoundVarContext { tcx : * tcx, rbv , scope : & wrap_scope } ;
1145
1103
let span = debug_span ! ( "scope" , scope = ?this. scope. debug_truncated( ) ) ;
1146
1104
{
1147
1105
let _enter = span. enter ( ) ;
@@ -1150,10 +1108,10 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1150
1108
}
1151
1109
1152
1110
fn record_late_bound_vars ( & mut self , hir_id : HirId , binder : Vec < ty:: BoundVariableKind > ) {
1153
- if let Some ( old) = self . map . late_bound_vars . insert ( hir_id. local_id , binder) {
1111
+ if let Some ( old) = self . rbv . late_bound_vars . insert ( hir_id. local_id , binder) {
1154
1112
bug ! (
1155
1113
"overwrote bound vars for {hir_id:?}:\n old={old:?}\n new={:?}" ,
1156
- self . map . late_bound_vars[ & hir_id. local_id]
1114
+ self . rbv . late_bound_vars[ & hir_id. local_id]
1157
1115
)
1158
1116
}
1159
1117
}
@@ -1597,9 +1555,9 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1597
1555
kind. descr( param_def_id. to_def_id( ) )
1598
1556
) ,
1599
1557
} ;
1600
- self . map . defs . insert ( hir_id. local_id , ResolvedArg :: Error ( guar) ) ;
1558
+ self . rbv . defs . insert ( hir_id. local_id , ResolvedArg :: Error ( guar) ) ;
1601
1559
} else {
1602
- self . map . defs . insert ( hir_id. local_id , def) ;
1560
+ self . rbv . defs . insert ( hir_id. local_id , def) ;
1603
1561
}
1604
1562
return ;
1605
1563
}
@@ -1632,7 +1590,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1632
1590
bug ! ( "unexpected def-kind: {}" , kind. descr( param_def_id. to_def_id( ) ) )
1633
1591
}
1634
1592
} ) ;
1635
- self . map . defs . insert ( hir_id. local_id , ResolvedArg :: Error ( guar) ) ;
1593
+ self . rbv . defs . insert ( hir_id. local_id , ResolvedArg :: Error ( guar) ) ;
1636
1594
return ;
1637
1595
}
1638
1596
Scope :: Root { .. } => break ,
@@ -1725,7 +1683,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1725
1683
}
1726
1684
} ;
1727
1685
1728
- let map = & self . map ;
1686
+ let rbv = & self . rbv ;
1729
1687
let generics = self . tcx . generics_of ( def_id) ;
1730
1688
1731
1689
// `type_def_id` points to an item, so there is nothing to inherit generics from.
@@ -1744,7 +1702,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
1744
1702
// This index can be used with `generic_args` since `parent_count == 0`.
1745
1703
let index = generics. param_def_id_to_index [ & param_def_id] as usize ;
1746
1704
generic_args. args . get ( index) . and_then ( |arg| match arg {
1747
- GenericArg :: Lifetime ( lt) => map . defs . get ( & lt. hir_id . local_id ) . copied ( ) ,
1705
+ GenericArg :: Lifetime ( lt) => rbv . defs . get ( & lt. hir_id . local_id ) . copied ( ) ,
1748
1706
_ => None ,
1749
1707
} )
1750
1708
}
@@ -2042,7 +2000,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
2042
2000
#[ instrument( level = "debug" , skip( self ) ) ]
2043
2001
fn insert_lifetime ( & mut self , lifetime_ref : & ' tcx hir:: Lifetime , def : ResolvedArg ) {
2044
2002
debug ! ( span = ?lifetime_ref. ident. span) ;
2045
- self . map . defs . insert ( lifetime_ref. hir_id . local_id , def) ;
2003
+ self . rbv . defs . insert ( lifetime_ref. hir_id . local_id , def) ;
2046
2004
}
2047
2005
2048
2006
// When we have a return type notation type in a where clause, like
@@ -2197,7 +2155,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> {
2197
2155
// See where these vars are used in `HirTyLowerer::lower_ty_maybe_return_type_notation`.
2198
2156
// And this is exercised in:
2199
2157
// `tests/ui/associated-type-bounds/return-type-notation/higher-ranked-bound-works.rs`.
2200
- let existing_bound_vars = self . map . late_bound_vars . get_mut ( & hir_id. local_id ) . unwrap ( ) ;
2158
+ let existing_bound_vars = self . rbv . late_bound_vars . get_mut ( & hir_id. local_id ) . unwrap ( ) ;
2201
2159
let existing_bound_vars_saved = existing_bound_vars. clone ( ) ;
2202
2160
existing_bound_vars. extend ( bound_vars) ;
2203
2161
self . record_late_bound_vars ( item_segment. hir_id , existing_bound_vars_saved) ;
0 commit comments