Skip to content

Commit e458d45

Browse files
committed
rust: upgrade to Rust 1.70.0
This is the second upgrade to the Rust toolchain, from 1.68.2 to 1.70.0 (i.e. the latest). # Context and upgrade policy See the upgrade policy [1] and the comments on the first upgrade in commit 3ed03f4 ("rust: upgrade to Rust 1.68.2"). # Unstable features stabilized No unstable features (that we use) were stabilized. Therefore, the only unstable feature allowed to be used outside the `kernel` crate is still `new_uninit`, though other code to be upstreamed may increase the list. Please see [2] for details. # Other required changes No required changes this time around. # `alloc` upgrade and reviewing The vast majority of changes are due to our `alloc` fork being upgraded at once. There are two kinds of changes to be aware of: the ones coming from upstream, which we should follow as closely as possible, and the updates needed in our added fallible APIs to keep them matching the newer infallible APIs coming from upstream. Instead of taking a look at the diff of this patch, an alternative approach is reviewing a diff of the changes between upstream `alloc` and the kernel's. This allows to easily inspect the kernel additions only, especially to check if the fallible methods we already have still match the infallible ones in the new version coming from upstream. Another approach is reviewing the changes introduced in the additions in the kernel fork between the two versions. This is useful to spot potentially unintended changes to our additions. To apply these approaches, one may follow steps similar to the following to generate a pair of patches that show the differences between upstream Rust and the kernel (for the subset of `alloc` we use) before and after applying this patch: # Get the difference with respect to the old version. git -C rust checkout $(linux/scripts/min-tool-version.sh rustc) git -C linux ls-tree -r --name-only HEAD -- rust/alloc | cut -d/ -f3- | grep -Fv README.md | xargs -IPATH cp rust/library/alloc/src/PATH linux/rust/alloc/PATH git -C linux diff --patch-with-stat --summary -R > old.patch git -C linux restore rust/alloc # Apply this patch. git -C linux am rust-upgrade.patch # Get the difference with respect to the new version. git -C rust checkout $(linux/scripts/min-tool-version.sh rustc) git -C linux ls-tree -r --name-only HEAD -- rust/alloc | cut -d/ -f3- | grep -Fv README.md | xargs -IPATH cp rust/library/alloc/src/PATH linux/rust/alloc/PATH git -C linux diff --patch-with-stat --summary -R > new.patch git -C linux restore rust/alloc Now one may check the `new.patch` to take a look at the additions (first approach) or at the difference between those two patches (second approach). For the latter, a side-by-side tool is recommended. Link: https://rust-for-linux.com/rust-version-policy [1] Link: Rust-for-Linux#2 [2] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent d09a610 commit e458d45

File tree

9 files changed

+128
-147
lines changed

9 files changed

+128
-147
lines changed

Documentation/process/changes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ you probably needn't concern yourself with pcmciautils.
3131
====================== =============== ========================================
3232
GNU C 5.1 gcc --version
3333
Clang/LLVM (optional) 11.0.0 clang --version
34-
Rust (optional) 1.68.2 rustc --version
34+
Rust (optional) 1.70.0 rustc --version
3535
bindgen (optional) 0.56.0 bindgen --version
3636
GNU make 3.82 make --version
3737
bash 4.2 bash --version

rust/alloc/alloc.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ use core::ptr::{self, NonNull};
1616
#[doc(inline)]
1717
pub use core::alloc::*;
1818

19-
use core::marker::Destruct;
20-
2119
#[cfg(test)]
2220
mod tests;
2321

@@ -333,16 +331,12 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
333331

