Skip to content

Commit d121a7f

Browse files
committed
Address low hanging fruit review comments, more to come
1 parent 7dd9337 commit d121a7f

File tree

3 files changed

+13
-55
lines changed

3 files changed

+13
-55
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+13-33
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
512512
.borrow()
513513
.expr_ty_adjusted_opt(*expr)
514514
.unwrap_or_else(|| Ty::new_misc_error(tcx));
515-
(ty, normalize_span(expr.span))
515+
(self.resolve_vars_if_possible(ty), normalize_span(expr.span))
516516
})
517517
.collect();
518518
let callee_expr = match &call_expr.peel_blocks().kind {
@@ -649,7 +649,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
649649
}
650650

651651
let (arg_ty, arg_span) = provided_arg_tys[provided_idx];
652-
let arg_ty = self.resolve_vars_if_possible(arg_ty);
653652

654653
let expectation = Expectation::rvalue_hint(self, expected_input_ty);
655654
let coerced_ty = expectation.only_has_type(self).unwrap_or(formal_input_ty);
@@ -722,11 +721,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
722721
// Wrap up the N provided arguments starting at this position in a tuple.
723722
let provided_as_tuple = Ty::new_tup_from_iter(
724723
tcx,
725-
provided_arg_tys
726-
.iter()
727-
.map(|(ty, _)| self.resolve_vars_if_possible(*ty))
728-
.skip(mismatch_idx)
729-
.take(tys.len()),
724+
provided_arg_tys.iter().map(|(ty, _)| *ty).skip(mismatch_idx).take(tys.len()),
730725
);
731726

732727
let mut satisfied = true;
@@ -737,10 +732,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
737732
provided_arg_tys.iter().map(|(ty, _)| *ty).skip(mismatch_idx + tys.len()),
738733
),
739734
) {
740-
if !self.can_coerce(
741-
self.resolve_vars_if_possible(provided_ty),
742-
self.resolve_vars_if_possible(*expected_ty),
743-
) {
735+
if !self.can_coerce(provided_ty, self.resolve_vars_if_possible(*expected_ty)) {
744736
satisfied = false;
745737
break;
746738
}
@@ -767,10 +759,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
767759
mk_trace(
768760
*lo,
769761
(formal_ty, expected_ty),
770-
self.resolve_vars_if_possible(
771-
provided_arg_tys[mismatch_idx.into()],
772-
)
773-
.0,
762+
provided_arg_tys[mismatch_idx.into()].0,
774763
),
775764
terr,
776765
);
@@ -853,7 +842,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
853842
return true;
854843
};
855844
let (provided_ty, provided_span) = provided_arg_tys[*provided_idx];
856-
let provided_ty = self.resolve_vars_if_possible(provided_ty);
857845
let (formal_ty, expected_ty) = formal_and_expected_inputs[*expected_idx];
858846
let formal_ty = self.resolve_vars_if_possible(formal_ty);
859847
let expected_ty = self.resolve_vars_if_possible(expected_ty);
@@ -888,7 +876,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
888876
let formal_ty = self.resolve_vars_if_possible(formal_ty);
889877
let expected_ty = self.resolve_vars_if_possible(expected_ty);
890878
let (provided_ty, provided_arg_span) = provided_arg_tys[*provided_idx];
891-
let provided_ty = self.resolve_vars_if_possible(provided_ty);
892879
let trace = mk_trace(provided_arg_span, (formal_ty, expected_ty), provided_ty);
893880
let mut err = self.err_ctxt().report_and_explain_type_error(trace, *err);
894881
self.emit_coerce_suggestions(
@@ -903,7 +890,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
903890
);
904891
err.span_label(full_call_span, format!("arguments to this {call_name} are incorrect"));
905892

