Skip to content

Commit 37e1d5b

Browse files
committed
Some cleanup of index types in rustc_hir_typeck
1 parent 820bfff commit 37e1d5b

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
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: 18 additions & 18 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_arg_tys.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,10 +815,9 @@ 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 Some((_, lo)) = provided_arg_tys.get(mismatch_idx)
819+
// Note: `provided_args_to_tuple` is not empty since `tys` is also not empty
820+
&& let Some(&(_, hi)) = provided_args_to_tuple.last()
821821
{
822822
let mut err;
823823
if tys.len() == 1 {
@@ -826,8 +826,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
826826
err = self.err_ctxt().report_and_explain_type_error(
827827
mk_trace(
828828
*lo,
829-
formal_and_expected_inputs[mismatch_idx.into()],
830-
provided_arg_tys[mismatch_idx.into()].0,
829+
formal_and_expected_inputs[mismatch_idx.to_expected_idx()],
830+
provided_arg_tys[mismatch_idx].0,
831831
),
832832
self.param_env,
833833
terr,
@@ -866,7 +866,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
866866
callee_ty,
867867
call_expr,
868868
None,
869-
Some(mismatch_idx),
869+
Some(mismatch_idx.as_usize()),
870870
&matched_inputs,
871871
&formal_and_expected_inputs,
872872
is_method,
@@ -2648,7 +2648,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
26482648
}
26492649

26502650
let expected_display_type = self
2651-
.resolve_vars_if_possible(formal_and_expected_inputs[idx.into()].1)
2651+
.resolve_vars_if_possible(formal_and_expected_inputs[idx].1)
26522652
.sort_string(self.tcx);
26532653
let label = if idxs_matched == params_with_generics.len() - 1 {
26542654
format!(

0 commit comments

Comments
 (0)