Skip to content

Commit e583ab6

Browse files
committed
Auto merge of #30279 - Aatch:dst-ref-binding, r=pnkfelix
We shouldn't load DSTs when recursing into the sub-pattern of `& ref ident`. Fixes #30277
2 parents 5e82941 + 93154dd commit e583ab6

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/librustc_trans/trans/_match.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -1944,8 +1944,16 @@ pub fn bind_irrefutable_pat<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
19441944
}
19451945
hir::PatBox(ref inner) => {
19461946
let pat_ty = node_id_type(bcx, inner.id);
1947-
// Don't load DSTs, instead pass along a fat ptr
1948-
let val = if type_is_sized(tcx, pat_ty) {
1947+
// Pass along DSTs as fat pointers.
1948+
let val = if type_is_fat_ptr(tcx, pat_ty) {
1949+
// We need to check for this, as the pattern could be binding
1950+
// a fat pointer by-value.
1951+
if let hir::PatIdent(hir::BindByRef(_),_,_) = inner.node {
1952+
val.val
1953+
} else {
1954+
Load(bcx, val.val)
1955+
}
1956+
} else if type_is_sized(tcx, pat_ty) {
19491957
Load(bcx, val.val)
19501958
} else {
19511959
val.val
@@ -1955,8 +1963,16 @@ pub fn bind_irrefutable_pat<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
19551963
}
19561964
hir::PatRegion(ref inner, _) => {
19571965
let pat_ty = node_id_type(bcx, inner.id);
1958-
// Don't load DSTs, instead pass along a fat ptr
1959-
let val = if type_is_sized(tcx, pat_ty) {
1966+
// Pass along DSTs as fat pointers.
1967+
let val = if type_is_fat_ptr(tcx, pat_ty) {
1968+
// We need to check for this, as the pattern could be binding
1969+
// a fat pointer by-value.
1970+
if let hir::PatIdent(hir::BindByRef(_),_,_) = inner.node {
1971+
val.val
1972+
} else {
1973+
Load(bcx, val.val)
1974+
}
1975+
} else if type_is_sized(tcx, pat_ty) {
19601976
Load(bcx, val.val)
19611977
} else {
19621978
val.val
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2015 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+
struct Test<T: ?Sized>(T);
12+
13+
fn main() {
14+
let x = Test([1,2,3]);
15+
let x : &Test<[i32]> = &x;
16+
17+
let & ref _y = x;
18+
19+
// Make sure binding to a fat pointer behind a reference
20+
// still works
21+
let slice = &[1,2,3];
22+
let x = Test(&slice);
23+
let Test(&_slice) = x;
24+
}

0 commit comments

Comments
 (0)