@@ -5,6 +5,7 @@ use super::ImplTraitTypeIdVisitor;
5
5
use super :: LoweringContext ;
6
6
use super :: ParamMode ;
7
7
8
+ use crate :: arena:: Arena ;
8
9
use crate :: hir;
9
10
use crate :: hir:: def:: { DefKind , Res } ;
10
11
use crate :: hir:: def_id:: DefId ;
@@ -225,7 +226,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
225
226
pub fn lower_item ( & mut self , i : & Item ) -> Option < hir:: Item < ' hir > > {
226
227
let mut ident = i. ident ;
227
228
let mut vis = self . lower_visibility ( & i. vis , None ) ;
228
- let attrs = self . lower_attrs_arena ( & i. attrs ) ;
229
+ let attrs = self . lower_attrs ( & i. attrs ) ;
229
230
230
231
if let ItemKind :: MacroDef ( ref def) = i. kind {
231
232
if !def. legacy || attr:: contains_name ( & i. attrs , sym:: macro_export) {
@@ -506,7 +507,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
506
507
let new_id = this. lower_node_id ( new_node_id) ;
507
508
let res = this. lower_res ( res) ;
508
509
let path = this. lower_path_extra ( res, & path, ParamMode :: Explicit , None ) ;
509
- let kind = hir:: ItemKind :: Use ( this . arena . alloc ( path) , hir:: UseKind :: Single ) ;
510
+ let kind = hir:: ItemKind :: Use ( path, hir:: UseKind :: Single ) ;
510
511
let vis = this. rebuild_vis ( & vis) ;
511
512
512
513
this. insert_item ( hir:: Item {
@@ -521,15 +522,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
521
522
}
522
523
523
524
let path = self . lower_path_extra ( ret_res, & path, ParamMode :: Explicit , None ) ;
524
- let path = self . arena . alloc ( path) ;
525
525
hir:: ItemKind :: Use ( path, hir:: UseKind :: Single )
526
526
}
527
527
UseTreeKind :: Glob => {
528
- let path = self . arena . alloc ( self . lower_path (
529
- id,
530
- & Path { segments, span : path. span } ,
531
- ParamMode :: Explicit ,
532
- ) ) ;
528
+ let path =
529
+ self . lower_path ( id, & Path { segments, span : path. span } , ParamMode :: Explicit ) ;
533
530
hir:: ItemKind :: Use ( path, hir:: UseKind :: Glob )
534
531
}
535
532
UseTreeKind :: Nested ( ref trees) => {
@@ -617,7 +614,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
617
614
let res = self . expect_full_res_from_use ( id) . next ( ) . unwrap_or ( Res :: Err ) ;
618
615
let res = self . lower_res ( res) ;
619
616
let path = self . lower_path_extra ( res, & prefix, ParamMode :: Explicit , None ) ;
620
- let path = self . arena . alloc ( path) ;
621
617
hir:: ItemKind :: Use ( path, hir:: UseKind :: ListStem )
622
618
}
623
619
}
@@ -626,7 +622,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
626
622
/// Paths like the visibility path in `pub(super) use foo::{bar, baz}` are repeated
627
623
/// many times in the HIR tree; for each occurrence, we need to assign distinct
628
624
/// `NodeId`s. (See, e.g., #56128.)
629
- fn rebuild_use_path ( & mut self , path : & hir:: Path < ' hir > ) -> hir:: Path < ' hir > {
625
+ fn rebuild_use_path ( & mut self , path : & hir:: Path < ' hir > ) -> & ' hir hir:: Path < ' hir > {
630
626
debug ! ( "rebuild_use_path(path = {:?})" , path) ;
631
627
let segments =
632
628
self . arena . alloc_from_iter ( path. segments . iter ( ) . map ( |seg| hir:: PathSegment {
@@ -636,7 +632,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
636
632
args : None ,
637
633
infer_args : seg. infer_args ,
638
634
} ) ) ;
639
- hir:: Path { span : path. span , res : path. res , segments }
635
+ self . arena . alloc ( hir:: Path { span : path. span , res : path. res , segments } )
640
636
}
641
637
642
638
fn rebuild_vis ( & mut self , vis : & hir:: Visibility < ' hir > ) -> hir:: Visibility < ' hir > {
@@ -646,7 +642,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
646
642
hir:: VisibilityKind :: Inherited => hir:: VisibilityKind :: Inherited ,
647
643
hir:: VisibilityKind :: Restricted { ref path, hir_id : _ } => {
648
644
hir:: VisibilityKind :: Restricted {
649
- path : self . arena . alloc ( self . rebuild_use_path ( path) ) ,
645
+ path : self . rebuild_use_path ( path) ,
650
646
hir_id : self . next_id ( ) ,
651
647
}
652
648
}
@@ -659,7 +655,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
659
655
hir:: ForeignItem {
660
656
hir_id : self . lower_node_id ( i. id ) ,
661
657
ident : i. ident ,
662
- attrs : self . lower_attrs_arena ( & i. attrs ) ,
658
+ attrs : self . lower_attrs ( & i. attrs ) ,
663
659
kind : match i. kind {
664
660
ForeignItemKind :: Fn ( ref fdec, ref generics) => {
665
661
let ( generics, ( fn_dec, fn_args) ) = self . add_in_band_defs (
@@ -674,7 +670,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
674
670
)
675
671
} ,
676
672
) ;
677
- let fn_args = self . arena . alloc_from_iter ( fn_args. into_iter ( ) ) ;
678
673
679
674
hir:: ForeignItemKind :: Fn ( fn_dec, fn_args, generics)
680
675
}
@@ -703,7 +698,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
703
698
704
699
fn lower_variant ( & mut self , v : & Variant ) -> hir:: Variant < ' hir > {
705
700
hir:: Variant {
706
- attrs : self . lower_attrs_arena ( & v. attrs ) ,
701
+ attrs : self . lower_attrs ( & v. attrs ) ,
707
702
data : self . lower_variant_data ( & v. data ) ,
708
703
disr_expr : v. disr_expr . as_ref ( ) . map ( |e| self . lower_anon_const ( e) ) ,
709
704
id : self . lower_node_id ( v. id ) ,
@@ -751,7 +746,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
751
746
} ,
752
747
vis : self . lower_visibility ( & f. vis , None ) ,
753
748
ty,
754
- attrs : self . lower_attrs_arena ( & f. attrs ) ,
749
+ attrs : self . lower_attrs ( & f. attrs ) ,
755
750
}
756
751
}
757
752
@@ -772,7 +767,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
772
767
}
773
768
AssocItemKind :: Fn ( ref sig, None ) => {
774
769
let names = self . lower_fn_params_to_names ( & sig. decl ) ;
775
- let names: & [ Ident ] = self . arena . alloc_from_iter ( names. into_iter ( ) ) ;
776
770
let ( generics, sig) =
777
771
self . lower_method_sig ( & i. generics , sig, trait_item_def_id, false , None ) ;
778
772
( generics, hir:: TraitItemKind :: Method ( sig, hir:: TraitMethod :: Required ( names) ) )
@@ -799,7 +793,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
799
793
hir:: TraitItem {
800
794
hir_id : self . lower_node_id ( i. id ) ,
801
795
ident : i. ident ,
802
- attrs : self . lower_attrs_arena ( & i. attrs ) ,
796
+ attrs : self . lower_attrs ( & i. attrs ) ,
803
797
generics,
804
798
kind,
805
799
span : i. span ,
@@ -886,7 +880,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
886
880
hir:: ImplItem {
887
881
hir_id : self . lower_node_id ( i. id ) ,
888
882
ident : i. ident ,
889
- attrs : self . lower_attrs_arena ( & i. attrs ) ,
883
+ attrs : self . lower_attrs ( & i. attrs ) ,
890
884
generics,
891
885
vis : self . lower_visibility ( & i. vis , None ) ,
892
886
defaultness : self . lower_defaultness ( i. defaultness , true /* [1] */ ) ,
@@ -945,12 +939,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
945
939
let res = self . expect_full_res ( id) ;
946
940
let res = self . lower_res ( res) ;
947
941
hir:: VisibilityKind :: Restricted {
948
- path : self . arena . alloc ( self . lower_path_extra (
949
- res,
950
- path,
951
- ParamMode :: Explicit ,
952
- explicit_owner,
953
- ) ) ,
942
+ path : self . lower_path_extra ( res, path, ParamMode :: Explicit , explicit_owner) ,
954
943
hir_id : lowered_id,
955
944
}
956
945
}
@@ -993,7 +982,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
993
982
994
983
fn lower_param ( & mut self , param : & Param ) -> hir:: Param < ' hir > {
995
984
hir:: Param {
996
- attrs : self . lower_attrs_arena ( & param. attrs ) ,
985
+ attrs : self . lower_attrs ( & param. attrs ) ,
997
986
hir_id : self . lower_node_id ( param. id ) ,
998
987
pat : self . lower_pat ( & param. pat ) ,
999
988
span : param. span ,
@@ -1133,7 +1122,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1133
1122
let stmt = this. stmt_let_pat (
1134
1123
stmt_attrs,
1135
1124
desugared_span,
1136
- Some ( this . arena . alloc ( expr) ) ,
1125
+ Some ( expr) ,
1137
1126
parameter. pat ,
1138
1127
hir:: LocalSource :: AsyncFn ,
1139
1128
) ;
@@ -1163,7 +1152,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1163
1152
let move_stmt = this. stmt_let_pat (
1164
1153
AttrVec :: new ( ) ,
1165
1154
desugared_span,
1166
- Some ( this . arena . alloc ( move_expr) ) ,
1155
+ Some ( move_expr) ,
1167
1156
move_pat,
1168
1157
hir:: LocalSource :: AsyncFn ,
1169
1158
) ;
@@ -1174,7 +1163,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
1174
1163
let pattern_stmt = this. stmt_let_pat (
1175
1164
stmt_attrs,
1176
1165
desugared_span,
1177
- Some ( this . arena . alloc ( pattern_expr) ) ,
1166
+ Some ( pattern_expr) ,
1178
1167
parameter. pat ,
1179
1168
hir:: LocalSource :: AsyncFn ,
1180
1169
) ;
@@ -1295,11 +1284,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
1295
1284
}
1296
1285
}
1297
1286
1298
- pub ( super ) fn lower_generics (
1287
+ pub ( super ) fn lower_generics_mut (
1299
1288
& mut self ,
1300
1289
generics : & Generics ,
1301
1290
itctx : ImplTraitContext < ' _ , ' hir > ,
1302
- ) -> hir :: Generics < ' hir > {
1291
+ ) -> GenericsCtor < ' hir > {
1303
1292
// Collect `?Trait` bounds in where clause and move them to parameter definitions.
1304
1293
// FIXME: this could probably be done with less rightward drift. It also looks like two
1305
1294
// control paths where `report_error` is called are the only paths that advance to after the
@@ -1355,13 +1344,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
1355
1344
}
1356
1345
}
1357
1346
1358
- hir :: Generics {
1359
- params : self . lower_generic_params ( & generics. params , & add_bounds, itctx) ,
1347
+ GenericsCtor {
1348
+ params : self . lower_generic_params_mut ( & generics. params , & add_bounds, itctx) . collect ( ) ,
1360
1349
where_clause : self . lower_where_clause ( & generics. where_clause ) ,
1361
1350
span : generics. span ,
1362
1351
}
1363
1352
}
1364
1353
1354
+ pub ( super ) fn lower_generics (
1355
+ & mut self ,
1356
+ generics : & Generics ,
1357
+ itctx : ImplTraitContext < ' _ , ' hir > ,
1358
+ ) -> hir:: Generics < ' hir > {
1359
+ let generics_ctor = self . lower_generics_mut ( generics, itctx) ;
1360
+ generics_ctor. into_generics ( self . arena )
1361
+ }
1362
+
1365
1363
fn lower_where_clause ( & mut self , wc : & WhereClause ) -> hir:: WhereClause < ' hir > {
1366
1364
self . with_anonymous_lifetime_mode ( AnonymousLifetimeMode :: ReportError , |this| {
1367
1365
hir:: WhereClause {
@@ -1383,13 +1381,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
1383
1381
} ) => {
1384
1382
self . with_in_scope_lifetime_defs ( & bound_generic_params, |this| {
1385
1383
hir:: WherePredicate :: BoundPredicate ( hir:: WhereBoundPredicate {
1386
- bound_generic_params : this. arena . alloc_from_iter (
1387
- this. lower_generic_params (
1388
- bound_generic_params,
1389
- & NodeMap :: default ( ) ,
1390
- ImplTraitContext :: disallowed ( ) ,
1391
- )
1392
- . into_iter ( ) ,
1384
+ bound_generic_params : this. lower_generic_params (
1385
+ bound_generic_params,
1386
+ & NodeMap :: default ( ) ,
1387
+ ImplTraitContext :: disallowed ( ) ,
1393
1388
) ,
1394
1389
bounded_ty : this. lower_ty ( bounded_ty, ImplTraitContext :: disallowed ( ) ) ,
1395
1390
bounds : this. arena . alloc_from_iter ( bounds. iter ( ) . filter_map ( |bound| {
@@ -1426,3 +1421,20 @@ impl<'hir> LoweringContext<'_, 'hir> {
1426
1421
}
1427
1422
}
1428
1423
}
1424
+
1425
+ /// Helper struct for delayed construction of Generics.
1426
+ pub ( super ) struct GenericsCtor < ' hir > {
1427
+ pub ( super ) params : SmallVec < [ hir:: GenericParam < ' hir > ; 4 ] > ,
1428
+ where_clause : hir:: WhereClause < ' hir > ,
1429
+ span : Span ,
1430
+ }
1431
+
1432
+ impl GenericsCtor < ' hir > {
1433
+ pub ( super ) fn into_generics ( self , arena : & ' hir Arena < ' hir > ) -> hir:: Generics < ' hir > {
1434
+ hir:: Generics {
1435
+ params : arena. alloc_from_iter ( self . params ) ,
1436
+ where_clause : self . where_clause ,
1437
+ span : self . span ,
1438
+ }
1439
+ }
1440
+ }
0 commit comments