Skip to content

Commit 91af000

Browse files
authored
Rollup merge of #89162 - petrochenkov:ivmap, r=davidtwco
rustc_index: Add some map-like APIs to `IndexVec` `IndexVec` is often used as a map, but its map APIs are lacking. This PR adds a couple of useful methods.
2 parents 3bdc894 + fbe5e5c commit 91af000

File tree

4 files changed

+28
-24
lines changed

4 files changed

+28
-24
lines changed

compiler/rustc_ast_lowering/src/item.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -474,9 +474,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
474474
res
475475
} else {
476476
// Associate an HirId to both ids even if there is no resolution.
477-
self.node_id_to_hir_id.ensure_contains_elem(new_node_id, || None);
478-
debug_assert!(self.node_id_to_hir_id[new_node_id].is_none());
479-
self.node_id_to_hir_id[new_node_id] = Some(hir::HirId::make_owner(new_id));
477+
let _old = self
478+
.node_id_to_hir_id
479+
.insert(new_node_id, hir::HirId::make_owner(new_id));
480+
debug_assert!(_old.is_none());
480481
continue;
481482
};
482483
let ident = *ident;

compiler/rustc_ast_lowering/src/lib.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
469469
let def_id = self.resolver.local_def_id(owner);
470470

471471
// Always allocate the first `HirId` for the owner itself.
472-
self.node_id_to_hir_id.ensure_contains_elem(owner, || None);
473-
if let Some(_lowered) = self.node_id_to_hir_id[owner] {
474-
panic!("with_hir_id_owner must not be called multiple times on owner {:?}", def_id);
475-
}
476-
self.node_id_to_hir_id[owner] = Some(hir::HirId::make_owner(def_id));
472+
let _old = self.node_id_to_hir_id.insert(owner, hir::HirId::make_owner(def_id));
473+
debug_assert_eq!(_old, None);
477474

478475
let current_owner = std::mem::replace(&mut self.current_hir_id_owner, def_id);
479476
let current_local_counter =
@@ -484,8 +481,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
484481
self.current_hir_id_owner = current_owner;
485482
self.item_local_id_counter = current_local_counter;
486483

487-
self.owners.ensure_contains_elem(def_id, || None);
488-
self.owners[def_id] = Some(item);
484+
let _old = self.owners.insert(def_id, item);
485+
debug_assert!(_old.is_none());
489486

490487
def_id
491488
}
@@ -499,18 +496,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
499496
fn lower_node_id(&mut self, ast_node_id: NodeId) -> hir::HirId {
500497
assert_ne!(ast_node_id, DUMMY_NODE_ID);
501498

502-
self.node_id_to_hir_id.ensure_contains_elem(ast_node_id, || None);
503-
if let Some(existing_hir_id) = self.node_id_to_hir_id[ast_node_id] {
504-
existing_hir_id
505-
} else {
499+
*self.node_id_to_hir_id.get_or_insert_with(ast_node_id, || {
506500
// Generate a new `HirId`.
507501
let owner = self.current_hir_id_owner;
508502
let local_id = self.item_local_id_counter;
509503
self.item_local_id_counter.increment_by(1);
510-
let hir_id = hir::HirId { owner, local_id };
511-
self.node_id_to_hir_id[ast_node_id] = Some(hir_id);
512-
hir_id
513-
}
504+
hir::HirId { owner, local_id }
505+
})
514506
}
515507

516508
fn next_id(&mut self) -> hir::HirId {

compiler/rustc_index/src/bit_set.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1072,13 +1072,9 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
10721072
}
10731073

10741074
fn ensure_row(&mut self, row: R) -> &mut HybridBitSet<C> {
1075-
// Instantiate any missing rows up to and including row `row` with an
1076-
// empty HybridBitSet.
1077-
self.rows.ensure_contains_elem(row, || None);
1078-
1075+
// Instantiate any missing rows up to and including row `row` with an empty HybridBitSet.
10791076
// Then replace row `row` with a full HybridBitSet if necessary.
1080-
let num_columns = self.num_columns;
1081-
self.rows[row].get_or_insert_with(|| HybridBitSet::new_empty(num_columns))
1077+
self.rows.get_or_insert_with(row, || HybridBitSet::new_empty(self.num_columns))
10821078
}
10831079

10841080
/// Sets the cell at `(row, column)` to true. Put another way, insert

compiler/rustc_index/src/vec.rs

+15
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,21 @@ impl<I: Idx, T> IndexVec<I, T> {
720720
}
721721
}
722722

723+
/// `IndexVec` is often used as a map, so it provides some map-like APIs.
724+
impl<I: Idx, T> IndexVec<I, Option<T>> {
725+
#[inline]
726+
pub fn insert(&mut self, index: I, value: T) -> Option<T> {
727+
self.ensure_contains_elem(index, || None);
728+
self[index].replace(value)
729+
}
730+
731+
#[inline]
732+
pub fn get_or_insert_with(&mut self, index: I, value: impl FnOnce() -> T) -> &mut T {
733+
self.ensure_contains_elem(index, || None);
734+
self[index].get_or_insert_with(value)
735+
}
736+
}
737+
723738
impl<I: Idx, T: Clone> IndexVec<I, T> {
724739
#[inline]
725740
pub fn resize(&mut self, new_len: usize, value: T) {

0 commit comments

Comments
 (0)