From b79fd82559642f4fe504c0c382b86d57c666be1d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 18 Apr 2020 22:16:04 +0200 Subject: Correctly infer types in guard expressions The root cause was that we forgot to add bindings from the arm to the guard expression closes #3980 --- crates/ra_hir_def/src/body/scope.rs | 4 ++++ crates/ra_hir_ty/src/tests/patterns.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index 4d489f692..fe4137176 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs @@ -157,6 +157,10 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope for arm in arms { let scope = scopes.new_scope(scope); scopes.add_bindings(body, scope, arm.pat); + if let Some(guard) = arm.guard { + scopes.set_scope(guard, scope); + compute_expr_scopes(guard, body, scopes, scope); + } scopes.set_scope(arm.expr, scope); compute_expr_scopes(arm.expr, body, scopes, scope); } diff --git a/crates/ra_hir_ty/src/tests/patterns.rs b/crates/ra_hir_ty/src/tests/patterns.rs index 07cbc521a..6ea51d5d3 100644 --- a/crates/ra_hir_ty/src/tests/patterns.rs +++ b/crates/ra_hir_ty/src/tests/patterns.rs @@ -455,3 +455,29 @@ fn test() { "### ); } + +#[test] +fn infer_guard() { + assert_snapshot!( + infer(r#" +struct S; +impl S { fn foo(&self) -> bool { false } } + +fn main() { + match S { + s if s.foo() => (), + } +} + "#), @" + [28; 32) 'self': &S + [42; 51) '{ false }': bool + [44; 49) 'false': bool + [65; 116) '{ ... } }': () + [71; 114) 'match ... }': () + [77; 78) 'S': S + [89; 90) 's': S + [94; 95) 's': S + [94; 101) 's.foo()': bool + [105; 107) '()': () + ") +} -- cgit v1.2.3