Skip to content

Commit acabd7c

Browse files
committed
Add duplicate lowering check
1 parent 2b28424 commit acabd7c

File tree

6 files changed

+38
-38
lines changed

6 files changed

+38
-38
lines changed

compiler/rustc_ast_lowering/src/block.rs

-9
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
1414
self.arena.alloc(self.lower_block_noalloc(hir_id, b, targeted_by_break))
1515
}
1616

17-
pub(super) fn lower_block_with_hir_id(
18-
&mut self,
19-
b: &Block,
20-
hir_id: hir::HirId,
21-
targeted_by_break: bool,
22-
) -> &'hir hir::Block<'hir> {
23-
self.arena.alloc(self.lower_block_noalloc(hir_id, b, targeted_by_break))
24-
}
25-
2617
pub(super) fn lower_block_noalloc(
2718
&mut self,
2819
hir_id: hir::HirId,

compiler/rustc_ast_lowering/src/delegation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
260260
};
261261
self_resolver.visit_block(block);
262262
// Target expr needs to lower `self` path.
263-
this.ident_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
263+
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
264264
this.lower_target_expr(&block)
265265
} else {
266266
this.generate_arg(param.pat.hir_id, span)

compiler/rustc_ast_lowering/src/expr.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
261261
// expr node id.
262262
let block_hir_id = self.lower_node_id(blk.id);
263263
let opt_label = self.lower_label(*opt_label, blk.id, block_hir_id);
264-
hir::ExprKind::Block(
265-
self.lower_block_with_hir_id(blk, block_hir_id, opt_label.is_some()),
266-
opt_label,
267-
)
264+
let hir_block = self.arena.alloc(self.lower_block_noalloc(
265+
block_hir_id,
266+
blk,
267+
opt_label.is_some(),
268+
));
269+
hir::ExprKind::Block(hir_block, opt_label)
268270
}
269271
ExprKind::Assign(el, er, span) => self.lower_expr_assign(el, er, *span, e.span),
270272
ExprKind::AssignOp(op, el, er) => hir::ExprKind::AssignOp(
@@ -1483,15 +1485,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
14831485
dest_hir_id: hir::HirId,
14841486
) -> Option<Label> {
14851487
let label = opt_label?;
1486-
self.labelled_node_id_to_local_id.insert(dest_id, dest_hir_id.local_id);
1488+
self.ident_and_label_to_local_id.insert(dest_id, dest_hir_id.local_id);
14871489
Some(Label { ident: self.lower_ident(label.ident) })
14881490
}
14891491

