Skip to content

Commit c4375c9

Browse files
committed
Auto merge of #66645 - RalfJung:dereferenceable, r=pnkfelix
remove the 'dereferenceable' attribute from Box Fixes #66600 r? @eddyb @rkruppe
2 parents fe969f4 + d0b9833 commit c4375c9

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/librustc/ty/layout.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2526,9 +2526,16 @@ where
25262526

25272527
if let Some(pointee) = layout.pointee_info_at(cx, offset) {
25282528
if let Some(kind) = pointee.safe {
2529-
attrs.pointee_size = pointee.size;
25302529
attrs.pointee_align = Some(pointee.align);
25312530

2531+
// `Box` (`UniqueBorrowed`) are not necessarily dereferencable
2532+
// for the entire duration of the function as they can be deallocated
2533+
// any time. Set their valid size to 0.
2534+
attrs.pointee_size = match kind {
2535+
PointerKind::UniqueOwned => Size::ZERO,
2536+
_ => pointee.size
2537+
};
2538+
25322539
// `Box` pointer parameters never alias because ownership is transferred
25332540
// `&mut` pointer parameters never alias other parameters,
25342541
// or mutable global data

src/librustc_target/abi/call/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ mod attr_impl {
6969
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
7070
pub struct ArgAttributes {
7171
pub regular: ArgAttribute,
72+
/// The minimum size of the pointee, guaranteed to be valid for the duration of the whole call
73+
/// (corresponding to LLVM's dereferenceable and dereferenceable_or_null attributes).
7274
pub pointee_size: Size,
7375
pub pointee_align: Option<Align>
7476
}

src/test/codegen/function-arguments.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ pub fn indirect_struct(_: S) {
6565
pub fn borrowed_struct(_: &S) {
6666
}
6767

68-
// CHECK: noalias align 4 dereferenceable(4) i32* @_box(i32* noalias align 4 dereferenceable(4) %x)
68+
// `Box` can get deallocated during execution of the function, so it should
69+
// not get `dereferenceable`.
70+
// CHECK: noalias nonnull align 4 i32* @_box(i32* noalias nonnull align 4 %x)
6971
#[no_mangle]
7072
pub fn _box(x: Box<i32>) -> Box<i32> {
7173
x

0 commit comments

Comments
 (0)