From 61468add4b004a2ea56cf7444dc73ee7396e1182 Mon Sep 17 00:00:00 2001 From: yukang Date: Fri, 14 Oct 2022 11:58:49 +0800 Subject: [PATCH 01/10] use find_parent_node get parent node --- compiler/rustc_passes/src/hir_id_validator.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/compiler/rustc_passes/src/hir_id_validator.rs b/compiler/rustc_passes/src/hir_id_validator.rs index 88bb39debb114..e270c7ed90db5 100644 --- a/compiler/rustc_passes/src/hir_id_validator.rs +++ b/compiler/rustc_passes/src/hir_id_validator.rs @@ -143,6 +143,16 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { }); } + if let Some(owner_hir_id) = self.hir_map.find_parent_node(hir_id) && + owner_hir_id.local_id >= hir_id.local_id && hir_id.local_id != ItemLocalId::from_u32(0) { + self.error(|| { + format!( + "HirIdValidator: The local_id {} 's parent local_id {} is not smaller than children", + self.hir_map.node_to_string(hir_id), + self.hir_map.node_to_string(owner_hir_id), + ) + }); + } self.hir_ids_seen.insert(hir_id.local_id); } From 885ca2b5bcff3f174c43cb50c52b695a551a8bb7 Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 15 Oct 2022 01:06:43 +0800 Subject: [PATCH 02/10] add debug --- compiler/rustc_ast_lowering/src/block.rs | 35 ++++++-- compiler/rustc_ast_lowering/src/expr.rs | 88 +++++++++++-------- compiler/rustc_ast_lowering/src/item.rs | 20 +++-- compiler/rustc_ast_lowering/src/lib.rs | 4 +- compiler/rustc_passes/src/hir_id_validator.rs | 20 +++-- 5 files changed, 103 insertions(+), 64 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index 12a0cc0d25508..516c26bf3d961 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); @@ -44,8 +52,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { 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(), + 0 => { + debug!("yukang lower_node_id {:?}", s.id); + let id = self.lower_node_id(s.id); + debug!("yukang lower_node_id id={:?}", id); + id + } + _ => { + debug!("yukang lower_stmts item_id={:?}", item_id); + let id = self.next_id(); + debug!("yukang lower_stmts id={:?}", id); + id + } }; let kind = hir::StmtKind::Item(item_id); let span = self.lower_span(s.span); @@ -54,11 +72,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 +85,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); @@ -86,8 +105,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { .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 init = l.kind.init().map(|init| self.lower_expr(init)); 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..f5b4ce0808e1e 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) } }) @@ -480,8 +481,6 @@ impl<'hir> LoweringContext<'_, 'hir> { let lowered_cond = self.with_loop_condition_scope(|t| t.lower_cond(cond)); let then = self.lower_block_expr(body); let expr_break = self.expr_break(span, AttrVec::new()); - let stmt_break = self.stmt_expr(span, expr_break); - let else_blk = self.block_all(span, arena_vec![self; stmt_break], None); let else_expr = self.arena.alloc(self.expr_block(else_blk, AttrVec::new())); let if_kind = hir::ExprKind::If(lowered_cond, self.arena.alloc(then), Some(else_expr)); let if_expr = self.expr(span, if_kind, AttrVec::new()); @@ -1495,8 +1494,11 @@ impl<'hir> LoweringContext<'_, 'hir> { // Some() => , let some_arm = { let some_pat = self.pat_some(pat_span, pat); - let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false)); - let body_expr = self.arena.alloc(self.expr_block(body_block, AttrVec::new())); + //let body_block = || self.with_loop_scope(e.id, |this| this.lower_block(body, false)); + let body_expr = self.arena.alloc(self.expr_block( + |this| this.with_loop_scope(e.id, |this| this.lower_block(body, false)), + AttrVec::new(), + )); self.arm(some_pat, body_expr) }; @@ -1881,17 +1883,23 @@ impl<'hir> LoweringContext<'_, 'hir> { } fn expr_block_empty(&mut self, span: Span) -> &'hir hir::Expr<'hir> { - let blk = self.block_all(span, &[], None); - let expr = self.expr_block(blk, AttrVec::new()); + //let blk = || self.block_all(span, &[], None); + let expr = self.expr_block(|this| this.block_all(span, &[], None), AttrVec::new()); self.arena.alloc(expr) } pub(super) fn expr_block( &mut self, - b: &'hir hir::Block<'hir>, + //b: &'hir hir::Block<'hir>, + gen_block: impl FnOnce(&mut Self) -> &'hir hir::Block<'hir>, attrs: AttrVec, ) -> hir::Expr<'hir> { - self.expr(b.span, hir::ExprKind::Block(b, None), attrs) + let hir_id = self.next_id(); + debug!("yuakng expr hir_id: {:?}", hir_id); + self.lower_attrs(hir_id, &attrs); + let b = gen_block(self); + hir::Expr { hir_id, kind: hir::ExprKind::Block(b, None), span: self.lower_span(b.span) } + //self.expr(b.span, hir::ExprKind::Block(b, None), attrs) } pub(super) fn expr( @@ -1900,7 +1908,9 @@ impl<'hir> LoweringContext<'_, 'hir> { kind: hir::ExprKind<'hir>, attrs: AttrVec, ) -> hir::Expr<'hir> { + debug!("yukang expr({:?}, {:?})", span, kind); let hir_id = self.next_id(); + debug!("yuakng expr hir_id: {:?}", hir_id); self.lower_attrs(hir_id, &attrs); hir::Expr { hir_id, kind, span: self.lower_span(span) } } diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 05022c1a14c70..2c52892c8bad1 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -270,7 +270,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let asyncness = header.asyncness; let body_id = this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref()); - + debug!("yukang finished lower_maybe_async_body"); let mut itctx = ImplTraitContext::Universal; let (generics, decl) = this.lower_generics(generics, id, &mut itctx, |this| { let ret_id = asyncness.opt_return_id(); @@ -751,6 +751,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } fn lower_field_def(&mut self, (index, f): (usize, &FieldDef)) -> hir::FieldDef<'hir> { + let hir_id = self.lower_node_id(f.id); let ty = if let TyKind::Path(ref qself, ref path) = f.ty.kind { let t = self.lower_path_ty( &f.ty, @@ -763,7 +764,6 @@ impl<'hir> LoweringContext<'_, 'hir> { } else { self.lower_ty(&f.ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)) }; - let hir_id = self.lower_node_id(f.id); self.lower_attrs(hir_id, &f.attrs); hir::FieldDef { span: self.lower_span(f.span), @@ -882,6 +882,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> { // Since `default impl` is not yet implemented, this is always true in impls. + let hir_id = self.lower_node_id(i.id); let has_value = true; let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value); @@ -930,7 +931,6 @@ impl<'hir> LoweringContext<'_, 'hir> { AssocItemKind::MacCall(..) => panic!("`TyMac` should have been expanded by now"), }; - let hir_id = self.lower_node_id(i.id); self.lower_attrs(hir_id, &i.attrs); let item = hir::ImplItem { owner_id: hir_id.expect_owner(), @@ -1072,7 +1072,7 @@ impl<'hir> LoweringContext<'_, 'hir> { (Async::Yes { closure_id, .. }, Some(body)) => (closure_id, body), _ => return self.lower_fn_body_block(span, decl, body), }; - + debug!("yukang continue Lower"); self.lower_body(|this| { let mut parameters: Vec> = Vec::new(); let mut statements: Vec> = Vec::new(); @@ -1240,13 +1240,19 @@ impl<'hir> LoweringContext<'_, 'hir> { // drop-temps { } // } // ``` - let body = this.block_all( + /* let body = this.block_all( desugared_span, this.arena.alloc_from_iter(statements), Some(user_body), - ); + ); */ - this.expr_block(body, AttrVec::new()) + this.expr_block(|this| { + this.block_all( + desugared_span, + this.arena.alloc_from_iter(statements), + Some(user_body), + ) + }, AttrVec::new()) }, ); diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index e1703b0b02b05..2b6c5b665468f 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -2326,8 +2326,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { /// Lowers a block directly to an expression, presuming that it /// has no attributes and is not targeted by a `break`. fn lower_block_expr(&mut self, b: &Block) -> hir::Expr<'hir> { - let block = self.lower_block(b, false); - self.expr_block(block, AttrVec::new()) + //let block = || self.lower_block(b, false); + self.expr_block(|this| this.lower_block(b, false), AttrVec::new()) } fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen { diff --git a/compiler/rustc_passes/src/hir_id_validator.rs b/compiler/rustc_passes/src/hir_id_validator.rs index e270c7ed90db5..d4778a7497704 100644 --- a/compiler/rustc_passes/src/hir_id_validator.rs +++ b/compiler/rustc_passes/src/hir_id_validator.rs @@ -85,6 +85,10 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { .filter(|&i| !self.hir_ids_seen.contains(ItemLocalId::from_u32(i))) .collect(); + //span_bug!(self.hir_map.span(), "{}\nchild_span:{:?}", message, child_span); + println!("missing ids number: {:?}", missing.len()); + println!("missing ids: {:?}", missing); + // Try to map those to something more useful let mut missing_items = Vec::with_capacity(missing.len()); @@ -143,16 +147,16 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { }); } + let debug_log = std::env::var("RUSTC_LOG"); if let Some(owner_hir_id) = self.hir_map.find_parent_node(hir_id) && - owner_hir_id.local_id >= hir_id.local_id && hir_id.local_id != ItemLocalId::from_u32(0) { - self.error(|| { - format!( - "HirIdValidator: The local_id {} 's parent local_id {} is not smaller than children", - self.hir_map.node_to_string(hir_id), - self.hir_map.node_to_string(owner_hir_id), - ) - }); + owner_hir_id.local_id >= hir_id.local_id && hir_id.local_id != ItemLocalId::from_u32(0) && + debug_log.is_ok() { + let message = format!("HirIdValidator: The parent local_id `{}` is not smaller than children id: {:?}", + self.hir_map.node_to_string(owner_hir_id), hir_id.local_id); + let child_span = self.hir_map.span(hir_id); + span_bug!(self.hir_map.span(owner_hir_id), "{}\nchild_span:{:?}", message, child_span); } + self.hir_ids_seen.insert(hir_id.local_id); } From e5b5e06a7979e7ee4dba721b9dbb27d32ea7d502 Mon Sep 17 00:00:00 2001 From: yukang Date: Sat, 15 Oct 2022 04:02:36 +0800 Subject: [PATCH 03/10] fix bug --- compiler/rustc_ast_lowering/src/block.rs | 14 +------ compiler/rustc_ast_lowering/src/expr.rs | 6 ++- compiler/rustc_ast_lowering/src/item.rs | 19 ++++++---- compiler/rustc_ast_lowering/src/lib.rs | 38 ++++++++++--------- compiler/rustc_ast_lowering/src/path.rs | 2 +- compiler/rustc_middle/src/hir/map/mod.rs | 3 ++ compiler/rustc_passes/src/hir_id_validator.rs | 17 +++++---- 7 files changed, 51 insertions(+), 48 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/block.rs b/compiler/rustc_ast_lowering/src/block.rs index 516c26bf3d961..643a2964b58ec 100644 --- a/compiler/rustc_ast_lowering/src/block.rs +++ b/compiler/rustc_ast_lowering/src/block.rs @@ -52,18 +52,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { stmts.extend(self.lower_item_ref(it).into_iter().enumerate().map( |(i, item_id)| { let hir_id = match i { - 0 => { - debug!("yukang lower_node_id {:?}", s.id); - let id = self.lower_node_id(s.id); - debug!("yukang lower_node_id id={:?}", id); - id - } - _ => { - debug!("yukang lower_stmts item_id={:?}", item_id); - let id = self.next_id(); - debug!("yukang lower_stmts id={:?}", id); - id - } + 0 => self.lower_node_id(s.id), + _ => self.next_id(), }; let kind = hir::StmtKind::Item(item_id); let span = self.lower_span(s.span); diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index f5b4ce0808e1e..bf0e498c64e4a 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -592,6 +592,8 @@ impl<'hir> LoweringContext<'_, 'hir> { async_gen_kind: hir::AsyncGeneratorKind, body: impl FnOnce(&mut Self) -> hir::Expr<'hir>, ) -> hir::ExprKind<'hir> { + let hir_id = self.lower_node_id(closure_node_id); + let output = match ret_ty { Some(ty) => hir::FnRetTy::Return( self.lower_ty(&ty, &ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock)), @@ -1476,6 +1478,7 @@ impl<'hir> LoweringContext<'_, 'hir> { body: &Block, opt_label: Option