mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
Determine expected parameters from expected return in calls
Fixes #9560
This commit is contained in:
parent
cd64353288
commit
1791a35f9f
3 changed files with 102 additions and 11 deletions
|
@ -340,11 +340,25 @@ impl<'a> InferenceContext<'a> {
|
|||
None => (Vec::new(), self.err_ty()),
|
||||
};
|
||||
self.register_obligations_for_call(&callee_ty);
|
||||
self.check_call_arguments(args, ¶m_tys);
|
||||
|
||||
let expected_inputs = self.expected_inputs_for_expected_output(
|
||||
expected,
|
||||
ret_ty.clone(),
|
||||
param_tys.clone(),
|
||||
);
|
||||
|
||||
self.check_call_arguments(args, &expected_inputs, ¶m_tys);
|
||||
self.normalize_associated_types_in(ret_ty)
|
||||
}
|
||||
Expr::MethodCall { receiver, args, method_name, generic_args } => self
|
||||
.infer_method_call(tgt_expr, *receiver, args, method_name, generic_args.as_deref()),
|
||||
.infer_method_call(
|
||||
tgt_expr,
|
||||
*receiver,
|
||||
args,
|
||||
method_name,
|
||||
generic_args.as_deref(),
|
||||
expected,
|
||||
),
|
||||
Expr::Match { expr, arms } => {
|
||||
let input_ty = self.infer_expr(*expr, &Expectation::none());
|
||||
|
||||
|
@ -584,7 +598,7 @@ impl<'a> InferenceContext<'a> {
|
|||
// FIXME: record type error - expected reference but found ptr,
|
||||
// which cannot be coerced
|
||||
}
|
||||
Expectation::rvalue_hint(Ty::clone(exp_inner))
|
||||
Expectation::rvalue_hint(&mut self.table, Ty::clone(exp_inner))
|
||||
} else {
|
||||
Expectation::none()
|
||||
};
|
||||
|
@ -911,6 +925,7 @@ impl<'a> InferenceContext<'a> {
|
|||
args: &[ExprId],
|
||||
method_name: &Name,
|
||||
generic_args: Option<&GenericArgs>,
|
||||
expected: &Expectation,
|
||||
) -> Ty {
|
||||
let receiver_ty = self.infer_expr(receiver, &Expectation::none());
|
||||
let canonicalized_receiver = self.canonicalize(receiver_ty.clone());
|
||||
|
@ -944,7 +959,7 @@ impl<'a> InferenceContext<'a> {
|
|||
};
|
||||
let method_ty = method_ty.substitute(&Interner, &substs);
|
||||
self.register_obligations_for_call(&method_ty);
|
||||
let (expected_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) {
|
||||
let (formal_receiver_ty, param_tys, ret_ty) = match method_ty.callable_sig(self.db) {
|
||||
Some(sig) => {
|
||||
if !sig.params().is_empty() {
|
||||
(sig.params()[0].clone(), sig.params()[1..].to_vec(), sig.ret().clone())
|
||||
|
@ -954,13 +969,43 @@ impl<'a> InferenceContext<'a> {
|
|||
}
|
||||
None => (self.err_ty(), Vec::new(), self.err_ty()),
|
||||
};
|
||||
self.unify(&expected_receiver_ty, &receiver_ty);
|
||||
self.unify(&formal_receiver_ty, &receiver_ty);
|
||||
|
||||
self.check_call_arguments(args, ¶m_tys);
|
||||
let expected_inputs =
|
||||
self.expected_inputs_for_expected_output(expected, ret_ty.clone(), param_tys.clone());
|
||||
|
||||
self.check_call_arguments(args, &expected_inputs, ¶m_tys);
|
||||
self.normalize_associated_types_in(ret_ty)
|
||||
}
|
||||
|
||||
fn check_call_arguments(&mut self, args: &[ExprId], param_tys: &[Ty]) {
|
||||
fn expected_inputs_for_expected_output(
|
||||
&mut self,
|
||||
expected_output: &Expectation,
|
||||
output: Ty,
|
||||
inputs: Vec<Ty>,
|
||||
) -> Vec<Ty> {
|
||||
if let Some(expected_ty) = expected_output.to_option(&mut self.table) {
|
||||
let snapshot = self.table.snapshot();
|
||||
let result = if self.table.try_unify(&expected_ty, &output).is_ok() {
|
||||
// FIXME: the unification could introduce lifetime variables, which we'd need to handle here
|
||||
self.table.resolve_with_fallback(inputs, |var, kind, _, _| match kind {
|
||||
chalk_ir::VariableKind::Ty(tk) => var.to_ty(&Interner, tk).cast(&Interner),
|
||||
chalk_ir::VariableKind::Lifetime => var.to_lifetime(&Interner).cast(&Interner),
|
||||
chalk_ir::VariableKind::Const(ty) => {
|
||||
var.to_const(&Interner, ty).cast(&Interner)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
self.table.rollback_to(snapshot);
|
||||
result
|
||||
} else {
|
||||
Vec::new()
|
||||
}
|
||||
}
|
||||
|
||||
fn check_call_arguments(&mut self, args: &[ExprId], expected_inputs: &[Ty], param_tys: &[Ty]) {
|
||||
// Quoting https://github.com/rust-lang/rust/blob/6ef275e6c3cb1384ec78128eceeb4963ff788dca/src/librustc_typeck/check/mod.rs#L3325 --
|
||||
// We do this in a pretty awful way: first we type-check any arguments
|
||||
// that are not closures, then we type-check the closures. This is so
|
||||
|
@ -968,14 +1013,45 @@ impl<'a> InferenceContext<'a> {
|
|||
// type-check the functions. This isn't really the right way to do this.
|
||||
for &check_closures in &[false, true] {
|
||||
let param_iter = param_tys.iter().cloned().chain(repeat(self.err_ty()));
|
||||
for (&arg, param_ty) in args.iter().zip(param_iter) {
|
||||
let expected_iter = expected_inputs
|
||||
.iter()
|
||||
.cloned()
|
||||
.chain(param_iter.clone().skip(expected_inputs.len()));
|
||||
for ((&arg, param_ty), expected_ty) in args.iter().zip(param_iter).zip(expected_iter) {
|
||||
let is_closure = matches!(&self.body[arg], Expr::Lambda { .. });
|
||||
if is_closure != check_closures {
|
||||
continue;
|
||||
}
|
||||
|
||||
// the difference between param_ty and expected here is that
|
||||
// expected is the parameter when the expected *return* type is
|
||||
// taken into account. So in `let _: &[i32] = identity(&[1, 2])`
|
||||
// the expected type is already `&[i32]`, whereas param_ty is
|
||||
// still an unbound type variable. We don't always want to force
|
||||
// the parameter to coerce to the expected type (for example in
|
||||
// `coerce_unsize_expected_type_4`).
|
||||
let param_ty = self.normalize_associated_types_in(param_ty);
|
||||
self.infer_expr_coerce(arg, &Expectation::has_type(param_ty.clone()));
|
||||
let expected = Expectation::rvalue_hint(&mut self.table, expected_ty);
|
||||
// infer with the expected type we have...
|
||||
let ty = self.infer_expr_inner(arg, &expected);
|
||||
|
||||
// then coerce to either the expected type or just the formal parameter type
|
||||
let coercion_target = if let Some(ty) = expected.only_has_type(&mut self.table) {
|
||||
// if we are coercing to the expectation, unify with the
|
||||
// formal parameter type to connect everything
|
||||
self.unify(&ty, ¶m_ty);
|
||||
ty
|
||||
} else {
|
||||
param_ty
|
||||
};
|
||||
if !coercion_target.is_unknown() {
|
||||
if self.coerce(Some(arg), &ty, &coercion_target).is_err() {
|
||||
self.result.type_mismatches.insert(
|
||||
arg.into(),
|
||||
TypeMismatch { expected: coercion_target, actual: ty.clone() },
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue