Skip to content

Commit

Permalink
Don't do expected type fudging if the inputs don't need inference gui…
Browse files Browse the repository at this point in the history
…dance
  • Loading branch information
compiler-errors committed Aug 25, 2024
1 parent 6ab301c commit f6f5d72
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 36 deletions.
24 changes: 14 additions & 10 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1669,17 +1669,21 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let tcx = self.tcx;

let adt_ty = self.resolve_vars_with_obligations(adt_ty);
let adt_ty_hint = expected.only_has_type(self).and_then(|expected| {
self.fudge_inference_if_ok(|| {
let ocx = ObligationCtxt::new(self);
ocx.sup(&self.misc(span), self.param_env, expected, adt_ty)?;
if !ocx.select_where_possible().is_empty() {
return Err(TypeError::Mismatch);
}
Ok(self.resolve_vars_if_possible(adt_ty))
let adt_ty_hint = if adt_ty.has_non_region_infer() {
expected.only_has_type(self).and_then(|expected| {
self.fudge_inference_if_ok(|| {
let ocx = ObligationCtxt::new(self);
ocx.sup(&self.misc(span), self.param_env, expected, adt_ty)?;
if !ocx.select_where_possible().is_empty() {
return Err(TypeError::Mismatch);
}
Ok(self.resolve_vars_if_possible(adt_ty))
})
.ok()
})
.ok()
});
} else {
None
};
if let Some(adt_ty_hint) = adt_ty_hint {
// re-link the variables that the fudging above can create.
self.demand_eqtype(span, adt_ty_hint, adt_ty);
Expand Down
59 changes: 33 additions & 26 deletions compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,35 +211,42 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// We use this to guide coercion inference; it's output is "fudged" which means
// any remaining type variables are assigned to new, unrelated variables. This
// is because the inference guidance here is only speculative.
//
// We only do this if the formals have non-region infer vars, since this is only
// meant to guide inference.
let formal_output = self.resolve_vars_with_obligations(formal_output);
let expected_input_tys: Option<Vec<_>> = expectation
.only_has_type(self)
.and_then(|expected_output| {
self.fudge_inference_if_ok(|| {
let ocx = ObligationCtxt::new(self);

// Attempt to apply a subtyping relationship between the formal
// return type (likely containing type variables if the function
// is polymorphic) and the expected return type.
// No argument expectations are produced if unification fails.
let origin = self.misc(call_span);
ocx.sup(&origin, self.param_env, expected_output, formal_output)?;
if !ocx.select_where_possible().is_empty() {
return Err(TypeError::Mismatch);
}
let expected_input_tys: Option<Vec<_>> = if formal_input_tys.has_non_region_infer() {
expectation
.only_has_type(self)
.and_then(|expected_output| {
self.fudge_inference_if_ok(|| {
let ocx = ObligationCtxt::new(self);

// Attempt to apply a subtyping relationship between the formal
// return type (likely containing type variables if the function
// is polymorphic) and the expected return type.
// No argument expectations are produced if unification fails.
let origin = self.misc(call_span);
ocx.sup(&origin, self.param_env, expected_output, formal_output)?;
if !ocx.select_where_possible().is_empty() {
return Err(TypeError::Mismatch);
}

// Record all the argument types, with the args
// produced from the above subtyping unification.
Ok(Some(
formal_input_tys
.iter()
.map(|&ty| self.resolve_vars_if_possible(ty))
.collect(),
))
// Record all the argument types, with the args
// produced from the above subtyping unification.
Ok(Some(
formal_input_tys
.iter()
.map(|&ty| self.resolve_vars_if_possible(ty))
.collect(),
))
})
.ok()
})
.ok()
})
.unwrap_or_default();
.unwrap_or_default()
} else {
None
};

let mut err_code = E0061;

Expand Down

0 comments on commit f6f5d72

Please sign in to comment.