From be079117f068af74a133dcaaae33ed495f035cc2 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 22 Nov 2019 22:04:22 +0100 Subject: [PATCH 1/2] remove the 'dereferenceable' attribute from Box --- src/librustc/ty/layout.rs | 8 +++++++- src/librustc_target/abi/call/mod.rs | 1 + src/test/codegen/function-arguments.rs | 4 +++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index b6050a5caf13e..e14a78a8c44ca 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -2526,9 +2526,15 @@ where if let Some(pointee) = layout.pointee_info_at(cx, offset) { if let Some(kind) = pointee.safe { - attrs.pointee_size = pointee.size; attrs.pointee_align = Some(pointee.align); + // `Box` (`UniqueBorrowed`) are not necessarily dereferencable + // for the entire duration of the function, so set their size to 0. + attrs.pointee_size = match kind { + PointerKind::UniqueOwned => Size::ZERO, + _ => pointee.size + }; + // `Box` pointer parameters never alias because ownership is transferred // `&mut` pointer parameters never alias other parameters, // or mutable global data diff --git a/src/librustc_target/abi/call/mod.rs b/src/librustc_target/abi/call/mod.rs index aced12aa32acb..1b55676fe4f98 100644 --- a/src/librustc_target/abi/call/mod.rs +++ b/src/librustc_target/abi/call/mod.rs @@ -69,6 +69,7 @@ mod attr_impl { #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct ArgAttributes { pub regular: ArgAttribute, + /// The dereferenceable size of the pointee. pub pointee_size: Size, pub pointee_align: Option } diff --git a/src/test/codegen/function-arguments.rs b/src/test/codegen/function-arguments.rs index 7e1791cd4f296..5c9aa48c0a5d6 100644 --- a/src/test/codegen/function-arguments.rs +++ b/src/test/codegen/function-arguments.rs @@ -65,7 +65,9 @@ pub fn indirect_struct(_: S) { pub fn borrowed_struct(_: &S) { } -// CHECK: noalias align 4 dereferenceable(4) i32* @_box(i32* noalias align 4 dereferenceable(4) %x) +// `Box` can get deallocated during execution of the function, so it should +// not get `dereferenceable`. +// CHECK: noalias nonnull align 4 i32* @_box(i32* noalias nonnull align 4 %x) #[no_mangle] pub fn _box(x: Box) -> Box { x From d0b983307b1d4469396389e54b9d9ab2b5f43c48 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 25 Nov 2019 22:45:00 +0100 Subject: [PATCH 2/2] review feedback: comments and spacing --- src/librustc/ty/layout.rs | 5 +++-- src/librustc_target/abi/call/mod.rs | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/librustc/ty/layout.rs b/src/librustc/ty/layout.rs index e14a78a8c44ca..c67e6a0a13e63 100644 --- a/src/librustc/ty/layout.rs +++ b/src/librustc/ty/layout.rs @@ -2529,8 +2529,9 @@ where attrs.pointee_align = Some(pointee.align); // `Box` (`UniqueBorrowed`) are not necessarily dereferencable - // for the entire duration of the function, so set their size to 0. - attrs.pointee_size = match kind { + // for the entire duration of the function as they can be deallocated + // any time. Set their valid size to 0. + attrs.pointee_size = match kind { PointerKind::UniqueOwned => Size::ZERO, _ => pointee.size }; diff --git a/src/librustc_target/abi/call/mod.rs b/src/librustc_target/abi/call/mod.rs index 1b55676fe4f98..3a3ad763790ba 100644 --- a/src/librustc_target/abi/call/mod.rs +++ b/src/librustc_target/abi/call/mod.rs @@ -69,7 +69,8 @@ mod attr_impl { #[derive(Copy, Clone, PartialEq, Eq, Debug)] pub struct ArgAttributes { pub regular: ArgAttribute, - /// The dereferenceable size of the pointee. + /// The minimum size of the pointee, guaranteed to be valid for the duration of the whole call + /// (corresponding to LLVM's dereferenceable and dereferenceable_or_null attributes). pub pointee_size: Size, pub pointee_align: Option }