Skip to content

Commit 7608018

Browse files
committed
Auto merge of #131029 - matthiaskrgr:rollup-rh66wir, r=matthiaskrgr
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
2 parents 42ff2ee + a0ae32d commit 7608018

File tree

13 files changed

+299
-217
lines changed

13 files changed

+299
-217
lines changed

compiler/rustc_codegen_cranelift/patches/0027-stdlib-128bit-atomic-operations.patch

-13
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,6 @@ index d9de37e..8293fce 100644
8282
#[cfg(target_has_atomic_load_store = "ptr")]
8383
macro_rules! atomic_int_ptr_sized {
8484
( $($target_pointer_width:literal $align:literal)* ) => { $(
85-
diff --git a/library/core/src/cell.rs b/library/core/src/cell.rs
86-
index 58b9ba4..91bbd0a 100644
87-
--- a/library/core/src/cell.rs
88-
+++ b/library/core/src/cell.rs
89-
@@ -2246,8 +2246,6 @@ unsafe_cell_primitive_into_inner! {
90-
u32 "32"
91-
i64 "64"
92-
u64 "64"
93-
- i128 "128"
94-
- u128 "128"
95-
isize "ptr"
96-
usize "ptr"
97-
}
9885
--
9986
2.26.2.7.g19db9cfb68
10087

compiler/rustc_middle/src/mir/consts.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,9 @@ pub enum Const<'tcx> {
221221
}
222222

223223
impl<'tcx> Const<'tcx> {
224-
pub fn identity_unevaluated(
224+
/// Creates an unevaluated const from a `DefId` for a const item.
225+
/// The binders of the const item still need to be instantiated.
226+
pub fn from_unevaluated(
225227
tcx: TyCtxt<'tcx>,
226228
def_id: DefId,
227229
) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
@@ -329,18 +331,6 @@ impl<'tcx> Const<'tcx> {
329331
}
330332
}
331333

332-
/// Normalizes the constant to a value or an error if possible.
333-
#[inline]
334-
pub fn normalize(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Self {
335-
match self.eval(tcx, param_env, DUMMY_SP) {
336-
Ok(val) => Self::Val(val, self.ty()),
337-
Err(ErrorHandled::Reported(guar, _span)) => {
338-
Self::Ty(Ty::new_error(tcx, guar.into()), ty::Const::new_error(tcx, guar.into()))
339-
}
340-
Err(ErrorHandled::TooGeneric(_span)) => self,
341-
}
342-
}
343-
344334
#[inline]
345335
pub fn try_eval_scalar(
346336
self,

compiler/rustc_mir_build/src/thir/cx/expr.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -699,23 +699,17 @@ impl<'tcx> Cx<'tcx> {
699699
}
700700
}
701701
hir::InlineAsmOperand::Const { ref anon_const } => {
702-
let value = mir::Const::identity_unevaluated(
703-
tcx,
704-
anon_const.def_id.to_def_id(),
705-
)
706-
.instantiate_identity()
707-
.normalize(tcx, self.param_env);
702+
let value =
703+
mir::Const::from_unevaluated(tcx, anon_const.def_id.to_def_id())
704+
.instantiate_identity();
708705
let span = tcx.def_span(anon_const.def_id);
709706

710707
InlineAsmOperand::Const { value, span }
711708
}
712709
hir::InlineAsmOperand::SymFn { ref anon_const } => {
713-
let value = mir::Const::identity_unevaluated(
714-
tcx,
715-
anon_const.def_id.to_def_id(),
716-
)
717-
.instantiate_identity()
718-
.normalize(tcx, self.param_env);
710+
let value =
711+
mir::Const::from_unevaluated(tcx, anon_const.def_id.to_def_id())
712+
.instantiate_identity();
719713
let span = tcx.def_span(anon_const.def_id);
720714

721715
InlineAsmOperand::SymFn { value, span }

compiler/rustc_mir_transform/src/jump_threading.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,7 @@ impl<'a, 'tcx> TOFinder<'a, 'tcx> {
516516
// Avoid handling them, though this could be extended in the future.
517517
return;
518518
}
519-
let Some(value) =
520-
value.const_.normalize(self.tcx, self.param_env).try_to_scalar_int()
521-
else {
519+
let Some(value) = value.const_.try_eval_scalar_int(self.tcx, self.param_env) else {
522520
return;
523521
};
524522
let conds = conditions.map(self.arena, |c| Condition {

library/core/src/alloc/global.rs

+17-15
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ pub unsafe trait GlobalAlloc {
124124
///
125125
/// # Safety
126126
///
127-
/// This function is unsafe because undefined behavior can result
128-
/// if the caller does not ensure that `layout` has non-zero size.
127+
/// `layout` must have non-zero size. Attempting to allocate for a zero-sized `layout` may
128+
/// result in undefined behavior.
129129
///
130130
/// (Extension subtraits might provide more specific bounds on
131131
/// behavior, e.g., guarantee a sentinel address or a null pointer
@@ -156,14 +156,14 @@ pub unsafe trait GlobalAlloc {
156156
///
157157
/// # Safety
158158
///
159-
/// This function is unsafe because undefined behavior can result
160-
/// if the caller does not ensure all of the following:
159+
/// The caller must ensure:
161160
///
162-
/// * `ptr` must denote a block of memory currently allocated via
163-
/// this allocator,
161+
/// * `ptr` is a block of memory currently allocated via this allocator and,
164162
///
165-
/// * `layout` must be the same layout that was used
166-
/// to allocate that block of memory.
163+
/// * `layout` is the same layout that was used to allocate that block of
164+
/// memory.
165+
///
166+
/// Otherwise undefined behavior can result.
167167
#[stable(feature = "global_alloc", since = "1.28.0")]
168168
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout);
169169

@@ -172,7 +172,8 @@ pub unsafe trait GlobalAlloc {
172172
///
173173
/// # Safety
174174
///
175-
/// This function is unsafe for the same reasons that `alloc` is.
175+
/// The caller has to ensure that `layout` has non-zero size. Like `alloc`
176+
/// zero sized `layout` can result in undefined behaviour.
176177
/// However the allocated block of memory is guaranteed to be initialized.
177178
///
178179
/// # Errors
@@ -220,20 +221,21 @@ pub unsafe trait GlobalAlloc {
220221
///
221222
/// # Safety
222223
///
223-
/// This function is unsafe because undefined behavior can result
224-
/// if the caller does not ensure all of the following:
224+
/// The caller must ensure that:
225225
///
226-
/// * `ptr` must be currently allocated via this allocator,
226+
/// * `ptr` is allocated via this allocator,
227227
///
228-
/// * `layout` must be the same layout that was used
228+
/// * `layout` is the same layout that was used
229229
/// to allocate that block of memory,
230230
///
231-
/// * `new_size` must be greater than zero.
231+
/// * `new_size` is greater than zero.
232232
///
233233
/// * `new_size`, when rounded up to the nearest multiple of `layout.align()`,
234-
/// must not overflow `isize` (i.e., the rounded value must be less than or
234+
/// does not overflow `isize` (i.e., the rounded value must be less than or
235235
/// equal to `isize::MAX`).
236236
///
237+
/// If these are not followed, undefined behaviour can result.
238+
///
237239
/// (Extension subtraits might provide more specific bounds on
238240
/// behavior, e.g., guarantee a sentinel address or a null pointer
239241
/// in response to a zero-size allocation request.)

library/core/src/cell.rs

+6-45
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ impl<T> Cell<T> {
514514
/// assert_eq!(five, 5);
515515
/// ```
516516
#[stable(feature = "move_cell", since = "1.17.0")]
517-
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
517+
#[rustc_const_stable(feature = "const_cell_into_inner", since = "CURRENT_RUSTC_VERSION")]
518+
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
518519
pub const fn into_inner(self) -> T {
519520
self.value.into_inner()
520521
}
@@ -857,7 +858,8 @@ impl<T> RefCell<T> {
857858
/// let five = c.into_inner();
858859
/// ```
859860
#[stable(feature = "rust1", since = "1.0.0")]
860-
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
861+
#[rustc_const_stable(feature = "const_cell_into_inner", since = "CURRENT_RUSTC_VERSION")]
862+
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
861863
#[inline]
862864
pub const fn into_inner(self) -> T {
863865
// Since this function takes `self` (the `RefCell`) by value, the
@@ -2100,8 +2102,8 @@ impl<T> UnsafeCell<T> {
21002102
/// ```
21012103
#[inline(always)]
21022104
#[stable(feature = "rust1", since = "1.0.0")]
2103-
// When this is const stabilized, please remove `primitive_into_inner` below.
2104-
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
2105+
#[rustc_const_stable(feature = "const_cell_into_inner", since = "CURRENT_RUSTC_VERSION")]
2106+
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
21052107
pub const fn into_inner(self) -> T {
21062108
self.value
21072109
}
@@ -2247,47 +2249,6 @@ impl<T: CoerceUnsized<U>, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T> {}
22472249
#[unstable(feature = "dispatch_from_dyn", issue = "none")]
22482250
impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<UnsafeCell<U>> for UnsafeCell<T> {}
22492251

2250-
// Special cases of UnsafeCell::into_inner where T is a primitive. These are
2251-
// used by Atomic*::into_inner.
2252-
//
2253-
// The real UnsafeCell::into_inner cannot be used yet in a stable const function.
2254-
// That is blocked on a "precise drop analysis" unstable const feature.
2255-
// https://github.com/rust-lang/rust/issues/73255
2256-
macro_rules! unsafe_cell_primitive_into_inner {
2257-
($($primitive:ident $atomic:literal)*) => {
2258-
$(
2259-
#[cfg(target_has_atomic_load_store = $atomic)]
2260-
impl UnsafeCell<$primitive> {
2261-
pub(crate) const fn primitive_into_inner(self) -> $primitive {
2262-
self.value
2263-
}
2264-
}
2265-
)*
2266-
};
2267-
}
2268-
2269-
unsafe_cell_primitive_into_inner! {
2270-
i8 "8"
2271-
u8 "8"
2272-
i16 "16"
2273-
u16 "16"
2274-
i32 "32"
2275-
u32 "32"
2276-
i64 "64"
2277-
u64 "64"
2278-
i128 "128"
2279-
u128 "128"
2280-
isize "ptr"
2281-
usize "ptr"
2282-
}
2283-
2284-
#[cfg(target_has_atomic_load_store = "ptr")]
2285-
impl<T> UnsafeCell<*mut T> {
2286-
pub(crate) const fn primitive_into_inner(self) -> *mut T {
2287-
self.value
2288-
}
2289-
}
2290-
22912252
/// [`UnsafeCell`], but [`Sync`].
22922253
///
22932254
/// This is just an `UnsafeCell`, except it implements `Sync`

library/core/src/cell/once.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ impl<T> OnceCell<T> {
309309
/// ```
310310
#[inline]
311311
#[stable(feature = "once_cell", since = "1.70.0")]
312-
#[rustc_const_unstable(feature = "const_cell_into_inner", issue = "78729")]
312+
#[rustc_const_stable(feature = "const_cell_into_inner", since = "CURRENT_RUSTC_VERSION")]
313+
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
313314
pub const fn into_inner(self) -> Option<T> {
314315
// Because `into_inner` takes `self` by value, the compiler statically verifies
315316
// that it is not currently borrowed. So it is safe to move out `Option<T>`.

0 commit comments

Comments
 (0)