906-
self.emit_generic_mismatches(
893+
self.label_generic_mismatches(
907894
&mut err,
908895
fn_def_id,
909896
&matched_inputs,
@@ -1019,7 +1006,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10191006
let formal_ty = self.resolve_vars_if_possible(formal_ty);
10201007
let expected_ty = self.resolve_vars_if_possible(expected_ty);
10211008
let (provided_ty, provided_span) = provided_arg_tys[provided_idx];
1022-
let provided_ty = self.resolve_vars_if_possible(provided_ty);
10231009
if let Compatibility::Incompatible(error) = compatibility {
10241010
let trace = mk_trace(provided_span, (formal_ty, expected_ty), provided_ty);
10251011
if let Some(e) = error {
@@ -1048,7 +1034,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10481034
}
10491035
Error::Extra(arg_idx) => {
10501036
let (provided_ty, provided_span) = provided_arg_tys[arg_idx];
1051-
let provided_ty = self.resolve_vars_if_possible(provided_ty);
10521037
let provided_ty_name = if !has_error_or_infer([provided_ty]) {
10531038
// FIXME: not suggestable, use something else
10541039
format!(" of type `{provided_ty}`")
@@ -1132,12 +1117,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11321117
} else {
11331118
args_span
11341119
};
1135-
let rendered =
1136-
if !has_error_or_infer([self.resolve_vars_if_possible(input_ty)]) {
1137-
format!(" of type `{input_ty}`")
1138-
} else {
1139-
"".to_string()
1140-
};
1120+
let rendered = if !has_error_or_infer([input_ty]) {
1121+
format!(" of type `{input_ty}`")
1122+
} else {
1123+
"".to_string()
1124+
};
11411125
labels.push((span, format!("an argument{rendered} is missing")));
11421126
suggestion_text = match suggestion_text {
11431127
SuggestionText::None => SuggestionText::Provide(false),
@@ -1244,7 +1228,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12441228
second_expected_idx,
12451229
) => {
12461230
let (first_provided_ty, first_span) = provided_arg_tys[first_provided_idx];
1247-
let first_provided_ty = self.resolve_vars_if_possible(first_provided_ty);
12481231
let (_, first_expected_ty) = formal_and_expected_inputs[first_expected_idx];
12491232
let first_expected_ty = self.resolve_vars_if_possible(first_expected_ty);
12501233
let first_provided_ty_name = if !has_error_or_infer([first_provided_ty]) {
@@ -1258,7 +1241,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12581241
));
12591242

12601243
let (second_provided_ty, second_span) = provided_arg_tys[second_provided_idx];
1261-
let second_provided_ty = self.resolve_vars_if_possible(second_provided_ty);
12621244
let (_, second_expected_ty) = formal_and_expected_inputs[second_expected_idx];
12631245
let second_provided_ty = self.resolve_vars_if_possible(second_provided_ty);
12641246
let second_provided_ty_name = if !has_error_or_infer([second_provided_ty]) {
@@ -1281,7 +1263,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12811263
let (_, expected_ty) = formal_and_expected_inputs[dst_arg];
12821264
let expected_ty = self.resolve_vars_if_possible(expected_ty);
12831265
let (provided_ty, provided_span) = provided_arg_tys[dest_input];
1284-
let provided_ty = self.resolve_vars_if_possible(provided_ty);
12851266
let provided_ty_name = if !has_error_or_infer([provided_ty]) {
12861267
format!(", found `{provided_ty}`")
12871268
} else {
@@ -1301,7 +1282,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13011282
}
13021283
}
13031284

1304-
self.emit_generic_mismatches(
1285+
self.label_generic_mismatches(
13051286
&mut err,
13061287
fn_def_id,
13071288
&matched_inputs,
@@ -2259,7 +2240,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22592240
.collect();
22602241

22612242
if params.len() == param_generics.len() {
2262-
// TODO: IndexVec or something like that from data structures better?
22632243
let mut generics_map: Vec<(usize, &hir::GenericParam<'_>)> = Vec::new();
22642244
// This is a map from the index of the generic to the index of the parameter and the
22652245
// parameter
@@ -2480,7 +2460,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24802460
}
24812461
}
24822462

2483-
fn emit_generic_mismatches(
2463+
fn label_generic_mismatches(
24842464
&self,
24852465
err: &mut DiagnosticBuilder<'_>,
24862466
callable_def_id: Option<DefId>,
@@ -2516,7 +2496,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25162496
.filter(|(idx, _)| *idx > matched_idx)
25172497
{
25182498
if let ty::Infer(ty::TyVar(b)) = formal_ty.kind() {
2519-
if self.check_ty_vars_equal(*a, *b) {
2499+
if self.root_var(*a) == self.root_var(*b) {
25202500
formal_ty_idxs_matched.push(input_idx.into());
25212501
if expected_ty_matched.is_none() {
25222502
expected_ty_matched = Some(expected_ty);

compiler/rustc_infer/src/infer/mod.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1370,11 +1370,6 @@ impl<'tcx> InferCtxt<'tcx> {
13701370
self.inner.borrow_mut().effect_unification_table().find(var).vid
13711371
}
13721372

1373-
/// Check variables are equal
1374-
pub fn check_ty_vars_equal(&self, a: ty::TyVid, b: ty::TyVid) -> bool {
1375-
self.inner.borrow_mut().type_variables().check_equal(a, b)
1376-
}
1377-
13781373
/// Resolves an int var to a rigid int type, if it was constrained to one,
13791374
/// or else the root int var in the unification table.
13801375
pub fn opportunistic_resolve_int_var(&self, vid: ty::IntVid) -> Ty<'tcx> {

compiler/rustc_infer/src/infer/type_variable.rs

-17
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,6 @@ impl<'tcx> TypeVariableTable<'_, 'tcx> {
191191
self.eq_relations().find(vid).vid
192192
}
193193

194-
/// Returns the "root" variable of `vid` in the `sub_relations`
195-
/// equivalence table. All type variables that have been are
196-
/// related via equality or subtyping will yield the same root
197-
/// variable (per the union-find algorithm), so `sub_root_var(a)
198-
/// == sub_root_var(b)` implies that:
199-
/// ```text
200-
/// exists X. (a <: X || X <: a) && (b <: X || X <: b)
201-
/// ```
202-
pub fn sub_root_var(&mut self, vid: ty::TyVid) -> ty::TyVid {
203-
self.sub_relations().find(vid)
204-
}
205-
206-
/// Returns `true` if `a` and `b` have same "root" (i.e., `a == b`).
207-
pub fn check_equal(&mut self, a: ty::TyVid, b: ty::TyVid) -> bool {
208-
self.root_var(a) == self.root_var(b)
209-
}
210-
211194
/// Retrieves the type to which `vid` has been instantiated, if
212195
/// any.
213196
pub fn probe(&mut self, vid: ty::TyVid) -> TypeVariableValue<'tcx> {

0 commit comments

Comments
 (0)