match self.coerce(Some(expr), &ty, &target) {
Ok(res) => res,
Err(_) => {
- self.result
- .type_mismatches
- .insert(expr.into(), TypeMismatch { expected: target, actual: ty.clone() });
- // Return actual type when type mismatch.
- // This is needed for diagnostic when return type mismatch.
- ty
+ self.result.type_mismatches.insert(
+ expr.into(),
+ TypeMismatch { expected: target.clone(), actual: ty.clone() },
+ );
+ target
}
}
} else {
self.err_ty()
}
Expr::Async { body } => {
+ let ret_ty = self.table.new_type_var();
+ let prev_diverges = mem::replace(&mut self.diverges, Diverges::Maybe);
+ let prev_ret_ty = mem::replace(&mut self.return_ty, ret_ty.clone());
+
+ let inner_ty = self.infer_expr_coerce(*body, &Expectation::has_type(ret_ty));
+
+ self.diverges = prev_diverges;
+ self.return_ty = prev_ret_ty;
+
// Use the first type parameter as the output type of future.
// existential type AsyncBlockImplTrait<InnerType>: Future<Output = InnerType>
- let inner_ty = self.infer_expr(*body, &Expectation::none());
let impl_trait_id = crate::ImplTraitId::AsyncBlockTypeImplTrait(self.owner, *body);
let opaque_ty_id = self.db.intern_impl_trait_id(impl_trait_id).into();
TyKind::OpaqueType(opaque_ty_id, Substitution::from1(Interner, inner_ty))
break;
}
}
+ // if the function is unresolved, we use is_varargs=true to
+ // suppress the arg count diagnostic here
+ let is_varargs =
+ derefed_callee.callable_sig(self.db).map_or(false, |sig| sig.is_varargs)
+ || res.is_none();
let (param_tys, ret_ty) = match res {
Some(res) => {
let adjustments = auto_deref_adjust_steps(&derefs);
self.write_expr_adj(*callee, adjustments);
res
}
- None => (Vec::new(), self.err_ty()),
+ None => (Vec::new(), self.err_ty()), // FIXME diagnostic
};
let indices_to_skip = self.check_legacy_const_generics(derefed_callee, args);
self.register_obligations_for_call(&callee_ty);
param_tys.clone(),
);
- self.check_call_arguments(args, &expected_inputs, ¶m_tys, &indices_to_skip);
+ self.check_call_arguments(
+ tgt_expr,
+ args,
+ &expected_inputs,
+ ¶m_tys,
+ &indices_to_skip,
+ is_varargs,
+ );
self.normalize_associated_types_in(ret_ty)
}
Expr::MethodCall { receiver, args, method_name, generic_args } => self
self.table.new_maybe_never_var()
} else {
if let Some(t) = expected.only_has_type(&mut self.table) {
- let _ = self.coerce(Some(expr), &TyBuilder::unit(), &t);
+ if self.coerce(Some(expr), &TyBuilder::unit(), &t).is_err() {
+ self.result.type_mismatches.insert(
+ expr.into(),
+ TypeMismatch { expected: t.clone(), actual: TyBuilder::unit() },
+ );
+ }
+ t
+ } else {
+ TyBuilder::unit()
}
- TyBuilder::unit()
}
}
}
};
let method_ty = method_ty.substitute(Interner, &substs);
self.register_obligations_for_call(&method_ty);
- 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())
- } else {
- (self.err_ty(), Vec::new(), sig.ret().clone())
+ let (formal_receiver_ty, param_tys, ret_ty, is_varargs) =
+ 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(),
+ sig.is_varargs,
+ )
+ } else {
+ (self.err_ty(), Vec::new(), sig.ret().clone(), sig.is_varargs)
+ }
}
- }
- None => (self.err_ty(), Vec::new(), self.err_ty()),
- };
+ None => (self.err_ty(), Vec::new(), self.err_ty(), true),
+ };
self.unify(&formal_receiver_ty, &receiver_ty);
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.check_call_arguments(tgt_expr, args, &expected_inputs, ¶m_tys, &[], is_varargs);
self.normalize_associated_types_in(ret_ty)
}
fn check_call_arguments(
&mut self,
+ expr: ExprId,
args: &[ExprId],
expected_inputs: &[Ty],
param_tys: &[Ty],
skip_indices: &[u32],
+ is_varargs: bool,
) {
+ if args.len() != param_tys.len() + skip_indices.len() && !is_varargs {
+ self.push_diagnostic(InferenceDiagnostic::MismatchedArgCount {
+ call_expr: expr,
+ expected: param_tys.len() + skip_indices.len(),
+ found: args.len(),
+ });
+ }
+
// 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
// only use legacy const generics if the param count matches with them
if data.params.len() + data.legacy_const_generics_indices.len() != args.len() {
- return Vec::new();
+ if args.len() <= data.params.len() {
+ return Vec::new();
+ } else {
+ // there are more parameters than there should be without legacy
+ // const params; use them
+ let mut indices = data.legacy_const_generics_indices.clone();
+ indices.sort();
+ return indices;
+ }
}
// check legacy const parameters