diff options
| author | Jakub Wieczorek <jakub@jakub.cc> | 2014-07-06 23:54:40 +0200 |
|---|---|---|
| committer | Jakub Wieczorek <jakub@jakub.cc> | 2014-07-07 00:26:41 +0200 |
| commit | 9f460e7af88ca6efc1a58ebe781b47447acdb21b (patch) | |
| tree | e1336abf1d175e857027b97cc094ee4c6d454a51 /src | |
| parent | e05ec9a2bf92dfda1951fec11d56b40b6761574a (diff) | |
| download | rust-9f460e7af88ca6efc1a58ebe781b47447acdb21b.tar.gz rust-9f460e7af88ca6efc1a58ebe781b47447acdb21b.zip | |
Properly bind nested pattern bindings when there's more than one
Fixes #15488.
Diffstat (limited to 'src')
| -rw-r--r-- | src/librustc/middle/check_match.rs | 9 | ||||
| -rw-r--r-- | src/librustc/middle/trans/_match.rs | 35 | ||||
| -rw-r--r-- | src/test/run-pass/match-pattern-bindings.rs | 38 |
3 files changed, 56 insertions, 26 deletions
diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs index abc4212dbb2..db5268220fa 100644 --- a/src/librustc/middle/check_match.rs +++ b/src/librustc/middle/check_match.rs @@ -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] }; diff --git a/src/librustc/middle/trans/_match.rs b/src/librustc/middle/trans/_match.rs index e0be1d89a94..0809079347b 100644 --- a/src/librustc/middle/trans/_match.rs +++ b/src/librustc/middle/trans/_match.rs @@ -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() } diff --git a/src/test/run-pass/match-pattern-bindings.rs b/src/test/run-pass/match-pattern-bindings.rs new file mode 100644 index 00000000000..61e905e5b17 --- /dev/null +++ b/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)); +} |