334332
#[cfg_attr(not(test), lang = "box_free")]
335333
#[inline]
336-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
337334
// This signature has to be the same as `Box`, otherwise an ICE will happen.
338335
// When an additional parameter to `Box` is added (like `A: Allocator`), this has to be added here as
339336
// well.
340337
// For example if `Box` is changed to `struct Box<T: ?Sized, A: Allocator>(Unique<T>, A)`,
341338
// this function has to be changed to `fn box_free<T: ?Sized, A: Allocator>(Unique<T>, A)` as well.
342-
pub(crate) const unsafe fn box_free<T: ?Sized, A: ~const Allocator + ~const Destruct>(
343-
ptr: Unique<T>,
344-
alloc: A,
345-
) {
339+
pub(crate) unsafe fn box_free<T: ?Sized, A: Allocator>(ptr: Unique<T>, alloc: A) {
346340
unsafe {
347341
let size = size_of_val(ptr.as_ref());
348342
let align = min_align_of_val(ptr.as_ref());

rust/alloc/boxed.rs

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,13 @@ use core::any::Any;
152152
use core::async_iter::AsyncIterator;
153153
use core::borrow;
154154
use core::cmp::Ordering;
155-
use core::convert::{From, TryFrom};
156155
use core::error::Error;
157156
use core::fmt;
158157
use core::future::Future;
159158
use core::hash::{Hash, Hasher};
160-
#[cfg(not(no_global_oom_handling))]
161-
use core::iter::FromIterator;
162-
use core::iter::{FusedIterator, Iterator};
159+
use core::iter::FusedIterator;
163160
use core::marker::Tuple;
164-
use core::marker::{Destruct, Unpin, Unsize};
161+
use core::marker::Unsize;
165162
use core::mem;
166163
use core::ops::{
167164
CoerceUnsized, Deref, DerefMut, DispatchFromDyn, Generator, GeneratorState, Receiver,
@@ -218,6 +215,7 @@ impl<T> Box<T> {
218215
#[inline(always)]
219216
#[stable(feature = "rust1", since = "1.0.0")]
220217
#[must_use]
218+
#[rustc_diagnostic_item = "box_new"]
221219
pub fn new(x: T) -> Self {
222220
#[rustc_box]
223221
Box::new(x)
@@ -287,9 +285,7 @@ impl<T> Box<T> {
287285
#[must_use]
288286
#[inline(always)]
289287
pub fn pin(x: T) -> Pin<Box<T>> {
290-
(#[rustc_box]
291-
Box::new(x))
292-
.into()
288+
Box::new(x).into()
293289
}
294290

295291
/// Allocates memory on the heap then places `x` into it,
@@ -381,12 +377,11 @@ impl<T, A: Allocator> Box<T, A> {
381377
/// ```
382378
#[cfg(not(no_global_oom_handling))]
383379
#[unstable(feature = "allocator_api", issue = "32838")]
384-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
385380
#[must_use]
386381
#[inline]
387-
pub const fn new_in(x: T, alloc: A) -> Self
382+
pub fn new_in(x: T, alloc: A) -> Self
388383
where
389-
A: ~const Allocator + ~const Destruct,
384+
A: Allocator,
390385
{
391386
let mut boxed = Self::new_uninit_in(alloc);
392387
unsafe {
@@ -411,12 +406,10 @@ impl<T, A: Allocator> Box<T, A> {
411406
/// # Ok::<(), std::alloc::AllocError>(())
412407
/// ```
413408
#[unstable(feature = "allocator_api", issue = "32838")]
414-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
415409
#[inline]
416-
pub const fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
410+
pub fn try_new_in(x: T, alloc: A) -> Result<Self, AllocError>
417411
where
418-
T: ~const Destruct,
419-
A: ~const Allocator + ~const Destruct,
412+
A: Allocator,
420413
{
421414
let mut boxed = Self::try_new_uninit_in(alloc)?;
422415
unsafe {
@@ -446,13 +439,12 @@ impl<T, A: Allocator> Box<T, A> {
446439
/// assert_eq!(*five, 5)
447440
/// ```
448441
#[unstable(feature = "allocator_api", issue = "32838")]
449-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
450442
#[cfg(not(no_global_oom_handling))]
451443
#[must_use]
452444
// #[unstable(feature = "new_uninit", issue = "63291")]
453-
pub const fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
445+
pub fn new_uninit_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
454446
where
455-
A: ~const Allocator + ~const Destruct,
447+
A: Allocator,
456448
{
457449
let layout = Layout::new::<mem::MaybeUninit<T>>();
458450
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -487,10 +479,9 @@ impl<T, A: Allocator> Box<T, A> {
487479
/// ```
488480
#[unstable(feature = "allocator_api", issue = "32838")]
489481
// #[unstable(feature = "new_uninit", issue = "63291")]
490-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
491-
pub const fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
482+
pub fn try_new_uninit_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
492483
where
493-
A: ~const Allocator + ~const Destruct,
484+
A: Allocator,
494485
{
495486
let layout = Layout::new::<mem::MaybeUninit<T>>();
496487
let ptr = alloc.allocate(layout)?.cast();
@@ -518,13 +509,12 @@ impl<T, A: Allocator> Box<T, A> {
518509
///
519510
/// [zeroed]: mem::MaybeUninit::zeroed
520511
#[unstable(feature = "allocator_api", issue = "32838")]
521-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
522512
#[cfg(not(no_global_oom_handling))]
523513
// #[unstable(feature = "new_uninit", issue = "63291")]
524514
#[must_use]
525-
pub const fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
515+
pub fn new_zeroed_in(alloc: A) -> Box<mem::MaybeUninit<T>, A>
526516
where
527-
A: ~const Allocator + ~const Destruct,
517+
A: Allocator,
528518
{
529519
let layout = Layout::new::<mem::MaybeUninit<T>>();
530520
// NOTE: Prefer match over unwrap_or_else since closure sometimes not inlineable.
@@ -559,10 +549,9 @@ impl<T, A: Allocator> Box<T, A> {
559549
/// [zeroed]: mem::MaybeUninit::zeroed
560550
#[unstable(feature = "allocator_api", issue = "32838")]
561551
// #[unstable(feature = "new_uninit", issue = "63291")]
562-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
563-
pub const fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
552+
pub fn try_new_zeroed_in(alloc: A) -> Result<Box<mem::MaybeUninit<T>, A>, AllocError>
564553
where
565-
A: ~const Allocator + ~const Destruct,
554+
A: Allocator,
566555
{
567556
let layout = Layout::new::<mem::MaybeUninit<T>>();
568557
let ptr = alloc.allocate_zeroed(layout)?.cast();
@@ -578,12 +567,11 @@ impl<T, A: Allocator> Box<T, A> {
578567
/// construct a (pinned) `Box` in a different way than with [`Box::new_in`].
579568
#[cfg(not(no_global_oom_handling))]
580569
#[unstable(feature = "allocator_api", issue = "32838")]
581-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
582570
#[must_use]
583571
#[inline(always)]
584-
pub const fn pin_in(x: T, alloc: A) -> Pin<Self>
572+
pub fn pin_in(x: T, alloc: A) -> Pin<Self>
585573
where
586-
A: 'static + ~const Allocator + ~const Destruct,
574+
A: 'static + Allocator,
587575
{
588576
Self::into_pin(Self::new_in(x, alloc))
589577
}
@@ -610,12 +598,8 @@ impl<T, A: Allocator> Box<T, A> {
610598
/// assert_eq!(Box::into_inner(c), 5);
611599
/// ```
612600
#[unstable(feature = "box_into_inner", issue = "80437")]
613-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
614601
#[inline]
615-
pub const fn into_inner(boxed: Self) -> T
616-
where
617-
Self: ~const Destruct,
618-
{
602+
pub fn into_inner(boxed: Self) -> T {
619603
*boxed
620604
}
621605
}
@@ -1246,8 +1230,8 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Box<T, A> {
12461230
#[stable(feature = "rust1", since = "1.0.0")]
12471231
impl<T: Default> Default for Box<T> {
12481232
/// Creates a `Box<T>`, with the `Default` value for T.
1233+
#[inline]
12491234
fn default() -> Self {
1250-
#[rustc_box]
12511235
Box::new(T::default())
12521236
}
12531237
}
@@ -1256,6 +1240,7 @@ impl<T: Default> Default for Box<T> {
12561240
#[stable(feature = "rust1", since = "1.0.0")]
12571241
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
12581242
impl<T> const Default for Box<[T]> {
1243+
#[inline]
12591244
fn default() -> Self {
12601245
let ptr: Unique<[T]> = Unique::<[T; 0]>::dangling();
12611246
Box(ptr, Global)
@@ -1266,6 +1251,7 @@ impl<T> const Default for Box<[T]> {
12661251
#[stable(feature = "default_box_extra", since = "1.17.0")]
12671252
#[rustc_const_unstable(feature = "const_default_impls", issue = "87864")]
12681253
impl const Default for Box<str> {
1254+
#[inline]
12691255
fn default() -> Self {
12701256
// SAFETY: This is the same as `Unique::cast<U>` but with an unsized `U = str`.
12711257
let ptr: Unique<str> = unsafe {
@@ -1620,7 +1606,6 @@ impl<T, const N: usize> From<[T; N]> for Box<[T]> {
16201606
/// println!("{boxed:?}");
16211607
/// ```
16221608
fn from(array: [T; N]) -> Box<[T]> {
1623-
#[rustc_box]
16241609
Box::new(array)
16251610
}
16261611
}

rust/alloc/lib.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,14 @@
8989
#![warn(missing_debug_implementations)]
9090
#![warn(missing_docs)]
9191
#![allow(explicit_outlives_requirements)]
92+
#![warn(multiple_supertrait_upcastable)]
9293
//
9394
// Library features:
95+
// tidy-alphabetical-start
96+
#![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
97+
#![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))]
98+
#![cfg_attr(test, feature(is_sorted))]
99+
#![cfg_attr(test, feature(new_uninit))]
94100
#![feature(alloc_layout_extra)]
95101
#![feature(allocator_api)]
96102
#![feature(array_chunks)]
@@ -100,24 +106,21 @@
100106
#![feature(assert_matches)]
101107
#![feature(async_iterator)]
102108
#![feature(coerce_unsized)]
103-
#![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
109+
#![feature(const_align_of_val)]
104110
#![feature(const_box)]
105-
#![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))]
106-
#![cfg_attr(not(no_borrow), feature(const_cow_is_borrowed))]
107111
#![feature(const_convert)]
108-
#![feature(const_size_of_val)]
109-
#![feature(const_align_of_val)]
110-
#![feature(const_ptr_read)]
111-
#![feature(const_maybe_uninit_zeroed)]
112-
#![feature(const_maybe_uninit_write)]
112+
#![cfg_attr(not(no_borrow), feature(const_cow_is_borrowed))]
113+
#![feature(const_eval_select)]
113114
#![feature(const_maybe_uninit_as_mut_ptr)]
115+
#![feature(const_maybe_uninit_write)]
116+
#![feature(const_maybe_uninit_zeroed)]
117+
#![feature(const_pin)]
118+
#![feature(const_ptr_read)]
114119
#![feature(const_refs_to_cell)]
120+
#![feature(const_size_of_val)]
121+
#![feature(const_waker)]
115122
#![feature(core_intrinsics)]
116123
#![feature(core_panic)]
117-
#![feature(const_eval_select)]
118-
#![feature(const_pin)]
119-
#![feature(const_waker)]
120-
#![feature(cstr_from_bytes_until_nul)]
121124
#![feature(dispatch_from_dyn)]
122125
#![feature(error_generic_member_access)]
123126
#![feature(error_in_core)]
@@ -128,16 +131,13 @@
128131
#![feature(hasher_prefixfree_extras)]
129132
#![feature(inline_const)]
130133
#![feature(inplace_iteration)]
131-
#![cfg_attr(test, feature(is_sorted))]
132134
#![feature(iter_advance_by)]
133135
#![feature(iter_next_chunk)]
134136
#![feature(iter_repeat_n)]
135137
#![feature(layout_for_ptr)]
136138
#![feature(maybe_uninit_slice)]
137139
#![feature(maybe_uninit_uninit_array)]
138140
#![feature(maybe_uninit_uninit_array_transpose)]
139-
#![cfg_attr(test, feature(new_uninit))]
140-
#![feature(nonnull_slice_from_raw_parts)]
141141
#![feature(pattern)]
142142
#![feature(pointer_byte_offsets)]
143143
#![feature(provide_any)]
@@ -153,6 +153,7 @@
153153
#![feature(slice_ptr_get)]
154154
#![feature(slice_ptr_len)]
155155
#![feature(slice_range)]
156+
#![feature(std_internals)]
156157
#![feature(str_internals)]
157158
#![feature(strict_provenance)]
158159
#![feature(trusted_len)]
@@ -163,40 +164,43 @@
163164
#![feature(unicode_internals)]
164165
#![feature(unsize)]
165166
#![feature(utf8_chunks)]
166-
#![feature(std_internals)]
167+
// tidy-alphabetical-end
167168
//
168169
// Language features:
170+
// tidy-alphabetical-start
171+
#![cfg_attr(not(test), feature(generator_trait))]
172+
#![cfg_attr(test, feature(panic_update_hook))]
173+
#![cfg_attr(test, feature(test))]
169174
#![feature(allocator_internals)]
170175
#![feature(allow_internal_unstable)]
171176
#![feature(associated_type_bounds)]
177+
#![feature(c_unwind)]
172178
#![feature(cfg_sanitize)]
173179
#![feature(const_deref)]
174180
#![feature(const_mut_refs)]
175-
#![feature(const_ptr_write)]
176181
#![feature(const_precise_live_drops)]
182+
#![feature(const_ptr_write)]
177183
#![feature(const_trait_impl)]
178184
#![feature(const_try)]
179185
#![feature(dropck_eyepatch)]
180186
#![feature(exclusive_range_pattern)]
181187
#![feature(fundamental)]
182-
#![cfg_attr(not(test), feature(generator_trait))]
183188
#![feature(hashmap_internals)]
184189
#![feature(lang_items)]
185190
#![feature(min_specialization)]
191+
#![feature(multiple_supertrait_upcastable)]
186192
#![feature(negative_impls)]
187193
#![feature(never_type)]
194+
#![feature(pointer_is_aligned)]
188195
#![feature(rustc_allow_const_fn_unstable)]
189196
#![feature(rustc_attrs)]
190-
#![feature(pointer_is_aligned)]
191197
#![feature(slice_internals)]
192198
#![feature(staged_api)]
193199
#![feature(stmt_expr_attributes)]
194-
#![cfg_attr(test, feature(test))]
195200
#![feature(unboxed_closures)]
196201
#![feature(unsized_fn_params)]
197-
#![feature(c_unwind)]
198202
#![feature(with_negative_coherence)]
199-
#![cfg_attr(test, feature(panic_update_hook))]
203+
// tidy-alphabetical-end
200204
//
201205
// Rustdoc features:
202206
#![feature(doc_cfg)]

rust/alloc/raw_vec.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use core::alloc::LayoutError;
66
use core::cmp;
77
use core::intrinsics;
88
use core::mem::{self, ManuallyDrop, MaybeUninit, SizedTypeProperties};
9-
use core::ops::Drop;
109
use core::ptr::{self, NonNull, Unique};
1110
use core::slice;
1211

@@ -274,10 +273,15 @@ impl<T, A: Allocator> RawVec<T, A> {
274273
if T::IS_ZST || self.cap == 0 {
275274
None
276275
} else {
277-
// We have an allocated chunk of memory, so we can bypass runtime
278-
// checks to get our current layout.
276+
// We could use Layout::array here which ensures the absence of isize and usize overflows
277+
// and could hypothetically handle differences between stride and size, but this memory
278+
// has already been allocated so we know it can't overflow and currently rust does not
279+
// support such types. So we can do better by skipping some checks and avoid an unwrap.
280+
let _: () = const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };
279281
unsafe {
280-
let layout = Layout::array::<T>(self.cap).unwrap_unchecked();
282+
let align = mem::align_of::<T>();
283+
let size = mem::size_of::<T>().unchecked_mul(self.cap);
284+
let layout = Layout::from_size_align_unchecked(size, align);
281285
Some((self.ptr.cast().into(), layout))
282286
}
283287
}
@@ -465,11 +469,13 @@ impl<T, A: Allocator> RawVec<T, A> {
465469
assert!(cap <= self.capacity(), "Tried to shrink to a larger capacity");
466470

467471
let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) };
468-
472+
// See current_memory() why this assert is here
473+
let _: () = const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };
469474
let ptr = unsafe {
470475
// `Layout::array` cannot overflow here because it would have
471476
// overflowed earlier when capacity was larger.
472-
let new_layout = Layout::array::<T>(cap).unwrap_unchecked();
477+
let new_size = mem::size_of::<T>().unchecked_mul(cap);
478+
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
473479
self.alloc
474480
.shrink(ptr, layout, new_layout)
475481
.map_err(|_| AllocError { layout: new_layout, non_exhaustive: () })?

0 commit comments

Comments
 (0)