35
35
mod expr;
36
36
mod item;
37
37
38
+ use crate :: arena:: Arena ;
38
39
use crate :: dep_graph:: DepGraph ;
39
40
use crate :: hir:: { self , ParamName } ;
40
41
use crate :: hir:: HirVec ;
@@ -77,7 +78,7 @@ use rustc_error_codes::*;
77
78
78
79
const HIR_ID_COUNTER_LOCKED : u32 = 0xFFFFFFFF ;
79
80
80
- pub struct LoweringContext < ' a > {
81
+ pub struct LoweringContext < ' a , ' hir : ' a > {
81
82
crate_root : Option < Symbol > ,
82
83
83
84
/// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.
@@ -90,6 +91,9 @@ pub struct LoweringContext<'a> {
90
91
/// librustc is independent of the parser, we use dynamic dispatch here.
91
92
nt_to_tokenstream : NtToTokenstream ,
92
93
94
+ /// Used to allocate HIR nodes
95
+ arena : & ' hir Arena < ' hir > ,
96
+
93
97
/// The items being lowered are collected here.
94
98
items : BTreeMap < hir:: HirId , hir:: Item > ,
95
99
@@ -240,13 +244,14 @@ impl<'a> ImplTraitContext<'a> {
240
244
}
241
245
}
242
246
243
- pub fn lower_crate (
244
- sess : & Session ,
245
- dep_graph : & DepGraph ,
246
- krate : & Crate ,
247
- resolver : & mut dyn Resolver ,
247
+ pub fn lower_crate < ' a , ' hir > (
248
+ sess : & ' a Session ,
249
+ dep_graph : & ' a DepGraph ,
250
+ krate : & ' a Crate ,
251
+ resolver : & ' a mut dyn Resolver ,
248
252
nt_to_tokenstream : NtToTokenstream ,
249
- ) -> hir:: Crate {
253
+ arena : & ' hir Arena < ' hir > ,
254
+ ) -> hir:: Crate < ' hir > {
250
255
// We're constructing the HIR here; we don't care what we will
251
256
// read, since we haven't even constructed the *input* to
252
257
// incr. comp. yet.
@@ -259,6 +264,7 @@ pub fn lower_crate(
259
264
sess,
260
265
resolver,
261
266
nt_to_tokenstream,
267
+ arena,
262
268
items : BTreeMap :: new ( ) ,
263
269
trait_items : BTreeMap :: new ( ) ,
264
270
impl_items : BTreeMap :: new ( ) ,
@@ -382,19 +388,19 @@ impl<'a, 'b> Visitor<'a> for ImplTraitTypeIdVisitor<'b> {
382
388
}
383
389
}
384
390
385
- impl < ' a > LoweringContext < ' a > {
386
- fn lower_crate ( mut self , c : & Crate ) -> hir:: Crate {
391
+ impl < ' a , ' hir > LoweringContext < ' a , ' hir > {
392
+ fn lower_crate ( mut self , c : & Crate ) -> hir:: Crate < ' hir > {
387
393
/// Full-crate AST visitor that inserts into a fresh
388
394
/// `LoweringContext` any information that may be
389
395
/// needed from arbitrary locations in the crate,
390
396
/// e.g., the number of lifetime generic parameters
391
397
/// declared for every type and trait definition.
392
- struct MiscCollector < ' tcx , ' interner > {
393
- lctx : & ' tcx mut LoweringContext < ' interner > ,
398
+ struct MiscCollector < ' tcx , ' lowering , ' hir > {
399
+ lctx : & ' tcx mut LoweringContext < ' lowering , ' hir > ,
394
400
hir_id_owner : Option < NodeId > ,
395
401
}
396
402
397
- impl MiscCollector < ' _ , ' _ > {
403
+ impl MiscCollector < ' _ , ' _ , ' _ > {
398
404
fn allocate_use_tree_hir_id_counters (
399
405
& mut self ,
400
406
tree : & UseTree ,
@@ -434,7 +440,7 @@ impl<'a> LoweringContext<'a> {
434
440
}
435
441
}
436
442
437
- impl < ' tcx , ' interner > Visitor < ' tcx > for MiscCollector < ' tcx , ' interner > {
443
+ impl < ' tcx , ' lowering , ' hir > Visitor < ' tcx > for MiscCollector < ' tcx , ' lowering , ' hir > {
438
444
fn visit_pat ( & mut self , p : & ' tcx Pat ) {
439
445
if let PatKind :: Paren ( ..) | PatKind :: Rest = p. kind {
440
446
// Doesn't generate a HIR node
@@ -537,7 +543,7 @@ impl<'a> LoweringContext<'a> {
537
543
visit:: walk_crate ( & mut item:: ItemLowerer { lctx : & mut self } , c) ;
538
544
539
545
let module = self . lower_mod ( & c. module ) ;
540
- let attrs = self . lower_attrs ( & c. attrs ) ;
546
+ let attrs = self . arena . alloc_from_iter ( self . lower_attrs ( & c. attrs ) . into_iter ( ) ) ;
541
547
let body_ids = body_ids ( & self . bodies ) ;
542
548
543
549
self . resolver
@@ -548,8 +554,8 @@ impl<'a> LoweringContext<'a> {
548
554
module,
549
555
attrs,
550
556
span : c. span ,
551
- exported_macros : hir :: HirVec :: from ( self . exported_macros ) ,
552
- non_exported_macro_attrs : hir :: HirVec :: from ( self . non_exported_macro_attrs ) ,
557
+ exported_macros : self . arena . alloc_from_iter ( self . exported_macros ) ,
558
+ non_exported_macro_attrs : self . arena . alloc_from_iter ( self . non_exported_macro_attrs ) ,
553
559
items : self . items ,
554
560
trait_items : self . trait_items ,
555
561
impl_items : self . impl_items ,
@@ -750,7 +756,7 @@ impl<'a> LoweringContext<'a> {
750
756
f : F ,
751
757
) -> ( Vec < hir:: GenericParam > , T )
752
758
where
753
- F : FnOnce ( & mut LoweringContext < ' _ > ) -> ( Vec < hir:: GenericParam > , T ) ,
759
+ F : FnOnce ( & mut LoweringContext < ' _ , ' _ > ) -> ( Vec < hir:: GenericParam > , T ) ,
754
760
{
755
761
assert ! ( !self . is_collecting_in_band_lifetimes) ;
756
762
assert ! ( self . lifetimes_to_define. is_empty( ) ) ;
@@ -867,7 +873,7 @@ impl<'a> LoweringContext<'a> {
867
873
// for them.
868
874
fn with_in_scope_lifetime_defs < T , F > ( & mut self , params : & [ GenericParam ] , f : F ) -> T
869
875
where
870
- F : FnOnce ( & mut LoweringContext < ' _ > ) -> T ,
876
+ F : FnOnce ( & mut LoweringContext < ' _ , ' _ > ) -> T ,
871
877
{
872
878
let old_len = self . in_scope_lifetimes . len ( ) ;
873
879
let lt_def_names = params. iter ( ) . filter_map ( |param| match param. kind {
@@ -896,7 +902,7 @@ impl<'a> LoweringContext<'a> {
896
902
f : F ,
897
903
) -> ( hir:: Generics , T )
898
904
where
899
- F : FnOnce ( & mut LoweringContext < ' _ > , & mut Vec < hir:: GenericParam > ) -> T ,
905
+ F : FnOnce ( & mut LoweringContext < ' _ , ' _ > , & mut Vec < hir:: GenericParam > ) -> T ,
900
906
{
901
907
let ( in_band_defs, ( mut lowered_generics, res) ) = self . with_in_scope_lifetime_defs (
902
908
& generics. params ,
@@ -945,7 +951,7 @@ impl<'a> LoweringContext<'a> {
945
951
946
952
fn with_dyn_type_scope < T , F > ( & mut self , in_scope : bool , f : F ) -> T
947
953
where
948
- F : FnOnce ( & mut LoweringContext < ' _ > ) -> T ,
954
+ F : FnOnce ( & mut LoweringContext < ' _ , ' _ > ) -> T ,
949
955
{
950
956
let was_in_dyn_type = self . is_in_dyn_type ;
951
957
self . is_in_dyn_type = in_scope;
@@ -959,7 +965,7 @@ impl<'a> LoweringContext<'a> {
959
965
960
966
fn with_new_scopes < T , F > ( & mut self , f : F ) -> T
961
967
where
962
- F : FnOnce ( & mut LoweringContext < ' _ > ) -> T ,
968
+ F : FnOnce ( & mut LoweringContext < ' _ , ' _ > ) -> T ,
963
969
{
964
970
let was_in_loop_condition = self . is_in_loop_condition ;
965
971
self . is_in_loop_condition = false ;
@@ -1446,7 +1452,7 @@ impl<'a> LoweringContext<'a> {
1446
1452
span : Span ,
1447
1453
fn_def_id : Option < DefId > ,
1448
1454
opaque_ty_node_id : NodeId ,
1449
- lower_bounds : impl FnOnce ( & mut LoweringContext < ' _ > ) -> hir:: GenericBounds ,
1455
+ lower_bounds : impl FnOnce ( & mut LoweringContext < ' _ , ' _ > ) -> hir:: GenericBounds ,
1450
1456
) -> hir:: TyKind {
1451
1457
debug ! (
1452
1458
"lower_opaque_impl_trait(fn_def_id={:?}, opaque_ty_node_id={:?}, span={:?})" ,
@@ -1563,8 +1569,8 @@ impl<'a> LoweringContext<'a> {
1563
1569
// This visitor walks over `impl Trait` bounds and creates defs for all lifetimes that
1564
1570
// appear in the bounds, excluding lifetimes that are created within the bounds.
1565
1571
// E.g., `'a`, `'b`, but not `'c` in `impl for<'c> SomeTrait<'a, 'b, 'c>`.
1566
- struct ImplTraitLifetimeCollector < ' r , ' a > {
1567
- context : & ' r mut LoweringContext < ' a > ,
1572
+ struct ImplTraitLifetimeCollector < ' r , ' a , ' hir > {
1573
+ context : & ' r mut LoweringContext < ' a , ' hir > ,
1568
1574
parent : DefIndex ,
1569
1575
opaque_ty_id : NodeId ,
1570
1576
collect_elided_lifetimes : bool ,
@@ -1574,7 +1580,7 @@ impl<'a> LoweringContext<'a> {
1574
1580
output_lifetime_params : Vec < hir:: GenericParam > ,
1575
1581
}
1576
1582
1577
- impl < ' r , ' a , ' v > hir:: intravisit:: Visitor < ' v > for ImplTraitLifetimeCollector < ' r , ' a > {
1583
+ impl < ' r , ' a , ' v , ' hir > hir:: intravisit:: Visitor < ' v > for ImplTraitLifetimeCollector < ' r , ' a , ' hir > {
1578
1584
fn nested_visit_map < ' this > (
1579
1585
& ' this mut self ,
1580
1586
) -> hir:: intravisit:: NestedVisitorMap < ' this , ' v > {
@@ -2757,8 +2763,9 @@ impl<'a> LoweringContext<'a> {
2757
2763
let node = match p. kind {
2758
2764
PatKind :: Wild => hir:: PatKind :: Wild ,
2759
2765
PatKind :: Ident ( ref binding_mode, ident, ref sub) => {
2760
- let lower_sub = |this : & mut Self | sub. as_ref ( ) . map ( |x| this. lower_pat ( x) ) ;
2761
- self . lower_pat_ident ( p, binding_mode, ident, lower_sub)
2766
+ let lower_sub = |this : & mut Self | sub. as_ref ( ) . map ( |s| this. lower_pat ( & * s) ) ;
2767
+ let node = self . lower_pat_ident ( p, binding_mode, ident, lower_sub) ;
2768
+ node
2762
2769
}
2763
2770
PatKind :: Lit ( ref e) => hir:: PatKind :: Lit ( P ( self . lower_expr ( e) ) ) ,
2764
2771
PatKind :: TupleStruct ( ref path, ref pats) => {
0 commit comments