Skip to content

[WIP] Assign HirIds in HIR traversal order #103114

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,62 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
b: &Block,
targeted_by_break: bool,
) -> hir::Block<'hir> {
let (stmts, expr) = self.lower_stmts(&b.stmts);
let rules = self.lower_block_check_mode(&b.rules);
let hir_id = self.lower_node_id(b.id);
debug!("yukang lower_block_noalloc hir_id={:?}", hir_id);
let rules = self.lower_block_check_mode(&b.rules);
let (stmts, expr) = self.lower_stmts(&b.stmts);
if stmts.len() > 0 {
debug!("yukang lower_block_noalloc stmts={:?}", stmts);
} else {
debug!("yukang lower_block_noalloc stmts is empty");
}
hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break }
}

fn lower_stmts(
&mut self,
mut ast_stmts: &[Stmt],
) -> (&'hir [hir::Stmt<'hir>], Option<&'hir hir::Expr<'hir>>) {
debug!("yukang lower_stmts ast_stmts={:?}", ast_stmts);
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 local) => {
let hir_id = self.lower_node_id(s.id);
debug!("yukang local hir_id={:?}", hir_id);
let local = self.lower_local(local);
self.alias_attrs(hir_id, local.hir_id);
let kind = hir::StmtKind::Local(local);
let span = self.lower_span(s.span);
stmts.push(hir::Stmt { hir_id, kind, span });
}
StmtKind::Item(ref it) => {
let hir_id = self.lower_node_id(s.id);
stmts.extend(self.lower_item_ref(it).into_iter().enumerate().map(
|(i, item_id)| {
let hir_id = match i {
0 => self.lower_node_id(s.id),
_ => self.next_id(),
};
|(_i, item_id)| {
let kind = hir::StmtKind::Item(item_id);
let span = self.lower_span(s.span);
hir::Stmt { hir_id, kind, span }
},
));
}
StmtKind::Expr(ref e) => {
let e = self.lower_expr(e);
if tail.is_empty() {
expr = Some(e);
expr = Some(self.lower_expr(e));
} else {
let hir_id = self.lower_node_id(s.id);
debug!("yukang lower_stmts hir_id={:?}", hir_id);
let e = self.lower_expr(e);
self.alias_attrs(hir_id, e.hir_id);
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) => {
let e = self.lower_expr(e);
let hir_id = self.lower_node_id(s.id);
let e = self.lower_expr(e);
self.alias_attrs(hir_id, e.hir_id);
let kind = hir::StmtKind::Semi(e);
let span = self.lower_span(s.span);
Expand All @@ -82,12 +88,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}

fn lower_local(&mut self, l: &Local) -> &'hir hir::Local<'hir> {
let hir_id = self.lower_node_id(l.id);
let ty = l
.ty
.as_ref()
.map(|t| self.lower_ty(t, &ImplTraitContext::Disallowed(ImplTraitPosition::Variable)));
let init = l.kind.init().map(|init| self.lower_expr(init));
let hir_id = self.lower_node_id(l.id);
let pat = self.lower_pat(&l.pat);
let els = if let LocalKind::InitElse(_, els) = &l.kind {
Some(self.lower_block(els, false))
Expand Down
Loading