@@ -44,6 +44,7 @@ extern crate tracing;
44
44
45
45
use crate :: errors:: { AssocTyParentheses , AssocTyParenthesesSub , MisplacedImplTrait } ;
46
46
47
+ use rustc_arena:: declare_arena;
47
48
use rustc_ast:: ptr:: P ;
48
49
use rustc_ast:: visit;
49
50
use rustc_ast:: { self as ast, * } ;
@@ -95,6 +96,13 @@ struct LoweringContext<'a, 'hir> {
95
96
/// Used to allocate HIR nodes.
96
97
arena : & ' hir hir:: Arena < ' hir > ,
97
98
99
+ /// Used to allocate temporary AST nodes for use during lowering.
100
+ /// This allows us to create "fake" AST -- these nodes can sometimes
101
+ /// be allocated on the stack, but other times we need them to live longer
102
+ /// than the current stack frame, so they can be collected into vectors
103
+ /// and things like that.
104
+ ast_arena : & ' a Arena < ' static > ,
105
+
98
106
/// Bodies inside the owner being lowered.
99
107
bodies : Vec < ( hir:: ItemLocalId , & ' hir hir:: Body < ' hir > ) > ,
100
108
/// Attributes inside the owner being lowered.
@@ -140,6 +148,15 @@ struct LoweringContext<'a, 'hir> {
140
148
generics_def_id_map : Vec < FxHashMap < LocalDefId , LocalDefId > > ,
141
149
}
142
150
151
+ declare_arena ! ( [
152
+ [ ] tys: rustc_ast:: Ty ,
153
+ [ ] aba: rustc_ast:: AngleBracketedArgs ,
154
+ [ ] ptr: rustc_ast:: PolyTraitRef ,
155
+ // This _marker field is needed because `declare_arena` creates `Arena<'tcx>` and we need to
156
+ // use `'tcx`. If we don't have this we get a compile error.
157
+ [ ] _marker: std:: marker:: PhantomData <& ' tcx ( ) >,
158
+ ] ) ;
159
+
143
160
trait ResolverAstLoweringExt {
144
161
fn legacy_const_generic_args ( & self , expr : & Expr ) -> Option < Vec < usize > > ;
145
162
fn get_partial_res ( & self , id : NodeId ) -> Option < PartialRes > ;
@@ -401,10 +418,13 @@ pub fn lower_to_hir<'hir>(tcx: TyCtxt<'hir>, (): ()) -> hir::Crate<'hir> {
401
418
tcx. definitions_untracked ( ) . def_index_count ( ) ,
402
419
) ;
403
420
421
+ let ast_arena = Arena :: default ( ) ;
422
+
404
423
for def_id in ast_index. indices ( ) {
405
424
item:: ItemLowerer {
406
425
tcx,
407
426
resolver : & mut resolver,
427
+ ast_arena : & ast_arena,
408
428
ast_index : & ast_index,
409
429
owners : & mut owners,
410
430
}
@@ -964,12 +984,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
964
984
}
965
985
GenericArgs :: Parenthesized ( ref data) => {
966
986
self . emit_bad_parenthesized_trait_in_assoc_ty ( data) ;
967
- self . lower_angle_bracketed_parameter_data (
968
- & data. as_angle_bracketed_args ( ) ,
969
- ParamMode :: Explicit ,
970
- itctx,
971
- )
972
- . 0
987
+ let aba = self . ast_arena . aba . alloc ( data. as_angle_bracketed_args ( ) ) ;
988
+ self . lower_angle_bracketed_parameter_data ( aba, ParamMode :: Explicit , itctx) . 0
973
989
}
974
990
} ;
975
991
gen_args_ctor. into_generic_args ( self )
@@ -1037,15 +1053,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1037
1053
1038
1054
self . with_dyn_type_scope ( false , |this| {
1039
1055
let node_id = this. next_node_id ( ) ;
1040
- let ty = this. lower_ty (
1041
- & Ty {
1042
- id : node_id,
1043
- kind : TyKind :: ImplTrait ( impl_trait_node_id, bounds. clone ( ) ) ,
1044
- span : this. lower_span ( constraint. span ) ,
1045
- tokens : None ,
1046
- } ,
1047
- itctx,
1048
- ) ;
1056
+ let ty = this. ast_arena . tys . alloc ( Ty {
1057
+ id : node_id,
1058
+ kind : TyKind :: ImplTrait ( impl_trait_node_id, bounds. clone ( ) ) ,
1059
+ span : this. lower_span ( constraint. span ) ,
1060
+ tokens : None ,
1061
+ } ) ;
1062
+ let ty = this. lower_ty ( ty, itctx) ;
1049
1063
1050
1064
hir:: TypeBindingKind :: Equality { term : ty. into ( ) }
1051
1065
} )
@@ -1181,12 +1195,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1181
1195
&& let Res :: Def ( DefKind :: Trait | DefKind :: TraitAlias , _) = partial_res. base_res ( )
1182
1196
{
1183
1197
let ( bounds, lifetime_bound) = self . with_dyn_type_scope ( true , |this| {
1198
+ let poly_trait_ref = this. ast_arena . ptr . alloc ( PolyTraitRef {
1199
+ bound_generic_params : vec ! [ ] ,
1200
+ trait_ref : TraitRef { path : path. clone ( ) , ref_id : t. id } ,
1201
+ span : t. span
1202
+ } ) ;
1184
1203
let bound = this. lower_poly_trait_ref (
1185
- & PolyTraitRef {
1186
- bound_generic_params : vec ! [ ] ,
1187
- trait_ref : TraitRef { path : path. clone ( ) , ref_id : t. id } ,
1188
- span : t. span
1189
- } ,
1204
+ poly_trait_ref,
1190
1205
itctx,
1191
1206
) ;
1192
1207
let bounds = this. arena . alloc_from_iter ( [ bound] ) ;
0 commit comments