Skip to content

Commit e5b5e06

Browse files
committed
fix bug
1 parent 885ca2b commit e5b5e06

File tree

7 files changed

+51
-48
lines changed

7 files changed

+51
-48
lines changed

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
5252
stmts.extend(self.lower_item_ref(it).into_iter().enumerate().map(
5353
|(i, item_id)| {
5454
let hir_id = match i {
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-
}
55+
0 => self.lower_node_id(s.id),
56+
_ => self.next_id(),
6757
};
6858
let kind = hir::StmtKind::Item(item_id);
6959
let span = self.lower_span(s.span);

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
592592
async_gen_kind: hir::AsyncGeneratorKind,
593593
body: impl FnOnce(&mut Self) -> hir::Expr<'hir>,
594594
) -> hir::ExprKind<'hir> {
595+
let hir_id = self.lower_node_id(closure_node_id);
596+
595597
let output = match ret_ty {
596598
Some(ty) => hir::FnRetTy::Return(
597599
self.lower_ty(&ty, &ImplTraitContext::Disallowed(ImplTraitPosition::AsyncBlock)),
@@ -1476,6 +1478,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14761478
body: &Block,
14771479
opt_label: Option<Label>,
14781480
) -> hir::Expr<'hir> {
1481+
let hir_id = self.lower_node_id(e.id);
14791482
let head = self.lower_expr_mut(head);
14801483
let pat = self.lower_pat(pat);
14811484
let for_span =
@@ -1532,8 +1535,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15321535
hir::LoopSource::ForLoop,
15331536
self.lower_span(for_span.with_hi(head.span.hi())),
15341537
);
1535-
let loop_expr =
1536-
self.arena.alloc(hir::Expr { hir_id: self.lower_node_id(e.id), kind, span: for_span });
1538+
let loop_expr = self.arena.alloc(hir::Expr { hir_id, kind, span: for_span });
15371539

15381540
// `mut iter => { ... }`
15391541
let iter_arm = self.arm(iter_pat, loop_expr);

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,19 +1240,22 @@ 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),
12471247
); */
12481248

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())
1249+
this.expr_block(
1250+
|this| {
1251+
this.block_all(
1252+
desugared_span,
1253+
this.arena.alloc_from_iter(statements),
1254+
Some(user_body),
1255+
)
1256+
},
1257+
AttrVec::new(),
1258+
)
12561259
},
12571260
);
12581261

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10001000
itctx: &ImplTraitContext,
10011001
) -> hir::TypeBinding<'hir> {
10021002
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", constraint, itctx);
1003+
let hir_id = self.lower_node_id(constraint.id);
10031004
// lower generic arguments of identifier in constraint
10041005
let gen_args = if let Some(ref gen_args) = constraint.gen_args {
10051006
let gen_args_ctor = match gen_args {
@@ -1095,7 +1096,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10951096
};
10961097

10971098
hir::TypeBinding {
1098-
hir_id: self.lower_node_id(constraint.id),
1099+
hir_id,
10991100
ident: self.lower_ident(constraint.ident),
11001101
gen_args,
11011102
kind,
@@ -1222,6 +1223,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12221223
&& let Some(partial_res) = self.resolver.get_partial_res(t.id)
12231224
&& let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = partial_res.full_res()
12241225
{
1226+
let hir_id = self.next_id();
12251227
let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| {
12261228
let poly_trait_ref = this.ast_arena.ptr.alloc(PolyTraitRef {
12271229
bound_generic_params: vec![],
@@ -1237,7 +1239,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12371239
(bounds, lifetime_bound)
12381240
});
12391241
let kind = hir::TyKind::TraitObject(bounds, &lifetime_bound, TraitObjectSyntax::None);
1240-
return hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.next_id() };
1242+
return hir::Ty { kind, span: self.lower_span(t.span), hir_id };
12411243
}
12421244

12431245
let id = self.lower_node_id(t.id);
@@ -1254,6 +1256,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12541256
}
12551257

12561258
fn lower_ty_direct(&mut self, t: &Ty, itctx: &ImplTraitContext) -> hir::Ty<'hir> {
1259+
if let TyKind::Paren(ref ty) = t.kind {
1260+
return self.lower_ty_direct(ty, itctx);
1261+
}
1262+
if let TyKind::Path(ref qself, ref path) = t.kind {
1263+
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
1264+
}
1265+
// alloc hir_id
1266+
let hir_id = self.lower_node_id(t.id);
12571267
let kind = match t.kind {
12581268
TyKind::Infer => hir::TyKind::Infer,
12591269
TyKind::Err => hir::TyKind::Err,
@@ -1289,12 +1299,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
12891299
TyKind::Tup(ref tys) => hir::TyKind::Tup(
12901300
self.arena.alloc_from_iter(tys.iter().map(|ty| self.lower_ty_direct(ty, itctx))),
12911301
),
1292-
TyKind::Paren(ref ty) => {
1293-
return self.lower_ty_direct(ty, itctx);
1294-
}
1295-
TyKind::Path(ref qself, ref path) => {
1296-
return self.lower_path_ty(t, qself, path, ParamMode::Explicit, itctx);
1297-
}
12981302
TyKind::ImplicitSelf => {
12991303
let hir_id = self.next_id();
13001304
let res = self.expect_full_res(t.id);
@@ -1414,9 +1418,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14141418
);
14151419
hir::TyKind::Err
14161420
}
1421+
_ => panic!("unexpected type: {:?}", t),
14171422
};
14181423

