4969: Handle bindings after @ in patterns r=flodiebold a=jonas-schievink

This is unstable, behind the `bindings_after_at` feature gate, but the semantics are fairly clear, and this is used at lot in rustc.

Co-authored-by: Jonas Schievink <jonasschievink@gmail.com>
This commit is contained in:
bors[bot] 2020-06-21 14:24:51 +00:00 committed by GitHub
commit f9dffade9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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 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);