2618: Fix coercion of last expression in function body r=flodiebold a=flodiebold



Co-authored-by: Florian Diebold <florian.diebold@freiheit.com>
This commit is contained in:
bors[bot] 2019-12-20 17:29:11 +00:00 committed by GitHub
commit 1d7931e5ff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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!(