Skip to content

Commit 4913e88

Browse files
Rollup merge of rust-lang#33734 - luqmana:33387-mir_fat_ptr_coerce, r=arielb1
[MIR] Add PointerCast for Unsize casts of fat pointers. Fixes rust-lang#33387. r? @eddyb
2 parents 5dc8dfa + 94f0918 commit 4913e88

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

src/librustc_trans/base.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,13 @@ pub fn coerce_unsized_into<'blk, 'tcx>(bcx: Block<'blk, 'tcx>,
617617
(&ty::TyRawPtr(..), &ty::TyRawPtr(..)) => {
618618
let (base, info) = if common::type_is_fat_ptr(bcx.tcx(), src_ty) {
619619
// fat-ptr to fat-ptr unsize preserves the vtable
620-
load_fat_ptr(bcx, src, src_ty)
620+
// i.e. &'a fmt::Debug+Send => &'a fmt::Debug
621+
// So we need to pointercast the base to ensure
622+
// the types match up.
623+
let (base, info) = load_fat_ptr(bcx, src, src_ty);
624+
let llcast_ty = type_of::fat_ptr_base_ty(bcx.ccx(), dst_ty);
625+
let base = PointerCast(bcx, base, llcast_ty);
626+
(base, info)
621627
} else {
622628
let base = load_ty(bcx, src, src_ty);
623629
unsize_thin_ptr(bcx, base, src_ty, dst_ty)

src/librustc_trans/mir/rvalue.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -262,14 +262,17 @@ impl<'bcx, 'tcx> MirContext<'bcx, 'tcx> {
262262
assert!(common::type_is_fat_ptr(bcx.tcx(), cast_ty));
263263

264264
match operand.val {
265-
OperandValue::FatPtr(..) => {
265+
OperandValue::FatPtr(lldata, llextra) => {
266266
// unsize from a fat pointer - this is a
267267
// "trait-object-to-supertrait" coercion, for
268268
// example,
269269
// &'a fmt::Debug+Send => &'a fmt::Debug,
270-
// and is a no-op at the LLVM level
270+
// So we need to pointercast the base to ensure
271+
// the types match up.
271272
self.set_operand_dropped(&bcx, source);
272-
operand.val
273+
let llcast_ty = type_of::fat_ptr_base_ty(bcx.ccx(), cast_ty);
274+
let lldata = bcx.pointercast(lldata, llcast_ty);
275+
OperandValue::FatPtr(lldata, llextra)
273276
}
274277
OperandValue::Immediate(lldata) => {
275278
// "standard" unsize

src/librustc_trans/type_of.rs

+11
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ pub fn sizing_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Typ
157157
llsizingty
158158
}
159159

160+
pub fn fat_ptr_base_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> Type {
161+
match ty.sty {
162+
ty::TyBox(t) |
163+
ty::TyRef(_, ty::TypeAndMut { ty: t, .. }) |
164+
ty::TyRawPtr(ty::TypeAndMut { ty: t, .. }) if !type_is_sized(ccx.tcx(), t) => {
165+
in_memory_type_of(ccx, t).ptr_to()
166+
}
167+
_ => bug!("expected fat ptr ty but got {:?}", ty)
168+
}
169+
}
170+
160171
fn unsized_info_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> Type {
161172
let unsized_part = ccx.tcx().struct_tail(ty);
162173
match unsized_part.sty {

src/test/run-pass/issue-33387.rs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2016 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(rustc_attrs)]
12+
13+
use std::sync::Arc;
14+
15+
trait Foo {
16+
fn get(&self) -> [u8; 2];
17+
}
18+
19+
impl Foo for [u8; 2] {
20+
fn get(&self) -> [u8; 2] {
21+
*self
22+
}
23+
}
24+
25+
struct Bar<T: ?Sized>(T);
26+
27+
#[rustc_mir]
28+
fn unsize_fat_ptr<'a>(x: &'a Bar<Foo + Send + 'a>) -> &'a Bar<Foo + 'a> {
29+
x
30+
}
31+
32+
#[rustc_mir]
33+
fn unsize_nested_fat_ptr(x: Arc<Foo + Send>) -> Arc<Foo> {
34+
x
35+
}
36+
37+
#[rustc_mir]
38+
fn main() {
39+
let x: Box<Bar<Foo + Send>> = Box::new(Bar([1,2]));
40+
assert_eq!(unsize_fat_ptr(&*x).0.get(), [1, 2]);
41+
42+
let x: Arc<Foo + Send> = Arc::new([3, 4]);
43+
assert_eq!(unsize_nested_fat_ptr(x).get(), [3, 4]);
44+
}

0 commit comments

Comments
 (0)