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 7246d31 commit c286326
Showing 1 changed file with 33 additions and 26 deletions.
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,34 +211,41 @@ 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.
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);
}
//
// We only do this if the formals have non-region infer vars, since this is only
// meant to guide inference.
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 c286326

Please sign in to comment.