Skip to content

Commit e3bf50e

Browse files
committed
more consistent debug_assertions
1 parent 1a39247 commit e3bf50e

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]>,
@@ -172,6 +173,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
172173
current_def_id_parent: CRATE_DEF_ID,
173174
item_local_id_counter: hir::ItemLocalId::ZERO,
174175
ident_and_label_to_local_id: Default::default(),
176+
#[cfg(debug_assertions)]
175177
node_id_to_local_id: Default::default(),
176178
trait_map: Default::default(),
177179

@@ -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
@@ -264,18 +264,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
264264
match self.resolver.get_partial_res(p.id).map(|d| d.expect_full_res()) {
265265
// `None` can occur in body-less function signatures
266266
res @ (None | Some(Res::Local(_))) => {
267-
let canonical_id = match res {
268-
Some(Res::Local(id)) => id,
269-
_ => p.id,
270-
};
271-
// All identifiers resolves to this canonical identifier share its `HirId`.
272-
let binding_id = if canonical_id == p.id {
273-
self.ident_and_label_to_local_id.insert(canonical_id, hir_id.local_id);
274-
hir_id
275-
} else {
276-
hir::HirId {
277-
owner: self.current_hir_id_owner,
278-
local_id: self.ident_and_label_to_local_id[&canonical_id],
267+
let binding_id = match res {
268+
Some(Res::Local(id)) => {
269+
// In `Or` patterns like `VariantA(s) | VariantB(s, _)`, multiple identifier patterns
270+
// will be resolved to the same `Res::Local`. Thus they just share a single
271+
// `HirId`.
272+
if id == p.id {
273+
self.ident_and_label_to_local_id.insert(id, hir_id.local_id);
274+
hir_id
275+
} else {
276+
hir::HirId {
277+
owner: self.current_hir_id_owner,
278+
local_id: self.ident_and_label_to_local_id[&id],
279+
}
280+
}
281+
}
282+
_ => {
283+
self.ident_and_label_to_local_id.insert(p.id, hir_id.local_id);
284+
hir_id
279285
}
280286
};
281287
hir::PatKind::Binding(

0 commit comments

Comments
 (0)