Skip to content

Commit e4f5b15

Browse files
committed
Auto merge of #95798 - Dylan-DPC:rollup-51hx1wl, r=Dylan-DPC
Rollup of 7 pull requests Successful merges: - #95102 (Add known-bug for #95034) - #95579 (Add `<[[T; N]]>::flatten{_mut}`) - #95634 (Mailmap update) - #95705 (Promote x86_64-unknown-none target to Tier 2 and distribute build artifacts) - #95761 (Kickstart the inner usage of `macro_metavar_expr`) - #95782 (Windows: Increase a pipe's buffer capacity to 64kb) - #95791 (hide an #[allow] directive from the Arc::new_cyclic doc example) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 1a4b9a8 + 7b285d0 commit e4f5b15

File tree

20 files changed

+544
-135
lines changed

20 files changed

+544
-135
lines changed

.mailmap

+246-4
Large diffs are not rendered by default.

compiler/rustc_expand/src/expand.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,7 @@ macro_rules! ast_fragments {
8383
}
8484
match self {
8585
$($(AstFragment::$Kind(ast) => ast.extend(placeholders.iter().flat_map(|id| {
86-
// We are repeating through arguments with `many`, to do that we have to
87-
// mention some macro variable from those arguments even if it's not used.
88-
macro _repeating($flat_map_ast_elt) {}
86+
${ignore(flat_map_ast_elt)}
8987
placeholder(AstFragmentKind::$Kind, *id, None).$make_ast()
9088
})),)?)*
9189
_ => panic!("unexpected AST fragment kind")

compiler/rustc_expand/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
#![allow(rustc::potential_query_instability)]
12
#![feature(associated_type_bounds)]
23
#![feature(associated_type_defaults)]
34
#![feature(crate_visibility_modifier)]
45
#![feature(decl_macro)]
56
#![feature(if_let_guard)]
67
#![feature(let_chains)]
78
#![feature(let_else)]
9+
#![feature(macro_metavar_expr)]
810
#![feature(proc_macro_diagnostic)]
911
#![feature(proc_macro_internals)]
1012
#![feature(proc_macro_span)]
1113
#![feature(try_blocks)]
1214
#![recursion_limit = "256"]
13-
#![allow(rustc::potential_query_instability)]
1415

1516
#[macro_use]
1617
extern crate rustc_macros;

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
#![feature(trusted_len)]
132132
#![feature(trusted_random_access)]
133133
#![feature(try_trait_v2)]
134+
#![feature(unchecked_math)]
134135
#![feature(unicode_internals)]
135136
#![feature(unsize)]
136137
//

library/alloc/src/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ impl<T> Arc<T> {
369369
///
370370
/// # Example
371371
/// ```
372-
/// #![allow(dead_code)]
372+
/// # #![allow(dead_code)]
373373
/// use std::sync::{Arc, Weak};
374374
///
375375
/// struct Gadget {

library/alloc/src/vec/mod.rs

+45
Original file line numberDiff line numberDiff line change
@@ -2274,6 +2274,51 @@ impl<T: Clone, A: Allocator> Vec<T, A> {
22742274
}
22752275
}
22762276

2277+
impl<T, A: Allocator, const N: usize> Vec<[T; N], A> {
2278+
/// Takes a `Vec<[T; N]>` and flattens it into a `Vec<T>`.
2279+
///
2280+
/// # Panics
2281+
///
2282+
/// Panics if the length of the resulting vector would overflow a `usize`.
2283+
///
2284+
/// This is only possible when flattening a vector of arrays of zero-sized
2285+
/// types, and thus tends to be irrelevant in practice. If
2286+
/// `size_of::<T>() > 0`, this will never panic.
2287+
///
2288+
/// # Examples
2289+
///
2290+
/// ```
2291+
/// #![feature(slice_flatten)]
2292+
///
2293+
/// let mut vec = vec![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
2294+
/// assert_eq!(vec.pop(), Some([7, 8, 9]));
2295+
///
2296+
/// let mut flattened = vec.into_flattened();
2297+
/// assert_eq!(flattened.pop(), Some(6));
2298+
/// ```
2299+
#[unstable(feature = "slice_flatten", issue = "95629")]
2300+
pub fn into_flattened(self) -> Vec<T, A> {
2301+
let (ptr, len, cap, alloc) = self.into_raw_parts_with_alloc();
2302+
let (new_len, new_cap) = if mem::size_of::<T>() == 0 {
2303+
(len.checked_mul(N).expect("vec len overflow"), usize::MAX)
2304+
} else {
2305+
// SAFETY:
2306+
// - `cap * N` cannot overflow because the allocation is already in
2307+
// the address space.
2308+
// - Each `[T; N]` has `N` valid elements, so there are `len * N`
2309+
// valid elements in the allocation.
2310+
unsafe { (len.unchecked_mul(N), cap.unchecked_mul(N)) }
2311+
};
2312+
// SAFETY:
2313+
// - `ptr` was allocated by `self`
2314+
// - `ptr` is well-aligned because `[T; N]` has the same alignment as `T`.
2315+
// - `new_cap` refers to the same sized allocation as `cap` because
2316+
// `new_cap * size_of::<T>()` == `cap * size_of::<[T; N]>()`
2317+
// - `len` <= `cap`, so `len * N` <= `cap * N`.
2318+
unsafe { Vec::<T, A>::from_raw_parts_in(ptr.cast(), new_len, new_cap, alloc) }
2319+
}
2320+
}
2321+
22772322
// This code generalizes `extend_with_{element,default}`.
22782323
trait ExtendWith<T> {
22792324
fn next(&mut self) -> T;

library/alloc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#![feature(const_str_from_utf8)]
3939
#![feature(nonnull_slice_from_raw_parts)]
4040
#![feature(panic_update_hook)]
41+
#![feature(slice_flatten)]
4142

4243
use std::collections::hash_map::DefaultHasher;
4344
use std::hash::{Hash, Hasher};

library/alloc/tests/vec.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2408,3 +2408,10 @@ fn test_extend_from_within_panicing_clone() {
24082408

24092409
assert_eq!(count.load(Ordering::SeqCst), 4);
24102410
}
2411+
2412+
#[test]
2413+
#[should_panic = "vec len overflow"]
2414+
fn test_into_flattened_size_overflow() {
2415+
let v = vec![[(); usize::MAX]; 2];
2416+
let _ = v.into_flattened();
2417+
}

library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@
181181
#![feature(intrinsics)]
182182
#![feature(lang_items)]
183183
#![feature(link_llvm_intrinsics)]
184+
#![feature(macro_metavar_expr)]
184185
#![feature(min_specialization)]
185186
#![feature(mixed_integer_ops)]
186187
#![feature(must_not_suspend)]

library/core/src/slice/mod.rs

+82
Original file line numberDiff line numberDiff line change
@@ -3992,6 +3992,88 @@ impl<T> [T] {
39923992
}
39933993
}
39943994

3995+
#[cfg(not(bootstrap))]
3996+
impl<T, const N: usize> [[T; N]] {
3997+
/// Takes a `&[[T; N]]`, and flattens it to a `&[T]`.
3998+
///
3999+
/// # Panics
4000+
///
4001+
/// This panics if the length of the resulting slice would overflow a `usize`.
4002+
///
4003+
/// This is only possible when flattening a slice of arrays of zero-sized
4004+
/// types, and thus tends to be irrelevant in practice. If
4005+
/// `size_of::<T>() > 0`, this will never panic.
4006+
///
4007+
/// # Examples
4008+
///
4009+
/// ```
4010+
/// #![feature(slice_flatten)]
4011+
///
4012+
/// assert_eq!([[1, 2, 3], [4, 5, 6]].flatten(), &[1, 2, 3, 4, 5, 6]);
4013+
///
4014+
/// assert_eq!(
4015+
/// [[1, 2, 3], [4, 5, 6]].flatten(),
4016+
/// [[1, 2], [3, 4], [5, 6]].flatten(),
4017+
/// );
4018+
///
4019+
/// let slice_of_empty_arrays: &[[i32; 0]] = &[[], [], [], [], []];
4020+
/// assert!(slice_of_empty_arrays.flatten().is_empty());
4021+
///
4022+
/// let empty_slice_of_arrays: &[[u32; 10]] = &[];
4023+
/// assert!(empty_slice_of_arrays.flatten().is_empty());
4024+
/// ```
4025+
#[unstable(feature = "slice_flatten", issue = "95629")]
4026+
pub fn flatten(&self) -> &[T] {
4027+
let len = if crate::mem::size_of::<T>() == 0 {
4028+
self.len().checked_mul(N).expect("slice len overflow")
4029+
} else {
4030+
// SAFETY: `self.len() * N` cannot overflow because `self` is
4031+
// already in the address space.
4032+
unsafe { self.len().unchecked_mul(N) }
4033+
};
4034+
// SAFETY: `[T]` is layout-identical to `[T; N]`
4035+
unsafe { from_raw_parts(self.as_ptr().cast(), len) }
4036+
}
4037+
4038+
/// Takes a `&mut [[T; N]]`, and flattens it to a `&mut [T]`.
4039+
///
4040+
/// # Panics
4041+
///
4042+
/// This panics if the length of the resulting slice would overflow a `usize`.
4043+
///
4044+
/// This is only possible when flattening a slice of arrays of zero-sized
4045+
/// types, and thus tends to be irrelevant in practice. If
4046+
/// `size_of::<T>() > 0`, this will never panic.
4047+
///
4048+
/// # Examples
4049+
///
4050+
/// ```
4051+
/// #![feature(slice_flatten)]
4052+
///
4053+
/// fn add_5_to_all(slice: &mut [i32]) {
4054+
/// for i in slice {
4055+
/// *i += 5;
4056+
/// }
4057+
/// }
4058+
///
4059+
/// let mut array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
4060+
/// add_5_to_all(array.flatten_mut());
4061+
/// assert_eq!(array, [[6, 7, 8], [9, 10, 11], [12, 13, 14]]);
4062+
/// ```
4063+
#[unstable(feature = "slice_flatten", issue = "95629")]
4064+
pub fn flatten_mut(&mut self) -> &mut [T] {
4065+
let len = if crate::mem::size_of::<T>() == 0 {
4066+
self.len().checked_mul(N).expect("slice len overflow")
4067+
} else {
4068+
// SAFETY: `self.len() * N` cannot overflow because `self` is
4069+
// already in the address space.
4070+
unsafe { self.len().unchecked_mul(N) }
4071+
};
4072+
// SAFETY: `[T]` is layout-identical to `[T; N]`
4073+
unsafe { from_raw_parts_mut(self.as_mut_ptr().cast(), len) }
4074+
}
4075+
}
4076+
39954077
trait CloneFromSpec<T> {
39964078
fn spec_clone_from(&mut self, src: &[T]);
39974079
}

0 commit comments

Comments
 (0)