Skip to content

Commit

Permalink
Rollup merge of rust-lang#48448 - nikomatsakis:default-binding-mode-i…
Browse files Browse the repository at this point in the history
…ssue-46688, r=cramertj

reset default binding mode when we pass through a `&` pattern

Fixes rust-lang#46688.

r? @cramertj
  • Loading branch information
Manishearth committed Feb 24, 2018
2 parents 69757c5 + 31f66a0 commit 90f21d4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/librustc_typeck/check/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
err.emit();
}
}
} else if let PatKind::Ref(..) = pat.node {
// When you encounter a `&pat` pattern, reset to "by
// value". This is so that `x` and `y` here are by value,
// as they appear to be:
//
// ```
// match &(&22, &44) {
// (&x, &y) => ...
// }
// ```
//
// cc #46688
def_bm = ty::BindByValue(hir::MutImmutable);
}

// Lose mutability now that we know binding mode and discriminant type.
Expand Down
25 changes: 25 additions & 0 deletions src/test/run-pass/rfc-2005-default-binding-mode/reset-mode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2017 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.

#![feature(match_default_bindings)]

// Test that we "reset" the mode as we pass through a `&` pattern.
//
// cc #46688

fn surprise(x: i32) {
assert_eq!(x, 2);
}

fn main() {
let x = &(1, &2);
let (_, &b) = x;
surprise(b);
}

0 comments on commit 90f21d4

Please sign in to comment.