Skip to content

Commit 58b908d

Browse files
committed
Lowering for hir::Expr.
1 parent 52e9825 commit 58b908d

File tree

5 files changed

+347
-229
lines changed

5 files changed

+347
-229
lines changed

src/librustc/arena.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,25 @@ macro_rules! arena_types {
125125

126126
// HIR types
127127
[few] hir_forest: rustc::hir::map::Forest<$tcx>,
128+
[] arm: rustc::hir::Arm<$tcx>,
128129
[] attribute: syntax::ast::Attribute,
130+
[] block: rustc::hir::Block<$tcx>,
129131
[few] global_asm: rustc::hir::GlobalAsm,
132+
[] expr: rustc::hir::Expr<$tcx>,
133+
[] field: rustc::hir::Field<$tcx>,
134+
[] field_pat: rustc::hir::FieldPat<$tcx>,
130135
[] fn_decl: rustc::hir::FnDecl,
131136
[] foreign_item: rustc::hir::ForeignItem<$tcx>,
132137
[] impl_item_ref: rustc::hir::ImplItemRef,
138+
[] inline_asm: rustc::hir::InlineAsm<$tcx>,
139+
[] local: rustc::hir::Local<$tcx>,
133140
[few] macro_def: rustc::hir::MacroDef<$tcx>,
134141
[] param: rustc::hir::Param<$tcx>,
142+
[] pat: rustc::hir::Pat<$tcx>,
135143
[] path: rustc::hir::Path,
144+
[] path_segment: rustc::hir::PathSegment,
145+
[] qpath: rustc::hir::QPath,
146+
[] stmt: rustc::hir::Stmt<$tcx>,
136147
[] struct_field: rustc::hir::StructField<$tcx>,
137148
[] trait_item_ref: rustc::hir::TraitItemRef,
138149
[] ty: rustc::hir::Ty,

src/librustc/hir/lowering.rs

Lines changed: 80 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -921,7 +921,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
921921

922922
fn with_new_scopes<T, F>(&mut self, f: F) -> T
923923
where
924-
F: FnOnce(&mut LoweringContext<'_, '_>) -> T,
924+
F: FnOnce(&mut Self) -> T,
925925
{
926926
let was_in_loop_condition = self.is_in_loop_condition;
927927
self.is_in_loop_condition = false;
@@ -2031,21 +2031,27 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20312031
}
20322032
}
20332033
let parent_def_id = DefId::local(self.current_hir_id_owner.last().unwrap().0);
2034+
let ty = l.ty.as_ref().map(|t| {
2035+
self.lower_ty(
2036+
t,
2037+
if self.sess.features_untracked().impl_trait_in_bindings {
2038+
ImplTraitContext::OpaqueTy(Some(parent_def_id))
2039+
} else {
2040+
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
2041+
},
2042+
)
2043+
});
2044+
let ty = ty.map(|ty| -> &'hir hir::Ty { self.arena.alloc(ty.into_inner()) });
2045+
let init = l
2046+
.init
2047+
.as_ref()
2048+
.map(|e| -> &'hir hir::Expr<'hir> { self.arena.alloc(self.lower_expr(e)) });
20342049
(
20352050
hir::Local {
20362051
hir_id: self.lower_node_id(l.id),
2037-
ty: l.ty.as_ref().map(|t| {
2038-
self.lower_ty(
2039-
t,
2040-
if self.sess.features_untracked().impl_trait_in_bindings {
2041-
ImplTraitContext::OpaqueTy(Some(parent_def_id))
2042-
} else {
2043-
ImplTraitContext::Disallowed(ImplTraitPosition::Binding)
2044-
},
2045-
)
2046-
}),
2052+
ty,
20472053
pat: self.lower_pat(&l.pat),
2048-
init: l.init.as_ref().map(|e| P(self.lower_expr(e))),
2054+
init,
20492055
span: l.span,
20502056
attrs: l.attrs.clone(),
20512057
source: hir::LocalSource::Normal,
@@ -2586,14 +2592,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
25862592
bounds.iter().map(|bound| self.lower_param_bound(bound, itctx.reborrow())).collect()
25872593
}
25882594

2589-
fn lower_block(&mut self, b: &Block, targeted_by_break: bool) -> P<hir::Block<'hir>> {
2595+
fn lower_block(&mut self, b: &Block, targeted_by_break: bool) -> &'hir hir::Block<'hir> {
2596+
self.arena.alloc(self.lower_block_noalloc(b, targeted_by_break))
2597+
}
2598+
2599+
fn lower_block_noalloc(&mut self, b: &Block, targeted_by_break: bool) -> hir::Block<'hir> {
25902600
let mut stmts = vec![];
2591-
let mut expr = None;
2601+
let mut expr: Option<&'hir _> = None;
25922602

25932603
for (index, stmt) in b.stmts.iter().enumerate() {
25942604
if index == b.stmts.len() - 1 {
25952605
if let StmtKind::Expr(ref e) = stmt.kind {
2596-
expr = Some(P(self.lower_expr(e)));
2606+
expr = Some(self.arena.alloc(self.lower_expr(e)));
25972607
} else {
25982608
stmts.extend(self.lower_stmt(stmt));
25992609
}
@@ -2602,14 +2612,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26022612
}
26032613
}
26042614

2605-
P(hir::Block {
2615+
hir::Block {
26062616
hir_id: self.lower_node_id(b.id),
2607-
stmts: stmts.into(),
2617+
stmts: self.arena.alloc_from_iter(stmts),
26082618
expr,
26092619
rules: self.lower_block_check_mode(&b.rules),
26102620
span: b.span,
26112621
targeted_by_break,
2612-
})
2622+
}
26132623
}
26142624

26152625
/// Lowers a block directly to an expression, presuming that it
@@ -2619,15 +2629,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26192629
self.expr_block(block, AttrVec::new())
26202630
}
26212631

