From 500d8b2f6047660eeb6fc883b1aa60c297aa073b Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 21 Jun 2020 15:18:10 +0200 Subject: Handle bindings after @ in patterns --- crates/ra_hir_def/src/body/scope.rs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/crates/ra_hir_def/src/body/scope.rs b/crates/ra_hir_def/src/body/scope.rs index e48ff38f9..661f00407 100644 --- a/crates/ra_hir_def/src/body/scope.rs +++ b/crates/ra_hir_def/src/body/scope.rs @@ -87,15 +87,13 @@ impl ExprScopes { } fn add_bindings(&mut self, body: &Body, scope: ScopeId, pat: PatId) { - match &body[pat] { - Pat::Bind { name, .. } => { - // bind can have a sub pattern, but it's actually not allowed - // to bind to things in there - let entry = ScopeEntry { name: name.clone(), pat }; - self.scopes[scope].entries.push(entry) - } - p => p.walk_child_pats(|pat| self.add_bindings(body, scope, pat)), + let pattern = &body[pat]; + if let Pat::Bind { name, .. } = pattern { + let entry = ScopeEntry { name: name.clone(), pat }; + self.scopes[scope].entries.push(entry); } + + pattern.walk_child_pats(|pat| self.add_bindings(body, scope, pat)); } fn add_params_bindings(&mut self, body: &Body, scope: ScopeId, params: &[PatId]) { @@ -190,8 +188,8 @@ mod tests { } } - fn do_check(code: &str, expected: &[&str]) { - let (off, code) = extract_offset(code); + fn do_check(ra_fixture: &str, expected: &[&str]) { + let (off, code) = extract_offset(ra_fixture); let code = { let mut buf = String::new(); let off: usize = off.into(); @@ -300,6 +298,22 @@ mod tests { ); } + #[test] + fn test_bindings_after_at() { + do_check( + r" + fn foo() { + match Some(()) { + opt @ Some(unit) => { + <|> + } + _ => {} + } + }", + &["opt", "unit"], + ); + } + fn do_check_local_name(code: &str, expected_offset: u32) { let (off, code) = extract_offset(code); -- cgit v1.2.3