Skip to content

Commit 6d83110

Browse files
Use inttoptr to support usize as dyn* value, use pointercast to make sure pointers are compatible
1 parent e99a1bc commit 6d83110

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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,13 @@ 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 src = match bx.cx().type_kind(bx.cx().backend_type(src_ty_and_layout)) {
278+
TypeKind::Pointer => bx.pointercast(src, bx.cx().type_i8p()),
279+
TypeKind::Integer => bx.inttoptr(src, bx.cx().type_i8p()),
280+
// FIXME(dyn-star): We probably have to do a bitcast first, then inttoptr.
281+
kind => bug!("unexpected TypeKind for left-hand side of `dyn*` cast: {kind:?}"),
282+
};
276283
(src, unsized_info(bx, src_ty_and_layout.ty, dst_ty, old_info))
277284
}
278285

tests/ui/llvm-old-style-ptrs.rs

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

0 commit comments

Comments
 (0)