@@ -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
}
@@ -974,12 +994,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
974
994
}
975
995
GenericArgs :: Parenthesized ( ref data) => {
976
996
self . emit_bad_parenthesized_trait_in_assoc_ty ( data) ;
977
- self . lower_angle_bracketed_parameter_data (
978
- & data. as_angle_bracketed_args ( ) ,
979
- ParamMode :: Explicit ,
980
- itctx,
981
- )
982
- . 0
997
+ let aba = self . ast_arena . aba . alloc ( data. as_angle_bracketed_args ( ) ) ;
998
+ self . lower_angle_bracketed_parameter_data ( aba, ParamMode :: Explicit , itctx) . 0
983
999
}
984
1000
} ;
985
1001
gen_args_ctor. into_generic_args ( self )
@@ -1048,15 +1064,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1048
1064
1049
1065
self . with_dyn_type_scope ( false , |this| {
1050
1066
let node_id = this. next_node_id ( ) ;
1051
- let ty = this. lower_ty (
1052
- & Ty {
1053
- id : node_id,
1054
- kind : TyKind :: ImplTrait ( impl_trait_node_id, bounds. clone ( ) ) ,
1055
- span : this. lower_span ( constraint. span ) ,
1056
- tokens : None ,
1057
- } ,
1058
- itctx,
1059
- ) ;
1067
+ let ty = this. ast_arena . tys . alloc ( Ty {
1068
+ id : node_id,
1069
+ kind : TyKind :: ImplTrait ( impl_trait_node_id, bounds. clone ( ) ) ,
1070
+ span : this. lower_span ( constraint. span ) ,
1071
+ tokens : None ,
1072
+ } ) ;
1073
+ let ty = this. lower_ty ( ty, itctx) ;
1060
1074
1061
1075
hir:: TypeBindingKind :: Equality { term : ty. into ( ) }
1062
1076
} )
@@ -1192,12 +1206,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1192
1206
&& let Res :: Def ( DefKind :: Trait | DefKind :: TraitAlias , _) = partial_res. base_res ( )
1193
1207
{
1194
1208
let ( bounds, lifetime_bound) = self . with_dyn_type_scope ( true , |this| {
1209
+ let poly_trait_ref = this. ast_arena . ptr . alloc ( PolyTraitRef {
1210
+ bound_generic_params : vec ! [ ] ,
1211
+ trait_ref : TraitRef { path : path. clone ( ) , ref_id : t. id } ,
1212
+ span : t. span
1213
+ } ) ;
1195
1214
let bound = this. lower_poly_trait_ref (
1196
- & PolyTraitRef {
1197
- bound_generic_params : vec ! [ ] ,
1198
- trait_ref : TraitRef { path : path. clone ( ) , ref_id : t. id } ,
1199
- span : t. span
1200
- } ,
1215
+ poly_trait_ref,
1201
1216
itctx,
1202
1217
) ;
1203
1218
let bounds = this. arena . alloc_from_iter ( [ bound] ) ;
0 commit comments