Skip to content

Commit bb029a1

Browse files
committed
Auto merge of #137511 - jhpratt:rollup-07whsax, r=jhpratt
Rollup of 10 pull requests Successful merges: - #136610 (Allow `IndexSlice` to be indexed by ranges.) - #136991 ([rustdoc] Add new setting to wrap source code lines when too long) - #137061 (Unstable `gen_future` Feature Tracking ) - #137393 (Stabilize `unbounded_shifts`) - #137482 (Windows: use existing wrappers in `File::open_native`) - #137484 (Fix documentation for unstable sort on slice) - #137491 (Tighten `str-to-string-128690.rs``CHECK{,-NOT}`s to make it less likely to incorrectly fail with symbol name mangling) - #137495 (Added into_value function to ControlFlow<T, T>) - #137501 (Move `impl` blocks out of `rustc_middle/src/mir/syntax.rs`) - #137505 (Add a span to `CompilerBuiltinsCannotCall`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f43e549 + 42014b4 commit bb029a1

File tree

37 files changed

+852
-333
lines changed

37 files changed

+852
-333
lines changed

compiler/rustc_codegen_cranelift/src/abi/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,13 @@ pub(crate) fn codegen_terminator_call<'tcx>(
402402

403403
if is_call_from_compiler_builtins_to_upstream_monomorphization(fx.tcx, instance) {
404404
if target.is_some() {
405-
let caller = with_no_trimmed_paths!(fx.tcx.def_path_str(fx.instance.def_id()));
406-
let callee = with_no_trimmed_paths!(fx.tcx.def_path_str(def_id));
407-
fx.tcx.dcx().emit_err(CompilerBuiltinsCannotCall { caller, callee });
405+
let caller_def = fx.instance.def_id();
406+
let e = CompilerBuiltinsCannotCall {
407+
span: fx.tcx.def_span(caller_def),
408+
caller: with_no_trimmed_paths!(fx.tcx.def_path_str(caller_def)),
409+
callee: with_no_trimmed_paths!(fx.tcx.def_path_str(def_id)),
410+
};
411+
fx.tcx.dcx().emit_err(e);
408412
} else {
409413
fx.bcx.ins().trap(TrapCode::user(2).unwrap());
410414
return;

compiler/rustc_codegen_ssa/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,8 @@ pub(crate) struct ErrorCreatingRemarkDir {
11801180
pub struct CompilerBuiltinsCannotCall {
11811181
pub caller: String,
11821182
pub callee: String,
1183+
#[primary_span]
1184+
pub span: Span,
11831185
}
11841186

11851187
#[derive(Diagnostic)]

compiler/rustc_codegen_ssa/src/mir/block.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,13 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
165165
if let Some(instance) = instance {
166166
if is_call_from_compiler_builtins_to_upstream_monomorphization(tcx, instance) {
167167
if destination.is_some() {
168-
let caller = with_no_trimmed_paths!(tcx.def_path_str(fx.instance.def_id()));
169-
let callee = with_no_trimmed_paths!(tcx.def_path_str(instance.def_id()));
170-
tcx.dcx().emit_err(CompilerBuiltinsCannotCall { caller, callee });
168+
let caller_def = fx.instance.def_id();
169+
let e = CompilerBuiltinsCannotCall {
170+
span: tcx.def_span(caller_def),
171+
caller: with_no_trimmed_paths!(tcx.def_path_str(caller_def)),
172+
callee: with_no_trimmed_paths!(tcx.def_path_str(instance.def_id())),
173+
};
174+
tcx.dcx().emit_err(e);
171175
} else {
172176
info!(
173177
"compiler_builtins call to diverging function {:?} replaced with abort",

compiler/rustc_codegen_ssa/src/mir/mod.rs

+2-2
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

+1-1
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/arg_matrix.rs

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ impl ExpectedIdx {
2222
}
2323
}
2424

25+
impl ProvidedIdx {
26+
pub(crate) fn to_expected_idx(self) -> ExpectedIdx {
27+
ExpectedIdx::from_u32(self.as_u32())
28+
}
29+
}
30+
2531
// An issue that might be found in the compatibility matrix
2632
#[derive(Debug)]
2733
enum Issue {

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+17-19
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
775775

776776
// First, check if we just need to wrap some arguments in a tuple.
777777
if let Some((mismatch_idx, terr)) =
778-
compatibility_diagonal.iter().enumerate().find_map(|(i, c)| {
778+
compatibility_diagonal.iter_enumerated().find_map(|(i, c)| {
779779
if let Compatibility::Incompatible(Some(terr)) = c {
780780
Some((i, *terr))
781781
} else {
@@ -787,24 +787,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
787787
// Do we have as many extra provided arguments as the tuple's length?
788788
// If so, we might have just forgotten to wrap some args in a tuple.
789789
if let Some(ty::Tuple(tys)) =
790-
formal_and_expected_inputs.get(mismatch_idx.into()).map(|tys| tys.1.kind())
790+
formal_and_expected_inputs.get(mismatch_idx.to_expected_idx()).map(|tys| tys.1.kind())
791791
// If the tuple is unit, we're not actually wrapping any arguments.
792792
&& !tys.is_empty()
793793
&& provided_arg_tys.len() == formal_and_expected_inputs.len() - 1 + tys.len()
794794
{
795795
// Wrap up the N provided arguments starting at this position in a tuple.
796-
let provided_as_tuple = Ty::new_tup_from_iter(
797-
tcx,
798-
provided_arg_tys.iter().map(|(ty, _)| *ty).skip(mismatch_idx).take(tys.len()),
799-
);
796+
let provided_args_to_tuple = &provided_arg_tys[mismatch_idx..];
797+
let (provided_args_to_tuple, provided_args_after_tuple) =
798+
provided_args_to_tuple.split_at(tys.len());
799+
let provided_as_tuple =
800+
Ty::new_tup_from_iter(tcx, provided_args_to_tuple.iter().map(|&(ty, _)| ty));
800801

801802
let mut satisfied = true;
802803
// Check if the newly wrapped tuple + rest of the arguments are compatible.
803804
for ((_, expected_ty), provided_ty) in std::iter::zip(
804-
formal_and_expected_inputs.iter().skip(mismatch_idx),
805-
[provided_as_tuple].into_iter().chain(
806-
provided_arg_tys.iter().map(|(ty, _)| *ty).skip(mismatch_idx + tys.len()),
807-
),
805+
formal_and_expected_inputs[mismatch_idx.to_expected_idx()..].iter(),
806+
[provided_as_tuple]
807+
.into_iter()
808+
.chain(provided_args_after_tuple.iter().map(|&(ty, _)| ty)),
808809
) {
809810
if !self.may_coerce(provided_ty, *expected_ty) {
810811
satisfied = false;
@@ -816,20 +817,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
816817
// Take some care with spans, so we don't suggest wrapping a macro's
817818
// innards in parenthesis, for example.
818819
if satisfied
819-
&& let Some((_, lo)) =
820-
provided_arg_tys.get(ProvidedIdx::from_usize(mismatch_idx))
821-
&& let Some((_, hi)) =
822-
provided_arg_tys.get(ProvidedIdx::from_usize(mismatch_idx + tys.len() - 1))
820+
&& let &[(_, hi @ lo)] | &[(_, lo), .., (_, hi)] = provided_args_to_tuple
823821
{
824822
let mut err;
825823
if tys.len() == 1 {
826824
// A tuple wrap suggestion actually occurs within,
827825
// so don't do anything special here.
828826
err = self.err_ctxt().report_and_explain_type_error(
829827
mk_trace(
830-
*lo,
831-
formal_and_expected_inputs[mismatch_idx.into()],
832-
provided_arg_tys[mismatch_idx.into()].0,
828+
lo,
829+
formal_and_expected_inputs[mismatch_idx.to_expected_idx()],
830+
provided_arg_tys[mismatch_idx].0,
833831
),
834832
self.param_env,
835833
terr,
@@ -868,7 +866,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
868866
callee_ty,
869867
call_expr,
870868
None,
871-
Some(mismatch_idx),
869+
Some(mismatch_idx.as_usize()),
872870
&matched_inputs,
873871
&formal_and_expected_inputs,
874872
is_method,
@@ -2615,7 +2613,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26152613
}
26162614

26172615
let expected_display_type = self
2618-
.resolve_vars_if_possible(formal_and_expected_inputs[idx.into()].1)
2616+
.resolve_vars_if_possible(formal_and_expected_inputs[idx].1)
26192617
.sort_string(self.tcx);
26202618
let label = if idxs_matched == params_with_generics.len() - 1 {
26212619
format!(

compiler/rustc_index/src/idx.rs

+91
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

+2-1
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

+20-13
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
///
@@ -97,13 +98,19 @@ impl<I: Idx, T> IndexSlice<I, T> {
9798
}
9899

99100
#[inline]
100-
pub fn get(&self, index: I) -> Option<&T> {
101-
self.raw.get(index.index())
101+
pub fn get<R: IntoSliceIdx<I, [T]>>(
102+
&self,
103+
index: R,
104+
) -> Option<&<R::Output as SliceIndex<[T]>>::Output> {
105+
self.raw.get(index.into_slice_idx())
102106
}
103107

104108
#[inline]
105-
pub fn get_mut(&mut self, index: I) -> Option<&mut T> {
106-
self.raw.get_mut(index.index())
109+
pub fn get_mut<R: IntoSliceIdx<I, [T]>>(
110+
&mut self,
111+
index: R,
112+
) -> Option<&mut <R::Output as SliceIndex<[T]>>::Output> {
113+
self.raw.get_mut(index.into_slice_idx())
107114
}
108115

109116
/// Returns mutable references to two distinct elements, `a` and `b`.
@@ -184,19 +191,19 @@ impl<I: Idx, T: fmt::Debug> fmt::Debug for IndexSlice<I, T> {
184191
}
185192
}
186193

187-
impl<I: Idx, T> Index<I> for IndexSlice<I, T> {
188-
type Output = T;
194+
impl<I: Idx, T, R: IntoSliceIdx<I, [T]>> Index<R> for IndexSlice<I, T> {
195+
type Output = <R::Output as SliceIndex<[T]>>::Output;
189196

190197
#[inline]
191-
fn index(&self, index: I) -> &T {
192-
&self.raw[index.index()]
198+
fn index(&self, index: R) -> &Self::Output {
199+
&self.raw[index.into_slice_idx()]
193200
}
194201
}
195202

196-
impl<I: Idx, T> IndexMut<I> for IndexSlice<I, T> {
203+
impl<I: Idx, T, R: IntoSliceIdx<I, [T]>> IndexMut<R> for IndexSlice<I, T> {
197204
#[inline]
198-
fn index_mut(&mut self, index: I) -> &mut T {
199-
&mut self.raw[index.index()]
205+
fn index_mut(&mut self, index: R) -> &mut Self::Output {
206+
&mut self.raw[index.into_slice_idx()]
200207
}
201208
}
202209

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

+1-1
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_middle/src/mir/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ impl<'tcx> HasLocalDecls<'tcx> for Body<'tcx> {
9696
}
9797

9898
impl MirPhase {
99+
pub fn name(&self) -> &'static str {
100+
match *self {
101+
MirPhase::Built => "built",
102+
MirPhase::Analysis(AnalysisPhase::Initial) => "analysis",
103+
MirPhase::Analysis(AnalysisPhase::PostCleanup) => "analysis-post-cleanup",
104+
MirPhase::Runtime(RuntimePhase::Initial) => "runtime",
105+
MirPhase::Runtime(RuntimePhase::PostCleanup) => "runtime-post-cleanup",
106+
MirPhase::Runtime(RuntimePhase::Optimized) => "runtime-optimized",
107+
}
108+
}
109+
99110
/// Gets the (dialect, phase) index of the current `MirPhase`. Both numbers
100111
/// are 1-indexed.
101112
pub fn index(&self) -> (usize, usize) {

0 commit comments

Comments
 (0)