Skip to content
/ rust Public
forked from rust-lang/rust

Commit 162fb71

Browse files
committed
Allow SliceIndex to be indexed by ranges.
1 parent a74f3fb commit 162fb71

File tree

10 files changed

+121
-22
lines changed

10 files changed

+121
-22
lines changed

compiler/rustc_codegen_ssa/src/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::iter;
33
use rustc_index::IndexVec;
44
use rustc_index::bit_set::DenseBitSet;
55
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
6-
use rustc_middle::mir::{UnwindTerminateReason, traversal};
6+
use rustc_middle::mir::{Local, UnwindTerminateReason, traversal};
77
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv, TyAndLayout};
88
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable, TypeVisitableExt};
99
use rustc_middle::{bug, mir, span_bug};
@@ -240,7 +240,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
240240
let local_values = {
241241
let args = arg_local_refs(&mut start_bx, &mut fx, &memory_locals);
242242

243-
let mut allocate_local = |local| {
243+
let mut allocate_local = |local: Local| {
244244
let decl = &mir.local_decls[local];
245245
let layout = start_bx.layout_of(fx.monomorphize(decl.ty));
246246
assert!(!layout.ty.has_erasable_regions());

compiler/rustc_data_structures/src/sorted_map/index_map.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<I: Idx, K: Ord, V> FromIterator<(K, V)> for SortedIndexMultiMap<I, K, V> {
147147
where
148148
J: IntoIterator<Item = (K, V)>,
149149
{
150-
let items = IndexVec::from_iter(iter);
150+
let items = IndexVec::<I, _>::from_iter(iter);
151151
let mut idx_sorted_by_item_key: Vec<_> = items.indices().collect();
152152

153153
// `sort_by_key` is stable, so insertion order is preserved for duplicate items.

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,7 +791,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
791791
&& provided_arg_tys.len() == formal_and_expected_inputs.len() - 1 + tys.len()
792792
{
793793
// Wrap up the N provided arguments starting at this position in a tuple.
794-
let provided_args_to_tuple = &provided_arg_tys.raw[mismatch_idx.idx()..];
794+
let provided_args_to_tuple = &provided_arg_tys[mismatch_idx..];
795795
let (provided_args_to_tuple, provided_args_after_tuple) =
796796
provided_args_to_tuple.split_at(tys.len());
797797
let provided_as_tuple =

compiler/rustc_index/src/idx.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::fmt::Debug;
22
use std::hash::Hash;
3+
use std::ops;
4+
use std::slice::SliceIndex;
35

46
/// Represents some newtyped `usize` wrapper.
57
///
@@ -43,3 +45,92 @@ impl Idx for u32 {
4345
self as usize
4446
}
4547
}
48+
49+
/// Helper trait for indexing operations with a custom index type.
50+
pub trait IntoSliceIdx<I, T: ?Sized> {
51+
type Output: SliceIndex<T>;
52+
fn into_slice_idx(self) -> Self::Output;
53+
}
54+
55+
impl<I: Idx, T> IntoSliceIdx<I, [T]> for I {
56+
type Output = usize;
57+
#[inline]
58+
fn into_slice_idx(self) -> Self::Output {
59+
self.index()
60+
}
61+
}
62+
63+
impl<I, T> IntoSliceIdx<I, [T]> for ops::RangeFull {
64+
type Output = ops::RangeFull;
65+
#[inline]
66+
fn into_slice_idx(self) -> Self::Output {
67+
self
68+
}
69+
}
70+
71+
impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::Range<I> {
72+
type Output = ops::Range<usize>;
73+
#[inline]
74+
fn into_slice_idx(self) -> Self::Output {
75+
ops::Range { start: self.start.index(), end: self.end.index() }
76+
}
77+
}
78+
79+
impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeFrom<I> {
80+
type Output = ops::RangeFrom<usize>;
81+
#[inline]
82+
fn into_slice_idx(self) -> Self::Output {
83+
ops::RangeFrom { start: self.start.index() }
84+
}
85+
}
86+
87+
impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeTo<I> {
88+
type Output = ops::RangeTo<usize>;
89+
#[inline]
90+
fn into_slice_idx(self) -> Self::Output {
91+
..self.end.index()
92+
}
93+
}
94+
95+
impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeInclusive<I> {
96+
type Output = ops::RangeInclusive<usize>;
97+
#[inline]
98+
fn into_slice_idx(self) -> Self::Output {
99+
ops::RangeInclusive::new(self.start().index(), self.end().index())
100+
}
101+
}
102+
103+
impl<I: Idx, T> IntoSliceIdx<I, [T]> for ops::RangeToInclusive<I> {
104+
type Output = ops::RangeToInclusive<usize>;
105+
#[inline]
106+
fn into_slice_idx(self) -> Self::Output {
107+
..=self.end.index()
108+
}
109+
}
110+
111+
#[cfg(feature = "nightly")]
112+
impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::Range<I> {
113+
type Output = core::range::Range<usize>;
114+
#[inline]
115+
fn into_slice_idx(self) -> Self::Output {
116+
core::range::Range { start: self.start.index(), end: self.end.index() }
117+
}
118+
}
119+
120+
#[cfg(feature = "nightly")]
121+
impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeFrom<I> {
122+
type Output = core::range::RangeFrom<usize>;
123+
#[inline]
124+
fn into_slice_idx(self) -> Self::Output {
125+
core::range::RangeFrom { start: self.start.index() }
126+
}
127+
}
128+
129+
#[cfg(feature = "nightly")]
130+
impl<I: Idx, T> IntoSliceIdx<I, [T]> for core::range::RangeInclusive<I> {
131+
type Output = core::range::RangeInclusive<usize>;
132+
#[inline]
133+
fn into_slice_idx(self) -> Self::Output {
134+
core::range::RangeInclusive { start: self.start.index(), end: self.end.index() }
135+
}
136+
}

compiler/rustc_index/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![cfg_attr(all(feature = "nightly", test), feature(stmt_expr_attributes))]
33
#![cfg_attr(feature = "nightly", allow(internal_features))]
44
#![cfg_attr(feature = "nightly", feature(extend_one, step_trait, test))]
5+
#![cfg_attr(feature = "nightly", feature(new_range_api))]
56
#![cfg_attr(feature = "nightly", feature(new_zeroed_alloc))]
67
#![warn(unreachable_pub)]
78
// tidy-alphabetical-end
@@ -14,7 +15,7 @@ mod idx;
1415
mod slice;
1516
mod vec;
1617

17-
pub use idx::Idx;
18+
pub use idx::{Idx, IntoSliceIdx};
1819
pub use rustc_index_macros::newtype_index;
1920
pub use slice::IndexSlice;
2021
#[doc(no_inline)]

compiler/rustc_index/src/slice.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
use std::fmt;
12
use std::marker::PhantomData;
23
use std::ops::{Index, IndexMut};
3-
use std::{fmt, slice};
4+
use std::slice::{self, SliceIndex};
45

5-
use crate::{Idx, IndexVec};
6+
use crate::{Idx, IndexVec, IntoSliceIdx};
67

78
/// A view into contiguous `T`s, indexed by `I` rather than by `usize`.
89
///
@@ -99,13 +100,19 @@ impl<I: Idx, T> IndexSlice<I, T> {
99100
}
100101

101102
#[inline]
102-
pub fn get(&self, index: I) -> Option<&T> {
103-
self.raw.get(index.index())
103+
pub fn get<R: IntoSliceIdx<I, [T]>>(
104+
&self,
105+
index: R,
106+
) -> Option<&<R::Output as SliceIndex<[T]>>::Output> {
107+
self.raw.get(index.into_slice_idx())
104108
}
105109

106110
#[inline]
107-
pub fn get_mut(&mut self, index: I) -> Option<&mut T> {
108-
self.raw.get_mut(index.index())
111+
pub fn get_mut<R: IntoSliceIdx<I, [T]>>(
112+
&mut self,
113+
index: R,
114+
) -> Option<&mut <R::Output as SliceIndex<[T]>>::Output> {
115+
self.raw.get_mut(index.into_slice_idx())
109116
}
110117

111118
/// Returns mutable references to two distinct elements, `a` and `b`.
@@ -186,19 +193,19 @@ impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexSlice<I, T> {
186193
}
187194
}
188195

189-
impl<I: Idx, T> Index<I> for IndexSlice<I, T> {
190-
type Output = T;
196+
impl<I: Idx, T, R: IntoSliceIdx<I, [T]>> Index<R> for IndexSlice<I, T> {
197+
type Output = <R::Output as SliceIndex<[T]>>::Output;
191198

192199
#[inline]
193-
fn index(&self, index: I) -> &T {
194-
&self.raw[index.index()]
200+
fn index(&self, index: R) -> &Self::Output {
201+
&self.raw[index.into_slice_idx()]
195202
}
196203
}
197204

198-
impl<I: Idx, T> IndexMut<I> for IndexSlice<I, T> {
205+
impl<I: Idx, T, R: IntoSliceIdx<I, [T]>> IndexMut<R> for IndexSlice<I, T> {
199206
#[inline]
200-
fn index_mut(&mut self, index: I) -> &mut T {
201-
&mut self.raw[index.index()]
207+
fn index_mut(&mut self, index: R) -> &mut Self::Output {
208+
&mut self.raw[index.into_slice_idx()]
202209
}
203210
}
204211

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
160160
/// empty region. The `expansion` phase will grow this larger.
161161
fn construct_var_data(&self) -> LexicalRegionResolutions<'tcx> {
162162
LexicalRegionResolutions {
163-
values: IndexVec::from_fn_n(
163+
values: IndexVec::<RegionVid, _>::from_fn_n(
164164
|vid| {
165165
let vid_universe = self.var_infos[vid].universe;
166166
VarValue::Empty(vid_universe)

compiler/rustc_mir_transform/src/coverage/counters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn make_node_flow_priority_list(
4747
// A "reloop" node has exactly one out-edge, which jumps back to the top
4848
// of an enclosing loop. Reloop nodes are typically visited more times
4949
// than loop-exit nodes, so try to avoid giving them physical counters.
50-
let is_reloop_node = IndexVec::from_fn_n(
50+
let is_reloop_node = IndexVec::<BasicCoverageBlock, _>::from_fn_n(
5151
|node| match graph.successors[node].as_slice() {
5252
&[succ] => graph.dominates(succ, node),
5353
_ => false,

compiler/rustc_mir_transform/src/coverage/graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl CoverageGraph {
4242
// `SwitchInt` to have multiple targets to the same destination `BasicBlock`, so
4343
// de-duplication is required. This is done without reordering the successors.
4444

45-
let successors = IndexVec::from_fn_n(
45+
let successors = IndexVec::<BasicCoverageBlock, _>::from_fn_n(
4646
|bcb| {
4747
let mut seen_bcbs = FxHashSet::default();
4848
let terminator = mir_body[bcbs[bcb].last_bb()].terminator();

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
12591259

12601260
let layout = self.ecx.layout_of(lhs_ty).ok()?;
12611261

1262-
let as_bits = |value| {
1262+
let as_bits = |value: VnIndex| {
12631263
let constant = self.evaluated[value].as_ref()?;
12641264
if layout.backend_repr.is_scalar() {
12651265
let scalar = self.ecx.read_scalar(constant).discard_err()?;

0 commit comments

Comments
 (0)