mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +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
|
@ -866,8 +866,9 @@ impl Expectation {
|
||||||
/// which still is useful, because it informs integer literals and the like.
|
/// which still is useful, because it informs integer literals and the like.
|
||||||
/// See the test case `test/ui/coerce-expect-unsized.rs` and #20169
|
/// See the test case `test/ui/coerce-expect-unsized.rs` and #20169
|
||||||
/// for examples of where this comes up,.
|
/// for examples of where this comes up,.
|
||||||
fn rvalue_hint(ty: Ty) -> Self {
|
fn rvalue_hint(table: &mut unify::InferenceTable, ty: Ty) -> Self {
|
||||||
match ty.strip_references().kind(&Interner) {
|
// FIXME: do struct_tail_without_normalization
|
||||||
|
match table.resolve_ty_shallow(&ty).kind(&Interner) {
|
||||||
TyKind::Slice(_) | TyKind::Str | TyKind::Dyn(_) => Expectation::RValueLikeUnsized(ty),
|
TyKind::Slice(_) | TyKind::Str | TyKind::Dyn(_) => Expectation::RValueLikeUnsized(ty),
|
||||||
_ => Expectation::has_type(ty),
|
_ => Expectation::has_type(ty),
|
||||||
}
|
}
|
||||||
|
|
|
@ -340,11 +340,25 @@ impl<'a> InferenceContext<'a> {
|
||||||
None => (Vec::new(), self.err_ty()),
|
None => (Vec::new(), self.err_ty()),
|
||||||
};
|
};
|
||||||
self.register_obligations_for_call(&callee_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)
|
self.normalize_associated_types_in(ret_ty)
|
||||||
}
|
}
|
||||||
Expr::MethodCall { receiver, args, method_name, generic_args } => self
|
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 } => {
|
Expr::Match { expr, arms } => {
|
||||||
let input_ty = self.infer_expr(*expr, &Expectation::none());
|
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,
|
// FIXME: record type error - expected reference but found ptr,
|
||||||
// which cannot be coerced
|
// which cannot be coerced
|
||||||
}
|
}
|
||||||
Expectation::rvalue_hint(Ty::clone(exp_inner))
|
Expectation::rvalue_hint(&mut self.table, Ty::clone(exp_inner))
|
||||||
} else {
|
} else {
|
||||||
Expectation::none()
|
Expectation::none()
|
||||||
};
|
};
|
||||||
|
@ -911,6 +925,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
args: &[ExprId],
|
args: &[ExprId],
|
||||||
method_name: &Name,
|
method_name: &Name,
|
||||||
generic_args: Option<&GenericArgs>,
|
generic_args: Option<&GenericArgs>,
|
||||||
|
expected: &Expectation,
|
||||||
) -> Ty {
|
) -> Ty {
|
||||||
let receiver_ty = self.infer_expr(receiver, &Expectation::none());
|
let receiver_ty = self.infer_expr(receiver, &Expectation::none());
|
||||||
let canonicalized_receiver = self.canonicalize(receiver_ty.clone());
|
let canonicalized_receiver = self.canonicalize(receiver_ty.clone());
|
||||||
|
@ -944,7 +959,7 @@ impl<'a> InferenceContext<'a> {
|
||||||
};
|
};
|
||||||
let method_ty = method_ty.substitute(&Interner, &substs);
|
let method_ty = method_ty.substitute(&Interner, &substs);
|
||||||
self.register_obligations_for_call(&method_ty);
|
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) => {
|
Some(sig) => {
|
||||||
if !sig.params().is_empty() {
|
if !sig.params().is_empty() {
|
||||||
(sig.params()[0].clone(), sig.params()[1..].to_vec(), sig.ret().clone())
|
(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()),
|
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)
|
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 --
|
// 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
|
// 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
|
// 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.
|
// type-check the functions. This isn't really the right way to do this.
|
||||||
for &check_closures in &[false, true] {
|
for &check_closures in &[false, true] {
|
||||||
let param_iter = param_tys.iter().cloned().chain(repeat(self.err_ty()));
|
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 { .. });
|
let is_closure = matches!(&self.body[arg], Expr::Lambda { .. });
|
||||||
if is_closure != check_closures {
|
if is_closure != check_closures {
|
||||||
continue;
|
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);
|
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() },
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -143,6 +143,11 @@ pub(crate) struct InferenceTable<'a> {
|
||||||
pending_obligations: Vec<Canonicalized<InEnvironment<Goal>>>,
|
pending_obligations: Vec<Canonicalized<InEnvironment<Goal>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) struct InferenceTableSnapshot {
|
||||||
|
var_table_snapshot: chalk_solve::infer::InferenceSnapshot<Interner>,
|
||||||
|
// FIXME: snapshot type_variable_table, pending_obligations?
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> InferenceTable<'a> {
|
impl<'a> InferenceTable<'a> {
|
||||||
pub(crate) fn new(db: &'a dyn HirDatabase, trait_env: Arc<TraitEnvironment>) -> Self {
|
pub(crate) fn new(db: &'a dyn HirDatabase, trait_env: Arc<TraitEnvironment>) -> Self {
|
||||||
InferenceTable {
|
InferenceTable {
|
||||||
|
@ -335,6 +340,15 @@ impl<'a> InferenceTable<'a> {
|
||||||
self.var_unification_table.normalize_ty_shallow(&Interner, ty).unwrap_or_else(|| ty.clone())
|
self.var_unification_table.normalize_ty_shallow(&Interner, ty).unwrap_or_else(|| ty.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn snapshot(&mut self) -> InferenceTableSnapshot {
|
||||||
|
let snapshot = self.var_unification_table.snapshot();
|
||||||
|
InferenceTableSnapshot { var_table_snapshot: snapshot }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn rollback_to(&mut self, snapshot: InferenceTableSnapshot) {
|
||||||
|
self.var_unification_table.rollback_to(snapshot.var_table_snapshot);
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn register_obligation(&mut self, goal: Goal) {
|
pub(crate) fn register_obligation(&mut self, goal: Goal) {
|
||||||
let in_env = InEnvironment::new(&self.trait_env.env, goal);
|
let in_env = InEnvironment::new(&self.trait_env.env, goal);
|
||||||
self.register_obligation_in_env(in_env)
|
self.register_obligation_in_env(in_env)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue