Skip to content

Commit

Permalink
Remove option_payload_ptr; redundant to offset_of
Browse files Browse the repository at this point in the history
  • Loading branch information
GKFX committed Nov 2, 2023
1 parent b800c30 commit e3ced15
Show file tree
Hide file tree
Showing 11 changed files with 12 additions and 170 deletions.
19 changes: 0 additions & 19 deletions compiler/rustc_hir_analysis/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,25 +225,6 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
],
Ty::new_ptr(tcx, ty::TypeAndMut { ty: param(0), mutbl: hir::Mutability::Not }),
),
sym::option_payload_ptr => {
let option_def_id = tcx.require_lang_item(hir::LangItem::Option, None);
let p0 = param(0);
(
1,
vec![Ty::new_ptr(
tcx,
ty::TypeAndMut {
ty: Ty::new_adt(
tcx,
tcx.adt_def(option_def_id),
tcx.mk_args_from_iter([ty::GenericArg::from(p0)].into_iter()),
),
mutbl: hir::Mutability::Not,
},
)],
Ty::new_ptr(tcx, ty::TypeAndMut { ty: p0, mutbl: hir::Mutability::Not }),
)
}
sym::ptr_mask => (
1,
vec![
Expand Down
32 changes: 0 additions & 32 deletions compiler/rustc_mir_transform/src/lower_intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::MirPass;
use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::symbol::sym;
use rustc_target::abi::{FieldIdx, VariantIdx};

pub struct LowerIntrinsics;

Expand Down Expand Up @@ -251,37 +250,6 @@ impl<'tcx> MirPass<'tcx> for LowerIntrinsics {
});
terminator.kind = TerminatorKind::Goto { target };
}
sym::option_payload_ptr => {
if let (Some(target), Some(arg)) = (*target, args[0].place()) {
let ty::RawPtr(ty::TypeAndMut { ty: dest_ty, .. }) =
destination.ty(local_decls, tcx).ty.kind()
else {
bug!();
};

block.statements.push(Statement {
source_info: terminator.source_info,
kind: StatementKind::Assign(Box::new((
*destination,
Rvalue::AddressOf(
Mutability::Not,
arg.project_deeper(
&[
PlaceElem::Deref,
PlaceElem::Downcast(
Some(sym::Some),
VariantIdx::from_u32(1),
),
PlaceElem::Field(FieldIdx::from_u32(0), *dest_ty),
],
tcx,
),
),
))),
});
terminator.kind = TerminatorKind::Goto { target };
}
}
sym::transmute | sym::transmute_unchecked => {
let dst_ty = destination.ty(local_decls, tcx).ty;
let Ok([arg]) = <[_; 1]>::try_from(std::mem::take(args)) else {
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,6 @@ symbols! {
optin_builtin_traits,
option,
option_env,
option_payload_ptr,
options,
or,
or_patterns,
Expand Down
6 changes: 0 additions & 6 deletions library/core/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2487,12 +2487,6 @@ extern "rust-intrinsic" {
where
G: FnOnce<ARG, Output = RET>,
F: FnOnce<ARG, Output = RET>;

/// This method creates a pointer to any `Some` value. If the argument is
/// `None`, an invalid within-bounds pointer (that is still acceptable for
/// constructing an empty slice) is returned.
#[rustc_nounwind]
pub fn option_payload_ptr<T>(arg: *const Option<T>) -> *const T;
}

// Some functions are defined here because they accidentally got made
Expand Down
2 changes: 1 addition & 1 deletion library/core/src/iter/adapters/filter_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ where
// SAFETY: Loop conditions ensure the index is in bounds.

unsafe {
let opt_payload_at = core::intrinsics::option_payload_ptr(&val);
let opt_payload_at = (&val as *const Option<B>).byte_add(core::mem::offset_of!(Option<B>, Some.0));
let dst = guard.array.as_mut_ptr().add(idx);
crate::ptr::copy_nonoverlapping(opt_payload_at.cast(), dst, 1);
crate::mem::forget(val);
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
#![feature(is_ascii_octdigit)]
#![feature(isqrt)]
#![feature(maybe_uninit_uninit_array)]
#![feature(offset_of)]
#![feature(ptr_alignment_type)]
#![feature(ptr_metadata)]
#![feature(set_ptr_value)]
Expand Down
5 changes: 2 additions & 3 deletions library/core/src/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ impl<T> Option<T> {
// `None` case it's just padding).
unsafe {
slice::from_raw_parts(
crate::intrinsics::option_payload_ptr(crate::ptr::from_ref(self)),
(self as *const Self).byte_add(core::mem::offset_of!(Self, Some.0)).cast(),
usize::from(self.is_some()),
)
}
Expand Down Expand Up @@ -829,8 +829,7 @@ impl<T> Option<T> {
// the `None` case it's just padding).
unsafe {
slice::from_raw_parts_mut(
crate::intrinsics::option_payload_ptr(crate::ptr::from_mut(self).cast_const())
.cast_mut(),
(self as *mut Self).byte_add(core::mem::offset_of!(Self, Some.0)).cast(),
usize::from(self.is_some()),
)
}
Expand Down
8 changes: 8 additions & 0 deletions library/core/tests/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,11 @@ fn zip_unzip_roundtrip() {
let a = z.unzip();
assert_eq!(a, (x, y));
}

#[test]
fn as_slice() {
assert_eq!(Some(42).as_slice(), &[42]);
assert_eq!(Some(43).as_mut_slice(), &[43]);
assert_eq!(None::<i32>.as_slice(), &[]);
assert_eq!(None::<i32>.as_mut_slice(), &[]);
}

This file was deleted.

This file was deleted.

12 changes: 0 additions & 12 deletions tests/mir-opt/lower_intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,18 +222,6 @@ pub fn write_via_move_string(r: &mut String, v: String) {

pub enum Never {}

// EMIT_MIR lower_intrinsics.option_payload.LowerIntrinsics.diff
pub fn option_payload(o: &Option<usize>, p: &Option<String>) {
// CHECK-LABEL: fn option_payload(
// CHECK: {{_.*}} = &raw const (((*{{_.*}}) as Some).0: usize);
// CHECK: {{_.*}} = &raw const (((*{{_.*}}) as Some).0: std::string::String);

unsafe {
let _x = core::intrinsics::option_payload_ptr(o);
let _y = core::intrinsics::option_payload_ptr(p);
}
}

// EMIT_MIR lower_intrinsics.ptr_offset.LowerIntrinsics.diff
pub unsafe fn ptr_offset(p: *const i32, d: isize) -> *const i32 {
// CHECK-LABEL: fn ptr_offset(
Expand Down

0 comments on commit e3ced15

Please sign in to comment.