Skip to content

Commit bf02483

Browse files
committed
more on LocalItemId
1 parent e5b5e06 commit bf02483

File tree

9 files changed

+139
-107
lines changed

9 files changed

+139
-107
lines changed

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9191
}
9292

9393
fn lower_local(&mut self, l: &Local) -> &'hir hir::Local<'hir> {
94+
let hir_id = self.lower_node_id(l.id);
9495
let ty = l
9596
.ty
9697
.as_ref()
9798
.map(|t| self.lower_ty(t, &ImplTraitContext::Disallowed(ImplTraitPosition::Variable)));
98-
let hir_id = self.lower_node_id(l.id);
9999
let init = l.kind.init().map(|init| self.lower_expr(init));
100100
let pat = self.lower_pat(&l.pat);
101101
let els = if let LocalKind::InitElse(_, els) = &l.kind {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
549549
}
550550

551551
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm<'hir> {
552+
let hir_id = self.next_id();
552553
let pat = self.lower_pat(&arm.pat);
553554
let guard = arm.guard.as_ref().map(|cond| {
554555
if let ExprKind::Let(ref pat, ref scrutinee, span) = cond.kind {
@@ -563,7 +564,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
563564
hir::Guard::If(self.lower_expr(cond))
564565
}
565566
});
566-
let hir_id = self.next_id();
567+
567568
self.lower_attrs(hir_id, &arm.attrs);
568569
hir::Arm {
569570
hir_id,
@@ -1220,9 +1221,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
12201221
// Structs.
12211222
ExprKind::Struct(se) => {
12221223
let field_pats = self.arena.alloc_from_iter(se.fields.iter().map(|f| {
1224+
let hir_id = self.next_id();
12231225
let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments);
12241226
hir::PatField {
1225-
hir_id: self.next_id(),
1227+
hir_id,
12261228
ident: self.lower_ident(f.ident),
12271229
pat,
12281230
is_shorthand: f.is_shorthand,

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
7070
debug_assert_eq!(self.owner, hir_id.owner);
7171
debug_assert_ne!(hir_id.local_id.as_u32(), 0);
7272
debug_assert_ne!(hir_id.local_id, self.parent_node);
73+
let debug_log = std::env::var("RUSTC_HIR").is_ok();
74+
if debug_log {
75+
if hir_id.local_id < self.parent_node {
76+
let message = format!(
77+
"HirIdValidator: The parent local_id: {:?} is not smaller than children id: {:?}",
78+
self.parent_node, hir_id.local_id
79+
);
80+
span_bug!(rustc_span::DUMMY_SP, "error NodeCollector.insert: {}", message);
81+
}
82+
}
7383

7484
// Make sure that the DepNode of some node coincides with the HirId
7585
// owner of that node.
@@ -89,7 +99,7 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
8999
}
90100
}
91101

92-
self.nodes.insert(hir_id.local_id, ParentedNode { parent: self.parent_node, node: node });
102+
self.nodes.insert(hir_id.local_id, ParentedNode { parent: self.parent_node, node });
93103
}
94104

95105
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,21 +1486,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
14861486
match kind {
14871487
GenericParamKind::Const { .. } => None,
14881488
GenericParamKind::Type { .. } => {
1489-
let def_id = self.local_def_id(id).to_def_id();
14901489
let hir_id = self.next_id();
1490+
let def_id = self.local_def_id(id).to_def_id();
1491+
let path_id = self.next_id();
14911492
let res = Res::Def(DefKind::TyParam, def_id);
14921493
let ty_path = self.arena.alloc(hir::Path {
14931494
span: param_span,
14941495
res,
14951496
segments: self
14961497
.arena
1497-
.alloc_from_iter([hir::PathSegment::new(ident, hir_id, res)]),
1498+
.alloc_from_iter([hir::PathSegment::new(ident, path_id, res)]),
14981499
});
14991500
let ty_id = self.next_id();
15001501
let bounded_ty =
15011502
self.ty_path(ty_id, param_span, hir::QPath::Resolved(None, ty_path));
15021503
Some(hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
1503-
hir_id: self.next_id(),
1504+
hir_id,
15041505
bounded_ty: self.arena.alloc(bounded_ty),
15051506
bounds,
15061507
span,
@@ -1509,10 +1510,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
15091510
}))
15101511
}
15111512
GenericParamKind::Lifetime => {
1512-
let ident_span = self.lower_span(ident.span);
1513-
let ident = self.lower_ident(ident);
15141513
let lt_id = self.next_node_id();
1515-
let lifetime = self.new_named_lifetime(id, lt_id, ident_span, ident);
1514+
let lifetime = self.new_named_lifetime(
1515+
id,
1516+
lt_id,
1517+
self.lower_span(ident.span),
1518+
self.lower_ident(ident),
1519+
);
15161520
Some(hir::WherePredicate::RegionPredicate(hir::WhereRegionPredicate {
15171521
lifetime,
15181522
span,

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 92 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -21,94 +21,95 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
2121
pub(crate) fn lower_pat_mut(&mut self, mut pattern: &Pat) -> hir::Pat<'hir> {
2222
ensure_sufficient_stack(|| {
2323
// loop here to avoid recursion
24-
let node = loop {
25-
match pattern.kind {
26-
PatKind::Wild => break hir::PatKind::Wild,
27-
PatKind::Ident(binding_mode, ident, ref sub) => {
28-
let lower_sub = |this: &mut Self| sub.as_ref().map(|s| this.lower_pat(&*s));
29-
break self.lower_pat_ident(pattern, binding_mode, ident, lower_sub);
30-
}
31-
PatKind::Lit(ref e) => {
32-
break hir::PatKind::Lit(self.lower_expr_within_pat(e, false));
33-
}
34-
PatKind::TupleStruct(ref qself, ref path, ref pats) => {
35-
let qpath = self.lower_qpath(
36-
pattern.id,
37-
qself,
38-
path,
39-
ParamMode::Optional,
40-
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
41-
);
42-
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple struct");
43-
break hir::PatKind::TupleStruct(qpath, pats, ddpos);
44-
}
45-
PatKind::Or(ref pats) => {
46-
break hir::PatKind::Or(
47-
self.arena.alloc_from_iter(pats.iter().map(|x| self.lower_pat_mut(x))),
48-
);
49-
}
50-
PatKind::Path(ref qself, ref path) => {
51-
let qpath = self.lower_qpath(
52-
pattern.id,
53-
qself,
54-
path,
55-
ParamMode::Optional,
56-
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
57-
);
58-
break hir::PatKind::Path(qpath);
59-
}
60-
PatKind::Struct(ref qself, ref path, ref fields, etc) => {
61-
let qpath = self.lower_qpath(
62-
pattern.id,
63-
qself,
64-
path,
65-
ParamMode::Optional,
66-
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
67-
);
6824

69-
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| {
70-
let hir_id = self.lower_node_id(f.id);
71-
self.lower_attrs(hir_id, &f.attrs);
25+
while let PatKind::Paren(ref inner) = pattern.kind {
26+
// return inner to be processed in next loop
27+
pattern = inner;
28+
}
29+
30+
let pat_hir_id = self.lower_node_id(pattern.id);
31+
let node = match pattern.kind {
32+
PatKind::Wild => hir::PatKind::Wild,
33+
PatKind::Ident(binding_mode, ident, ref sub) => {
34+
let lower_sub = |this: &mut Self| sub.as_ref().map(|s| this.lower_pat(&*s));
35+
self.lower_pat_ident(pattern, binding_mode, ident, lower_sub)
36+
}
37+
PatKind::Lit(ref e) => hir::PatKind::Lit(self.lower_expr_within_pat(e, false)),
38+
PatKind::TupleStruct(ref qself, ref path, ref pats) => {
39+
let qpath = self.lower_qpath(
40+
pattern.id,
41+
qself,
42+
path,
43+
ParamMode::Optional,
44+
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
45+
);
46+
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple struct");
47+
hir::PatKind::TupleStruct(qpath, pats, ddpos)
48+
}
49+
PatKind::Or(ref pats) => hir::PatKind::Or(
50+
self.arena.alloc_from_iter(pats.iter().map(|x| self.lower_pat_mut(x))),
51+
),
52+
PatKind::Path(ref qself, ref path) => {
53+
let qpath = self.lower_qpath(
54+
pattern.id,
55+
qself,
56+
path,
57+
ParamMode::Optional,
58+
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
59+
);
60+
hir::PatKind::Path(qpath)
61+
}
62+
PatKind::Struct(ref qself, ref path, ref fields, etc) => {
63+
let qpath = self.lower_qpath(
64+
pattern.id,
65+
qself,
66+
path,
67+
ParamMode::Optional,
68+
&mut ImplTraitContext::Disallowed(ImplTraitPosition::Path),
69+
);
70+
71+
let fs = self.arena.alloc_from_iter(fields.iter().map(|f| {
72+
let hir_id = self.lower_node_id(f.id);
73+
self.lower_attrs(hir_id, &f.attrs);
7274

73-
hir::PatField {
74-
hir_id,
75-
ident: self.lower_ident(f.ident),
76-
pat: self.lower_pat(&f.pat),
77-
is_shorthand: f.is_shorthand,
78-
span: self.lower_span(f.span),
79-
}
80-
}));
81-
break hir::PatKind::Struct(qpath, fs, etc);
82-
}
83-
PatKind::Tuple(ref pats) => {
84-
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple");
85-
break hir::PatKind::Tuple(pats, ddpos);
86-
}
87-
PatKind::Box(ref inner) => {
88-
break hir::PatKind::Box(self.lower_pat(inner));
89-
}
90-
PatKind::Ref(ref inner, mutbl) => {
91-
break hir::PatKind::Ref(self.lower_pat(inner), mutbl);
92-
}
93-
PatKind::Range(ref e1, ref e2, Spanned { node: ref end, .. }) => {
94-
break hir::PatKind::Range(
95-
e1.as_deref().map(|e| self.lower_expr_within_pat(e, true)),
96-
e2.as_deref().map(|e| self.lower_expr_within_pat(e, true)),
97-
self.lower_range_end(end, e2.is_some()),
98-
);
99-
}
100-
PatKind::Slice(ref pats) => break self.lower_pat_slice(pats),
101-
PatKind::Rest => {
102-
// If we reach here the `..` pattern is not semantically allowed.
103-
break self.ban_illegal_rest_pat(pattern.span);
104-
}
105-
// return inner to be processed in next loop
106-
PatKind::Paren(ref inner) => pattern = inner,
107-
PatKind::MacCall(_) => panic!("{:?} shouldn't exist here", pattern.span),
75+
hir::PatField {
76+
hir_id,
77+
ident: self.lower_ident(f.ident),
78+
pat: self.lower_pat(&f.pat),
79+
is_shorthand: f.is_shorthand,
80+
span: self.lower_span(f.span),
81+
}
82+
}));
83+
hir::PatKind::Struct(qpath, fs, etc)
10884
}
85+
PatKind::Tuple(ref pats) => {
86+
let (pats, ddpos) = self.lower_pat_tuple(pats, "tuple");
87+
hir::PatKind::Tuple(pats, ddpos)
88+
}
89+
PatKind::Box(ref inner) => hir::PatKind::Box(self.lower_pat(inner)),
90+
PatKind::Ref(ref inner, mutbl) => hir::PatKind::Ref(self.lower_pat(inner), mutbl),
91+
PatKind::Range(ref e1, ref e2, Spanned { node: ref end, .. }) => {
92+
hir::PatKind::Range(
93+
e1.as_deref().map(|e| self.lower_expr_within_pat(e, true)),
94+
e2.as_deref().map(|e| self.lower_expr_within_pat(e, true)),
95+
self.lower_range_end(end, e2.is_some()),
96+
)
97+
}
98+
PatKind::Slice(ref pats) => self.lower_pat_slice(pats),
99+
PatKind::Rest => {
100+
// If we reach here the `..` pattern is not semantically allowed.
101+
self.ban_illegal_rest_pat(pattern.span)
102+
}
103+
PatKind::MacCall(_) | _ => panic!("{:?} shouldn't exist here", pattern.span),
109104
};
110105

111-
self.pat_with_node_id_of(pattern, node)
106+
//self.pat_with_node_id_of(pattern, node)
107+
hir::Pat {
108+
hir_id: pat_hir_id,
109+
kind: node,
110+
span: self.lower_span(pattern.span),
111+
default_binding_modes: true,
112+
}
112113
})
113114
}
114115

@@ -176,10 +177,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
176177
let mut prev_rest_span = None;
177178

178179
// Lowers `$bm $ident @ ..` to `$bm $ident @ _`.
179-
let lower_rest_sub = |this: &mut Self, pat, ann, ident, sub| {
180+
let lower_rest_sub = |this: &mut Self, pat: &Pat, ann, ident, sub| {
181+
let hir_id = this.lower_node_id(pat.id);
180182
let lower_sub = |this: &mut Self| Some(this.pat_wild_with_node_id_of(sub));
181183
let node = this.lower_pat_ident(pat, ann, ident, lower_sub);
182-
this.pat_with_node_id_of(pat, node)
184+
hir::Pat {
185+
hir_id,
186+
kind: node,
187+
span: this.lower_span(pat.span),
188+
default_binding_modes: true,
189+
}
183190
};
184191

185192
let mut iter = pats.iter();

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
3434

3535
let path_span_lo = p.span.shrink_to_lo();
3636
let proj_start = p.segments.len() - unresolved_segments;
37+
let mut path_hir = None;
3738
let path = self.arena.alloc(hir::Path {
3839
res: self.lower_res(base_res),
3940
segments: self.arena.alloc_from_iter(p.segments[..proj_start].iter().enumerate().map(
@@ -67,6 +68,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
6768
_ => ParenthesizedGenericArgs::Err,
6869
};
6970

71+
if path_hir.is_none() && unresolved_segments > 0 {
72+
path_hir = Some(self.next_id());
73+
}
7074
self.lower_path_segment(
7175
p.span,
7276
segment,
@@ -98,8 +102,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
98102
// Otherwise, the base path is an implicit `Self` type path,
99103
// e.g., `Vec` in `Vec::new` or `<I as Iterator>::Item` in
100104
// `<I as Iterator>::Item::default`.
101-
let new_id = self.next_id();
102-
self.arena.alloc(self.ty_path(new_id, path.span, hir::QPath::Resolved(qself, path)))
105+
self.arena.alloc(self.ty_path(
106+
path_hir.unwrap(),
107+
path.span,
108+
hir::QPath::Resolved(qself, path),
109+
))
103110
};
104111

105112
// Anything after the base path are associated "extensions",
@@ -112,6 +119,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
112119
// 2. `<std::vec::Vec<T>>::IntoIter`
113120
// 3. `<<std::vec::Vec<T>>::IntoIter>::Item`
114121
// * final path is `<<<std::vec::Vec<T>>::IntoIter>::Item>::clone`
122+
let mut ret_hir_id = None;
115123
for (i, segment) in p.segments.iter().enumerate().skip(proj_start) {
116124
let hir_segment = self.arena.alloc(self.lower_path_segment(
117125
p.span,
@@ -126,9 +134,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
126134
if i == p.segments.len() - 1 {
127135
return qpath;
128136
}
137+
if ret_hir_id.is_none() {
138+
ret_hir_id = Some(self.next_id());
139+
}
129140

130141
// Wrap the associated extension in another type node.
131-
let new_id = self.next_id();
142+
let new_id =
143+
if i == p.segments.len() - 2 { ret_hir_id.unwrap() } else { self.next_id() };
132144
ty = self.arena.alloc(self.ty_path(new_id, path_span_lo.to(segment.span()), qpath));
133145
}
134146

@@ -393,6 +405,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
393405
span: Span,
394406
ty: &'hir hir::Ty<'hir>,
395407
) -> hir::TypeBinding<'hir> {
408+
let hir_id = self.next_id();
396409
let ident = Ident::with_dummy_span(hir::FN_OUTPUT_NAME);
397410
let kind = hir::TypeBindingKind::Equality { term: ty.into() };
398411
let args = arena_vec![self;];
@@ -403,12 +416,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
403416
parenthesized: false,
404417
span_ext: DUMMY_SP,
405418
});
406-
hir::TypeBinding {
407-
hir_id: self.next_id(),
408-
gen_args,
409-
span: self.lower_span(span),
410-
ident,
411-
kind,
412-
}
419+
hir::TypeBinding { hir_id, gen_args, span: self.lower_span(span), ident, kind }
413420
}
414421
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ impl<'hir> Map<'hir> {
309309
let hir_id = HirId { owner: id.owner, local_id: node.parent };
310310
// HIR indexing should have checked that.
311311
debug_assert_ne!(id.local_id, node.parent);
312+
//debug_assert!(node.parent < id.local_id); // parent's LocalItemId should be smaller
312313
Some(hir_id)
313314
}
314315
}

0 commit comments

Comments
 (0)