Skip to content

Commit 0790996

Browse files
authored
Rollup merge of #110394 - scottmcm:less-idx-new, r=WaffleLapkin
Various minor Idx-related tweaks Nothing particularly exciting here, but a couple of things I noticed as I was looking for more index conversions to simplify. cc rust-lang/compiler-team#606 r? `@WaffleLapkin`
2 parents a6c1fa5 + c98895d commit 0790996

File tree

17 files changed

+55
-52
lines changed

17 files changed

+55
-52
lines changed

compiler/rustc_abi/src/layout.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,8 @@ pub trait LayoutCalculator {
461461
let all_indices = variants.indices();
462462
let needs_disc =
463463
|index: VariantIdx| index != largest_variant_index && !absent(&variants[index]);
464-
let niche_variants = all_indices.clone().find(|v| needs_disc(*v)).unwrap().index()
465-
..=all_indices.rev().find(|v| needs_disc(*v)).unwrap().index();
464+
let niche_variants = all_indices.clone().find(|v| needs_disc(*v)).unwrap()
465+
..=all_indices.rev().find(|v| needs_disc(*v)).unwrap();
466466

467467
let count = niche_variants.size_hint().1.unwrap() as u128;
468468

@@ -560,8 +560,7 @@ pub trait LayoutCalculator {
560560
tag: niche_scalar,
561561
tag_encoding: TagEncoding::Niche {
562562
untagged_variant: largest_variant_index,
563-
niche_variants: (VariantIdx::new(*niche_variants.start())
564-
..=VariantIdx::new(*niche_variants.end())),
563+
niche_variants,
565564
niche_start,
566565
},
567566
tag_field: 0,

compiler/rustc_abi/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bitflags::bitflags;
1111
use rustc_data_structures::intern::Interned;
1212
#[cfg(feature = "nightly")]
1313
use rustc_data_structures::stable_hasher::StableOrd;
14-
use rustc_index::vec::{Idx, IndexSlice, IndexVec};
14+
use rustc_index::vec::{IndexSlice, IndexVec};
1515
#[cfg(feature = "nightly")]
1616
use rustc_macros::HashStable_Generic;
1717
#[cfg(feature = "nightly")]

compiler/rustc_ast/src/node_id.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ rustc_index::newtype_index! {
99
///
1010
/// [`DefId`]: rustc_span::def_id::DefId
1111
#[debug_format = "NodeId({})"]
12-
pub struct NodeId {}
12+
pub struct NodeId {
13+
/// The [`NodeId`] used to represent the root of the crate.
14+
const CRATE_NODE_ID = 0;
15+
}
1316
}
1417

1518
rustc_data_structures::define_id_collections!(NodeMap, NodeSet, NodeMapEntry, NodeId);
1619

17-
/// The [`NodeId`] used to represent the root of the crate.
18-
pub const CRATE_NODE_ID: NodeId = NodeId::from_u32(0);
19-
2020
/// When parsing and at the beginning of doing expansions, we initially give all AST nodes
2121
/// this dummy AST [`NodeId`]. Then, during a later phase of expansion, we renumber them
2222
/// to have small, positive IDs.

compiler/rustc_borrowck/src/diagnostics/var_name.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::region_infer::RegionInferenceContext;
55
use crate::Upvar;
6-
use rustc_index::vec::{Idx, IndexSlice};
6+
use rustc_index::vec::IndexSlice;
77
use rustc_middle::mir::{Body, Local};
88
use rustc_middle::ty::{RegionVid, TyCtxt};
99
use rustc_span::source_map::Span;
@@ -117,7 +117,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
117117
argument_index: usize,
118118
) -> (Option<Symbol>, Span) {
119119
let implicit_inputs = self.universal_regions().defining_ty.implicit_inputs();
120-
let argument_local = Local::new(implicit_inputs + argument_index + 1);
120+
let argument_local = Local::from_usize(implicit_inputs + argument_index + 1);
121121
debug!("get_argument_name_and_span_for_region: argument_local={argument_local:?}");
122122

123123
let argument_name = local_names[argument_local];

compiler/rustc_borrowck/src/facts.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::location::{LocationIndex, LocationTable};
44
use crate::BorrowIndex;
55
use polonius_engine::AllFacts as PoloniusFacts;
66
use polonius_engine::Atom;
7-
use rustc_index::vec::Idx;
87
use rustc_middle::mir::Local;
98
use rustc_middle::ty::{RegionVid, TyCtxt};
109
use rustc_mir_dataflow::move_paths::MovePathIndex;
@@ -93,13 +92,13 @@ impl AllFactsExt for AllFacts {
9392

9493
impl Atom for BorrowIndex {
9594
fn index(self) -> usize {
96-
Idx::index(self)
95+
self.as_usize()
9796
}
9897
}
9998

10099
impl Atom for LocationIndex {
101100
fn index(self) -> usize {
102-
Idx::index(self)
101+
self.as_usize()
103102
}
104103
}
105104

compiler/rustc_borrowck/src/location.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![deny(rustc::untranslatable_diagnostic)]
22
#![deny(rustc::diagnostic_outside_of_impl)]
3-
use rustc_index::vec::{Idx, IndexVec};
3+
use rustc_index::vec::IndexVec;
44
use rustc_middle::mir::{BasicBlock, Body, Location};
55

66
/// Maps between a MIR Location, which identifies a particular
@@ -50,19 +50,19 @@ impl LocationTable {
5050
}
5151

5252
pub fn all_points(&self) -> impl Iterator<Item = LocationIndex> {
53-
(0..self.num_points).map(LocationIndex::new)
53+
(0..self.num_points).map(LocationIndex::from_usize)
5454
}
5555

5656
pub fn start_index(&self, location: Location) -> LocationIndex {
5757
let Location { block, statement_index } = location;
5858
let start_index = self.statements_before_block[block];
59-
LocationIndex::new(start_index + statement_index * 2)
59+
LocationIndex::from_usize(start_index + statement_index * 2)
6060
}
6161

6262
pub fn mid_index(&self, location: Location) -> LocationIndex {
6363
let Location { block, statement_index } = location;
6464
let start_index = self.statements_before_block[block];
65-
LocationIndex::new(start_index + statement_index * 2 + 1)
65+
LocationIndex::from_usize(start_index + statement_index * 2 + 1)
6666
}
6767

6868
pub fn to_location(&self, index: LocationIndex) -> RichLocation {

compiler/rustc_borrowck/src/nll.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use rustc_hir::def_id::LocalDefId;
77
use rustc_index::vec::IndexSlice;
88
use rustc_middle::mir::{create_dump_file, dump_enabled, dump_mir, PassWhere};
99
use rustc_middle::mir::{
10-
BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location,
11-
Promoted,
10+
Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location, Promoted,
11+
START_BLOCK,
1212
};
1313
use rustc_middle::ty::{self, OpaqueHiddenType, TyCtxt};
1414
use rustc_span::symbol::sym;
@@ -94,8 +94,8 @@ fn populate_polonius_move_facts(
9494
}
9595
}
9696

97-
let fn_entry_start = location_table
98-
.start_index(Location { block: BasicBlock::from_u32(0u32), statement_index: 0 });
97+
let fn_entry_start =
98+
location_table.start_index(Location { block: START_BLOCK, statement_index: 0 });
9999

100100
// initialized_at
101101
for init in move_data.inits.iter() {

compiler/rustc_borrowck/src/type_check/input_output.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
//! `RETURN_PLACE` the MIR arguments) are always fully normalized (and
88
//! contain revealed `impl Trait` values).
99
10-
use rustc_index::vec::Idx;
1110
use rustc_infer::infer::LateBoundRegionConversionTime;
1211
use rustc_middle::mir::*;
1312
use rustc_middle::ty::{self, Ty};
@@ -83,7 +82,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
8382
}
8483

8584
// In MIR, argument N is stored in local N+1.
86-
let local = Local::new(argument_index + 1);
85+
let local = Local::from_usize(argument_index + 1);
8786

8887
let mir_input_ty = body.local_decls[local].ty;
8988

compiler/rustc_borrowck/src/universal_regions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_hir as hir;
1919
use rustc_hir::def_id::{DefId, LocalDefId};
2020
use rustc_hir::lang_items::LangItem;
2121
use rustc_hir::BodyOwnerKind;
22-
use rustc_index::vec::{Idx, IndexVec};
22+
use rustc_index::vec::IndexVec;
2323
use rustc_infer::infer::NllRegionVariableOrigin;
2424
use rustc_middle::ty::fold::TypeFoldable;
2525
use rustc_middle::ty::{self, InlineConstSubsts, InlineConstSubstsParts, RegionVid, Ty, TyCtxt};
@@ -289,7 +289,7 @@ impl<'tcx> UniversalRegions<'tcx> {
289289
/// Returns an iterator over all the RegionVids corresponding to
290290
/// universally quantified free regions.
291291
pub fn universal_regions(&self) -> impl Iterator<Item = RegionVid> {
292-
(FIRST_GLOBAL_INDEX..self.num_universals).map(RegionVid::new)
292+
(FIRST_GLOBAL_INDEX..self.num_universals).map(RegionVid::from_usize)
293293
}
294294

295295
/// Returns `true` if `r` is classified as an local region.

compiler/rustc_codegen_ssa/src/mir/block.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use crate::MemFlags;
1212
use rustc_ast as ast;
1313
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
1414
use rustc_hir::lang_items::LangItem;
15-
use rustc_index::vec::Idx;
1615
use rustc_middle::mir::{self, AssertKind, SwitchTargets};
1716
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf, ValidityRequirement};
1817
use rustc_middle::ty::print::{with_no_trimmed_paths, with_no_visible_paths};
@@ -369,7 +368,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
369368
if self.fn_abi.c_variadic {
370369
// The `VaList` "spoofed" argument is just after all the real arguments.
371370
let va_list_arg_idx = self.fn_abi.args.len();
372-
match self.locals[mir::Local::new(1 + va_list_arg_idx)] {
371+
match self.locals[mir::Local::from_usize(1 + va_list_arg_idx)] {
373372
LocalRef::Place(va_list) => {
374373
bx.va_end(va_list.llval);
375374
}

compiler/rustc_const_eval/src/interpret/discriminant.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,19 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
211211
let variant_index_relative = u32::try_from(variant_index_relative)
212212
.expect("we checked that this fits into a u32");
213213
// Then computing the absolute variant idx should not overflow any more.
214-
let variant_index = variants_start
215-
.checked_add(variant_index_relative)
216-
.expect("overflow computing absolute variant idx");
217-
let variants_len = op
214+
let variant_index = VariantIdx::from_u32(
215+
variants_start
216+
.checked_add(variant_index_relative)
217+
.expect("overflow computing absolute variant idx"),
218+
);
219+
let variants = op
218220
.layout
219221
.ty
220222
.ty_adt_def()
221223
.expect("tagged layout for non adt")
222-
.variants()
223-
.len();
224-
assert!(usize::try_from(variant_index).unwrap() < variants_len);
225-
VariantIdx::from_u32(variant_index)
224+
.variants();
225+
assert!(variant_index < variants.next_index());
226+
variant_index
226227
} else {
227228
untagged_variant
228229
}

compiler/rustc_hir_typeck/src/demand.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_middle::ty::print::with_no_trimmed_paths;
1717
use rustc_middle::ty::{self, Article, AssocItem, Ty, TypeAndMut, TypeFoldable};
1818
use rustc_span::symbol::{sym, Symbol};
1919
use rustc_span::{BytePos, Span, DUMMY_SP};
20-
use rustc_target::abi::FieldIdx;
2120
use rustc_trait_selection::infer::InferCtxtExt as _;
2221
use rustc_trait_selection::traits::ObligationCause;
2322

@@ -875,7 +874,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
875874
variant.fields.len() == 1
876875
})
877876
.filter_map(|variant| {
878-
let sole_field = &variant.fields[FieldIdx::from_u32(0)];
877+
let sole_field = &variant.single_field();
879878

880879
let field_is_local = sole_field.did.is_local();
881880
let field_is_accessible =

compiler/rustc_hir_typeck/src/intrinsicck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hir as hir;
44
use rustc_index::vec::Idx;
55
use rustc_middle::ty::layout::{LayoutError, SizeSkeleton};
66
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
7-
use rustc_target::abi::{FieldIdx, Pointer, VariantIdx};
7+
use rustc_target::abi::{Pointer, VariantIdx};
88

99
use super::FnCtxt;
1010

@@ -28,7 +28,7 @@ fn unpack_option_like<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
2828
}
2929

3030
if def.variant(data_idx).fields.len() == 1 {
31-
return def.variant(data_idx).fields[FieldIdx::from_u32(0)].ty(tcx, substs);
31+
return def.variant(data_idx).single_field().ty(tcx, substs);
3232
}
3333
}
3434

compiler/rustc_infer/src/infer/error_reporting/suggest.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_middle::traits::{
1010
use rustc_middle::ty::print::with_no_trimmed_paths;
1111
use rustc_middle::ty::{self as ty, GenericArgKind, IsSuggestable, Ty, TypeVisitableExt};
1212
use rustc_span::{sym, BytePos, Span};
13-
use rustc_target::abi::FieldIdx;
1413

1514
use crate::errors::{
1615
ConsiderAddingAwait, FnConsiderCasting, FnItemsAreDistinct, FnUniqTypes,
@@ -114,7 +113,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
114113
variant.fields.len() == 1 && variant.ctor_kind() == Some(CtorKind::Fn)
115114
})
116115
.filter_map(|variant| {
117-
let sole_field = &variant.fields[FieldIdx::from_u32(0)];
116+
let sole_field = &variant.single_field();
118117
let sole_field_ty = sole_field.ty(self.tcx, substs);
119118
if self.same_type_modulo_infer(sole_field_ty, exp_found.found) {
120119
let variant_path =

compiler/rustc_middle/src/ty/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,16 @@ impl VariantDef {
19631963
pub fn ctor_def_id(&self) -> Option<DefId> {
19641964
self.ctor.map(|(_, def_id)| def_id)
19651965
}
1966+
1967+
/// Returns the one field in this variant.
1968+
///
1969+
/// `panic!`s if there are no fields or multiple fields.
1970+
#[inline]
1971+
pub fn single_field(&self) -> &FieldDef {
1972+
assert!(self.fields.len() == 1);
1973+
1974+
&self.fields[FieldIdx::from_u32(0)]
1975+
}
19661976
}
19671977

19681978
impl PartialEq for VariantDef {

compiler/rustc_query_system/src/dep_graph/query.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ impl<K: DepKind> DepGraphQuery<K> {
2424

2525
pub fn push(&mut self, index: DepNodeIndex, node: DepNode<K>, edges: &[DepNodeIndex]) {
2626
let source = self.graph.add_node(node);
27-
if index.index() >= self.dep_index_to_index.len() {
28-
self.dep_index_to_index.resize(index.index() + 1, None);
29-
}
30-
self.dep_index_to_index[index] = Some(source);
27+
self.dep_index_to_index.insert(index, source);
3128
self.indices.insert(node, source);
3229

3330
for &target in edges.iter() {

compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111
use super::{CanonicalGoal, Certainty, EvalCtxt, Goal};
1212
use crate::solve::canonicalize::{CanonicalizeMode, Canonicalizer};
1313
use crate::solve::{CanonicalResponse, QueryResult, Response};
14+
use rustc_index::vec::IndexVec;
1415
use rustc_infer::infer::canonical::query_response::make_query_region_constraints;
1516
use rustc_infer::infer::canonical::CanonicalVarValues;
1617
use rustc_infer::infer::canonical::{CanonicalExt, QueryRegionConstraints};
1718
use rustc_middle::traits::query::NoSolution;
1819
use rustc_middle::traits::solve::{ExternalConstraints, ExternalConstraintsData};
19-
use rustc_middle::ty::{self, GenericArgKind};
20+
use rustc_middle::ty::{self, BoundVar, GenericArgKind};
2021
use rustc_span::DUMMY_SP;
2122
use std::iter;
2223
use std::ops::Deref;
@@ -139,25 +140,25 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
139140
//
140141
// We therefore instantiate the existential variable in the canonical response with the
141142
// inference variable of the input right away, which is more performant.
142-
let mut opt_values = vec![None; response.variables.len()];
143+
let mut opt_values = IndexVec::from_elem_n(None, response.variables.len());
143144
for (original_value, result_value) in iter::zip(original_values, var_values.var_values) {
144145
match result_value.unpack() {
145146
GenericArgKind::Type(t) => {
146147
if let &ty::Bound(debruijn, b) = t.kind() {
147148
assert_eq!(debruijn, ty::INNERMOST);
148-
opt_values[b.var.index()] = Some(*original_value);
149+
opt_values[b.var] = Some(*original_value);
149150
}
150151
}
151152
GenericArgKind::Lifetime(r) => {
152153
if let ty::ReLateBound(debruijn, br) = *r {
153154
assert_eq!(debruijn, ty::INNERMOST);
154-
opt_values[br.var.index()] = Some(*original_value);
155+
opt_values[br.var] = Some(*original_value);
155156
}
156157
}
157158
GenericArgKind::Const(c) => {
158159
if let ty::ConstKind::Bound(debrujin, b) = c.kind() {
159160
assert_eq!(debrujin, ty::INNERMOST);
160-
opt_values[b.index()] = Some(*original_value);
161+
opt_values[b] = Some(*original_value);
161162
}
162163
}
163164
}
@@ -180,7 +181,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
180181
// more placeholders then they should be able to. However the inference variables have
181182
// to "come from somewhere", so by equating them with the original values of the caller
182183
// later on, we pull them down into their correct universe again.
183-
if let Some(v) = opt_values[index] {
184+
if let Some(v) = opt_values[BoundVar::from_usize(index)] {
184185
v
185186
} else {
186187
self.infcx.instantiate_canonical_var(DUMMY_SP, info, |_| prev_universe)

0 commit comments

Comments
 (0)