Skip to content

Commit 6041d18

Browse files
committed
Some cleanup of index types in rustc_hir_typeck
1 parent 820bfff commit 6041d18

File tree

2 files changed

+23
-19
lines changed

2 files changed

+23
-19
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/arg_matrix.rs

Lines changed: 6 additions & 0 deletions
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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
773773

774774
// First, check if we just need to wrap some arguments in a tuple.
775775
if let Some((mismatch_idx, terr)) =
776-
compatibility_diagonal.iter().enumerate().find_map(|(i, c)| {
776+
compatibility_diagonal.iter_enumerated().find_map(|(i, c)| {
777777
if let Compatibility::Incompatible(Some(terr)) = c {
778778
Some((i, *terr))
779779
} else {
@@ -785,24 +785,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
785785
// Do we have as many extra provided arguments as the tuple's length?
786786
// If so, we might have just forgotten to wrap some args in a tuple.
787787
if let Some(ty::Tuple(tys)) =
788-
formal_and_expected_inputs.get(mismatch_idx.into()).map(|tys| tys.1.kind())
788+
formal_and_expected_inputs.get(mismatch_idx.to_expected_idx()).map(|tys| tys.1.kind())
789789
// If the tuple is unit, we're not actually wrapping any arguments.
790790
&& !tys.is_empty()
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_as_tuple = Ty::new_tup_from_iter(
795-
tcx,
796-
provided_arg_tys.iter().map(|(ty, _)| *ty).skip(mismatch_idx).take(tys.len()),
797-
);
794+
let provided_args_to_tuple = &provided_arg_tys.raw[mismatch_idx.idx()..];
795+
let (provided_args_to_tuple, provided_args_after_tuple) =
796+
provided_args_to_tuple.split_at(tys.len());
797+
let provided_as_tuple =
798+
Ty::new_tup_from_iter(tcx, provided_args_to_tuple.iter().map(|&(ty, _)| ty));
798799

799800
let mut satisfied = true;
800801
// Check if the newly wrapped tuple + rest of the arguments are compatible.
801802
for ((_, expected_ty), provided_ty) in std::iter::zip(
802-
formal_and_expected_inputs.iter().skip(mismatch_idx),
803-
[provided_as_tuple].into_iter().chain(
804-
provided_arg_tys.iter().map(|(ty, _)| *ty).skip(mismatch_idx + tys.len()),
805-
),
803+
formal_and_expected_inputs[mismatch_idx.to_expected_idx()..].iter(),
804+
[provided_as_tuple]
805+
.into_iter()
806+
.chain(provided_args_after_tuple.iter().map(|&(ty, _)| ty)),
806807
) {
807808
if !self.may_coerce(provided_ty, *expected_ty) {
808809
satisfied = false;
@@ -814,20 +815,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
814815
// Take some care with spans, so we don't suggest wrapping a macro's
815816
// innards in parenthesis, for example.
816817
if satisfied
817-
&& let Some((_, lo)) =
818-
provided_arg_tys.get(ProvidedIdx::from_usize(mismatch_idx))
819-
&& let Some((_, hi)) =
820-
provided_arg_tys.get(ProvidedIdx::from_usize(mismatch_idx + tys.len() - 1))
818+
&& let &[(_, hi @ lo)] | &[(_, lo), .., (_, hi)] = provided_args_to_tuple
821819
{
822820
let mut err;
823821
if tys.len() == 1 {
824822
// A tuple wrap suggestion actually occurs within,
825823
// so don't do anything special here.
826824
err = self.err_ctxt().report_and_explain_type_error(
827825
mk_trace(
828-
*lo,
829-
formal_and_expected_inputs[mismatch_idx.into()],
830-
provided_arg_tys[mismatch_idx.into()].0,
826+
lo,
827+
formal_and_expected_inputs[mismatch_idx.to_expected_idx()],
828+
provided_arg_tys[mismatch_idx].0,
831829
),
832830
self.param_env,
833831
terr,
@@ -866,7 +864,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
866864
callee_ty,
867865
call_expr,
868866
None,
869-
Some(mismatch_idx),
867+
Some(mismatch_idx.as_usize()),
870868
&matched_inputs,
871869
&formal_and_expected_inputs,
872870
is_method,
@@ -2648,7 +2646,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26482646
}
26492647

26502648
let expected_display_type = self
2651-
.resolve_vars_if_possible(formal_and_expected_inputs[idx.into()].1)
2649+
.resolve_vars_if_possible(formal_and_expected_inputs[idx].1)
26522650
.sort_string(self.tcx);
26532651
let label = if idxs_matched == params_with_generics.len() - 1 {
26542652
format!(

0 commit comments

Comments
 (0)