1419-
hir::Ty { kind, span: self.lower_span(t.span), hir_id: self.lower_node_id(t.id) }
1424+
hir::Ty { kind, span: self.lower_span(t.span), hir_id: hir_id }
14201425
}
14211426

14221427
/// Lowers a `ReturnPositionOpaqueTy` (`-> impl Trait`) or a `TypeAliasesOpaqueTy` (`type F =
@@ -2069,6 +2074,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20692074
span: Span,
20702075
mut nested_impl_trait_context: ImplTraitContext,
20712076
) -> hir::GenericBound<'hir> {
2077+
let hir_id = self.next_id();
20722078
// Compute the `T` in `Future<Output = T>` from the return type.
20732079
let output_ty = match output {
20742080
FnRetTy::Ty(ty) => {
@@ -2092,7 +2098,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
20922098
// ::std::future::Future<future_params>
20932099
hir::LangItem::Future,
20942100
self.lower_span(span),
2095-
self.next_id(),
2101+
hir_id,
20962102
future_args,
20972103
)
20982104
}
@@ -2128,6 +2134,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21282134
ident: Ident,
21292135
res: LifetimeRes,
21302136
) -> &'hir hir::Lifetime {
2137+
let hir_id = self.lower_node_id(id);
21312138
let name = match res {
21322139
LifetimeRes::Param { param, .. } => {
21332140
let p_name = ParamName::Plain(ident);
@@ -2148,11 +2155,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21482155
};
21492156

21502157
debug!(?name);
2151-
self.arena.alloc(hir::Lifetime {
2152-
hir_id: self.lower_node_id(id),
2153-
span: self.lower_span(span),
2154-
name,
2155-
})
2158+
self.arena.alloc(hir::Lifetime { hir_id, span: self.lower_span(span), name })
21562159
}
21572160

21582161
#[instrument(level = "debug", skip(self))]
@@ -2180,9 +2183,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
21802183

21812184
#[instrument(level = "trace", skip(self))]
21822185
fn lower_generic_param(&mut self, param: &GenericParam) -> hir::GenericParam<'hir> {
2186+
let hir_id = self.lower_node_id(param.id);
21832187
let (name, kind) = self.lower_generic_param_kind(param);
21842188

2185-
let hir_id = self.lower_node_id(param.id);
21862189
self.lower_attrs(hir_id, &param.attrs);
21872190
hir::GenericParam {
21882191
hir_id,
@@ -2237,11 +2240,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22372240
}
22382241

22392242
fn lower_trait_ref(&mut self, p: &TraitRef, itctx: &ImplTraitContext) -> hir::TraitRef<'hir> {
2243+
let hir_id = self.lower_node_id(p.ref_id);
22402244
let path = match self.lower_qpath(p.ref_id, &None, &p.path, ParamMode::Explicit, itctx) {
22412245
hir::QPath::Resolved(None, path) => path,
22422246
qpath => panic!("lower_trait_ref: unexpected QPath `{:?}`", qpath),
22432247
};
2244-
hir::TraitRef { path, hir_ref_id: self.lower_node_id(p.ref_id) }
2248+
hir::TraitRef { path, hir_ref_id: hir_id }
22452249
}
22462250

22472251
#[instrument(level = "debug", skip(self))]

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
185185
itctx: &ImplTraitContext,
186186
) -> hir::PathSegment<'hir> {
187187
debug!("path_span: {:?}, lower_path_segment(segment: {:?})", path_span, segment,);
188+
let hir_id = self.lower_node_id(segment.id);
188189
let (mut generic_args, infer_args) = if let Some(ref generic_args) = segment.args {
189190
match **generic_args {
190191
GenericArgs::AngleBracketed(ref data) => {
@@ -254,7 +255,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
254255
}
255256

256257
let res = self.expect_full_res(segment.id);
257-
let hir_id = self.lower_node_id(segment.id);
258258
debug!(
259259
"lower_path_segment: ident={:?} original-id={:?} new-id={:?}",
260260
segment.ident, segment.id, hir_id,

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ impl<'hir> Map<'hir> {
302302
Some(self.tcx.hir_owner_parent(id.owner))
303303
} else {
304304
let owner = self.tcx.hir_owner_nodes(id.owner).as_owner()?;
305+
if usize::from(id.local_id) >= owner.nodes.len() {
306+
return None;
307+
}
305308
let node = owner.nodes[id.local_id].as_ref()?;
306309
let hir_id = HirId { owner: id.owner, local_id: node.parent };
307310
// HIR indexing should have checked that.

compiler/rustc_passes/src/hir_id_validator.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,15 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
147147
});
148148
}
149149

150-
let debug_log = std::env::var("RUSTC_LOG");
151-
if let Some(owner_hir_id) = self.hir_map.find_parent_node(hir_id) &&
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);
150+
let debug_log = std::env::var("RUSTC_LOG_HIR");
151+
if debug_log.is_ok() {
152+
if let Some(owner_hir_id) = self.hir_map.find_parent_node(hir_id) &&
153+
owner_hir_id.local_id >= hir_id.local_id && hir_id.local_id != ItemLocalId::from_u32(0) {
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);
158+
}
158159
}
159160

160161
self.hir_ids_seen.insert(hir_id.local_id);

0 commit comments

Comments
 (0)