2622-
fn lower_pat(&mut self, p: &Pat) -> P<hir::Pat<'hir>> {
2632+
fn lower_pat(&mut self, p: &Pat) -> &'hir hir::Pat<'hir> {
26232633
let node = match p.kind {
26242634
PatKind::Wild => hir::PatKind::Wild,
26252635
PatKind::Ident(ref binding_mode, ident, ref sub) => {
26262636
let lower_sub = |this: &mut Self| sub.as_ref().map(|s| this.lower_pat(&*s));
26272637
let node = self.lower_pat_ident(p, binding_mode, ident, lower_sub);
26282638
node
26292639
}
2630-
PatKind::Lit(ref e) => hir::PatKind::Lit(P(self.lower_expr(e))),
2640+
PatKind::Lit(ref e) => hir::PatKind::Lit(self.arena.alloc(self.lower_expr(e))),
26312641
PatKind::TupleStruct(ref path, ref pats) => {
26322642
let qpath = self.lower_qpath(
26332643
p.id,
@@ -2640,7 +2650,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26402650
hir::PatKind::TupleStruct(qpath, pats, ddpos)
26412651
}
26422652
PatKind::Or(ref pats) => {
2643-
hir::PatKind::Or(pats.iter().map(|x| self.lower_pat(x)).collect())
2653+
hir::PatKind::Or(self.arena.alloc_from_iter(pats.iter().map(|x| self.lower_pat(x))))
26442654
}
26452655
PatKind::Path(ref qself, ref path) => {
26462656
let qpath = self.lower_qpath(
@@ -2661,16 +2671,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26612671
ImplTraitContext::disallowed(),
26622672
);
26632673

2664-
let fs = fields
2665-
.iter()
2666-
.map(|f| hir::FieldPat {
2667-
hir_id: self.next_id(),
2668-
ident: f.ident,
2669-
pat: self.lower_pat(&f.pat),
2670-
is_shorthand: f.is_shorthand,
2671-
span: f.span,
2672-
})
2673-
.collect();
2674+
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| hir::FieldPat {
2675+
hir_id: self.next_id(),
2676+
ident: f.ident,
2677+
pat: self.lower_pat(&f.pat),
2678+
is_shorthand: f.is_shorthand,
2679+
span: f.span,
2680+
}));
26742681
hir::PatKind::Struct(qpath, fs, etc)
26752682
}
26762683
PatKind::Tuple(ref pats) => {
@@ -2680,8 +2687,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
26802687
PatKind::Box(ref inner) => hir::PatKind::Box(self.lower_pat(inner)),
26812688
PatKind::Ref(ref inner, mutbl) => hir::PatKind::Ref(self.lower_pat(inner), mutbl),
26822689
PatKind::Range(ref e1, ref e2, Spanned { node: ref end, .. }) => hir::PatKind::Range(
2683-
P(self.lower_expr(e1)),
2684-
P(self.lower_expr(e2)),
2690+
self.arena.alloc(self.lower_expr(e1)),
2691+
self.arena.alloc(self.lower_expr(e2)),
26852692
self.lower_range_end(end),
26862693
),
26872694
PatKind::Slice(ref pats) => self.lower_pat_slice(pats),
@@ -2700,7 +2707,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
27002707
&mut self,
27012708
pats: &[AstP<Pat>],
27022709
ctx: &str,
2703-
) -> (HirVec<P<hir::Pat<'hir>>>, Option<usize>) {
2710+
) -> (&'hir [&'hir hir::Pat<'hir>], Option<usize>) {
27042711
let mut elems = Vec::with_capacity(pats.len());
27052712
let mut rest = None;
27062713

@@ -2728,7 +2735,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
27282735
}
27292736
}
27302737

