Skip to content

Commit 73bef97

Browse files
Rollup merge of rust-lang#107772 - compiler-errors:dyn-star-backend-is-ptr, r=eholk
Make `dyn*`'s value backend type a pointer One tweak on top of Ralf's commit should fix using `usize` as a `dyn*`-coercible type, and should fix when we're using various other pointer types when LLVM opaque pointers is disabled. r? `@eholk` but feel free to reassign cc rust-lang#107728 (comment) `@RalfJung`
2 parents 4412a04 + 75ca1ea commit 73bef97

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

compiler/rustc_codegen_ssa/src/base.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc_session::Session;
3939
use rustc_span::symbol::sym;
4040
use rustc_span::Symbol;
4141
use rustc_span::{DebuggerVisualizerFile, DebuggerVisualizerType};
42-
use rustc_target::abi::{Align, Size, VariantIdx};
42+
use rustc_target::abi::{Align, VariantIdx};
4343

4444
use std::collections::BTreeSet;
4545
use std::time::{Duration, Instant};
@@ -273,12 +273,12 @@ 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): this is probably not the best way to check if this is
277-
// a pointer, and really we should ensure that the value is a suitable
278-
// pointer earlier in the compilation process.
279-
let src = match src_ty_and_layout.pointee_info_at(bx.cx(), Size::ZERO) {
280-
Some(_) => bx.ptrtoint(src, bx.cx().type_isize()),
281-
None => bx.bitcast(src, bx.type_isize()),
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:?}"),
282282
};
283283
(src, unsized_info(bx, src_ty_and_layout.ty, dst_ty, old_info))
284284
}

compiler/rustc_middle/src/ty/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ where
770770

771771
ty::Dynamic(_, _, ty::DynStar) => {
772772
if i == 0 {
773-
TyMaybeWithLayout::Ty(tcx.types.usize)
773+
TyMaybeWithLayout::Ty(tcx.mk_mut_ptr(tcx.types.unit))
774774
} else if i == 1 {
775775
// FIXME(dyn-star) same FIXME as above applies here too
776776
TyMaybeWithLayout::Ty(

compiler/rustc_ty_utils/src/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ fn layout_of_uncached<'tcx>(
193193
}
194194

195195
ty::Dynamic(_, _, ty::DynStar) => {
196-
let mut data = scalar_unit(Int(dl.ptr_sized_integer(), false));
196+
let mut data = scalar_unit(Pointer(AddressSpace::DATA));
197197
data.valid_range_mut().start = 0;
198198
let mut vtable = scalar_unit(Pointer(AddressSpace::DATA));
199199
vtable.valid_range_mut().start = 1;

compiler/rustc_type_ir/src/sty.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ pub enum DynKind {
2626
Dyn,
2727
/// A sized `dyn* Trait` object
2828
///
29-
/// These objects are represented as a `(data, vtable)` pair where `data` is a ptr-sized value
30-
/// (often a pointer to the real object, but not necessarily) and `vtable` is a pointer to
31-
/// the vtable for `dyn* Trait`. The representation is essentially the same as `&dyn Trait`
32-
/// or similar, but the drop function included in the vtable is responsible for freeing the
33-
/// underlying storage if needed. This allows a `dyn*` object to be treated agnostically with
29+
/// These objects are represented as a `(data, vtable)` pair where `data` is a value of some
30+
/// ptr-sized and ptr-aligned dynamically determined type `T` and `vtable` is a pointer to the
31+
/// vtable of `impl T for Trait`. This allows a `dyn*` object to be treated agnostically with
3432
/// respect to whether it points to a `Box<T>`, `Rc<T>`, etc.
3533
DynStar,
3634
}

tests/codegen/function-arguments.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// compile-flags: -O -C no-prepopulate-passes
22

33
#![crate_type = "lib"]
4+
#![feature(dyn_star)]
45

56
use std::mem::MaybeUninit;
67
use std::num::NonZeroU64;
@@ -279,3 +280,9 @@ pub fn enum_id_1(x: Option<Result<u16, u16>>) -> Option<Result<u16, u16>> {
279280
pub fn enum_id_2(x: Option<u8>) -> Option<u8> {
280281
x
281282
}
283+
284+
// CHECK: { {{i8\*|ptr}}, {{i.*\*|ptr}} } @dyn_star({{i8\*|ptr}} noundef %x.0, {{i.*\*|ptr}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}) %x.1)
285+
#[no_mangle]
286+
pub fn dyn_star(x: dyn* Drop) -> dyn* Drop {
287+
x
288+
}

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

+24
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_like_trait)]
11+
#![allow(incomplete_features)]
12+
13+
use std::fmt::Debug;
14+
use std::marker::PointerLike;
15+
16+
fn make_dyn_star<'a>(t: impl PointerLike + 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)