Skip to content

Commit 6755c2a

Browse files
authored
Rollup merge of #102641 - eholk:dyn-star-box, r=compiler-errors
Support casting boxes to dyn* Boxes have a pointer type at codegen time which LLVM does not allow to be transparently converted to an integer. Work around this by inserting a `ptrtoint` instruction if the argument is a pointer. r? ``@compiler-errors`` Fixes #102427
2 parents 0938e16 + 8b2c3eb commit 6755c2a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_middle::ty::cast::{CastTy, IntTy};
1414
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
1515
use rustc_middle::ty::{self, adjustment::PointerCast, Instance, Ty, TyCtxt};
1616
use rustc_span::source_map::{Span, DUMMY_SP};
17+
use rustc_target::abi::Size;
1718

1819
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1920
#[instrument(level = "trace", skip(self, bx))]
@@ -285,6 +286,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
285286
bug!("Only valid to do a DynStar cast into a DynStar type")
286287
};
287288
let vtable = get_vtable(bx.cx(), source.ty(self.mir, bx.tcx()), trait_ref);
289+
let vtable = bx.pointercast(vtable, bx.cx().type_ptr_to(bx.cx().type_isize()));
290+
// FIXME(dyn-star): this is probably not the best way to check if this is
291+
// a pointer, and really we should ensure that the value is a suitable
292+
// pointer earlier in the compilation process.
293+
let data = match operand.layout.pointee_info_at(bx.cx(), Size::ZERO) {
294+
Some(_) => bx.ptrtoint(data, bx.cx().type_isize()),
295+
None => data,
296+
};
288297
OperandValue::Pair(data, vtable)
289298
}
290299
mir::CastKind::Pointer(

src/test/ui/dyn-star/box.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-pass
2+
// compile-flags: -C opt-level=0
3+
4+
#![feature(dyn_star)]
5+
#![allow(incomplete_features)]
6+
7+
use std::fmt::Display;
8+
9+
fn make_dyn_star() -> dyn* Display {
10+
Box::new(42) as dyn* Display
11+
}
12+
13+
fn main() {
14+
let x = make_dyn_star();
15+
16+
println!("{x}");
17+
}

0 commit comments

Comments
 (0)