2731-
(elems.into(), rest.map(|(ddpos, _)| ddpos))
2738+
(self.arena.alloc_from_iter(elems), rest.map(|(ddpos, _)| ddpos))
27322739
}
27332740

27342741
/// Lower a slice pattern of form `[pat_0, ..., pat_n]` into
@@ -2788,15 +2795,19 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
27882795
}
27892796
}
27902797

2791-
hir::PatKind::Slice(before.into(), slice, after.into())
2798+
hir::PatKind::Slice(
2799+
self.arena.alloc_from_iter(before),
2800+
slice,
2801+
self.arena.alloc_from_iter(after),
2802+
)
27922803
}
27932804

27942805
fn lower_pat_ident(
27952806
&mut self,
27962807
p: &Pat,
27972808
binding_mode: &BindingMode,
27982809
ident: Ident,
2799-
lower_sub: impl FnOnce(&mut Self) -> Option<P<hir::Pat<'hir>>>,
2810+
lower_sub: impl FnOnce(&mut Self) -> Option<&'hir hir::Pat<'hir>>,
28002811
) -> hir::PatKind<'hir> {
28012812
match self.resolver.get_partial_res(p.id).map(|d| d.base_res()) {
28022813
// `None` can occur in body-less function signatures
@@ -2824,13 +2835,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
28242835
}
28252836
}
28262837

2827-
fn pat_wild_with_node_id_of(&mut self, p: &Pat) -> P<hir::Pat<'hir>> {
2838+
fn pat_wild_with_node_id_of(&mut self, p: &Pat) -> &'hir hir::Pat<'hir> {
28282839
self.pat_with_node_id_of(p, hir::PatKind::Wild)
28292840
}
28302841

