Skip to content

Properly bind nested pattern bindings when there's more than one #15489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from Jul 7, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/librustc/middle/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,7 @@ fn is_useful(cx: &MatchCheckCtxt, matrix @ &Matrix(ref rows): &Matrix,
return NotUseful;
}
let real_pat = match rows.iter().find(|r| r.get(0).id != 0) {
Some(r) => {
match r.get(0).node {
// An arm of the form `ref x @ sub_pat` has type
// `sub_pat`, not `&sub_pat` as `x` itself does.
PatIdent(BindByRef(_), _, Some(sub)) => sub,
_ => *r.get(0)
}
}
Some(r) => raw_pat(*r.get(0)),
None if v.len() == 0 => return NotUseful,
None => v[0]
};
Expand Down
35 changes: 17 additions & 18 deletions src/librustc/middle/trans/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,26 +413,25 @@ fn expand_nested_bindings<'a, 'b>(
let _indenter = indenter();

m.iter().map(|br| {
match br.pats.get(col).node {
ast::PatIdent(_, ref path1, Some(inner)) => {
let pats = Vec::from_slice(br.pats.slice(0u, col))
.append((vec!(inner))
.append(br.pats.slice(col + 1u, br.pats.len())).as_slice());

let mut bound_ptrs = br.bound_ptrs.clone();
bound_ptrs.push((path1.node, val));
Match {
pats: pats,
data: &*br.data,
bound_ptrs: bound_ptrs
}
}
_ => Match {
pats: br.pats.clone(),
data: &*br.data,
bound_ptrs: br.bound_ptrs.clone()
let mut bound_ptrs = br.bound_ptrs.clone();
let mut pat = *br.pats.get(col);
loop {
pat = match pat.node {
ast::PatIdent(_, ref path, Some(inner)) => {
bound_ptrs.push((path.node, val));
inner.clone()
},
_ => break
}
}

let mut pats = br.pats.clone();
*pats.get_mut(col) = pat;
Match {
pats: pats,
data: &*br.data,
bound_ptrs: bound_ptrs
}
}).collect()
}

Expand Down
38 changes: 38 additions & 0 deletions src/test/run-pass/match-pattern-bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn main() {
let value = Some(1i);
assert_eq!(match value {
ref a @ Some(_) => a,
ref b @ None => b
}, &Some(1i));
assert_eq!(match value {
ref a @ ref _c @ Some(_) => a,
ref b @ None => b
}, &Some(1i));
assert_eq!(match value {
_a @ ref c @ Some(_) => c,
ref b @ None => b
}, &Some(1i));
assert_eq!(match "foobarbaz" {
_a @ b @ _ => b
}, "foobarbaz");

let a @ b @ c = "foobarbaz";
assert_eq!(a, "foobarbaz");
assert_eq!(b, "foobarbaz");
assert_eq!(c, "foobarbaz");
let value = Some(true);
let ref a @ b @ ref c = value;
assert_eq!(a, &Some(true));
assert_eq!(b, Some(true));
assert_eq!(c, &Some(true));
}