Skip to content

Commit 2a2eadf

Browse files
committed
add semantics to MaybeDangling
1 parent 901e77d commit 2a2eadf

File tree

6 files changed

+26
-6
lines changed

6 files changed

+26
-6
lines changed

compiler/rustc_hir/src/lang_items.rs

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

354354
PhantomData, sym::phantom_data, phantom_data, Target::Struct, GenericRequirement::Exact(1);
355355

356-
ManuallyDrop, sym::manually_drop, manually_drop, Target::Struct, GenericRequirement::None;
356+
ManuallyDrop, sym::manually_drop, manually_drop, Target::Struct, GenericRequirement::Exact(1);
357+
MaybeDangling, sym::maybe_dangling, maybe_dangling, Target::Struct, GenericRequirement::Exact(1);
357358
BikeshedGuaranteedNoDrop, sym::bikeshed_guaranteed_no_drop, bikeshed_guaranteed_no_drop, Target::Trait, GenericRequirement::Exact(0);
358359

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

compiler/rustc_middle/src/ty/adt.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ bitflags::bitflags! {
5959
const IS_PIN = 1 << 11;
6060
/// Indicates whether the type is `#[pin_project]`.
6161
const IS_PIN_PROJECT = 1 << 12;
62+
/// Indicates whether the type is `MaybeDangling<_>`.
63+
const IS_MAYBE_DANGLING = 1 << 13;
6264
}
6365
}
6466
rustc_data_structures::external_bitflags_debug! { AdtFlags }
@@ -315,6 +317,9 @@ impl AdtDefData {
315317
if tcx.is_lang_item(did, LangItem::ManuallyDrop) {
316318
flags |= AdtFlags::IS_MANUALLY_DROP;
317319
}
320+
if tcx.is_lang_item(did, LangItem::MaybeDangling) {
321+
flags |= AdtFlags::IS_MAYBE_DANGLING;
322+
}
318323
if tcx.is_lang_item(did, LangItem::UnsafeCell) {
319324
flags |= AdtFlags::IS_UNSAFE_CELL;
320325
}
@@ -439,6 +444,12 @@ impl<'tcx> AdtDef<'tcx> {
439444
self.flags().contains(AdtFlags::IS_MANUALLY_DROP)
440445
}
441446

447+
/// Returns `true` if this is `MaybeDangling<T>`.
448+
#[inline]
449+
pub fn is_maybe_dangling(self) -> bool {
450+
self.flags().contains(AdtFlags::IS_MAYBE_DANGLING)
451+
}
452+
442453
/// Returns `true` if this is `Pin<T>`.
443454
#[inline]
444455
pub fn is_pin(self) -> bool {

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,12 @@ where
10531053
})
10541054
}
10551055

1056+
ty::Adt(adt_def, ..) if adt_def.is_maybe_dangling() => {
1057+
// FIXME: what is the exact effect of maybe dangling?
1058+
Self::ty_and_layout_pointee_info_at(this.field(cx, 0), cx, offset)
1059+
.map(|info| PointeeInfo { safe: None, ..info })
1060+
}
1061+
10561062
_ => {
10571063
let mut data_variant = match &this.variants {
10581064
// Within the discriminant field, only the niche itself is
@@ -1091,7 +1097,7 @@ where
10911097
}
10921098
}
10931099
Variants::Multiple { .. } => None,
1094-
_ => Some(this),
1100+
Variants::Empty | Variants::Single { .. } => Some(this),
10951101
};
10961102

10971103
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
@@ -1401,6 +1401,7 @@ symbols! {
14011401
maxnumf128,
14021402
may_dangle,
14031403
may_unwind,
1404+
maybe_dangling,
14041405
maybe_uninit,
14051406
maybe_uninit_uninit,
14061407
maybe_uninit_zeroed,

library/core/src/mem/maybe_dangling.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ use crate::{mem, ptr};
5959
#[repr(transparent)]
6060
#[rustc_pub_transparent]
6161
#[derive(Debug, Copy, Clone, Default)]
62+
#[lang = "maybe_dangling"]
6263
pub struct MaybeDangling<P: ?Sized>(P);
6364

6465
impl<P: ?Sized> MaybeDangling<P> {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
//@ compile-flags: -Copt-level=3
1+
//@ compile-flags: -Copt-level=3 -Zmerge-functions=disabled
22

33
#![crate_type = "lib"]
44

55
use std::mem::ManuallyDrop;
66

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

11-
// 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
11+
// CHECK: define noundef nonnull ptr @g(ptr noundef nonnull readnone returned captures(ret: address, provenance) %x) unnamed_addr
1212
#[no_mangle]
1313
pub fn g(x: ManuallyDrop<&u8>) -> ManuallyDrop<&u8> { x }
1414

15-
// 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
15+
// CHECK: define noundef nonnull ptr @h(ptr noundef nonnull readnone returned captures(ret: address, provenance) %x) unnamed_addr
1616
#[no_mangle]
1717
pub fn h(x: ManuallyDrop<&mut u8>) -> ManuallyDrop<&mut u8> { x }

0 commit comments

Comments
 (0)