Handle bindings after @ in patterns

This commit is contained in:
Jonas Schievink 2020-06-21 15:18:10 +02:00
parent 6d0a765d34
commit 500d8b2f60

View File

@ -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 pattern = &body[pat];
if let Pat::Bind { name, .. } = pattern {
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)),
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);