Skip to content

Commit

Permalink
Rollup merge of rust-lang#107772 - compiler-errors:dyn-star-backend-i…
Browse files Browse the repository at this point in the history
…s-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`
  • Loading branch information
compiler-errors authored Feb 12, 2023
2 parents 4412a04 + 75ca1ea commit 73bef97
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 deletions.
14 changes: 7 additions & 7 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use rustc_session::Session;
use rustc_span::symbol::sym;
use rustc_span::Symbol;
use rustc_span::{DebuggerVisualizerFile, DebuggerVisualizerType};
use rustc_target::abi::{Align, Size, VariantIdx};
use rustc_target::abi::{Align, VariantIdx};

use std::collections::BTreeSet;
use std::time::{Duration, Instant};
Expand Down Expand Up @@ -273,12 +273,12 @@ pub fn cast_to_dyn_star<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
matches!(dst_ty.kind(), ty::Dynamic(_, _, ty::DynStar)),
"destination type must be a dyn*"
);
// FIXME(dyn-star): this is probably not the best way to check if this is
// a pointer, and really we should ensure that the value is a suitable
// pointer earlier in the compilation process.
let src = match src_ty_and_layout.pointee_info_at(bx.cx(), Size::ZERO) {
Some(_) => bx.ptrtoint(src, bx.cx().type_isize()),
None => bx.bitcast(src, bx.type_isize()),
// FIXME(dyn-star): We can remove this when all supported LLVMs use opaque ptrs only.
let src = match bx.cx().type_kind(bx.cx().backend_type(src_ty_and_layout)) {
TypeKind::Pointer => bx.pointercast(src, bx.cx().type_i8p()),
TypeKind::Integer => bx.inttoptr(src, bx.cx().type_i8p()),
// 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))
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ where

ty::Dynamic(_, _, ty::DynStar) => {
if i == 0 {
TyMaybeWithLayout::Ty(tcx.types.usize)
TyMaybeWithLayout::Ty(tcx.mk_mut_ptr(tcx.types.unit))
} else if i == 1 {
// FIXME(dyn-star) same FIXME as above applies here too
TyMaybeWithLayout::Ty(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ty_utils/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ fn layout_of_uncached<'tcx>(
}

ty::Dynamic(_, _, ty::DynStar) => {
let mut data = scalar_unit(Int(dl.ptr_sized_integer(), false));
let mut data = scalar_unit(Pointer(AddressSpace::DATA));
data.valid_range_mut().start = 0;
let mut vtable = scalar_unit(Pointer(AddressSpace::DATA));
vtable.valid_range_mut().start = 1;
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_type_ir/src/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ pub enum DynKind {
Dyn,
/// A sized `dyn* Trait` object
///
/// These objects are represented as a `(data, vtable)` pair where `data` is a ptr-sized value
/// (often a pointer to the real object, but not necessarily) and `vtable` is a pointer to
/// the vtable for `dyn* Trait`. The representation is essentially the same as `&dyn Trait`
/// or similar, but the drop function included in the vtable is responsible for freeing the
/// underlying storage if needed. This allows a `dyn*` object to be treated agnostically with
/// These objects are represented as a `(data, vtable)` pair where `data` is a value of some
/// ptr-sized and ptr-aligned dynamically determined type `T` and `vtable` is a pointer to the
/// vtable of `impl T for Trait`. This allows a `dyn*` object to be treated agnostically with
/// respect to whether it points to a `Box<T>`, `Rc<T>`, etc.
DynStar,
}
Expand Down
7 changes: 7 additions & 0 deletions tests/codegen/function-arguments.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// compile-flags: -O -C no-prepopulate-passes

#![crate_type = "lib"]
#![feature(dyn_star)]

use std::mem::MaybeUninit;
use std::num::NonZeroU64;
Expand Down Expand Up @@ -279,3 +280,9 @@ pub fn enum_id_1(x: Option<Result<u16, u16>>) -> Option<Result<u16, u16>> {
pub fn enum_id_2(x: Option<u8>) -> Option<u8> {
x
}

// CHECK: { {{i8\*|ptr}}, {{i.*\*|ptr}} } @dyn_star({{i8\*|ptr}} noundef %x.0, {{i.*\*|ptr}} noalias noundef readonly align {{.*}} dereferenceable({{.*}}) %x.1)
#[no_mangle]
pub fn dyn_star(x: dyn* Drop) -> dyn* Drop {
x
}
24 changes: 24 additions & 0 deletions tests/ui/llvm-old-style-ptrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// run-pass
// compile-flags: --target x86_64-unknown-linux-gnu -Copt-level=0 -Cllvm-args=-opaque-pointers=0
// needs-llvm-components: x86

// (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,)));
}

0 comments on commit 73bef97

Please sign in to comment.