28312842
/// Construct a `Pat` with the `HirId` of `p.id` lowered.
2832-
fn pat_with_node_id_of(&mut self, p: &Pat, kind: hir::PatKind<'hir>) -> P<hir::Pat<'hir>> {
2833-
P(hir::Pat { hir_id: self.lower_node_id(p.id), kind, span: p.span })
2843+
fn pat_with_node_id_of(&mut self, p: &Pat, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
2844+
self.arena.alloc(hir::Pat { hir_id: self.lower_node_id(p.id), kind, span: p.span })
28342845
}
28352846

28362847
/// Emit a friendly error for extra `..` patterns in a tuple/tuple struct/slice pattern.
@@ -2883,7 +2894,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
28832894
ids.push({
28842895
hir::Stmt {
28852896
hir_id: self.lower_node_id(s.id),
2886-
kind: hir::StmtKind::Local(P(l)),
2897+
kind: hir::StmtKind::Local(self.arena.alloc(l)),
28872898
span: s.span,
28882899
}
28892900
});
@@ -2905,8 +2916,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
29052916
})
29062917
.collect();
29072918
}
2908-
StmtKind::Expr(ref e) => hir::StmtKind::Expr(P(self.lower_expr(e))),
2909-
StmtKind::Semi(ref e) => hir::StmtKind::Semi(P(self.lower_expr(e))),
2919+
StmtKind::Expr(ref e) => hir::StmtKind::Expr(self.arena.alloc(self.lower_expr(e))),
2920+
StmtKind::Semi(ref e) => hir::StmtKind::Semi(self.arena.alloc(self.lower_expr(e))),
29102921
StmtKind::Mac(..) => panic!("shouldn't exist here"),
29112922
};
29122923
smallvec![hir::Stmt { hir_id: self.lower_node_id(s.id), kind, span: s.span }]
@@ -2949,30 +2960,30 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
29492960
}
29502961

29512962
fn stmt_expr(&mut self, span: Span, expr: hir::Expr<'hir>) -> hir::Stmt<'hir> {
2952-
self.stmt(span, hir::StmtKind::Expr(P(expr)))
2963+
self.stmt(span, hir::StmtKind::Expr(self.arena.alloc(expr)))
29532964
}
29542965

29552966
fn stmt_let_pat(
29562967
&mut self,
29572968
attrs: AttrVec,
29582969
span: Span,
2959-
init: Option<P<hir::Expr<'hir>>>,
2960-
pat: P<hir::Pat<'hir>>,
2970+
init: Option<&'hir hir::Expr<'hir>>,
2971+
pat: &'hir hir::Pat<'hir>,
29612972
source: hir::LocalSource,
29622973
) -> hir::Stmt<'hir> {
29632974
let local = hir::Local { attrs, hir_id: self.next_id(), init, pat, source, span, ty: None };
2964-
self.stmt(span, hir::StmtKind::Local(P(local)))
2975+
self.stmt(span, hir::StmtKind::Local(self.arena.alloc(local)))
29652976
}
29662977

2967-
fn block_expr(&mut self, expr: P<hir::Expr<'hir>>) -> hir::Block<'hir> {
2968-
self.block_all(expr.span, hir::HirVec::new(), Some(expr))
2978+
fn block_expr(&mut self, expr: &'hir hir::Expr<'hir>) -> hir::Block<'hir> {
2979+
self.block_all(expr.span, &[], Some(expr))
29692980
}
29702981

29712982
fn block_all(
29722983
&mut self,
29732984
span: Span,
2974-
stmts: hir::HirVec<hir::Stmt<'hir>>,
2975-
expr: Option<P<hir::Expr<'hir>>>,
2985+
stmts: &'hir [hir::Stmt<'hir>],
2986+
expr: Option<&'hir hir::Expr<'hir>>,
29762987
) -> hir::Block<'hir> {
29772988
hir::Block {
29782989
stmts,
@@ -2985,33 +2996,33 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
29852996
}
29862997

29872998
/// Constructs a `true` or `false` literal pattern.
2988-
fn pat_bool(&mut self, span: Span, val: bool) -> P<hir::Pat<'hir>> {
2999+
fn pat_bool(&mut self, span: Span, val: bool) -> &'hir hir::Pat<'hir> {
29893000
let expr = self.expr_bool(span, val);
2990-
self.pat(span, hir::PatKind::Lit(P(expr)))
3001+
self.pat(span, hir::PatKind::Lit(self.arena.alloc(expr)))
29913002
}
29923003

