Skip to content

Commit 4b63362

Browse files
committed
Use newtype_index!-generated types more idiomatically
1 parent c580c49 commit 4b63362

File tree

20 files changed

+61
-87
lines changed

20 files changed

+61
-87
lines changed

Diff for: compiler/rustc_ast_lowering/src/path.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
448448

449449
generic_args.args.insert_many(
450450
0,
451-
(start.as_u32()..end.as_u32()).map(|i| {
452-
let id = NodeId::from_u32(i);
451+
(start..end).map(|id| {
453452
let l = self.lower_lifetime_anon_in_path(id, elided_lifetime_span);
454453
GenericArg::Lifetime(l)
455454
}),

Diff for: compiler/rustc_borrowck/src/region_infer/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,7 @@ fn sccs_info<'tcx>(infcx: &BorrowckInferCtxt<'tcx>, sccs: &ConstraintSccs) {
338338
let num_components = sccs.num_sccs();
339339
let mut components = vec![FxIndexSet::default(); num_components];
340340

341-
for (reg_var_idx, scc_idx) in sccs.scc_indices().iter().enumerate() {
342-
let reg_var = ty::RegionVid::from_usize(reg_var_idx);
341+
for (reg_var, scc_idx) in sccs.scc_indices().iter_enumerated() {
343342
let origin = var_to_origin.get(&reg_var).unwrap_or(&RegionCtxt::Unknown);
344343
components[scc_idx.as_usize()].insert((reg_var, *origin));
345344
}

Diff for: compiler/rustc_codegen_llvm/src/debuginfo/create_scope_map.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::collections::hash_map::Entry;
33
use rustc_codegen_ssa::mir::debuginfo::{DebugScope, FunctionDebugContext};
44
use rustc_codegen_ssa::traits::*;
55
use rustc_data_structures::fx::FxHashMap;
6-
use rustc_index::Idx;
76
use rustc_index::bit_set::DenseBitSet;
87
use rustc_middle::mir::{Body, SourceScope};
98
use rustc_middle::ty::layout::{FnAbiOf, HasTypingEnv};
@@ -43,8 +42,7 @@ pub(crate) fn compute_mir_scopes<'ll, 'tcx>(
4342
let mut instantiated = DenseBitSet::new_empty(mir.source_scopes.len());
4443
let mut discriminators = FxHashMap::default();
4544
// Instantiate all scopes.
46-
for idx in 0..mir.source_scopes.len() {
47-
let scope = SourceScope::new(idx);
45+
for scope in mir.source_scopes.indices() {
4846
make_mir_scope(
4947
cx,
5048
instance,

Diff for: compiler/rustc_hir/src/definitions.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,9 @@ impl DefPathTable {
4747
debug_assert_eq!(self.stable_crate_id, def_path_hash.stable_crate_id());
4848
let local_hash = def_path_hash.local_hash();
4949

50-
let index = {
51-
let index = DefIndex::from(self.index_to_key.len());
52-
debug!("DefPathTable::insert() - {:?} <-> {:?}", key, index);
53-
self.index_to_key.push(key);
54-
index
55-
};
50+
let index = self.index_to_key.push(key);
51+
debug!("DefPathTable::insert() - {key:?} <-> {index:?}");
52+
5653
self.def_path_hashes.push(local_hash);
5754
debug_assert!(self.def_path_hashes.len() == self.index_to_key.len());
5855

Diff for: compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
606606
// with placeholders, which imply nothing about outlives bounds, and then
607607
// prove below that the hidden types are well formed.
608608
let universe = infcx.create_next_universe();
609-
let mut idx = 0;
609+
let mut idx = ty::BoundVar::ZERO;
610610
let mapping: FxIndexMap<_, _> = collector
611611
.types
612612
.iter()
@@ -623,10 +623,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
623623
tcx,
624624
ty::Placeholder {
625625
universe,
626-
bound: ty::BoundTy {
627-
var: ty::BoundVar::from_usize(idx),
628-
kind: ty::BoundTyKind::Anon,
629-
},
626+
bound: ty::BoundTy { var: idx, kind: ty::BoundTyKind::Anon },
630627
},
631628
),
632629
)

Diff for: compiler/rustc_infer/src/infer/region_constraints/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -618,9 +618,7 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
618618
RegionVid::from(value_count)..RegionVid::from(self.storage.unification_table.len());
619619
(
620620
range.clone(),
621-
(range.start.index()..range.end.index())
622-
.map(|index| self.storage.var_infos[ty::RegionVid::from(index)].origin)
623-
.collect(),
621+
(range.start..range.end).map(|index| self.storage.var_infos[index].origin).collect(),
624622
)
625623
}
626624

Diff for: compiler/rustc_infer/src/infer/snapshot/fudge.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ fn const_vars_since_snapshot<'tcx>(
3030
snapshot_var_len: usize,
3131
) -> (Range<ConstVid>, Vec<ConstVariableOrigin>) {
3232
let range = vars_since_snapshot(table, snapshot_var_len);
33+
let range = range.start.vid..range.end.vid;
3334

3435
(
35-
range.start.vid..range.end.vid,
36-
(range.start.index()..range.end.index())
37-
.map(|index| match table.probe_value(ConstVid::from_u32(index)) {
36+
range.clone(),
37+
range
38+
.map(|index| match table.probe_value(index) {
3839
ConstVariableValue::Known { value: _ } => {
3940
ConstVariableOrigin { param_def_id: None, span: rustc_span::DUMMY_SP }
4041
}

Diff for: compiler/rustc_middle/src/mir/pretty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,12 @@ fn write_mir_intro<'tcx>(
531531

532532
// construct a scope tree and write it out
533533
let mut scope_tree: FxHashMap<SourceScope, Vec<SourceScope>> = Default::default();
534-
for (index, scope_data) in body.source_scopes.iter().enumerate() {
534+
for (index, scope_data) in body.source_scopes.iter_enumerated() {
535535
if let Some(parent) = scope_data.parent_scope {
536-
scope_tree.entry(parent).or_default().push(SourceScope::new(index));
536+
scope_tree.entry(parent).or_default().push(index);
537537
} else {
538538
// Only the argument scope has no parent, because it's the root.
539-
assert_eq!(index, OUTERMOST_SOURCE_SCOPE.index());
539+
assert_eq!(index, OUTERMOST_SOURCE_SCOPE);
540540
}
541541
}
542542

Diff for: compiler/rustc_middle/src/ty/fold.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ impl<'tcx> TyCtxt<'tcx> {
278278
where
279279
T: TypeFoldable<TyCtxt<'tcx>>,
280280
{
281-
let shift_bv = |bv: ty::BoundVar| ty::BoundVar::from_usize(bv.as_usize() + bound_vars);
281+
let shift_bv = |bv: ty::BoundVar| bv + bound_vars;
282282
self.replace_escaping_bound_vars_uncached(
283283
value,
284284
FnMutDelegate {

Diff for: compiler/rustc_middle/src/ty/visit.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -231,29 +231,23 @@ impl MaxUniverse {
231231
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for MaxUniverse {
232232
fn visit_ty(&mut self, t: Ty<'tcx>) {
233233
if let ty::Placeholder(placeholder) = t.kind() {
234-
self.max_universe = ty::UniverseIndex::from_u32(
235-
self.max_universe.as_u32().max(placeholder.universe.as_u32()),
236-
);
234+
self.max_universe = self.max_universe.max(placeholder.universe);
237235
}
238236

239237
t.super_visit_with(self)
240238
}
241239

242240
fn visit_const(&mut self, c: ty::consts::Const<'tcx>) {
243241
if let ty::ConstKind::Placeholder(placeholder) = c.kind() {
244-
self.max_universe = ty::UniverseIndex::from_u32(
245-
self.max_universe.as_u32().max(placeholder.universe.as_u32()),
246-
);
242+
self.max_universe = self.max_universe.max(placeholder.universe);
247243
}
248244

249245
c.super_visit_with(self)
250246
}
251247

252248
fn visit_region(&mut self, r: ty::Region<'tcx>) {
253249
if let ty::RePlaceholder(placeholder) = r.kind() {
254-
self.max_universe = ty::UniverseIndex::from_u32(
255-
self.max_universe.as_u32().max(placeholder.universe.as_u32()),
256-
);
250+
self.max_universe = self.max_universe.max(placeholder.universe);
257251
}
258252
}
259253
}

Diff for: compiler/rustc_mir_build/src/thir/cx/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
191191
let pointer_target = ExprKind::Field {
192192
lhs: self.thir.exprs.push(expr),
193193
variant_index: FIRST_VARIANT,
194-
name: FieldIdx::from(0u32),
194+
name: FieldIdx::ZERO,
195195
};
196196
let arg = Expr { temp_lifetime, ty: pin_ty, span, kind: pointer_target };
197197
let arg = self.thir.exprs.push(arg);
@@ -226,7 +226,7 @@ impl<'tcx> ThirBuildCx<'tcx> {
226226
adt_def: self.tcx.adt_def(pin_did),
227227
variant_index: FIRST_VARIANT,
228228
args,
229-
fields: Box::new([FieldExpr { name: FieldIdx::from(0u32), expr }]),
229+
fields: Box::new([FieldExpr { name: FieldIdx::ZERO, expr }]),
230230
user_ty: None,
231231
base: AdtExprBase::None,
232232
}));

Diff for: compiler/rustc_mir_transform/src/coroutine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ struct TransformVisitor<'tcx> {
203203

204204
impl<'tcx> TransformVisitor<'tcx> {
205205
fn insert_none_ret_block(&self, body: &mut Body<'tcx>) -> BasicBlock {
206-
let block = BasicBlock::new(body.basic_blocks.len());
206+
let block = body.basic_blocks.next_index();
207207
let source_info = SourceInfo::outermost(body.span);
208208

209209
let none_value = match self.coroutine_kind {
@@ -1193,7 +1193,7 @@ fn insert_panic_block<'tcx>(
11931193
body: &mut Body<'tcx>,
11941194
message: AssertMessage<'tcx>,
11951195
) -> BasicBlock {
1196-
let assert_block = BasicBlock::new(body.basic_blocks.len());
1196+
let assert_block = body.basic_blocks.next_index();
11971197
let kind = TerminatorKind::Assert {
11981198
cond: Operand::Constant(Box::new(ConstOperand {
11991199
span: body.span,

Diff for: compiler/rustc_mir_transform/src/elaborate_drop.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -258,17 +258,16 @@ where
258258
) -> Vec<(Place<'tcx>, Option<D::Path>)> {
259259
variant
260260
.fields
261-
.iter()
262-
.enumerate()
263-
.map(|(i, f)| {
264-
let field = FieldIdx::new(i);
265-
let subpath = self.elaborator.field_subpath(variant_path, field);
261+
.iter_enumerated()
262+
.map(|(field_idx, field)| {
263+
let subpath = self.elaborator.field_subpath(variant_path, field_idx);
266264
let tcx = self.tcx();
267265

268266
assert_eq!(self.elaborator.typing_env().typing_mode, ty::TypingMode::PostAnalysis);
269-
let field_ty = match tcx
270-
.try_normalize_erasing_regions(self.elaborator.typing_env(), f.ty(tcx, args))
271-
{
267+
let field_ty = match tcx.try_normalize_erasing_regions(
268+
self.elaborator.typing_env(),
269+
field.ty(tcx, args),
270+
) {
272271
Ok(t) => t,
273272
Err(_) => Ty::new_error(
274273
self.tcx(),
@@ -279,7 +278,7 @@ where
279278
),
280279
};
281280

282-
(tcx.mk_place_field(base_place, field, field_ty), subpath)
281+
(tcx.mk_place_field(base_place, field_idx, field_ty), subpath)
283282
})
284283
.collect()
285284
}

Diff for: compiler/rustc_mir_transform/src/inline.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,9 @@ fn inline_call<'tcx, I: Inliner<'tcx>>(
903903

904904
let mut integrator = Integrator {
905905
args: &args,
906-
new_locals: Local::new(caller_body.local_decls.len())..,
907-
new_scopes: SourceScope::new(caller_body.source_scopes.len())..,
908-
new_blocks: BasicBlock::new(caller_body.basic_blocks.len())..,
906+
new_locals: caller_body.local_decls.next_index()..,
907+
new_scopes: caller_body.source_scopes.next_index()..,
908+
new_blocks: caller_body.basic_blocks.next_index()..,
909909
destination: destination_local,
910910
callsite_scope: caller_body.source_scopes[callsite.source_info.scope].clone(),
911911
callsite,
@@ -1169,21 +1169,21 @@ impl Integrator<'_, '_> {
11691169
if idx < self.args.len() {
11701170
self.args[idx]
11711171
} else {
1172-
Local::new(self.new_locals.start.index() + (idx - self.args.len()))
1172+
self.new_locals.start + (idx - self.args.len())
11731173
}
11741174
};
11751175
trace!("mapping local `{:?}` to `{:?}`", local, new);
11761176
new
11771177
}
11781178

11791179
fn map_scope(&self, scope: SourceScope) -> SourceScope {
1180-
let new = SourceScope::new(self.new_scopes.start.index() + scope.index());
1180+
let new = self.new_scopes.start + scope.index();
11811181
trace!("mapping scope `{:?}` to `{:?}`", scope, new);
11821182
new
11831183
}
11841184

11851185
fn map_block(&self, block: BasicBlock) -> BasicBlock {
1186-
let new = BasicBlock::new(self.new_blocks.start.index() + block.index());
1186+
let new = self.new_blocks.start + block.index();
11871187
trace!("mapping block `{:?}` to `{:?}`", block, new);
11881188
new
11891189
}

Diff for: compiler/rustc_mir_transform/src/patch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'tcx> MirPatch<'tcx> {
181181

182182
/// Queues the addition of a new basic block.
183183
pub(crate) fn new_block(&mut self, data: BasicBlockData<'tcx>) -> BasicBlock {
184-
let block = BasicBlock::new(self.term_patch_map.len());
184+
let block = self.term_patch_map.next_index();
185185
debug!("MirPatch: new_block: {:?}: {:?}", block, data);
186186
self.new_blocks.push(data);
187187
self.term_patch_map.push(None);

Diff for: compiler/rustc_mir_transform/src/promote_consts.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use either::{Left, Right};
1818
use rustc_const_eval::check_consts::{ConstCx, qualifs};
1919
use rustc_data_structures::fx::FxHashSet;
2020
use rustc_hir as hir;
21-
use rustc_index::{Idx, IndexSlice, IndexVec};
21+
use rustc_index::{IndexSlice, IndexVec};
2222
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
2323
use rustc_middle::mir::*;
2424
use rustc_middle::ty::{self, GenericArgs, List, Ty, TyCtxt, TypeVisitableExt};
@@ -864,17 +864,21 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
864864
new_temp
865865
}
866866

867-
fn promote_candidate(mut self, candidate: Candidate, next_promoted_id: usize) -> Body<'tcx> {
867+
fn promote_candidate(
868+
mut self,
869+
candidate: Candidate,
870+
next_promoted_index: Promoted,
871+
) -> Body<'tcx> {
868872
let def = self.source.source.def_id();
869873
let (mut rvalue, promoted_op) = {
870874
let promoted = &mut self.promoted;
871-
let promoted_id = Promoted::new(next_promoted_id);
872875
let tcx = self.tcx;
873876
let mut promoted_operand = |ty, span| {
874877
promoted.span = span;
875878
promoted.local_decls[RETURN_PLACE] = LocalDecl::new(ty, span);
876879
let args = tcx.erase_regions(GenericArgs::identity_for_item(tcx, def));
877-
let uneval = mir::UnevaluatedConst { def, args, promoted: Some(promoted_id) };
880+
let uneval =
881+
mir::UnevaluatedConst { def, args, promoted: Some(next_promoted_index) };
878882

879883
ConstOperand { span, user_ty: None, const_: Const::Unevaluated(uneval, ty) }
880884
};
@@ -1034,7 +1038,7 @@ fn promote_candidates<'tcx>(
10341038
required_consts: Vec::new(),
10351039
};
10361040

1037-
let mut promoted = promoter.promote_candidate(candidate, promotions.len());
1041+
let mut promoted = promoter.promote_candidate(candidate, promotions.next_index());
10381042
promoted.source.promoted = Some(promotions.next_index());
10391043
promotions.push(promoted);
10401044
}

Diff for: compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ where
355355
// exist at all (see the FIXME at the start of this method), we have to deal with
356356
// them for now.
357357
delegate.instantiate_canonical_var_with_infer(info, span, |idx| {
358-
ty::UniverseIndex::from(prev_universe.index() + idx.index())
358+
prev_universe + idx.index()
359359
})
360360
} else if info.is_existential() {
361361
// As an optimization we sometimes avoid creating a new inference variable here.

Diff for: compiler/rustc_passes/src/liveness.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
578578
where
579579
F: FnMut(Variable) -> bool,
580580
{
581-
for var_idx in 0..self.ir.var_kinds.len() {
582-
let var = Variable::from(var_idx);
581+
for var in self.ir.var_kinds.indices() {
583582
if test(var) {
584583
write!(wr, " {var:?}")?;
585584
}
@@ -609,8 +608,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
609608
debug!(
610609
"^^ liveness computation results for body {} (entry={:?})",
611610
{
612-
for ln_idx in 0..self.ir.lnks.len() {
613-
debug!("{:?}", self.ln_str(LiveNode::from(ln_idx)));
611+
for ln_idx in self.ir.lnks.indices() {
612+
debug!("{:?}", self.ln_str(ln_idx));
614613
}
615614
hir_id
616615
},

Diff for: compiler/rustc_resolve/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1948,7 +1948,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
19481948

19491949
self.record_lifetime_res(
19501950
anchor_id,
1951-
LifetimeRes::ElidedAnchor { start: id, end: NodeId::from_u32(id.as_u32() + 1) },
1951+
LifetimeRes::ElidedAnchor { start: id, end: id + 1 },
19521952
LifetimeElisionCandidate::Ignore,
19531953
);
19541954
self.resolve_anonymous_lifetime(&lt, anchor_id, true);

0 commit comments

Comments
 (0)