Skip to content

Commit 4db575a

Browse files
committed
more consistent debug_assertions
1 parent acabd7c commit 4db575a

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ struct LoweringContext<'a, 'hir> {
141141
/// NodeIds of pattern identifiers and labelled nodes that are lowered inside the current HIR owner.
142142
ident_and_label_to_local_id: NodeMap<hir::ItemLocalId>,
143143
/// NodeIds that are lowered inside the current HIR owner. Only used for duplicate lowering check.
144+
#[cfg(debug_assertions)]
144145
node_id_to_local_id: NodeMap<hir::ItemLocalId>,
145146

146147
allow_try_trait: Lrc<[Symbol]>,
@@ -179,6 +180,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
179180
current_def_id_parent: CRATE_DEF_ID,
180181
item_local_id_counter: hir::ItemLocalId::ZERO,
181182
ident_and_label_to_local_id: Default::default(),
183+
#[cfg(debug_assertions)]
182184
node_id_to_local_id: Default::default(),
183185
trait_map: Default::default(),
184186

@@ -591,6 +593,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
591593
let current_bodies = std::mem::take(&mut self.bodies);
592594
let current_ident_and_label_to_local_id =
593595
std::mem::take(&mut self.ident_and_label_to_local_id);
596+
597+
#[cfg(debug_assertions)]
594598
let current_node_id_to_local_id = std::mem::take(&mut self.node_id_to_local_id);
595599
let current_trait_map = std::mem::take(&mut self.trait_map);
596600
let current_owner =
@@ -605,8 +609,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
605609
// and the caller to refer to some of the subdefinitions' nodes' `LocalDefId`s.
606610

607611
// 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);
612+
#[cfg(debug_assertions)]
613+
{
614+
let _old = self.node_id_to_local_id.insert(owner, hir::ItemLocalId::ZERO);
615+
debug_assert_eq!(_old, None);
616+
}
610617

611618
let item = self.with_def_id_parent(def_id, f);
612619
debug_assert_eq!(def_id, item.def_id().def_id);
@@ -618,7 +625,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
618625
self.attrs = current_attrs;
619626
self.bodies = current_bodies;
620627
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;
628+
629+
#[cfg(debug_assertions)]
630+
{
631+
self.node_id_to_local_id = current_node_id_to_local_id;
632+
}
622633
self.trait_map = current_trait_map;
623634
self.current_hir_id_owner = current_owner;
624635
self.item_local_id_counter = current_local_counter;

compiler/rustc_ast_lowering/src/pat.rs

+18-12
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
261261
match self.resolver.get_partial_res(p.id).map(|d| d.expect_full_res()) {
262262
// `None` can occur in body-less function signatures
263263
res @ (None | Some(Res::Local(_))) => {
264-
let canonical_id = match res {
265-
Some(Res::Local(id)) => id,
266-
_ => p.id,
267-
};
268-
// All identifiers resolves to this canonical identifier share its `HirId`.
269-
let binding_id = if canonical_id == p.id {
270-
self.ident_and_label_to_local_id.insert(canonical_id, hir_id.local_id);
271-
hir_id
272-
} else {
273-
hir::HirId {
274-
owner: self.current_hir_id_owner,
275-
local_id: self.ident_and_label_to_local_id[&canonical_id],
264+
let binding_id = match res {
265+
Some(Res::Local(id)) => {
266+
// In `Or` patterns like `VariantA(s) | VariantB(s, _)`, multiple identifier patterns
267+
// will be resolved to the same `Res::Local`. Thus they just share a single
268+
// `HirId`.
269+
if id == p.id {
270+
self.ident_and_label_to_local_id.insert(id, hir_id.local_id);
271+
hir_id
272+
} else {
273+
hir::HirId {
274+
owner: self.current_hir_id_owner,
275+
local_id: self.ident_and_label_to_local_id[&id],
276+
}
277+
}
278+
}
279+
_ => {
280+
self.ident_and_label_to_local_id.insert(p.id, hir_id.local_id);
281+
hir_id
276282
}
277283
};
278284
hir::PatKind::Binding(

0 commit comments

Comments
 (0)