14901492
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
14911493
let target_id = match destination {
14921494
Some((id, _)) => {
14931495
if let Some(loop_id) = self.resolver.get_label_res(id) {
1494-
let local_id = self.labelled_node_id_to_local_id[&loop_id];
1496+
let local_id = self.ident_and_label_to_local_id[&loop_id];
14951497
let loop_hir_id = HirId { owner: self.current_hir_id_owner, local_id };
14961498
Ok(loop_hir_id)
14971499
} else {

compiler/rustc_ast_lowering/src/item.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
154154
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
155155
let mut ident = i.ident;
156156
let vis_span = self.lower_span(i.vis.span);
157-
let hir_id =
158-
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
157+
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
159158
let attrs = self.lower_attrs(hir_id, &i.attrs);
160159
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind);
161160
let item = hir::Item {
@@ -659,8 +658,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
659658
}
660659

661660
fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
662-
let hir_id =
663-
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
661+
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
664662
let owner_id = hir_id.expect_owner();
665663
self.lower_attrs(hir_id, &i.attrs);
666664
let item = hir::ForeignItem {
@@ -788,8 +786,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
788786
i: &AssocItem,
789787
trait_constness: Const,
790788
) -> &'hir hir::TraitItem<'hir> {
791-
let hir_id =
792-
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
789+
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
793790
self.lower_attrs(hir_id, &i.attrs);
794791
let trait_item_def_id = hir_id.expect_owner();
795792

@@ -929,8 +926,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
929926
// Since `default impl` is not yet implemented, this is always true in impls.
930927
let has_value = true;
931928
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
932-
let hir_id =
933-
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
929+
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
934930
self.lower_attrs(hir_id, &i.attrs);
935931

936932
let (generics, kind) = match &i.kind {

compiler/rustc_ast_lowering/src/lib.rs

+23-12
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ struct LoweringContext<'a, 'hir> {
138138
impl_trait_defs: Vec<hir::GenericParam<'hir>>,
139139
impl_trait_bounds: Vec<hir::WherePredicate<'hir>>,
140140

141-
/// NodeIds of labelled nodes that are lowered inside the current HIR owner.
142-
labelled_node_id_to_local_id: NodeMap<hir::ItemLocalId>,
143-
/// NodeIds of identifier that are lowered inside the current HIR owner.
144-
ident_to_local_id: NodeMap<hir::ItemLocalId>,
141+
/// NodeIds of pattern identifiers and labelled nodes that are lowered inside the current HIR owner.
142+
ident_and_label_to_local_id: NodeMap<hir::ItemLocalId>,
143+
/// NodeIds that are lowered inside the current HIR owner. Only used for duplicate lowering check.
144+
node_id_to_local_id: NodeMap<hir::ItemLocalId>,
145145

146146
allow_try_trait: Lrc<[Symbol]>,
147147
allow_gen_future: Lrc<[Symbol]>,
@@ -178,8 +178,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
178178
current_hir_id_owner: hir::CRATE_OWNER_ID,
179179
current_def_id_parent: CRATE_DEF_ID,
180180
item_local_id_counter: hir::ItemLocalId::ZERO,
181-
labelled_node_id_to_local_id: Default::default(),
182-
ident_to_local_id: Default::default(),
181+
ident_and_label_to_local_id: Default::default(),
182+
node_id_to_local_id: Default::default(),
183183
trait_map: Default::default(),
184184

185185
// Lowering state.
@@ -589,9 +589,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
589589

590590
let current_attrs = std::mem::take(&mut self.attrs);
591591
let current_bodies = std::mem::take(&mut self.bodies);
592-
let current_labelled_node_id_to_local_id =
593-
std::mem::take(&mut self.labelled_node_id_to_local_id);
594-
let current_ident_to_local_id = std::mem::take(&mut self.ident_to_local_id);
592+
let current_ident_and_label_to_local_id =
593+
std::mem::take(&mut self.ident_and_label_to_local_id);
594+
let current_node_id_to_local_id = std::mem::take(&mut self.node_id_to_local_id);
595595
let current_trait_map = std::mem::take(&mut self.trait_map);
596596
let current_owner =
597597
std::mem::replace(&mut self.current_hir_id_owner, hir::OwnerId { def_id });
@@ -604,6 +604,10 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
604604
// we want `f` to be able to refer to the `LocalDefId`s that the caller created.
605605
// and the caller to refer to some of the subdefinitions' nodes' `LocalDefId`s.
606606

607+
// Always allocate the first `HirId` for the owner itself.
608+
let _old = self.node_id_to_local_id.insert(owner, hir::ItemLocalId::ZERO);
609+
debug_assert_eq!(_old, None);
610+
607611
let item = self.with_def_id_parent(def_id, f);
608612
debug_assert_eq!(def_id, item.def_id().def_id);
609613
// `f` should have consumed all the elements in these vectors when constructing `item`.
@@ -613,8 +617,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
613617

614618
self.attrs = current_attrs;
615619
self.bodies = current_bodies;
616-
self.labelled_node_id_to_local_id = current_labelled_node_id_to_local_id;
617-
self.ident_to_local_id = current_ident_to_local_id;
620+
self.ident_and_label_to_local_id = current_ident_and_label_to_local_id;
621+
self.node_id_to_local_id = current_node_id_to_local_id;
618622
self.trait_map = current_trait_map;
619623
self.current_hir_id_owner = current_owner;
620624
self.item_local_id_counter = current_local_counter;
@@ -703,6 +707,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
703707
self.trait_map.insert(hir_id.local_id, traits.into_boxed_slice());
704708
}
705709

710+
// Check whether the same `NodeId` is lowered more than once.
711+
#[cfg(debug_assertions)]
712+
{
713+
let old = self.node_id_to_local_id.insert(ast_node_id, local_id);
714+
assert_eq!(old, None);
715+
}
716+
706717
hir_id
707718
}
708719

@@ -720,7 +731,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
720731
fn lower_res(&mut self, res: Res<NodeId>) -> Res {
721732
let res: Result<Res, ()> = res.apply_id(|id| {
722733
let owner = self.current_hir_id_owner;
723-
let local_id = self.ident_to_local_id.get(&id).copied().ok_or(())?;
734+
let local_id = self.ident_and_label_to_local_id.get(&id).copied().ok_or(())?;
724735
Ok(HirId { owner, local_id })
725736
});
726737
trace!(?res);

compiler/rustc_ast_lowering/src/pat.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
267267
};
268268
// All identifiers resolves to this canonical identifier share its `HirId`.
269269
let binding_id = if canonical_id == p.id {
270-
self.ident_to_local_id.insert(canonical_id, hir_id.local_id);
270+
self.ident_and_label_to_local_id.insert(canonical_id, hir_id.local_id);
271271
hir_id
272272
} else {
273273
hir::HirId {
274274
owner: self.current_hir_id_owner,
275-
local_id: self.ident_to_local_id[&canonical_id],
275+
local_id: self.ident_and_label_to_local_id[&canonical_id],
276276
}
277277
};
278278
hir::PatKind::Binding(

0 commit comments

Comments
 (0)