Skip to content

Commit

Permalink
Add duplicate lowering check
Browse files Browse the repository at this point in the history
  • Loading branch information
adwinwhite committed Sep 15, 2024
1 parent 2b28424 commit acabd7c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 38 deletions.
9 changes: 0 additions & 9 deletions compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.arena.alloc(self.lower_block_noalloc(hir_id, b, targeted_by_break))
}

pub(super) fn lower_block_with_hir_id(
&mut self,
b: &Block,
hir_id: hir::HirId,
targeted_by_break: bool,
) -> &'hir hir::Block<'hir> {
self.arena.alloc(self.lower_block_noalloc(hir_id, b, targeted_by_break))
}

pub(super) fn lower_block_noalloc(
&mut self,
hir_id: hir::HirId,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
};
self_resolver.visit_block(block);
// Target expr needs to lower `self` path.
this.ident_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
this.lower_target_expr(&block)
} else {
this.generate_arg(param.pat.hir_id, span)
Expand Down
14 changes: 8 additions & 6 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
// expr node id.
let block_hir_id = self.lower_node_id(blk.id);
let opt_label = self.lower_label(*opt_label, blk.id, block_hir_id);
hir::ExprKind::Block(
self.lower_block_with_hir_id(blk, block_hir_id, opt_label.is_some()),
opt_label,
)
let hir_block = self.arena.alloc(self.lower_block_noalloc(
block_hir_id,
blk,
opt_label.is_some(),
));
hir::ExprKind::Block(hir_block, opt_label)
}
ExprKind::Assign(el, er, span) => self.lower_expr_assign(el, er, *span, e.span),
ExprKind::AssignOp(op, el, er) => hir::ExprKind::AssignOp(
Expand Down Expand Up @@ -1483,15 +1485,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
dest_hir_id: hir::HirId,
) -> Option<Label> {
let label = opt_label?;
self.labelled_node_id_to_local_id.insert(dest_id, dest_hir_id.local_id);
self.ident_and_label_to_local_id.insert(dest_id, dest_hir_id.local_id);
Some(Label { ident: self.lower_ident(label.ident) })
}

fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
let target_id = match destination {
Some((id, _)) => {
if let Some(loop_id) = self.resolver.get_label_res(id) {
let local_id = self.labelled_node_id_to_local_id[&loop_id];
let local_id = self.ident_and_label_to_local_id[&loop_id];
let loop_hir_id = HirId { owner: self.current_hir_id_owner, local_id };
Ok(loop_hir_id)
} else {
Expand Down
12 changes: 4 additions & 8 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
fn lower_item(&mut self, i: &Item) -> &'hir hir::Item<'hir> {
let mut ident = i.ident;
let vis_span = self.lower_span(i.vis.span);
let hir_id =
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
let attrs = self.lower_attrs(hir_id, &i.attrs);
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind);
let item = hir::Item {
Expand Down Expand Up @@ -659,8 +658,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> {
let hir_id =
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
let owner_id = hir_id.expect_owner();
self.lower_attrs(hir_id, &i.attrs);
let item = hir::ForeignItem {
Expand Down Expand Up @@ -788,8 +786,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
i: &AssocItem,
trait_constness: Const,
) -> &'hir hir::TraitItem<'hir> {
let hir_id =
hir::HirId { owner: self.current_hir_id_owner, local_id: hir::ItemLocalId::ZERO };
let hir_id = hir::HirId::make_owner(self.current_hir_id_owner.def_id);
self.lower_attrs(hir_id, &i.attrs);
let trait_item_def_id = hir_id.expect_owner();

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

let (generics, kind) = match &i.kind {
Expand Down
35 changes: 23 additions & 12 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ struct LoweringContext<'a, 'hir> {
impl_trait_defs: Vec<hir::GenericParam<'hir>>,
impl_trait_bounds: Vec<hir::WherePredicate<'hir>>,

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

allow_try_trait: Lrc<[Symbol]>,
allow_gen_future: Lrc<[Symbol]>,
Expand Down Expand Up @@ -178,8 +178,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
current_hir_id_owner: hir::CRATE_OWNER_ID,
current_def_id_parent: CRATE_DEF_ID,
item_local_id_counter: hir::ItemLocalId::ZERO,
labelled_node_id_to_local_id: Default::default(),
ident_to_local_id: Default::default(),
ident_and_label_to_local_id: Default::default(),
node_id_to_local_id: Default::default(),
trait_map: Default::default(),

// Lowering state.
Expand Down Expand Up @@ -589,9 +589,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

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

// Always allocate the first `HirId` for the owner itself.
let _old = self.node_id_to_local_id.insert(owner, hir::ItemLocalId::ZERO);
debug_assert_eq!(_old, None);

let item = self.with_def_id_parent(def_id, f);
debug_assert_eq!(def_id, item.def_id().def_id);
// `f` should have consumed all the elements in these vectors when constructing `item`.
Expand All @@ -613,8 +617,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

self.attrs = current_attrs;
self.bodies = current_bodies;
self.labelled_node_id_to_local_id = current_labelled_node_id_to_local_id;
self.ident_to_local_id = current_ident_to_local_id;
self.ident_and_label_to_local_id = current_ident_and_label_to_local_id;
self.node_id_to_local_id = current_node_id_to_local_id;
self.trait_map = current_trait_map;
self.current_hir_id_owner = current_owner;
self.item_local_id_counter = current_local_counter;
Expand Down Expand Up @@ -703,6 +707,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
self.trait_map.insert(hir_id.local_id, traits.into_boxed_slice());
}

// Check whether the same `NodeId` is lowered more than once.
#[cfg(debug_assertions)]
{
let old = self.node_id_to_local_id.insert(ast_node_id, local_id);
assert_eq!(old, None);
}

hir_id
}

Expand All @@ -720,7 +731,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
fn lower_res(&mut self, res: Res<NodeId>) -> Res {
let res: Result<Res, ()> = res.apply_id(|id| {
let owner = self.current_hir_id_owner;
let local_id = self.ident_to_local_id.get(&id).copied().ok_or(())?;
let local_id = self.ident_and_label_to_local_id.get(&id).copied().ok_or(())?;
Ok(HirId { owner, local_id })
});
trace!(?res);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,12 +267,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
};
// All identifiers resolves to this canonical identifier share its `HirId`.
let binding_id = if canonical_id == p.id {
self.ident_to_local_id.insert(canonical_id, hir_id.local_id);
self.ident_and_label_to_local_id.insert(canonical_id, hir_id.local_id);
hir_id
} else {
hir::HirId {
owner: self.current_hir_id_owner,
local_id: self.ident_to_local_id[&canonical_id],
local_id: self.ident_and_label_to_local_id[&canonical_id],
}
};
hir::PatKind::Binding(
Expand Down

0 comments on commit acabd7c

Please sign in to comment.