diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index 12a0cc0d25508..eb8ee8c2c1838 100644 --- a/compiler/rustc_ast_lowering/src/block.rs +++ b/compiler/rustc_ast_lowering/src/block.rs @@ -18,9 +18,15 @@ 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 } } @@ -28,12 +34,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { &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); @@ -41,12 +49,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { 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 } @@ -54,11 +59,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { )); } 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); @@ -66,8 +72,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } } 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); @@ -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)) diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index eaa5a38388afc..07cd750130752 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -31,6 +31,36 @@ impl<'hir> LoweringContext<'_, 'hir> { pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> { ensure_sufficient_stack(|| { + // Desugar `ExprForLoop` + // from: `[opt_ident]: for in ` + if let ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) = e.kind { + return self.lower_expr_for(e, pat, head, body, opt_label); + } + + if let ExprKind::Paren(ref ex) = e.kind { + let mut ex = self.lower_expr_mut(ex); + // Include parens in span, but only if it is a super-span. + if e.span.contains(ex.span) { + ex.span = self.lower_span(e.span); + } + // Merge attributes into the inner expression. + if !e.attrs.is_empty() { + let old_attrs = + self.attrs.get(&ex.hir_id.local_id).map(|la| *la).unwrap_or(&[]); + self.attrs.insert( + ex.hir_id.local_id, + &*self.arena.alloc_from_iter( + e.attrs + .iter() + .map(|a| self.lower_attr(a)) + .chain(old_attrs.iter().cloned()), + ), + ); + } + return ex; + } + + let hir_id = self.lower_node_id(e.id); let kind = match e.kind { ExprKind::Box(ref inner) => hir::ExprKind::Box(self.lower_expr(inner)), ExprKind::Array(ref exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)), @@ -48,7 +78,6 @@ impl<'hir> LoweringContext<'_, 'hir> { if e.attrs.get(0).map_or(false, |a| a.has_name(sym::rustc_box)) { if let [inner] = &args[..] && e.attrs.len() == 1 { let kind = hir::ExprKind::Box(self.lower_expr(&inner)); - let hir_id = self.lower_node_id(e.id); return hir::Expr { hir_id, kind, span: self.lower_span(e.span) }; } else { self.tcx.sess.emit_err(RustcBoxAttributeError { span: e.span }); @@ -281,38 +310,10 @@ impl<'hir> LoweringContext<'_, 'hir> { ExprKind::Yield(ref opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()), ExprKind::Err => hir::ExprKind::Err, ExprKind::Try(ref sub_expr) => self.lower_expr_try(e.span, sub_expr), - ExprKind::Paren(ref ex) => { - let mut ex = self.lower_expr_mut(ex); - // Include parens in span, but only if it is a super-span. - if e.span.contains(ex.span) { - ex.span = self.lower_span(e.span); - } - // Merge attributes into the inner expression. - if !e.attrs.is_empty() { - let old_attrs = - self.attrs.get(&ex.hir_id.local_id).map(|la| *la).unwrap_or(&[]); - self.attrs.insert( - ex.hir_id.local_id, - &*self.arena.alloc_from_iter( - e.attrs - .iter() - .map(|a| self.lower_attr(a)) - .chain(old_attrs.iter().cloned()), - ), - ); - } - return ex; - } - - // Desugar `ExprForLoop` - // from: `[opt_ident]: for in ` - ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => { - return self.lower_expr_for(e, pat, head, body, opt_label); - } - ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span), + ExprKind::MacCall(_) | _ => panic!("{:?} shouldn't exist here", e.span), }; - let hir_id = self.lower_node_id(e.id); + //let hir_id = self.lower_node_id(e.id); self.lower_attrs(hir_id, &e.attrs); hir::Expr { hir_id, kind, span: self.lower_span(e.span) } }) @@ -434,11 +435,12 @@ impl<'hir> LoweringContext<'_, 'hir> { ExprKind::Binary(op @ Spanned { node: ast::BinOpKind::And, .. }, lhs, rhs) if has_let_expr(cond) => { + let hir_id = self.next_id(); let op = self.lower_binop(*op); let lhs = self.lower_cond(lhs); let rhs = self.lower_cond(rhs); - - self.arena.alloc(self.expr( + self.arena.alloc(self.expr_with_hirid( + hir_id, cond.span, hir::ExprKind::Binary(op, lhs, rhs), AttrVec::new(), @@ -446,10 +448,11 @@ impl<'hir> LoweringContext<'_, 'hir> { } ExprKind::Let(..) => self.lower_expr(cond), _ => { + let hir_id = self.next_id(); let cond = self.lower_expr(cond); let reason = DesugaringKind::CondTemporary; let span_block = self.mark_span_with_reason(reason, cond.span, None); - self.expr_drop_temps(span_block, cond, AttrVec::new()) + self.expr_drop_temps_with_hirid(hir_id, span_block, cond, AttrVec::new()) } } } @@ -477,15 +480,23 @@ impl<'hir> LoweringContext<'_, 'hir> { body: &Block, opt_label: Option