Skip to content

Commit

Permalink
Fix & ref ident patterns for DSTs
Browse files Browse the repository at this point in the history
We shouldn't load DSTs when recursing into the sub-pattern of `& ref ident`.

Fixes #30277
  • Loading branch information
Aatch committed Dec 9, 2015
1 parent 6f95ae6 commit 93154dd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/librustc_trans/trans/_match.rs
Expand Up @@ -1944,8 +1944,16 @@ pub fn bind_irrefutable_pat<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
}
hir::PatBox(ref inner) => {
let pat_ty = node_id_type(bcx, inner.id);
// Don't load DSTs, instead pass along a fat ptr
let val = if type_is_sized(tcx, pat_ty) {
// Pass along DSTs as fat pointers.
let val = if type_is_fat_ptr(tcx, pat_ty) {
// We need to check for this, as the pattern could be binding
// a fat pointer by-value.
if let hir::PatIdent(hir::BindByRef(_),_,_) = inner.node {
val.val
} else {
Load(bcx, val.val)
}
} else if type_is_sized(tcx, pat_ty) {
Load(bcx, val.val)
} else {
val.val
Expand All @@ -1955,8 +1963,16 @@ pub fn bind_irrefutable_pat<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
}
hir::PatRegion(ref inner, _) => {
let pat_ty = node_id_type(bcx, inner.id);
// Don't load DSTs, instead pass along a fat ptr
let val = if type_is_sized(tcx, pat_ty) {
// Pass along DSTs as fat pointers.
let val = if type_is_fat_ptr(tcx, pat_ty) {
// We need to check for this, as the pattern could be binding
// a fat pointer by-value.
if let hir::PatIdent(hir::BindByRef(_),_,_) = inner.node {
val.val
} else {
Load(bcx, val.val)
}
} else if type_is_sized(tcx, pat_ty) {
Load(bcx, val.val)
} else {
val.val
Expand Down
24 changes: 24 additions & 0 deletions src/test/run-pass/dst-irrefutable-bind.rs
@@ -0,0 +1,24 @@
// Copyright 2015 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.

struct Test<T: ?Sized>(T);

fn main() {
let x = Test([1,2,3]);
let x : &Test<[i32]> = &x;

let & ref _y = x;

// Make sure binding to a fat pointer behind a reference
// still works
let slice = &[1,2,3];
let x = Test(&slice);
let Test(&_slice) = x;
}

0 comments on commit 93154dd

Please sign in to comment.