1
Fork 0

Use inttoptr to support usize as dyn* value, use pointercast to make sure pointers are compatible

This commit is contained in:
Michael Goulet 2023-02-07 19:17:04 +00:00
parent 88e39ee314
commit df52e2037a
2 changed files with 31 additions and 0 deletions

View file

@ -273,6 +273,14 @@ pub fn cast_to_dyn_star<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
matches!(dst_ty.kind(), ty::Dynamic(_, _, ty::DynStar)), matches!(dst_ty.kind(), ty::Dynamic(_, _, ty::DynStar)),
"destination type must be a dyn*" "destination type must be a dyn*"
); );
// FIXME(dyn-star): We can remove this when all supported LLVMs use opaque ptrs only.
let unit_ptr = bx.cx().type_ptr_to(bx.cx().type_struct(&[], false));
let src = match bx.cx().type_kind(bx.cx().backend_type(src_ty_and_layout)) {
TypeKind::Pointer => bx.pointercast(src, unit_ptr),
TypeKind::Integer => bx.inttoptr(src, unit_ptr),
// FIXME(dyn-star): We probably have to do a bitcast first, then inttoptr.
kind => bug!("unexpected TypeKind for left-hand side of `dyn*` cast: {kind:?}"),
};
(src, unsized_info(bx, src_ty_and_layout.ty, dst_ty, old_info)) (src, unsized_info(bx, src_ty_and_layout.ty, dst_ty, old_info))
} }

View file

@ -0,0 +1,23 @@
// run-pass
// compile-flags: -Copt-level=0 -Cllvm-args=-opaque-pointers=0
// (opaque-pointers flag is called force-opaque-pointers in LLVM 13...)
// min-llvm-version: 14.0
// This test can be removed once non-opaque pointers are gone from LLVM, maybe.
#![feature(dyn_star, pointer_like_trait)]
#![allow(incomplete_features)]
use std::fmt::Debug;
use std::marker::PointerLike;
fn make_dyn_star<'a>(t: impl PointerLike + Debug + 'a) -> dyn* Debug + 'a {
t as _
}
fn main() {
println!("{:?}", make_dyn_star(Box::new(1i32)));
println!("{:?}", make_dyn_star(2usize));
println!("{:?}", make_dyn_star((3usize,)));
}