Skip to content

Commit 31f66a0

Browse files
committed
reset default binding mode when we pass through a & pattern
Fixes rust-lang#46688.
1 parent b1f8e6f commit 31f66a0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Diff for: src/librustc_typeck/check/_match.rs

+13
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
151151
err.emit();
152152
}
153153
}
154+
} else if let PatKind::Ref(..) = pat.node {
155+
// When you encounter a `&pat` pattern, reset to "by
156+
// value". This is so that `x` and `y` here are by value,
157+
// as they appear to be:
158+
//
159+
// ```
160+
// match &(&22, &44) {
161+
// (&x, &y) => ...
162+
// }
163+
// ```
164+
//
165+
// cc #46688
166+
def_bm = ty::BindByValue(hir::MutImmutable);
154167
}
155168

156169
// Lose mutability now that we know binding mode and discriminant type.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(match_default_bindings)]
12+
13+
// Test that we "reset" the mode as we pass through a `&` pattern.
14+
//
15+
// cc #46688
16+
17+
fn surprise(x: i32) {
18+
assert_eq!(x, 2);
19+
}
20+
21+
fn main() {
22+
let x = &(1, &2);
23+
let (_, &b) = x;
24+
surprise(b);
25+
}

0 commit comments

Comments
 (0)