Skip to content

Commit 58021be

Browse files
committed
rustc: allow @ as-patterns to move when the sub-pattern contains no bindings.
A pattern like `foo @ Foo(Bar(*), _)` should be legal, even if `foo` moves, since the subpatterns are purely structural. Fixes #3761.
1 parent ac49e65 commit 58021be

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/librustc/middle/check_match.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,9 @@ pub fn check_legality_of_move_bindings(cx: &MatchCheckCtxt,
879879

880880
let check_move: &fn(@pat, Option<@pat>) = |p, sub| {
881881
// check legality of moving out of the enum
882-
if sub.is_some() {
882+
883+
// x @ Foo(*) is legal, but x @ Foo(y) isn't.
884+
if sub.map_move_default(false, |p| pat_contains_bindings(def_map, p)) {
883885
tcx.sess.span_err(
884886
p.span,
885887
"cannot bind by-move with sub-bindings");

src/librustc/middle/pat_util.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,18 @@ pub fn pat_binding_ids(dm: resolve::DefMap, pat: @pat) -> ~[NodeId] {
8888
pat_bindings(dm, pat, |_bm, b_id, _sp, _pt| found.push(b_id) );
8989
return found;
9090
}
91+
92+
/// Checks if the pattern contains any patterns that bind something to
93+
/// an ident, e.g. `foo`, or `Foo(foo)` or `foo @ Bar(*)`.
94+
pub fn pat_contains_bindings(dm: resolve::DefMap, pat: @pat) -> bool {
95+
let mut contains_bindings = false;
96+
do walk_pat(pat) |p| {
97+
if pat_is_binding(dm, p) {
98+
contains_bindings = true;
99+
false // there's at least one binding, can short circuit now.
100+
} else {
101+
true
102+
}
103+
};
104+
contains_bindings
105+
}

0 commit comments

Comments
 (0)