2993-
fn pat_ok(&mut self, span: Span, pat: P<hir::Pat<'hir>>) -> P<hir::Pat<'hir>> {
2994-
self.pat_std_enum(span, &[sym::result, sym::Result, sym::Ok], hir_vec![pat])
3004+
fn pat_ok(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
3005+
self.pat_std_enum(span, &[sym::result, sym::Result, sym::Ok], arena_vec![self; pat])
29953006
}
29963007

2997-
fn pat_err(&mut self, span: Span, pat: P<hir::Pat<'hir>>) -> P<hir::Pat<'hir>> {
2998-
self.pat_std_enum(span, &[sym::result, sym::Result, sym::Err], hir_vec![pat])
3008+
fn pat_err(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
3009+
self.pat_std_enum(span, &[sym::result, sym::Result, sym::Err], arena_vec![self; pat])
29993010
}
30003011

3001-
fn pat_some(&mut self, span: Span, pat: P<hir::Pat<'hir>>) -> P<hir::Pat<'hir>> {
3002-
self.pat_std_enum(span, &[sym::option, sym::Option, sym::Some], hir_vec![pat])
3012+
fn pat_some(&mut self, span: Span, pat: &'hir hir::Pat<'hir>) -> &'hir hir::Pat<'hir> {
3013+
self.pat_std_enum(span, &[sym::option, sym::Option, sym::Some], arena_vec![self; pat])
30033014
}
30043015

3005-
fn pat_none(&mut self, span: Span) -> P<hir::Pat<'hir>> {
3006-
self.pat_std_enum(span, &[sym::option, sym::Option, sym::None], hir_vec![])
3016+
fn pat_none(&mut self, span: Span) -> &'hir hir::Pat<'hir> {
3017+
self.pat_std_enum(span, &[sym::option, sym::Option, sym::None], &[])
30073018
}
30083019

30093020
fn pat_std_enum(
30103021
&mut self,
30113022
span: Span,
30123023
components: &[Symbol],
3013-
subpats: hir::HirVec<P<hir::Pat<'hir>>>,
3014-
) -> P<hir::Pat<'hir>> {
3024+
subpats: &'hir [&'hir hir::Pat<'hir>],
3025+
) -> &'hir hir::Pat<'hir> {
30153026
let path = self.std_path(span, components, None, true);
30163027
let qpath = hir::QPath::Resolved(None, P(path));
30173028
let pt = if subpats.is_empty() {
@@ -3022,7 +3033,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
30223033
self.pat(span, pt)
30233034
}
30243035

3025-
fn pat_ident(&mut self, span: Span, ident: Ident) -> (P<hir::Pat<'hir>>, hir::HirId) {
3036+
fn pat_ident(&mut self, span: Span, ident: Ident) -> (&'hir hir::Pat<'hir>, hir::HirId) {
30263037
self.pat_ident_binding_mode(span, ident, hir::BindingAnnotation::Unannotated)
30273038
}
30283039

@@ -3031,11 +3042,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
30313042
span: Span,
30323043
ident: Ident,
30333044
bm: hir::BindingAnnotation,
3034-
) -> (P<hir::Pat<'hir>>, hir::HirId) {
3045+
) -> (&'hir hir::Pat<'hir>, hir::HirId) {
30353046
let hir_id = self.next_id();
30363047

30373048
(
3038-
P(hir::Pat {
3049+
self.arena.alloc(hir::Pat {
30393050
hir_id,
30403051
kind: hir::PatKind::Binding(bm, hir_id, ident.with_span_pos(span), None),
30413052
span,
@@ -3044,12 +3055,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
30443055
)
30453056
}
30463057

3047-
fn pat_wild(&mut self, span: Span) -> P<hir::Pat<'hir>> {
3058+
fn pat_wild(&mut self, span: Span) -> &'hir hir::Pat<'hir> {
30483059
self.pat(span, hir::PatKind::Wild)
30493060
}
30503061

3051-
fn pat(&mut self, span: Span, kind: hir::PatKind<'hir>) -> P<hir::Pat<'hir>> {
3052-
P(hir::Pat { hir_id: self.next_id(), kind, span })
3062+
fn pat(&mut self, span: Span, kind: hir::PatKind<'hir>) -> &'hir hir::Pat<'hir> {
3063+
self.arena.alloc(hir::Pat { hir_id: self.next_id(), kind, span })
30533064
}
30543065

30553066
/// Given a suffix `["b", "c", "d"]`, returns path `::std::b::c::d` when

0 commit comments

Comments
 (0)