Skip to content

Commit b79fd82

Browse files
committed
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
1 parent fa2ea8f commit b79fd82

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

crates/ra_hir_def/src/body/scope.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
157157
for arm in arms {
158158
let scope = scopes.new_scope(scope);
159159
scopes.add_bindings(body, scope, arm.pat);
160+
if let Some(guard) = arm.guard {
161+
scopes.set_scope(guard, scope);
162+
compute_expr_scopes(guard, body, scopes, scope);
163+
}
160164
scopes.set_scope(arm.expr, scope);
161165
compute_expr_scopes(arm.expr, body, scopes, scope);
162166
}

crates/ra_hir_ty/src/tests/patterns.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,3 +455,29 @@ fn test() {
455455
"###
456456
);
457457
}
458+
459+
#[test]
460+
fn infer_guard() {
461+
assert_snapshot!(
462+
infer(r#"
463+
struct S;
464+
impl S { fn foo(&self) -> bool { false } }
465+
466+
fn main() {
467+
match S {
468+
s if s.foo() => (),
469+
}
470+
}
471+
"#), @"
472+
[28; 32) 'self': &S
473+
[42; 51) '{ false }': bool
474+
[44; 49) 'false': bool
475+
[65; 116) '{ ... } }': ()
476+
[71; 114) 'match ... }': ()
477+
[77; 78) 'S': S
478+
[89; 90) 's': S
479+
[94; 95) 's': S
480+
[94; 101) 's.foo()': bool
481+
[105; 107) '()': ()
482+
")
483+
}

0 commit comments

Comments
 (0)