Skip to content

Commit c40ca7d

Browse files
committed
add semantics to MaybeDangling
1 parent 4f74d8a commit c40ca7d

5 files changed

Lines changed: 14 additions & 9 deletions

File tree

compiler/rustc_hir/src/lang_items.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ language_item_table! {
341341

342342
PhantomData, sym::phantom_data, phantom_data, Target::Struct, GenericRequirement::Exact(1);
343343

344-
ManuallyDrop, sym::manually_drop, manually_drop, Target::Struct, GenericRequirement::None;
344+
ManuallyDrop, sym::manually_drop, manually_drop, Target::Struct, GenericRequirement::Exact(1);
345+
MaybeDangling, sym::maybe_dangling, maybe_dangling, Target::Struct, GenericRequirement::Exact(1);
345346
BikeshedGuaranteedNoDrop, sym::bikeshed_guaranteed_no_drop, bikeshed_guaranteed_no_drop, Target::Trait, GenericRequirement::Exact(0);
346347

347348
MaybeUninit, sym::maybe_uninit, maybe_uninit, Target::Union, GenericRequirement::None;

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,12 @@ where
10601060
})
10611061
}
10621062

1063+
ty::Adt(adt_def, ..) if adt_def.is_maybe_dangling() => {
1064+
// FIXME: what is the exact effect of maybe dangling?
1065+
Self::ty_and_layout_pointee_info_at(this.field(cx, 0), cx, offset)
1066+
.map(|info| PointeeInfo { safe: None, ..info })
1067+
}
1068+
10631069
_ => {
10641070
let mut data_variant = match &this.variants {
10651071
// Within the discriminant field, only the niche itself is
@@ -1098,7 +1104,7 @@ where
10981104
}
10991105
}
11001106
Variants::Multiple { .. } => None,
1101-
_ => Some(this),
1107+
Variants::Empty | Variants::Single { .. } => Some(this),
11021108
};
11031109

11041110
if let Some(variant) = data_variant

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,7 @@ symbols! {
14451445
maxnumf128,
14461446
may_dangle,
14471447
may_unwind,
1448+
maybe_dangling,
14481449
maybe_uninit,
14491450
maybe_uninit_uninit,
14501451
maybe_uninit_zeroed,

library/core/src/mem/maybe_dangling.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ use crate::{mem, ptr};
44

55
/// Allows wrapped [references] and [boxes] to dangle.
66
///
7-
/// <section class="warning">
8-
/// This type is not properly implemented yet, and the documentation below is thus not accurate.
9-
/// </section>
10-
///
117
/// That is, if a reference (or a `Box`) is wrapped in `MaybeDangling` (including when in a
128
/// (nested) field of a compound type wrapped in `MaybeDangling`), it does not have to follow
139
/// pointer aliasing rules or be dereferenceable.
@@ -73,6 +69,7 @@ use crate::{mem, ptr};
7369
#[repr(transparent)]
7470
#[rustc_pub_transparent]
7571
#[derive(Debug, Copy, Clone, Default)]
72+
#[lang = "maybe_dangling"]
7673
pub struct MaybeDangling<P: ?Sized>(P);
7774

7875
impl<P: ?Sized> MaybeDangling<P> {

tests/codegen-llvm/manually_drop_refs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33

44
use std::mem::ManuallyDrop;
55

6-
// CHECK: define noundef nonnull align 1 ptr @f(ptr noalias noundef nonnull readnone returned align 1 {{(captures\(ret: address, provenance\))?}} %x) unnamed_addr
6+
// CHECK: define noundef nonnull ptr @f(ptr noundef nonnull readnone returned {{(captures\(ret: address, provenance\) )?}}%x) unnamed_addr
77
#[no_mangle]
88
pub fn f(x: ManuallyDrop<Box<u8>>) -> ManuallyDrop<Box<u8>> {
99
x
1010
}
1111

12-
// CHECK: define noundef nonnull align 1 dereferenceable(1) ptr @g(ptr noalias noundef readonly returned align 1 {{(captures\(ret: address, read_provenance\))?}} dereferenceable(1) %x) unnamed_addr
12+
// CHECK: define noundef nonnull ptr @g(ptr noundef nonnull readnone returned {{(captures\(ret: address, provenance\) )?}}%x) unnamed_addr
1313
#[no_mangle]
1414
pub fn g(x: ManuallyDrop<&u8>) -> ManuallyDrop<&u8> {
1515
x
1616
}
1717

18-
// CHECK: define noundef nonnull align 1 dereferenceable(1) ptr @h(ptr noalias noundef readnone returned align 1 {{(captures\(ret: address, provenance\))?}} dereferenceable(1) %x) unnamed_addr
18+
// CHECK: define noundef nonnull ptr @h(ptr noundef nonnull readnone returned {{(captures\(ret: address, provenance\) )?}}%x) unnamed_addr
1919
#[no_mangle]
2020
pub fn h(x: ManuallyDrop<&mut u8>) -> ManuallyDrop<&mut u8> {
2121
x

0 commit comments

Comments
 (0)