Skip to content

Commit 885ca2b

Browse files
committed
add debug
1 parent 61468ad commit 885ca2b

File tree

5 files changed

+103
-64
lines changed

5 files changed

+103
-64
lines changed

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,30 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1818
b: &Block,
1919
targeted_by_break: bool,
2020
) -> hir::Block<'hir> {
21-
let (stmts, expr) = self.lower_stmts(&b.stmts);
22-
let rules = self.lower_block_check_mode(&b.rules);
2321
let hir_id = self.lower_node_id(b.id);
22+
debug!("yukang lower_block_noalloc hir_id={:?}", hir_id);
23+
let rules = self.lower_block_check_mode(&b.rules);
24+
let (stmts, expr) = self.lower_stmts(&b.stmts);
25+
if stmts.len() > 0 {
26+
debug!("yukang lower_block_noalloc stmts={:?}", stmts);
27+
} else {
28+
debug!("yukang lower_block_noalloc stmts is empty");
29+
}
2430
hir::Block { hir_id, stmts, expr, rules, span: self.lower_span(b.span), targeted_by_break }
2531
}
2632

2733
fn lower_stmts(
2834
&mut self,
2935
mut ast_stmts: &[Stmt],
3036
) -> (&'hir [hir::Stmt<'hir>], Option<&'hir hir::Expr<'hir>>) {
37+
debug!("yukang lower_stmts ast_stmts={:?}", ast_stmts);
3138
let mut stmts = SmallVec::<[hir::Stmt<'hir>; 8]>::new();
3239
let mut expr = None;
3340
while let [s, tail @ ..] = ast_stmts {
3441
match s.kind {
3542
StmtKind::Local(ref local) => {
3643
let hir_id = self.lower_node_id(s.id);
44+
debug!("yukang local hir_id={:?}", hir_id);
3745
let local = self.lower_local(local);
3846
self.alias_attrs(hir_id, local.hir_id);
3947
let kind = hir::StmtKind::Local(local);
@@ -44,8 +52,18 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
4452
stmts.extend(self.lower_item_ref(it).into_iter().enumerate().map(
4553
|(i, item_id)| {
4654
let hir_id = match i {
47-
0 => self.lower_node_id(s.id),
48-
_ => self.next_id(),
55+
0 => {
56+
debug!("yukang lower_node_id {:?}", s.id);
57+
let id = self.lower_node_id(s.id);
58+
debug!("yukang lower_node_id id={:?}", id);
59+
id
60+
}
61+
_ => {
62+
debug!("yukang lower_stmts item_id={:?}", item_id);
63+
let id = self.next_id();
64+
debug!("yukang lower_stmts id={:?}", id);
65+
id
66+
}
4967
};
5068
let kind = hir::StmtKind::Item(item_id);
5169
let span = self.lower_span(s.span);
@@ -54,20 +72,21 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
5472
));
5573
}
5674
StmtKind::Expr(ref e) => {
57-
let e = self.lower_expr(e);
5875
if tail.is_empty() {
59-
expr = Some(e);
76+
expr = Some(self.lower_expr(e));
6077
} else {
6178
let hir_id = self.lower_node_id(s.id);
79+
debug!("yukang lower_stmts hir_id={:?}", hir_id);
80+
let e = self.lower_expr(e);
6281
self.alias_attrs(hir_id, e.hir_id);
6382
let kind = hir::StmtKind::Expr(e);
6483
let span = self.lower_span(s.span);
6584
stmts.push(hir::Stmt { hir_id, kind, span });
6685
}
6786
}
6887
StmtKind::Semi(ref e) => {
69-
let e = self.lower_expr(e);
7088
let hir_id = self.lower_node_id(s.id);
89+
let e = self.lower_expr(e);
7190
self.alias_attrs(hir_id, e.hir_id);
7291
let kind = hir::StmtKind::Semi(e);
7392
let span = self.lower_span(s.span);
@@ -86,8 +105,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
86105
.ty
87106
.as_ref()
88107
.map(|t| self.lower_ty(t, &ImplTraitContext::Disallowed(ImplTraitPosition::Variable)));
89-
let init = l.kind.init().map(|init| self.lower_expr(init));
90108
let hir_id = self.lower_node_id(l.id);
109+
let init = l.kind.init().map(|init| self.lower_expr(init));
91110
let pat = self.lower_pat(&l.pat);
92111
let els = if let LocalKind::InitElse(_, els) = &l.kind {
93112
Some(self.lower_block(els, false))

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,36 @@ impl<'hir> LoweringContext<'_, 'hir> {
3131

3232
pub(super) fn lower_expr_mut(&mut self, e: &Expr) -> hir::Expr<'hir> {
3333
ensure_sufficient_stack(|| {
34+
// Desugar `ExprForLoop`
35+
// from: `[opt_ident]: for <pat> in <head> <body>`
36+
if let ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) = e.kind {
37+
return self.lower_expr_for(e, pat, head, body, opt_label);
38+
}
39+
40+
if let ExprKind::Paren(ref ex) = e.kind {
41+
let mut ex = self.lower_expr_mut(ex);
42+
// Include parens in span, but only if it is a super-span.
43+
if e.span.contains(ex.span) {
44+
ex.span = self.lower_span(e.span);
45+
}
46+
// Merge attributes into the inner expression.
47+
if !e.attrs.is_empty() {
48+
let old_attrs =
49+
self.attrs.get(&ex.hir_id.local_id).map(|la| *la).unwrap_or(&[]);
50+
self.attrs.insert(
51+
ex.hir_id.local_id,
52+
&*self.arena.alloc_from_iter(
53+
e.attrs
54+
.iter()
55+
.map(|a| self.lower_attr(a))
56+
.chain(old_attrs.iter().cloned()),
57+
),
58+
);
59+
}
60+
return ex;
61+
}
62+
63+
let hir_id = self.lower_node_id(e.id);
3464
let kind = match e.kind {
3565
ExprKind::Box(ref inner) => hir::ExprKind::Box(self.lower_expr(inner)),
3666
ExprKind::Array(ref exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)),
@@ -48,7 +78,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
4878
if e.attrs.get(0).map_or(false, |a| a.has_name(sym::rustc_box)) {
4979
if let [inner] = &args[..] && e.attrs.len() == 1 {
5080
let kind = hir::ExprKind::Box(self.lower_expr(&inner));
51-
let hir_id = self.lower_node_id(e.id);
5281
return hir::Expr { hir_id, kind, span: self.lower_span(e.span) };
5382
} else {
5483
self.tcx.sess.emit_err(RustcBoxAttributeError { span: e.span });
@@ -281,38 +310,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
281310
ExprKind::Yield(ref opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()),
282311
ExprKind::Err => hir::ExprKind::Err,
283312
ExprKind::Try(ref sub_expr) => self.lower_expr_try(e.span, sub_expr),
284-
ExprKind::Paren(ref ex) => {
285-
let mut ex = self.lower_expr_mut(ex);
286-
// Include parens in span, but only if it is a super-span.
287-
if e.span.contains(ex.span) {
288-
ex.span = self.lower_span(e.span);
289-
}
290-
// Merge attributes into the inner expression.
291-
if !e.attrs.is_empty() {
292-
let old_attrs =
293-
self.attrs.get(&ex.hir_id.local_id).map(|la| *la).unwrap_or(&[]);
294-
self.attrs.insert(
295-
ex.hir_id.local_id,
296-
&*self.arena.alloc_from_iter(
297-
e.attrs
298-
.iter()
299-
.map(|a| self.lower_attr(a))
300-
.chain(old_attrs.iter().cloned()),
301-
),
302-
);
303-
}
304-
return ex;
305-
}
306-
307-
// Desugar `ExprForLoop`
308-
// from: `[opt_ident]: for <pat> in <head> <body>`
309-
ExprKind::ForLoop(ref pat, ref head, ref body, opt_label) => {
310-
return self.lower_expr_for(e, pat, head, body, opt_label);
311-
}
312-
ExprKind::MacCall(_) => panic!("{:?} shouldn't exist here", e.span),
313+
ExprKind::MacCall(_) | _ => panic!("{:?} shouldn't exist here", e.span),
313314
};
314315

315-
let hir_id = self.lower_node_id(e.id);
316+
//let hir_id = self.lower_node_id(e.id);
316317
self.lower_attrs(hir_id, &e.attrs);
317318
hir::Expr { hir_id, kind, span: self.lower_span(e.span) }
318319
})
@@ -480,8 +481,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
480481
let lowered_cond = self.with_loop_condition_scope(|t| t.lower_cond(cond));
481482
let then = self.lower_block_expr(body);
482483
let expr_break = self.expr_break(span, AttrVec::new());
483-
let stmt_break = self.stmt_expr(span, expr_break);
484-
let else_blk = self.block_all(span, arena_vec![self; stmt_break], None);
485484
let else_expr = self.arena.alloc(self.expr_block(else_blk, AttrVec::new()));
486485
let if_kind = hir::ExprKind::If(lowered_cond, self.arena.alloc(then), Some(else_expr));
487486
let if_expr = self.expr(span, if_kind, AttrVec::new());
@@ -1495,8 +1494,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
14951494
// Some(<pat>) => <body>,
14961495
let some_arm = {
14971496
let some_pat = self.pat_some(pat_span, pat);
1498-
let body_block = self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1499-
let body_expr = self.arena.alloc(self.expr_block(body_block, AttrVec::new()));
1497+
//let body_block = || self.with_loop_scope(e.id, |this| this.lower_block(body, false));
1498+
let body_expr = self.arena.alloc(self.expr_block(
1499+
|this| this.with_loop_scope(e.id, |this| this.lower_block(body, false)),
1500+
AttrVec::new(),
1501+
));
15001502
self.arm(some_pat, body_expr)
15011503
};
15021504

@@ -1881,17 +1883,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
18811883
}
18821884

18831885
fn expr_block_empty(&mut self, span: Span) -> &'hir hir::Expr<'hir> {
1884-
let blk = self.block_all(span, &[], None);
1885-
let expr = self.expr_block(blk, AttrVec::new());
1886+
//let blk = || self.block_all(span, &[], None);
1887+
let expr = self.expr_block(|this| this.block_all(span, &[], None), AttrVec::new());
18861888
self.arena.alloc(expr)
18871889
}
18881890

18891891
pub(super) fn expr_block(
18901892
&mut self,
1891-
b: &'hir hir::Block<'hir>,
1893+
//b: &'hir hir::Block<'hir>,
1894+
gen_block: impl FnOnce(&mut Self) -> &'hir hir::Block<'hir>,
18921895
attrs: AttrVec,
18931896
) -> hir::Expr<'hir> {
1894-
self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
1897+
let hir_id = self.next_id();
1898+
debug!("yuakng expr hir_id: {:?}", hir_id);
1899+
self.lower_attrs(hir_id, &attrs);
1900+
let b = gen_block(self);
1901+
hir::Expr { hir_id, kind: hir::ExprKind::Block(b, None), span: self.lower_span(b.span) }
1902+
//self.expr(b.span, hir::ExprKind::Block(b, None), attrs)
18951903
}
18961904

18971905
pub(super) fn expr(
@@ -1900,7 +1908,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
19001908
kind: hir::ExprKind<'hir>,
19011909
attrs: AttrVec,
19021910
) -> hir::Expr<'hir> {
1911+
debug!("yukang expr({:?}, {:?})", span, kind);
19031912
let hir_id = self.next_id();
1913+
debug!("yuakng expr hir_id: {:?}", hir_id);
19041914
self.lower_attrs(hir_id, &attrs);
19051915
hir::Expr { hir_id, kind, span: self.lower_span(span) }
19061916
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
270270
let asyncness = header.asyncness;
271271
let body_id =
272272
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
273-
273+
debug!("yukang finished lower_maybe_async_body");
274274
let mut itctx = ImplTraitContext::Universal;
275275
let (generics, decl) = this.lower_generics(generics, id, &mut itctx, |this| {
276276
let ret_id = asyncness.opt_return_id();
@@ -751,6 +751,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
751751
}
752752

753753
fn lower_field_def(&mut self, (index, f): (usize, &FieldDef)) -> hir::FieldDef<'hir> {
754+
let hir_id = self.lower_node_id(f.id);
754755
let ty = if let TyKind::Path(ref qself, ref path) = f.ty.kind {
755756
let t = self.lower_path_ty(
756757
&f.ty,
@@ -763,7 +764,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
763764
} else {
764765
self.lower_ty(&f.ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type))
765766
};
766-
let hir_id = self.lower_node_id(f.id);
767767
self.lower_attrs(hir_id, &f.attrs);
768768
hir::FieldDef {
769769
span: self.lower_span(f.span),
@@ -882,6 +882,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
882882

883883
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
884884
// Since `default impl` is not yet implemented, this is always true in impls.
885+
let hir_id = self.lower_node_id(i.id);
885886
let has_value = true;
886887
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
887888

@@ -930,7 +931,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
930931
AssocItemKind::MacCall(..) => panic!("`TyMac` should have been expanded by now"),
931932
};
932933

933-
let hir_id = self.lower_node_id(i.id);
934934
self.lower_attrs(hir_id, &i.attrs);
935935
let item = hir::ImplItem {
936936
owner_id: hir_id.expect_owner(),
@@ -1072,7 +1072,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10721072
(Async::Yes { closure_id, .. }, Some(body)) => (closure_id, body),
10731073
_ => return self.lower_fn_body_block(span, decl, body),
10741074
};
1075-
1075+
debug!("yukang continue Lower");
10761076
self.lower_body(|this| {
10771077
let mut parameters: Vec<hir::Param<'_>> = Vec::new();
10781078
let mut statements: Vec<hir::Stmt<'_>> = Vec::new();
@@ -1240,13 +1240,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
12401240
// drop-temps { <user-body> }
12411241
// }
12421242
// ```
1243-
let body = this.block_all(
1243+
/* let body = this.block_all(
12441244
desugared_span,
12451245
this.arena.alloc_from_iter(statements),
12461246
Some(user_body),
1247-
);
1247+
); */
12481248

1249-
this.expr_block(body, AttrVec::new())
1249+
this.expr_block(|this| {
1250+
this.block_all(
1251+
desugared_span,
1252+
this.arena.alloc_from_iter(statements),
1253+
Some(user_body),
1254+
)
1255+
}, AttrVec::new())
12501256
},
12511257
);
12521258

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,8 +2326,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23262326
/// Lowers a block directly to an expression, presuming that it
23272327
/// has no attributes and is not targeted by a `break`.
23282328
fn lower_block_expr(&mut self, b: &Block) -> hir::Expr<'hir> {
2329-
let block = self.lower_block(b, false);
2330-
self.expr_block(block, AttrVec::new())
2329+
//let block = || self.lower_block(b, false);
2330+
self.expr_block(|this| this.lower_block(b, false), AttrVec::new())
23312331
}
23322332

23332333
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen {

compiler/rustc_passes/src/hir_id_validator.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
8585
.filter(|&i| !self.hir_ids_seen.contains(ItemLocalId::from_u32(i)))
8686
.collect();
8787

88+
//span_bug!(self.hir_map.span(), "{}\nchild_span:{:?}", message, child_span);
89+
println!("missing ids number: {:?}", missing.len());
90+
println!("missing ids: {:?}", missing);
91+
8892
// Try to map those to something more useful
8993
let mut missing_items = Vec::with_capacity(missing.len());
9094

@@ -143,16 +147,16 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
143147
});
144148
}
145149

150+
let debug_log = std::env::var("RUSTC_LOG");
146151
if let Some(owner_hir_id) = self.hir_map.find_parent_node(hir_id) &&
147-
owner_hir_id.local_id >= hir_id.local_id && hir_id.local_id != ItemLocalId::from_u32(0) {
148-
self.error(|| {
149-
format!(
150-
"HirIdValidator: The local_id {} 's parent local_id {} is not smaller than children",
151-
self.hir_map.node_to_string(hir_id),
152-
self.hir_map.node_to_string(owner_hir_id),
153-
)
154-
});
152+
owner_hir_id.local_id >= hir_id.local_id && hir_id.local_id != ItemLocalId::from_u32(0) &&
153+
debug_log.is_ok() {
154+
let message = format!("HirIdValidator: The parent local_id `{}` is not smaller than children id: {:?}",
155+
self.hir_map.node_to_string(owner_hir_id), hir_id.local_id);
156+
let child_span = self.hir_map.span(hir_id);
157+
span_bug!(self.hir_map.span(owner_hir_id), "{}\nchild_span:{:?}", message, child_span);
155158
}
159+
156160
self.hir_ids_seen.insert(hir_id.local_id);
157161
}
158162

0 commit comments

Comments
 (0)