1
Fork 0

Refactor lower_stmts

This commit is contained in:
Cameron Steffen 2021-08-04 12:47:58 -05:00
parent 581abbfc6d
commit 120d46e255

View file

@ -2,7 +2,7 @@ use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
use rustc_ast::{Block, BlockCheckMode, Local, Stmt, StmtKind}; use rustc_ast::{Block, BlockCheckMode, Local, Stmt, StmtKind};
use rustc_hir as hir; use rustc_hir as hir;
use smallvec::{smallvec, SmallVec}; use smallvec::SmallVec;
impl<'a, 'hir> LoweringContext<'a, 'hir> { impl<'a, 'hir> LoweringContext<'a, 'hir> {
pub(super) fn lower_block( pub(super) fn lower_block(
@ -18,66 +18,67 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
b: &Block, b: &Block,
targeted_by_break: bool, targeted_by_break: bool,
) -> hir::Block<'hir> { ) -> hir::Block<'hir> {
let (stmts, expr) = match &*b.stmts { let (stmts, expr) = self.lower_stmts(&b.stmts);
[stmts @ .., Stmt { kind: StmtKind::Expr(e), .. }] => (stmts, Some(&*e)),
stmts => (stmts, None),
};
let stmts = self.arena.alloc_from_iter(stmts.iter().flat_map(|stmt| self.lower_stmt(stmt)));
let expr = expr.map(|e| self.lower_expr(e));
let rules = self.lower_block_check_mode(&b.rules); let rules = self.lower_block_check_mode(&b.rules);
let hir_id = self.lower_node_id(b.id); let hir_id = self.lower_node_id(b.id);
hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break } hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break }
} }
fn lower_stmt(&mut self, s: &Stmt) -> SmallVec<[hir::Stmt<'hir>; 1]> { fn lower_stmts(
let (hir_id, kind) = match s.kind { &mut self,
mut ast_stmts: &[Stmt],
) -> (&'hir [hir::Stmt<'hir>], Option<&'hir hir::Expr<'hir>>) {
let mut stmts = SmallVec::<[hir::Stmt<'hir>; 8]>::new();
let mut expr = None;
while let [s, tail @ ..] = ast_stmts {
match s.kind {
StmtKind::Local(ref l) => { StmtKind::Local(ref l) => {
let l = self.lower_local(l); let l = self.lower_local(l);
let hir_id = self.lower_node_id(s.id); let hir_id = self.lower_node_id(s.id);
self.alias_attrs(hir_id, l.hir_id); self.alias_attrs(hir_id, l.hir_id);
return smallvec![hir::Stmt { let kind = hir::StmtKind::Local(self.arena.alloc(l));
hir_id, let span = self.lower_span(s.span);
kind: hir::StmtKind::Local(self.arena.alloc(l)), stmts.push(hir::Stmt { hir_id, kind, span });
span: self.lower_span(s.span),
}];
} }
StmtKind::Item(ref it) => { StmtKind::Item(ref it) => {
// Can only use the ID once. stmts.extend(self.lower_item_id(it).into_iter().enumerate().map(
let mut id = Some(s.id); |(i, item_id)| {
return self let hir_id = match i {
.lower_item_id(it) 0 => self.lower_node_id(s.id),
.into_iter() _ => self.next_id(),
.map(|item_id| { };
let hir_id = id let kind = hir::StmtKind::Item(item_id);
.take() let span = self.lower_span(s.span);
.map(|id| self.lower_node_id(id)) hir::Stmt { hir_id, kind, span }
.unwrap_or_else(|| self.next_id()); },
));
hir::Stmt {
hir_id,
kind: hir::StmtKind::Item(item_id),
span: self.lower_span(s.span),
}
})
.collect();
} }
StmtKind::Expr(ref e) => { StmtKind::Expr(ref e) => {
let e = self.lower_expr(e); let e = self.lower_expr(e);
if tail.is_empty() {
expr = Some(e);
} else {
let hir_id = self.lower_node_id(s.id); let hir_id = self.lower_node_id(s.id);
self.alias_attrs(hir_id, e.hir_id); self.alias_attrs(hir_id, e.hir_id);
(hir_id, hir::StmtKind::Expr(e)) let kind = hir::StmtKind::Expr(e);
let span = self.lower_span(s.span);
stmts.push(hir::Stmt { hir_id, kind, span });
}
} }
StmtKind::Semi(ref e) => { StmtKind::Semi(ref e) => {
let e = self.lower_expr(e); let e = self.lower_expr(e);
let hir_id = self.lower_node_id(s.id); let hir_id = self.lower_node_id(s.id);
self.alias_attrs(hir_id, e.hir_id); self.alias_attrs(hir_id, e.hir_id);
(hir_id, hir::StmtKind::Semi(e)) let kind = hir::StmtKind::Semi(e);
let span = self.lower_span(s.span);
stmts.push(hir::Stmt { hir_id, kind, span });
} }
StmtKind::Empty => return smallvec![], StmtKind::Empty => {}
StmtKind::MacCall(..) => panic!("shouldn't exist here"), StmtKind::MacCall(..) => panic!("shouldn't exist here"),
}; }
smallvec![hir::Stmt { hir_id, kind, span: self.lower_span(s.span) }] ast_stmts = &ast_stmts[1..];
}
(self.arena.alloc_from_iter(stmts), expr)
} }
fn lower_local(&mut self, l: &Local) -> hir::Local<'hir> { fn lower_local(&mut self, l: &Local) -> hir::Local<'hir> {