Fix coercion of last expression in function body

This commit is contained in:
Florian Diebold 2019-12-20 18:27:51 +01:00
parent 99d6f544f2
commit 9c3f00a906
3 changed files with 18 additions and 2 deletions

View file

@ -460,7 +460,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
}
fn infer_body(&mut self) {
self.infer_expr(self.body.body_expr, &Expectation::has_type(self.return_ty.clone()));
self.infer_expr_coerce(self.body.body_expr, &Expectation::has_type(self.return_ty.clone()));
}
fn resolve_into_iter_item(&self) -> Option<TypeAliasId> {

View file

@ -41,7 +41,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
/// Infer type of expression with possibly implicit coerce to the expected type.
/// Return the type after possible coercion.
fn infer_expr_coerce(&mut self, expr: ExprId, expected: &Expectation) -> Ty {
pub(super) fn infer_expr_coerce(&mut self, expr: ExprId, expected: &Expectation) -> Ty {
let ty = self.infer_expr_inner(expr, &expected);
let ty = if !self.coerce(&ty, &expected.ty) {
self.result

View file

@ -369,6 +369,22 @@ fn test() {
);
}
#[test]
fn return_coerce_unknown() {
assert_snapshot!(
infer_with_mismatches(r#"
fn foo() -> u32 {
return unknown;
}
"#, true),
@r###"
[17; 40) '{ ...own; }': !
[23; 37) 'return unknown': !
[30; 37) 'unknown': u32
"###
);
}
#[test]
fn coerce_autoderef() {
assert_snapshot!(