From 32fc944263ae0b30eba130fbcf28f4eb5578fdb3 Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Sat, 15 May 2021 17:01:27 +0200 Subject: Fix handling of diverging branches in match coercion Fixes #7626. --- crates/hir_ty/src/tests/coercion.rs | 39 +++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'crates/hir_ty/src/tests') diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs index 67295b663..bb568ea37 100644 --- a/crates/hir_ty/src/tests/coercion.rs +++ b/crates/hir_ty/src/tests/coercion.rs @@ -873,3 +873,42 @@ fn foo(c: i32) { "#, ) } + +#[test] +fn infer_match_diverging_branch_1() { + check_types( + r#" +enum Result { Ok(T), Err } +fn parse() -> T { loop {} } + +fn test() -> i32 { + let a = match parse() { + Ok(val) => val, + Err => return 0, + }; + a + //^ i32 +} + "#, + ) +} + +#[test] +fn infer_match_diverging_branch_2() { + // same as 1 except for order of branches + check_types( + r#" +enum Result { Ok(T), Err } +fn parse() -> T { loop {} } + +fn test() -> i32 { + let a = match parse() { + Err => return 0, + Ok(val) => val, + }; + a + //^ i32 +} + "#, + ) +} -- cgit v1.2.3