Skip to content

Commit

Permalink
Properly bind nested pattern bindings when there's more than one
Browse files Browse the repository at this point in the history
Fixes #15488.
  • Loading branch information
Jakub Wieczorek committed Jul 6, 2014
1 parent e05ec9a commit 9f460e7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
9 changes: 1 addition & 8 deletions src/librustc/middle/check_match.rs
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
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
@@ -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));
}

5 comments on commit 9f460e7

@bors
Copy link
Contributor

@bors bors commented on 9f460e7 Jul 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on 9f460e7 Jul 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jakub-/rust/issue-15488 = 9f460e7 into auto

@bors
Copy link
Contributor

@bors bors commented on 9f460e7 Jul 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jakub-/rust/issue-15488 = 9f460e7 merged ok, testing candidate = f78d2f5

@bors
Copy link
Contributor

@bors bors commented on 9f460e7 Jul 7, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = f78d2f5

Please sign in to comment.