Skip to content

Commit d6ca5c3

Browse files
committed
strip readonly/captures from MaybeDangling<&T>
1 parent 8aafa53 commit d6ca5c3

4 files changed

Lines changed: 17 additions & 31 deletions

File tree

compiler/rustc_abi/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,18 +2153,14 @@ pub enum PointerKind {
21532153
pub struct PointeeInfo {
21542154
/// If this is `None`, then this is a raw pointer.
21552155
pub safe: Option<PointerKind>,
2156-
/// If this is true, the pointer is wrapped in `MaybeDangling` and thus can't be assumed to
2157-
/// not alias. Having this as separate state from `safe: None` allows us to keep `readonly` and
2158-
/// `captures(address, read_provenance)` llvm attributes.
2159-
pub may_dangle: bool,
21602156
/// If `size` is not zero, then the pointer is either null or dereferenceable for this many bytes
2161-
/// (independent of `safe` and `may_dangle`).
2157+
/// (independent of `safe`).
21622158
///
21632159
/// On a function argument, "dereferenceable" here means "dereferenceable for the entire duration
21642160
/// of this function call", i.e. it is UB for the memory that this pointer points to be freed
21652161
/// while this function is still running.
21662162
pub size: Size,
2167-
/// The pointer is guaranteed to be aligned this much (independent of `safe` and `may_dangle`).
2163+
/// The pointer is guaranteed to be aligned this much (independent of `safe`).
21682164
pub align: Align,
21692165
}
21702166

compiler/rustc_middle/src/ty/layout.rs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,12 +1028,9 @@ where
10281028
let optimize = tcx.sess.opts.optimize != OptLevel::No;
10291029

10301030
let pointee_info = match *this.ty.kind() {
1031-
ty::RawPtr(_, _) | ty::FnPtr(..) if offset.bytes() == 0 => Some(PointeeInfo {
1032-
safe: None,
1033-
size: Size::ZERO,
1034-
align: Align::ONE,
1035-
may_dangle: false,
1036-
}),
1031+
ty::RawPtr(_, _) | ty::FnPtr(..) if offset.bytes() == 0 => {
1032+
Some(PointeeInfo { safe: None, size: Size::ZERO, align: Align::ONE })
1033+
}
10371034
ty::Ref(_, ty, mt) if offset.bytes() == 0 => {
10381035
tcx.layout_of(typing_env.as_query_input(ty)).ok().map(|layout| {
10391036
let (size, kind);
@@ -1062,12 +1059,7 @@ where
10621059
kind = PointerKind::MutableRef { unpin };
10631060
}
10641061
};
1065-
PointeeInfo {
1066-
safe: Some(kind),
1067-
size,
1068-
align: layout.align.abi,
1069-
may_dangle: false,
1070-
}
1062+
PointeeInfo { safe: Some(kind), size, align: layout.align.abi }
10711063
})
10721064
}
10731065

@@ -1090,20 +1082,19 @@ where
10901082
size: Size::ZERO,
10911083

10921084
align: layout.align.abi,
1093-
may_dangle: false,
10941085
})
10951086
}
10961087

10971088
ty::Adt(adt_def, ..) if adt_def.is_maybe_dangling() => {
10981089
Self::ty_and_layout_pointee_info_at(this.field(cx, 0), cx, offset).map(|info| {
10991090
PointeeInfo {
1100-
// Mark the pointer as possibly dangling
1101-
// (thus removing noalias in case of llvm backend)
1102-
may_dangle: true,
1091+
// Mark the pointer as raw
1092+
// (thus removing noalias/readonly/etc in case of the llvm backend)
1093+
safe: None,
11031094
// Make sure we don't assert dereferenceability of the pointer.
11041095
size: Size::ZERO,
11051096
// Preserve the alignment assertion! That is required even inside `MaybeDangling`.
1106-
..info
1097+
align: info.align,
11071098
}
11081099
})
11091100
}

compiler/rustc_ty_utils/src/abi.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,11 @@ fn arg_attrs_for_rust_scalar<'tcx>(
351351
// not return values.
352352
//
353353
// `&mut T` and `Box<T>` where `T: Unpin` are unique and hence `noalias`.
354-
let no_alias = !pointee.may_dangle
355-
&& match kind {
356-
PointerKind::SharedRef { frozen } => frozen,
357-
PointerKind::MutableRef { unpin } => unpin && noalias_mut_ref,
358-
PointerKind::Box { unpin, global } => unpin && global && noalias_for_box,
359-
};
354+
let no_alias = match kind {
355+
PointerKind::SharedRef { frozen } => frozen,
356+
PointerKind::MutableRef { unpin } => unpin && noalias_mut_ref,
357+
PointerKind::Box { unpin, global } => unpin && global && noalias_for_box,
358+
};
360359
// We can never add `noalias` in return position; that LLVM attribute has some very surprising semantics
361360
// (see <https://github.com/rust-lang/unsafe-code-guidelines/issues/385#issuecomment-1368055745>).
362361
if no_alias && !is_return {

tests/codegen-llvm/maybe_dangling_refs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn f(x: MaybeDangling<Box<u8>>) -> MaybeDangling<Box<u8>> {
1515
x
1616
}
1717

18-
// CHECK: define {{(dso_local )?}}noundef nonnull ptr @g(ptr noundef nonnull readonly captures(address, read_provenance) %x) unnamed_addr
18+
// CHECK: define {{(dso_local )?}}noundef nonnull ptr @g(ptr noundef nonnull %x) unnamed_addr
1919
#[no_mangle]
2020
pub fn g(x: MaybeDangling<&u8>) -> MaybeDangling<&u8> {
2121
x
@@ -33,7 +33,7 @@ pub fn i(x: MaybeDangling<Box<u32>>) -> MaybeDangling<Box<u32>> {
3333
x
3434
}
3535

36-
// CHECK: define {{(dso_local )?}}noundef nonnull align 4 ptr @j(ptr noundef nonnull readonly align 4 captures(address, read_provenance) %x) unnamed_addr
36+
// CHECK: define {{(dso_local )?}}noundef nonnull align 4 ptr @j(ptr noundef nonnull align 4 %x) unnamed_addr
3737
#[no_mangle]
3838
pub fn j(x: MaybeDangling<&u32>) -> MaybeDangling<&u32> {
3939
x

0 commit comments

Comments
 (0)