Skip to content

Commit df52e20

Browse files
Use inttoptr to support usize as dyn* value, use pointercast to make sure pointers are compatible
1 parent 88e39ee commit df52e20

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@ pub fn cast_to_dyn_star<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
273273
matches!(dst_ty.kind(), ty::Dynamic(_, _, ty::DynStar)),
274274
"destination type must be a dyn*"
275275
);
276+
// FIXME(dyn-star): We can remove this when all supported LLVMs use opaque ptrs only.
277+
let unit_ptr = bx.cx().type_ptr_to(bx.cx().type_struct(&[], false));
278+
let src = match bx.cx().type_kind(bx.cx().backend_type(src_ty_and_layout)) {
279+
TypeKind::Pointer => bx.pointercast(src, unit_ptr),
280+
TypeKind::Integer => bx.inttoptr(src, unit_ptr),
281+
// FIXME(dyn-star): We probably have to do a bitcast first, then inttoptr.
282+
kind => bug!("unexpected TypeKind for left-hand side of `dyn*` cast: {kind:?}"),
283+
};
276284
(src, unsized_info(bx, src_ty_and_layout.ty, dst_ty, old_info))
277285
}
278286

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// run-pass
2+
// compile-flags: -Copt-level=0 -Cllvm-args=-opaque-pointers=0
3+
4+
// (opaque-pointers flag is called force-opaque-pointers in LLVM 13...)
5+
// min-llvm-version: 14.0
6+
7+
// This test can be removed once non-opaque pointers are gone from LLVM, maybe.
8+
9+
#![feature(dyn_star, pointer_like_trait)]
10+
#![allow(incomplete_features)]
11+
12+
use std::fmt::Debug;
13+
use std::marker::PointerLike;
14+
15+
fn make_dyn_star<'a>(t: impl PointerLike + Debug + 'a) -> dyn* Debug + 'a {
16+
t as _
17+
}
18+
19+
fn main() {
20+
println!("{:?}", make_dyn_star(Box::new(1i32)));
21+
println!("{:?}", make_dyn_star(2usize));
22+
println!("{:?}", make_dyn_star((3usize,)));
23+
}

0 commit comments

Comments
 (0)