Skip to content

Commit 5c3e5af

Browse files
committed
Doc-comment IndexVec::from_elem and use it in a few more places
1 parent bf41e75 commit 5c3e5af

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

compiler/rustc_borrowck/src/type_check/liveness/local_use_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl LocalUseMap {
6363
elements: &RegionValueElements,
6464
body: &Body<'_>,
6565
) -> Self {
66-
let nones = IndexVec::from_elem_n(None, body.local_decls.len());
66+
let nones = IndexVec::from_elem(None, &body.local_decls);
6767
let mut local_use_map = LocalUseMap {
6868
first_def_at: nones.clone(),
6969
first_use_at: nones.clone(),
@@ -76,7 +76,7 @@ impl LocalUseMap {
7676
}
7777

7878
let mut locals_with_use_data: IndexVec<Local, bool> =
79-
IndexVec::from_elem_n(false, body.local_decls.len());
79+
IndexVec::from_elem(false, &body.local_decls);
8080
live_locals.iter().for_each(|&local| locals_with_use_data[local] = true);
8181

8282
LocalUseMapBuild { local_use_map: &mut local_use_map, elements, locals_with_use_data }

compiler/rustc_index/src/vec.rs

+11
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ impl<I: Idx, T> IndexVec<I, T> {
129129
IndexVec { raw: Vec::with_capacity(capacity), _marker: PhantomData }
130130
}
131131

132+
/// Creates a new vector with a copy of `elem` for each index in `universe`.
133+
///
134+
/// Thus `IndexVec::from_elem(elem, &universe)` is equivalent to
135+
/// `IndexVec::<I, _>::from_elem_n(elem, universe.len())`. That can help
136+
/// type inference as it ensures that the resulting vector uses the same
137+
/// index type as `universe`, rather than something potentially surprising.
138+
///
139+
/// For example, if you want to store data for each local in a MIR body,
140+
/// using `let mut uses = IndexVec::from_elem(vec![], &body.local_decls);`
141+
/// ensures that `uses` is an `IndexVec<Local, _>`, and thus can give
142+
/// better error messages later if one accidentally mismatches indices.
132143
#[inline]
133144
pub fn from_elem<S>(elem: T, universe: &IndexSlice<I, S>) -> Self
134145
where

compiler/rustc_infer/src/infer/lexical_region_resolve/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
203203

204204
// Tracks the `VarSubVar` constraints generated for each region vid. We
205205
// later use this to expand across vids.
206-
let mut constraints = IndexVec::from_elem_n(Vec::new(), var_values.values.len());
206+
let mut constraints = IndexVec::from_elem(Vec::new(), &var_values.values);
207207
// Tracks the changed region vids.
208208
let mut changes = Vec::new();
209209
for constraint in self.data.constraints.keys() {

compiler/rustc_mir_transform/src/coverage/graph.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ impl CoverageGraph {
3737
// `SwitchInt` to have multiple targets to the same destination `BasicBlock`, so
3838
// de-duplication is required. This is done without reordering the successors.
3939

40-
let bcbs_len = bcbs.len();
41-
let mut seen = IndexVec::from_elem_n(false, bcbs_len);
40+
let mut seen = IndexVec::from_elem(false, &bcbs);
4241
let successors = IndexVec::from_fn_n(
4342
|bcb| {
4443
for b in seen.iter_mut() {
@@ -60,7 +59,7 @@ impl CoverageGraph {
6059
bcbs.len(),
6160
);
6261

63-
let mut predecessors = IndexVec::from_elem_n(Vec::new(), bcbs.len());
62+
let mut predecessors = IndexVec::from_elem(Vec::new(), &bcbs);
6463
for (bcb, bcb_successors) in successors.iter_enumerated() {
6564
for &successor in bcb_successors {
6665
predecessors[successor].push(bcb);

compiler/rustc_ty_utils/src/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ fn generator_saved_local_eligibility(
522522
use SavedLocalEligibility::*;
523523

524524
let mut assignments: IndexVec<GeneratorSavedLocal, SavedLocalEligibility> =
525-
IndexVec::from_elem_n(Unassigned, info.field_tys.len());
525+
IndexVec::from_elem(Unassigned, &info.field_tys);
526526

527527
// The saved locals not eligible for overlap. These will get
528528
// "promoted" to the prefix of our generator.

0 commit comments

Comments
 (0)