Skip to content

Commit

Permalink
Auto merge of #131029 - matthiaskrgr:rollup-rh66wir, r=matthiaskrgr
Browse files Browse the repository at this point in the history
Rollup of 4 pull requests

Successful merges:

 - #123932 (restate GlobalAlloc method safety preconditions in terms of what the caller has to do for greater clarity)
 - #129003 (Improve Ord docs)
 - #130972 (stabilize const_cell_into_inner)
 - #130990 (try to get rid of mir::Const::normalize)

r? `@ghost`
`@rustbot` modify labels: rollup
  • Loading branch information
bors committed Sep 29, 2024
2 parents 42ff2ee + a0ae32d commit 7608018
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,6 @@ index d9de37e..8293fce 100644
#[cfg(target_has_atomic_load_store = "ptr")]
macro_rules! atomic_int_ptr_sized {
( $($target_pointer_width:literal $align:literal)* ) => { $(
diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs
index 58b9ba4..91bbd0a 100644
--- a/library/core/src/cell.rs
+++ b/library/core/src/cell.rs
@@ -2246,8 +2246,6 @@ unsafe_cell_primitive_into_inner! {
u32 "32"
i64 "64"
u64 "64"
- i128 "128"
- u128 "128"
isize "ptr"
usize "ptr"
}
--
2.26.2.7.g19db9cfb68

16 changes: 3 additions & 13 deletions compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ pub enum Const<'tcx> {
}

impl<'tcx> Const<'tcx> {
pub fn identity_unevaluated(
/// Creates an unevaluated const from a `DefId` for a const item.
/// The binders of the const item still need to be instantiated.
pub fn from_unevaluated(
tcx: TyCtxt<'tcx>,
def_id: DefId,
) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
Expand Down Expand Up @@ -329,18 +331,6 @@ impl<'tcx> Const<'tcx> {
}
}

/// Normalizes the constant to a value or an error if possible.
#[inline]
pub fn normalize(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
match self.eval(tcx, param_env, DUMMY_SP) {
Ok(val) => Self::Val(val, self.ty()),
Err(ErrorHandled::Reported(guar, _span)) => {
Self::Ty(Ty::new_error(tcx, guar.into()), ty::Const::new_error(tcx, guar.into()))
}
Err(ErrorHandled::TooGeneric(_span)) => self,
}
}

#[inline]
pub fn try_eval_scalar(
self,
Expand Down
18 changes: 6 additions & 12 deletions compiler/rustc_mir_build/src/thir/cx/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -699,23 +699,17 @@ impl<'tcx> Cx<'tcx> {
}
}
hir::InlineAsmOperand::Const { ref anon_const } => {
let value = mir::Const::identity_unevaluated(
tcx,
anon_const.def_id.to_def_id(),
)
.instantiate_identity()
.normalize(tcx, self.param_env);
let value =
mir::Const::from_unevaluated(tcx, anon_const.def_id.to_def_id())
.instantiate_identity();
let span = tcx.def_span(anon_const.def_id);

InlineAsmOperand::Const { value, span }
}
hir::InlineAsmOperand::SymFn { ref anon_const } => {
let value = mir::Const::identity_unevaluated(
tcx,
anon_const.def_id.to_def_id(),
)
.instantiate_identity()
.normalize(tcx, self.param_env);
let value =
mir::Const::from_unevaluated(tcx, anon_const.def_id.to_def_id())
.instantiate_identity();
let span = tcx.def_span(anon_const.def_id);

InlineAsmOperand::SymFn { value, span }
Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_mir_transform/src/jump_threading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,7 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> {
// Avoid handling them, though this could be extended in the future.
return;
}
let Some(value) =
value.const_.normalize(self.tcx, self.param_env).try_to_scalar_int()
else {
let Some(value) = value.const_.try_eval_scalar_int(self.tcx, self.param_env) else {
return;
};
let conds = conditions.map(self.arena, |c| Condition {
Expand Down
32 changes: 17 additions & 15 deletions library/core/src/alloc/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ pub unsafe trait GlobalAlloc {
///
/// # Safety
///
/// This function is unsafe because undefined behavior can result
/// if the caller does not ensure that `layout` has non-zero size.
/// `layout` must have non-zero size. Attempting to allocate for a zero-sized `layout` may
/// result in undefined behavior.
///
/// (Extension subtraits might provide more specific bounds on
/// behavior, e.g., guarantee a sentinel address or a null pointer
Expand Down Expand Up @@ -156,14 +156,14 @@ pub unsafe trait GlobalAlloc {
///
/// # Safety
///
/// This function is unsafe because undefined behavior can result
/// if the caller does not ensure all of the following:
/// The caller must ensure:
///
/// * `ptr` must denote a block of memory currently allocated via
/// this allocator,
/// * `ptr` is a block of memory currently allocated via this allocator and,
///
/// * `layout` must be the same layout that was used
/// to allocate that block of memory.
/// * `layout` is the same layout that was used to allocate that block of
/// memory.
///
/// Otherwise undefined behavior can result.
#[stable(feature = "global_alloc", since = "1.28.0")]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout);

Expand All @@ -172,7 +172,8 @@ pub unsafe trait GlobalAlloc {
///
/// # Safety
///
/// This function is unsafe for the same reasons that `alloc` is.
/// The caller has to ensure that `layout` has non-zero size. Like `alloc`
/// zero sized `layout` can result in undefined behaviour.
/// However the allocated block of memory is guaranteed to be initialized.
///
/// # Errors
Expand Down Expand Up @@ -220,20 +221,21 @@ pub unsafe trait GlobalAlloc {
///
/// # Safety
///
/// This function is unsafe because undefined behavior can result
/// if the caller does not ensure all of the following:
/// The caller must ensure that:
///
/// * `ptr` must be currently allocated via this allocator,
/// * `ptr` is allocated via this allocator,
///
/// * `layout` must be the same layout that was used
/// * `layout` is the same layout that was used
/// to allocate that block of memory,
///
/// * `new_size` must be greater than zero.
/// * `new_size` is greater than zero.
///
/// * `new_size`, when rounded up to the nearest multiple of `layout.align()`,
/// must not overflow `isize` (i.e., the rounded value must be less than or
/// does not overflow `isize` (i.e., the rounded value must be less than or
/// equal to `isize::MAX`).
///
/// If these are not followed, undefined behaviour can result.
///
/// (Extension subtraits might provide more specific bounds on
/// behavior, e.g., guarantee a sentinel address or a null pointer
/// in response to a zero-size allocation request.)
Expand Down
51 changes: 6 additions & 45 deletions library/core/src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ impl<T> Cell<T> {
/// assert_eq!(five, 5);
/// ```
#[stable(feature = "move_cell", since = "1.17.0")]
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
#[rustc_const_stable(feature = "const_cell_into_inner", since = "CURRENT_RUSTC_VERSION")]
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
pub const fn into_inner(self) -> T {
self.value.into_inner()
}
Expand Down Expand Up @@ -857,7 +858,8 @@ impl<T> RefCell<T> {
/// let five = c.into_inner();
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
#[rustc_const_stable(feature = "const_cell_into_inner", since = "CURRENT_RUSTC_VERSION")]
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
#[inline]
pub const fn into_inner(self) -> T {
// Since this function takes `self` (the `RefCell`) by value, the
Expand Down Expand Up @@ -2100,8 +2102,8 @@ impl<T> UnsafeCell<T> {
/// ```
#[inline(always)]
#[stable(feature = "rust1", since = "1.0.0")]
// When this is const stabilized, please remove `primitive_into_inner` below.
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
#[rustc_const_stable(feature = "const_cell_into_inner", since = "CURRENT_RUSTC_VERSION")]
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
pub const fn into_inner(self) -> T {
self.value
}
Expand Down Expand Up @@ -2247,47 +2249,6 @@ impl<T: CoerceUnsized<U>, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T> {}
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<UnsafeCell<U>> for UnsafeCell<T> {}

// Special cases of UnsafeCell::into_inner where T is a primitive. These are
// used by Atomic*::into_inner.
//
// The real UnsafeCell::into_inner cannot be used yet in a stable const function.
// That is blocked on a "precise drop analysis" unstable const feature.
// https://github.com/rust-lang/rust/issues/73255
macro_rules! unsafe_cell_primitive_into_inner {
($($primitive:ident $atomic:literal)*) => {
$(
#[cfg(target_has_atomic_load_store = $atomic)]
impl UnsafeCell<$primitive> {
pub(crate) const fn primitive_into_inner(self) -> $primitive {
self.value
}
}
)*
};
}

unsafe_cell_primitive_into_inner! {
i8 "8"
u8 "8"
i16 "16"
u16 "16"
i32 "32"
u32 "32"
i64 "64"
u64 "64"
i128 "128"
u128 "128"
isize "ptr"
usize "ptr"
}

#[cfg(target_has_atomic_load_store = "ptr")]
impl<T> UnsafeCell<*mut T> {
pub(crate) const fn primitive_into_inner(self) -> *mut T {
self.value
}
}

/// [`UnsafeCell`], but [`Sync`].
///
/// This is just an `UnsafeCell`, except it implements `Sync`
Expand Down
3 changes: 2 additions & 1 deletion library/core/src/cell/once.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ impl<T> OnceCell<T> {
/// ```
#[inline]
#[stable(feature = "once_cell", since = "1.70.0")]
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
#[rustc_const_stable(feature = "const_cell_into_inner", since = "CURRENT_RUSTC_VERSION")]
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
pub const fn into_inner(self) -> Option<T> {
// Because `into_inner` takes `self` by value, the compiler statically verifies
// that it is not currently borrowed. So it is safe to move out `Option<T>`.
Expand Down
Loading

0 comments on commit 7608018

Please sign in to comment.