From a3208108b0b2b98004dac023aaeb794c1ec9149a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Fern=C3=A1ndez=20Serrata?= <76864299+Rudxain@users.noreply.github.com> Date: Fri, 4 Jul 2025 19:10:39 -0400 Subject: [PATCH 01/68] fix(lib-std-fs): handle `usize` overflow in `read` & `read_to_string` --- library/std/src/fs.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index 72ad7c244eeb..9fbfd92eaf88 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -304,7 +304,7 @@ pub struct DirBuilder { pub fn read>(path: P) -> io::Result> { fn inner(path: &Path) -> io::Result> { let mut file = File::open(path)?; - let size = file.metadata().map(|m| m.len() as usize).ok(); + let size = file.metadata().map(|m| usize::try_from(m.len()).unwrap_or(usize::MAX)).ok(); let mut bytes = Vec::try_with_capacity(size.unwrap_or(0))?; io::default_read_to_end(&mut file, &mut bytes, size)?; Ok(bytes) @@ -346,7 +346,7 @@ pub fn read>(path: P) -> io::Result> { pub fn read_to_string>(path: P) -> io::Result { fn inner(path: &Path) -> io::Result { let mut file = File::open(path)?; - let size = file.metadata().map(|m| m.len() as usize).ok(); + let size = file.metadata().map(|m| usize::try_from(m.len()).unwrap_or(usize::MAX)).ok(); let mut string = String::new(); string.try_reserve_exact(size.unwrap_or(0))?; io::default_read_to_string(&mut file, &mut string, size)?; From 21bdfa14cbd99aba173751a2c723fb1e65de401b Mon Sep 17 00:00:00 2001 From: Aurelia Molzer <5550310+HeroicKatora@users.noreply.github.com> Date: Sat, 9 Aug 2025 17:54:43 +0200 Subject: [PATCH 02/68] Ensure consistent drop for hint::select_unpredictable There are a few alternatives to the implementation. The principal problem is that the selected value must be owned (in the sense of having any drop flag of sorts) when the unselected value is dropped, such that panic unwind goes through the drop of both. This ownership must then be passed on in return when the drop went smoothly. The basic way of achieving this is by extracting the selected value first, at the cost of relying on the optimizer a little more for detecting the copy as constructing the return value despite having a place in the body. --- library/core/src/hint.rs | 6 +++++- library/coretests/tests/hint.rs | 37 +++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/library/core/src/hint.rs b/library/core/src/hint.rs index c72eeb9a9c97..53c17d6c887c 100644 --- a/library/core/src/hint.rs +++ b/library/core/src/hint.rs @@ -790,8 +790,12 @@ pub fn select_unpredictable(condition: bool, true_val: T, false_val: T) -> T // is returned. This is necessary because the intrinsic doesn't drop the // value that is not selected. unsafe { + // Extract the selected value first, ensure it is dropped as well if dropping the unselected + // value panics. + let ret = crate::intrinsics::select_unpredictable(condition, &true_val, &false_val) + .assume_init_read(); crate::intrinsics::select_unpredictable(!condition, &mut true_val, &mut false_val) .assume_init_drop(); - crate::intrinsics::select_unpredictable(condition, true_val, false_val).assume_init() + ret } } diff --git a/library/coretests/tests/hint.rs b/library/coretests/tests/hint.rs index 032bbc1dcc80..9ef5567681e9 100644 --- a/library/coretests/tests/hint.rs +++ b/library/coretests/tests/hint.rs @@ -21,3 +21,40 @@ fn select_unpredictable_drop() { assert!(a_dropped.get()); assert!(b_dropped.get()); } + +#[test] +#[should_panic] +fn select_unpredictable_drop_on_panic() { + use core::cell::Cell; + + struct X<'a> { + cell: &'a Cell, + expect: u16, + write: u16, + } + + impl Drop for X<'_> { + fn drop(&mut self) { + let value = self.cell.get(); + self.cell.set(self.write); + assert_eq!(value, self.expect); + } + } + + let cell = Cell::new(0); + + // Trigger a double-panic if the selected cell was not dropped during panic. + let _armed = X { cell: &cell, expect: 0xdead, write: 0 }; + let selected = X { cell: &cell, write: 0xdead, expect: 1 }; + let unselected = X { cell: &cell, write: 1, expect: 0xff }; + + // The correct drop order is: + // + // 1. `unselected` drops, writes 1, and panics as 0 != 0xff + // 2. `selected` drops during unwind, writes 0xdead and does not panic as 1 == 1 + // 3. `armed` drops during unwind, writes 0 and does not panic as 0xdead == 0xdead + // + // If `selected` is not dropped, `armed` panics as 1 != 0xdead + let _unreachable = + core::hint::select_unpredictable(core::hint::black_box(true), selected, unselected); +} From 7d7fe3ff05c7c14834252e3345389e4dbf842cc5 Mon Sep 17 00:00:00 2001 From: Aurelia Molzer <5550310+HeroicKatora@users.noreply.github.com> Date: Sat, 9 Aug 2025 21:40:12 +0200 Subject: [PATCH 03/68] Adjust for codegen of unpredictable return value It seems important for LLVM that we select on the values by-value instead of reading and have no intermediate store. So make sure the guards selects both potential drops but defers the return value to the second selection. Since the two options alias we use raw mutable pointers instead of mutable references as before. --- library/core/src/hint.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/library/core/src/hint.rs b/library/core/src/hint.rs index 53c17d6c887c..1bb1c5695087 100644 --- a/library/core/src/hint.rs +++ b/library/core/src/hint.rs @@ -786,16 +786,42 @@ pub fn select_unpredictable(condition: bool, true_val: T, false_val: T) -> T // Change this to use ManuallyDrop instead. let mut true_val = MaybeUninit::new(true_val); let mut false_val = MaybeUninit::new(false_val); + + struct DropOnPanic { + inner: *mut MaybeUninit, + } + + impl Drop for DropOnPanic { + fn drop(&mut self) { + // SAFETY: Must be guaranteed on construction of local type `DropOnPanic`. + unsafe { (*self.inner).assume_init_drop() } + } + } + + let true_ptr = (&mut true_val) as *mut _; + let false_ptr = (&mut false_val) as *mut _; + // SAFETY: The value that is not selected is dropped, and the selected one // is returned. This is necessary because the intrinsic doesn't drop the // value that is not selected. unsafe { // Extract the selected value first, ensure it is dropped as well if dropping the unselected // value panics. - let ret = crate::intrinsics::select_unpredictable(condition, &true_val, &false_val) - .assume_init_read(); - crate::intrinsics::select_unpredictable(!condition, &mut true_val, &mut false_val) - .assume_init_drop(); - ret + let (guard, drop) = crate::intrinsics::select_unpredictable( + condition, + (true_ptr, false_ptr), + (false_ptr, true_ptr), + ); + + // SAFETY: both pointers are to valid `MaybeUninit`, in both variants they do not alias but + // the two arguments we have selected from did alias each other. + let guard = DropOnPanic { inner: guard }; + (*drop).assume_init_drop(); + crate::mem::forget(guard); + + // Note that it is important to use the values here. Reading from the pointer we got makes + // LLVM forget the !unpredictable annotation sometimes (in tests, integer sized values in + // particular seemed to confuse it, also observed in llvm/llvm-project #82340). + crate::intrinsics::select_unpredictable(condition, true_val, false_val).assume_init() } } From 489940ee1d24a0a2bc388b465f171da9ab0cb16b Mon Sep 17 00:00:00 2001 From: Kivooeo Date: Fri, 15 Aug 2025 16:42:21 +0000 Subject: [PATCH 04/68] stabilize array repeat --- library/core/src/array/mod.rs | 4 +--- tests/codegen-llvm/array-repeat.rs | 1 - tests/codegen-llvm/iter-repeat-n-trivial-drop.rs | 1 - 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/library/core/src/array/mod.rs b/library/core/src/array/mod.rs index b3a498570f95..e994fa00a232 100644 --- a/library/core/src/array/mod.rs +++ b/library/core/src/array/mod.rs @@ -41,8 +41,6 @@ pub use iter::IntoIter; /// /// Creating multiple copies of a `String`: /// ```rust -/// #![feature(array_repeat)] -/// /// use std::array; /// /// let string = "Hello there!".to_string(); @@ -50,7 +48,7 @@ pub use iter::IntoIter; /// assert_eq!(strings, ["Hello there!", "Hello there!"]); /// ``` #[inline] -#[unstable(feature = "array_repeat", issue = "126695")] +#[stable(feature = "array_repeat", since = "CURRENT_RUSTC_VERSION")] pub fn repeat(val: T) -> [T; N] { from_trusted_iterator(repeat_n(val, N)) } diff --git a/tests/codegen-llvm/array-repeat.rs b/tests/codegen-llvm/array-repeat.rs index 4c755df93903..1c45341d764c 100644 --- a/tests/codegen-llvm/array-repeat.rs +++ b/tests/codegen-llvm/array-repeat.rs @@ -1,7 +1,6 @@ //@ compile-flags: -Copt-level=3 #![crate_type = "lib"] -#![feature(array_repeat)] use std::array::repeat; diff --git a/tests/codegen-llvm/iter-repeat-n-trivial-drop.rs b/tests/codegen-llvm/iter-repeat-n-trivial-drop.rs index 281735303243..6f3409784280 100644 --- a/tests/codegen-llvm/iter-repeat-n-trivial-drop.rs +++ b/tests/codegen-llvm/iter-repeat-n-trivial-drop.rs @@ -4,7 +4,6 @@ #![crate_type = "lib"] #![feature(iter_repeat_n)] -#![feature(array_repeat)] #[derive(Clone)] pub struct NotCopy(u16); From 1bb9b151c9f9b5116254827f04add845aff33408 Mon Sep 17 00:00:00 2001 From: Karl Meakin Date: Mon, 11 Aug 2025 00:57:29 +0000 Subject: [PATCH 05/68] refactor: Hard-code `char::is_control` According to https://www.unicode.org/policies/stability_policy.html#Property_Value, the set of codepoints in `Cc` will never change. So we can hard-code the patterns to match against instead of using a table. --- library/core/src/char/methods.rs | 6 ++++- library/core/src/unicode/mod.rs | 1 - library/core/src/unicode/unicode_data.rs | 25 ------------------- src/tools/unicode-table-generator/src/main.rs | 1 - 4 files changed, 5 insertions(+), 28 deletions(-) diff --git a/library/core/src/char/methods.rs b/library/core/src/char/methods.rs index 7ee0962721f5..61ac7f8a339f 100644 --- a/library/core/src/char/methods.rs +++ b/library/core/src/char/methods.rs @@ -950,7 +950,11 @@ impl char { #[stable(feature = "rust1", since = "1.0.0")] #[inline] pub fn is_control(self) -> bool { - unicode::Cc(self) + // According to + // https://www.unicode.org/policies/stability_policy.html#Property_Value, + // the set of codepoints in `Cc` will never change. + // So we can just hard-code the patterns to match against instead of using a table. + matches!(self, '\0'..='\x1f' | '\x7f'..='\u{9f}') } /// Returns `true` if this `char` has the `Grapheme_Extend` property. diff --git a/library/core/src/unicode/mod.rs b/library/core/src/unicode/mod.rs index 49dbdeb1a6d1..e1cb69c3c4fe 100644 --- a/library/core/src/unicode/mod.rs +++ b/library/core/src/unicode/mod.rs @@ -9,7 +9,6 @@ pub use unicode_data::conversions; #[rustfmt::skip] pub(crate) use unicode_data::alphabetic::lookup as Alphabetic; -pub(crate) use unicode_data::cc::lookup as Cc; pub(crate) use unicode_data::grapheme_extend::lookup as Grapheme_Extend; pub(crate) use unicode_data::lowercase::lookup as Lowercase; pub(crate) use unicode_data::n::lookup as N; diff --git a/library/core/src/unicode/unicode_data.rs b/library/core/src/unicode/unicode_data.rs index b57234bbee9a..55f64f1e96e6 100644 --- a/library/core/src/unicode/unicode_data.rs +++ b/library/core/src/unicode/unicode_data.rs @@ -358,31 +358,6 @@ pub mod cased { } } -#[rustfmt::skip] -pub mod cc { - use super::ShortOffsetRunHeader; - - static SHORT_OFFSET_RUNS: [ShortOffsetRunHeader; 1] = [ - ShortOffsetRunHeader::new(0, 1114272), - ]; - static OFFSETS: [u8; 5] = [ - 0, 32, 95, 33, 0, - ]; - pub fn lookup(c: char) -> bool { - const { - assert!(SHORT_OFFSET_RUNS.last().unwrap().0 > char::MAX as u32); - let mut i = 0; - while i < SHORT_OFFSET_RUNS.len() { - assert!(SHORT_OFFSET_RUNS[i].start_index() < OFFSETS.len()); - i += 1; - } - } - // SAFETY: We just ensured the last element of `SHORT_OFFSET_RUNS` is greater than `std::char::MAX` - // and the start indices of all elements in `SHORT_OFFSET_RUNS` are smaller than `OFFSETS.len()`. - unsafe { super::skip_search(c, &SHORT_OFFSET_RUNS, &OFFSETS) } - } -} - #[rustfmt::skip] pub mod grapheme_extend { use super::ShortOffsetRunHeader; diff --git a/src/tools/unicode-table-generator/src/main.rs b/src/tools/unicode-table-generator/src/main.rs index 6cdb82a87bdf..38e5e8bbdb9c 100644 --- a/src/tools/unicode-table-generator/src/main.rs +++ b/src/tools/unicode-table-generator/src/main.rs @@ -92,7 +92,6 @@ static PROPERTIES: &[&str] = &[ "Case_Ignorable", "Grapheme_Extend", "White_Space", - "Cc", "N", ]; From e49d0008f77ff963185801cf9dff4a72958f95fb Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sat, 26 Jul 2025 01:09:45 -0700 Subject: [PATCH 06/68] Partial-stabilize the basics from `bigint_helper_methods` --- library/core/src/num/uint_macros.rs | 66 ++++++++++++++++++----------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 10d9498d15e4..c556d9510340 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -2447,7 +2447,7 @@ macro_rules! uint_impl { } /// Calculates `self` + `rhs` + `carry` and returns a tuple containing - /// the sum and the output carry. + /// the sum and the output carry (in that order). /// /// Performs "ternary addition" of two integer operands and a carry-in /// bit, and returns an output integer and a carry-out bit. This allows @@ -2465,8 +2465,6 @@ macro_rules! uint_impl { /// # Examples /// /// ``` - /// #![feature(bigint_helper_methods)] - /// #[doc = concat!("// 3 MAX (a = 3 × 2^", stringify!($BITS), " + 2^", stringify!($BITS), " - 1)")] #[doc = concat!("// + 5 7 (b = 5 × 2^", stringify!($BITS), " + 7)")] /// // --------- @@ -2483,7 +2481,7 @@ macro_rules! uint_impl { /// /// assert_eq!((sum1, sum0), (9, 6)); /// ``` - #[unstable(feature = "bigint_helper_methods", issue = "85532")] + #[stable(feature = "unsigned_bigint_helpers", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")] #[must_use = "this returns the result of the operation, \ without modifying the original"] @@ -2559,8 +2557,6 @@ macro_rules! uint_impl { /// # Examples /// /// ``` - /// #![feature(bigint_helper_methods)] - /// #[doc = concat!("// 9 6 (a = 9 × 2^", stringify!($BITS), " + 6)")] #[doc = concat!("// - 5 7 (b = 5 × 2^", stringify!($BITS), " + 7)")] /// // --------- @@ -2577,7 +2573,7 @@ macro_rules! uint_impl { /// #[doc = concat!("assert_eq!((diff1, diff0), (3, ", stringify!($SelfT), "::MAX));")] /// ``` - #[unstable(feature = "bigint_helper_methods", issue = "85532")] + #[stable(feature = "unsigned_bigint_helpers", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")] #[must_use = "this returns the result of the operation, \ without modifying the original"] @@ -2651,10 +2647,12 @@ macro_rules! uint_impl { /// indicating whether an arithmetic overflow would occur. If an /// overflow would have occurred then the wrapped value is returned. /// + /// If you want the *value* of the overflow, rather than just *whether* + /// an overflow occurred, see [`Self::carrying_mul`]. + /// /// # Examples /// - /// Please note that this example is shared among integer types, which is why why `u32` - /// is used. + /// Please note that this example is shared among integer types, which is why `u32` is used. /// /// ``` /// assert_eq!(5u32.overflowing_mul(2), (10, false)); @@ -2670,16 +2668,38 @@ macro_rules! uint_impl { (a as Self, b) } - /// Calculates the complete product `self * rhs` without the possibility to overflow. + /// Calculates the complete double-width product `self * rhs`. /// /// This returns the low-order (wrapping) bits and the high-order (overflow) bits - /// of the result as two separate values, in that order. + /// of the result as two separate values, in that order. As such, + /// `a.widening_mul(b).0` produces the same result as `a.wrapping_mul(b)`. + /// + /// If you also need to add a value and carry to the wide result, then you want + /// [`Self::carrying_mul_add`] instead. /// /// If you also need to add a carry to the wide result, then you want /// [`Self::carrying_mul`] instead. /// + /// If you just want to know *whether* the multiplication overflowed, then you + /// want [`Self::overflowing_mul`] instead. + /// /// # Examples /// + /// ``` + /// #![feature(bigint_helper_methods)] + #[doc = concat!("assert_eq!(5_", stringify!($SelfT), ".widening_mul(7), (35, 0));")] + #[doc = concat!("assert_eq!(", stringify!($SelfT), "::MAX.widening_mul(", stringify!($SelfT), "::MAX), (1, ", stringify!($SelfT), "::MAX - 1));")] + /// ``` + /// + /// Compared to other `*_mul` methods: + /// ``` + /// #![feature(bigint_helper_methods)] + #[doc = concat!("assert_eq!(", stringify!($SelfT), "::widening_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), (0, 3));")] + #[doc = concat!("assert_eq!(", stringify!($SelfT), "::overflowing_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), (0, true));")] + #[doc = concat!("assert_eq!(", stringify!($SelfT), "::wrapping_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), 0);")] + #[doc = concat!("assert_eq!(", stringify!($SelfT), "::checked_mul(1 << ", stringify!($BITS_MINUS_ONE), ", 6), None);")] + /// ``` + /// /// Please note that this example is shared among integer types, which is why `u32` is used. /// /// ``` @@ -2706,14 +2726,13 @@ macro_rules! uint_impl { /// additional amount of overflow. This allows for chaining together multiple /// multiplications to create "big integers" which represent larger values. /// - /// If you don't need the `carry`, then you can use [`Self::widening_mul`] instead. + /// If you also need to add a value, then use [`Self::carrying_mul_add`]. /// /// # Examples /// /// Please note that this example is shared among integer types, which is why `u32` is used. /// /// ``` - /// #![feature(bigint_helper_methods)] /// assert_eq!(5u32.carrying_mul(2, 0), (10, 0)); /// assert_eq!(5u32.carrying_mul(2, 10), (20, 0)); /// assert_eq!(1_000_000_000u32.carrying_mul(10, 0), (1410065408, 2)); @@ -2771,7 +2790,7 @@ macro_rules! uint_impl { /// 789_u16.wrapping_mul(456).wrapping_add(123), /// ); /// ``` - #[unstable(feature = "bigint_helper_methods", issue = "85532")] + #[stable(feature = "unsigned_bigint_helpers", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")] #[must_use = "this returns the result of the operation, \ without modifying the original"] @@ -2780,18 +2799,20 @@ macro_rules! uint_impl { Self::carrying_mul_add(self, rhs, carry, 0) } - /// Calculates the "full multiplication" `self * rhs + carry1 + carry2` - /// without the possibility to overflow. + /// Calculates the "full multiplication" `self * rhs + carry1 + carry2`. /// /// This returns the low-order (wrapping) bits and the high-order (overflow) bits /// of the result as two separate values, in that order. /// + /// This cannot overflow, as the double-width result has exactly enough + /// space for the largest possible result. This is equivalent to how, in + /// decimal, 9 × 9 + 9 + 9 = 81 + 18 = 99 = 9×10⁰ + 9×10¹ = 10² - 1. + /// /// Performs "long multiplication" which takes in an extra amount to add, and may return an /// additional amount of overflow. This allows for chaining together multiple /// multiplications to create "big integers" which represent larger values. /// - /// If you don't need either `carry`, then you can use [`Self::widening_mul`] instead, - /// and if you only need one `carry`, then you can use [`Self::carrying_mul`] instead. + /// If you don't need the `add` part, then you can use [`Self::carrying_mul`] instead. /// /// # Examples /// @@ -2799,7 +2820,6 @@ macro_rules! uint_impl { /// which explains why `u32` is used here. /// /// ``` - /// #![feature(bigint_helper_methods)] /// assert_eq!(5u32.carrying_mul_add(2, 0, 0), (10, 0)); /// assert_eq!(5u32.carrying_mul_add(2, 10, 10), (30, 0)); /// assert_eq!(1_000_000_000u32.carrying_mul_add(10, 0, 0), (1410065408, 2)); @@ -2816,8 +2836,6 @@ macro_rules! uint_impl { /// using `u8` for simplicity of the demonstration. /// /// ``` - /// #![feature(bigint_helper_methods)] - /// /// fn quadratic_mul(a: [u8; N], b: [u8; N]) -> [u8; N] { /// let mut out = [0; N]; /// for j in 0..N { @@ -2832,13 +2850,13 @@ macro_rules! uint_impl { /// // -1 * -1 == 1 /// assert_eq!(quadratic_mul([0xFF; 3], [0xFF; 3]), [1, 0, 0]); /// - /// assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xCFFC982D); + /// assert_eq!(u32::wrapping_mul(0x9e3779b9, 0x7f4a7c15), 0xcffc982d); /// assert_eq!( /// quadratic_mul(u32::to_le_bytes(0x9e3779b9), u32::to_le_bytes(0x7f4a7c15)), - /// u32::to_le_bytes(0xCFFC982D) + /// u32::to_le_bytes(0xcffc982d) /// ); /// ``` - #[unstable(feature = "bigint_helper_methods", issue = "85532")] + #[stable(feature = "unsigned_bigint_helpers", since = "CURRENT_RUSTC_VERSION")] #[rustc_const_unstable(feature = "bigint_helper_methods", issue = "85532")] #[must_use = "this returns the result of the operation, \ without modifying the original"] From d216ca0506d6a70ca70fc29974ed9155beaabf7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 18 Aug 2025 16:52:02 +0000 Subject: [PATCH 07/68] Detect missing `if let` or `let-else` During `let` binding parse error and encountering a block, detect if there is a likely missing `if` or `else`: ``` error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{` --> $DIR/missing-if-let-or-let-else.rs:14:25 | LL | let Some(x) = foo() { | ^ expected one of `.`, `;`, `?`, `else`, or an operator | help: you might have meant to use `if let` | LL | if let Some(x) = foo() { | ++ help: alternatively, you might have meant to use `let else` | LL | let Some(x) = foo() else { | ++++ ``` --- compiler/rustc_parse/src/lib.rs | 1 + compiler/rustc_parse/src/parser/stmt.rs | 94 +++++++++++++++++++ .../uninhabited/missing-if-let-or-let-else.rs | 24 +++++ .../missing-if-let-or-let-else.stderr | 39 ++++++++ 4 files changed, 158 insertions(+) create mode 100644 tests/ui/uninhabited/missing-if-let-or-let-else.rs create mode 100644 tests/ui/uninhabited/missing-if-let-or-let-else.stderr diff --git a/compiler/rustc_parse/src/lib.rs b/compiler/rustc_parse/src/lib.rs index 2050c5f96087..0dd5d89491b4 100644 --- a/compiler/rustc_parse/src/lib.rs +++ b/compiler/rustc_parse/src/lib.rs @@ -6,6 +6,7 @@ #![feature(assert_matches)] #![feature(box_patterns)] #![feature(debug_closure_helpers)] +#![feature(default_field_values)] #![feature(if_let_guard)] #![feature(iter_intersperse)] #![recursion_limit = "256"] diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index b4943ff7de64..732c653e4bc8 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -6,6 +6,7 @@ use ast::Label; use rustc_ast as ast; use rustc_ast::token::{self, Delimiter, InvisibleOrigin, MetaVarKind, TokenKind}; use rustc_ast::util::classify::{self, TrailingBrace}; +use rustc_ast::visit::{Visitor, walk_expr}; use rustc_ast::{ AttrStyle, AttrVec, Block, BlockCheckMode, DUMMY_NODE_ID, Expr, ExprKind, HasAttrs, Local, LocalKind, MacCall, MacCallStmt, MacStmtStyle, Recovered, Stmt, StmtKind, @@ -783,6 +784,71 @@ impl<'a> Parser<'a> { Ok(self.mk_block(stmts, s, lo.to(self.prev_token.span))) } + fn recover_missing_let_else(&mut self, err: &mut Diag<'_>, pat: &ast::Pat, stmt_span: Span) { + if self.token.kind != token::OpenBrace { + return; + } + match pat.kind { + ast::PatKind::Ident(..) | ast::PatKind::Missing | ast::PatKind::Wild => { + // Not if let or let else + return; + } + _ => {} + } + let snapshot = self.create_snapshot_for_diagnostic(); + let block_span = self.token.span; + let (if_let, let_else) = match self.parse_block() { + Ok(block) => { + let mut idents = vec![]; + pat.walk(&mut |pat: &ast::Pat| { + if let ast::PatKind::Ident(_, ident, _) = pat.kind { + idents.push(ident); + } + true + }); + // Collect all bindings in pattern and see if they appear in the block. Likely meant + // to write `if let`. See if the block has a return. Likely meant to write + // `let else`. + let mut visitor = IdentFinder { idents, .. }; + visitor.visit_block(&block); + + (visitor.references_ident, visitor.has_return) + } + Err(e) => { + e.cancel(); + self.restore_snapshot(snapshot); + (false, false) + } + }; + + let mut alternatively = ""; + if if_let || !let_else { + alternatively = "alternatively, "; + err.span_suggestion_verbose( + stmt_span.shrink_to_lo(), + "you might have meant to use `if let`", + "if ".to_string(), + if if_let { + Applicability::MachineApplicable + } else { + Applicability::MaybeIncorrect + }, + ); + } + if let_else || !if_let { + err.span_suggestion_verbose( + block_span.shrink_to_lo(), + format!("{alternatively}you might have meant to use `let else`"), + "else ".to_string(), + if let_else { + Applicability::MachineApplicable + } else { + Applicability::MaybeIncorrect + }, + ); + } + } + fn recover_missing_dot(&mut self, err: &mut Diag<'_>) { let Some((ident, _)) = self.token.ident() else { return; @@ -977,6 +1043,7 @@ impl<'a> Parser<'a> { self.check_mistyped_turbofish_with_multiple_type_params(e, expr).map_err( |mut e| { self.recover_missing_dot(&mut e); + self.recover_missing_let_else(&mut e, &local.pat, stmt.span); e }, )?; @@ -1065,3 +1132,30 @@ impl<'a> Parser<'a> { self.mk_block(thin_vec![self.mk_stmt_err(span, guar)], BlockCheckMode::Default, span) } } + +struct IdentFinder { + idents: Vec, + /// If a block references one of the bindings introduced by the let pattern, we likely meant to + /// use `if let`. + /// This is pre-expansion, so if we encounter `let Some(x) = foo() { println!("{x}") }` we won't + /// find it. + references_ident: bool = false, + /// If a block has a `return`, then we know with high certainty that the + has_return: bool = false, +} + +impl<'a> Visitor<'a> for IdentFinder { + fn visit_ident(&mut self, ident: &Ident) { + for i in &self.idents { + if ident.name == i.name { + self.references_ident = true; + } + } + } + fn visit_expr(&mut self, node: &'a Expr) { + if let ExprKind::Ret(..) = node.kind { + self.has_return = true; + } + walk_expr(self, node); + } +} diff --git a/tests/ui/uninhabited/missing-if-let-or-let-else.rs b/tests/ui/uninhabited/missing-if-let-or-let-else.rs new file mode 100644 index 000000000000..51fedb797562 --- /dev/null +++ b/tests/ui/uninhabited/missing-if-let-or-let-else.rs @@ -0,0 +1,24 @@ +fn a() { + let Some(x) = foo() { //~ ERROR expected one of + //~^ HELP you might have meant to use `if let` + let y = x; + } +} +fn b() { + let Some(x) = foo() { //~ ERROR expected one of + //~^ HELP you might have meant to use `let else` + return; + } +} +fn c() { + let Some(x) = foo() { //~ ERROR expected one of + //~^ HELP you might have meant to use `if let` + //~| HELP alternatively, you might have meant to use `let else` + // The parser check happens pre-macro-expansion, so we don't know for sure. + println!("{x}"); + } +} +fn foo() -> Option { + Some(42) +} +fn main() {} diff --git a/tests/ui/uninhabited/missing-if-let-or-let-else.stderr b/tests/ui/uninhabited/missing-if-let-or-let-else.stderr new file mode 100644 index 000000000000..4b78a0fa16e8 --- /dev/null +++ b/tests/ui/uninhabited/missing-if-let-or-let-else.stderr @@ -0,0 +1,39 @@ +error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{` + --> $DIR/missing-if-let-or-let-else.rs:2:25 + | +LL | let Some(x) = foo() { + | ^ expected one of `.`, `;`, `?`, `else`, or an operator + | +help: you might have meant to use `if let` + | +LL | if let Some(x) = foo() { + | ++ + +error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{` + --> $DIR/missing-if-let-or-let-else.rs:8:25 + | +LL | let Some(x) = foo() { + | ^ expected one of `.`, `;`, `?`, `else`, or an operator + | +help: you might have meant to use `let else` + | +LL | let Some(x) = foo() else { + | ++++ + +error: expected one of `.`, `;`, `?`, `else`, or an operator, found `{` + --> $DIR/missing-if-let-or-let-else.rs:14:25 + | +LL | let Some(x) = foo() { + | ^ expected one of `.`, `;`, `?`, `else`, or an operator + | +help: you might have meant to use `if let` + | +LL | if let Some(x) = foo() { + | ++ +help: alternatively, you might have meant to use `let else` + | +LL | let Some(x) = foo() else { + | ++++ + +error: aborting due to 3 previous errors + From eaf7fd2fed70cac9ca6d75d011e1d11554c8ef5b Mon Sep 17 00:00:00 2001 From: Connor Tsui Date: Sat, 23 Aug 2025 09:14:51 -0400 Subject: [PATCH 08/68] add `nonpoison::condvar` implementation Adds the equivalent `nonpoison` types to the `poison::condvar` module. These types and implementations are gated under the `nonpoison_condvar` feature gate. Signed-off-by: Connor Tsui --- library/std/src/sync/nonpoison.rs | 3 + library/std/src/sync/nonpoison/condvar.rs | 516 ++++++++++++++++++++++ library/std/src/sync/nonpoison/mutex.rs | 7 +- 3 files changed, 524 insertions(+), 2 deletions(-) create mode 100644 library/std/src/sync/nonpoison/condvar.rs diff --git a/library/std/src/sync/nonpoison.rs b/library/std/src/sync/nonpoison.rs index b3ae376e70d5..96ecf7084fa9 100644 --- a/library/std/src/sync/nonpoison.rs +++ b/library/std/src/sync/nonpoison.rs @@ -29,6 +29,8 @@ impl fmt::Display for WouldBlock { } } +#[unstable(feature = "nonpoison_condvar", issue = "134645")] +pub use self::condvar::{Condvar, WaitTimeoutResult}; #[unstable(feature = "mapped_lock_guards", issue = "117108")] pub use self::mutex::MappedMutexGuard; #[unstable(feature = "nonpoison_mutex", issue = "134645")] @@ -38,5 +40,6 @@ pub use self::rwlock::{MappedRwLockReadGuard, MappedRwLockWriteGuard}; #[unstable(feature = "nonpoison_rwlock", issue = "134645")] pub use self::rwlock::{RwLock, RwLockReadGuard, RwLockWriteGuard}; +mod condvar; mod mutex; mod rwlock; diff --git a/library/std/src/sync/nonpoison/condvar.rs b/library/std/src/sync/nonpoison/condvar.rs new file mode 100644 index 000000000000..9744c87d9cc1 --- /dev/null +++ b/library/std/src/sync/nonpoison/condvar.rs @@ -0,0 +1,516 @@ +use crate::fmt; +use crate::sync::nonpoison::{MutexGuard, mutex}; +use crate::sys::sync as sys; +use crate::time::{Duration, Instant}; + +/// A type indicating whether a timed wait on a condition variable returned +/// due to a time out or not. +/// +/// It is returned by the [`wait_timeout`] method. +/// +/// [`wait_timeout`]: Condvar::wait_timeout +#[derive(Debug, PartialEq, Eq, Copy, Clone)] +#[unstable(feature = "nonpoison_condvar", issue = "134645")] +pub struct WaitTimeoutResult(bool); + +// FIXME(nonpoison_condvar) this type is duplicated in `poison`. How do we share types that are +// poison-agnostic? +impl WaitTimeoutResult { + /// Returns `true` if the wait was known to have timed out. + /// + /// # Examples + /// + /// This example spawns a thread which will sleep 20 milliseconds before + /// updating a boolean value and then notifying the condvar. + /// + /// The main thread will wait with a 10 millisecond timeout on the condvar + /// and will leave the loop upon timeout. + /// + /// ``` + /// #![feature(nonpoison_mutex)] + /// #![feature(nonpoison_condvar)] + /// + /// use std::sync::nonpoison::{Mutex, Condvar}; + /// use std::sync::Arc; + /// use std::thread; + /// use std::time::Duration; + /// + /// let pair = Arc::new((Mutex::new(false), Condvar::new())); + /// let pair2 = Arc::clone(&pair); + /// + /// # let handle = + /// thread::spawn(move || { + /// let (lock, cvar) = &*pair2; + /// + /// // Let's wait 20 milliseconds before notifying the condvar. + /// thread::sleep(Duration::from_millis(20)); + /// + /// let mut started = lock.lock(); + /// // We update the boolean value. + /// *started = true; + /// cvar.notify_one(); + /// }); + /// + /// // Wait for the thread to start up. + /// let (lock, cvar) = &*pair; + /// loop { + /// // Let's put a timeout on the condvar's wait. + /// let result = cvar.wait_timeout(lock.lock(), Duration::from_millis(10)); + /// // 10 milliseconds have passed. + /// if result.1.timed_out() { + /// // timed out now and we can leave. + /// break + /// } + /// } + /// # // Prevent leaks for Miri. + /// # let _ = handle.join(); + /// ``` + #[must_use] + #[unstable(feature = "nonpoison_condvar", issue = "134645")] + pub fn timed_out(&self) -> bool { + self.0 + } +} + +/// A Condition Variable +/// +/// For more information about condition variables, check out the documentation for the poisoning +/// variant of this type at [`poison::Condvar`]. +/// +/// # Examples +/// +/// Note that this `Condvar` does **not** propagate information about threads that panic while +/// holding a lock. If you need this functionality, see [`poison::Mutex`] and [`poison::Condvar`]. +/// +/// ``` +/// #![feature(nonpoison_mutex)] +/// #![feature(nonpoison_condvar)] +/// +/// use std::sync::nonpoison::{Mutex, Condvar}; +/// use std::sync::Arc; +/// use std::thread; +/// +/// let pair = Arc::new((Mutex::new(false), Condvar::new())); +/// let pair2 = Arc::clone(&pair); +/// +/// // Inside of our lock, spawn a new thread, and then wait for it to start. +/// thread::spawn(move || { +/// let (lock, cvar) = &*pair2; +/// let mut started = lock.lock(); +/// *started = true; +/// // We notify the condvar that the value has changed. +/// cvar.notify_one(); +/// }); +/// +/// // Wait for the thread to start up. +/// let (lock, cvar) = &*pair; +/// let mut started = lock.lock(); +/// while !*started { +/// started = cvar.wait(started); +/// } +/// ``` +/// +/// [`poison::Mutex`]: crate::sync::poison::Mutex +/// [`poison::Condvar`]: crate::sync::poison::Condvar +#[unstable(feature = "nonpoison_condvar", issue = "134645")] +pub struct Condvar { + inner: sys::Condvar, +} + +impl Condvar { + /// Creates a new condition variable which is ready to be waited on and + /// notified. + /// + /// # Examples + /// + /// ``` + /// use std::sync::Condvar; + /// + /// let condvar = Condvar::new(); + /// ``` + #[unstable(feature = "nonpoison_condvar", issue = "134645")] + #[must_use] + #[inline] + pub const fn new() -> Condvar { + Condvar { inner: sys::Condvar::new() } + } + + /// Blocks the current thread until this condition variable receives a + /// notification. + /// + /// This function will atomically unlock the mutex specified (represented by + /// `guard`) and block the current thread. This means that any calls + /// to [`notify_one`] or [`notify_all`] which happen logically after the + /// mutex is unlocked are candidates to wake this thread up. When this + /// function call returns, the lock specified will have been re-acquired. + /// + /// Note that this function is susceptible to spurious wakeups. Condition + /// variables normally have a boolean predicate associated with them, and + /// the predicate must always be checked each time this function returns to + /// protect against spurious wakeups. + /// + /// # Panics + /// + /// This function may [`panic!`] if it is used with more than one mutex + /// over time. + /// + /// [`notify_one`]: Self::notify_one + /// [`notify_all`]: Self::notify_all + /// + /// # Examples + /// + /// ``` + /// #![feature(nonpoison_mutex)] + /// #![feature(nonpoison_condvar)] + /// + /// use std::sync::nonpoison::{Mutex, Condvar}; + /// use std::sync::Arc; + /// use std::thread; + /// + /// let pair = Arc::new((Mutex::new(false), Condvar::new())); + /// let pair2 = Arc::clone(&pair); + /// + /// thread::spawn(move || { + /// let (lock, cvar) = &*pair2; + /// let mut started = lock.lock(); + /// *started = true; + /// // We notify the condvar that the value has changed. + /// cvar.notify_one(); + /// }); + /// + /// // Wait for the thread to start up. + /// let (lock, cvar) = &*pair; + /// let mut started = lock.lock(); + /// // As long as the value inside the `Mutex` is `false`, we wait. + /// while !*started { + /// started = cvar.wait(started); + /// } + /// ``` + #[unstable(feature = "nonpoison_condvar", issue = "134645")] + pub fn wait<'a, T>(&self, guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> { + unsafe { + let lock = mutex::guard_lock(&guard); + self.inner.wait(lock); + } + guard + } + + /// Blocks the current thread until the provided condition becomes false. + /// + /// `condition` is checked immediately; if not met (returns `true`), this + /// will [`wait`] for the next notification then check again. This repeats + /// until `condition` returns `false`, in which case this function returns. + /// + /// This function will atomically unlock the mutex specified (represented by + /// `guard`) and block the current thread. This means that any calls + /// to [`notify_one`] or [`notify_all`] which happen logically after the + /// mutex is unlocked are candidates to wake this thread up. When this + /// function call returns, the lock specified will have been re-acquired. + /// + /// [`wait`]: Self::wait + /// [`notify_one`]: Self::notify_one + /// [`notify_all`]: Self::notify_all + /// + /// # Examples + /// + /// ``` + /// #![feature(nonpoison_mutex)] + /// #![feature(nonpoison_condvar)] + /// + /// use std::sync::nonpoison::{Mutex, Condvar}; + /// use std::sync::Arc; + /// use std::thread; + /// + /// let pair = Arc::new((Mutex::new(true), Condvar::new())); + /// let pair2 = Arc::clone(&pair); + /// + /// thread::spawn(move || { + /// let (lock, cvar) = &*pair2; + /// let mut pending = lock.lock(); + /// *pending = false; + /// // We notify the condvar that the value has changed. + /// cvar.notify_one(); + /// }); + /// + /// // Wait for the thread to start up. + /// let (lock, cvar) = &*pair; + /// // As long as the value inside the `Mutex` is `true`, we wait. + /// let _guard = cvar.wait_while(lock.lock(), |pending| { *pending }); + /// ``` + #[unstable(feature = "nonpoison_condvar", issue = "134645")] + pub fn wait_while<'a, T, F>( + &self, + mut guard: MutexGuard<'a, T>, + mut condition: F, + ) -> MutexGuard<'a, T> + where + F: FnMut(&mut T) -> bool, + { + while condition(&mut *guard) { + guard = self.wait(guard); + } + guard + } + + /// Waits on this condition variable for a notification, timing out after a + /// specified duration. + /// + /// The semantics of this function are equivalent to [`wait`] except that + /// the thread will be blocked for roughly no longer than `dur`. This + /// method should not be used for precise timing due to anomalies such as + /// preemption or platform differences that might not cause the maximum + /// amount of time waited to be precisely `dur`. + /// + /// Note that the best effort is made to ensure that the time waited is + /// measured with a monotonic clock, and not affected by the changes made to + /// the system time. This function is susceptible to spurious wakeups. + /// Condition variables normally have a boolean predicate associated with + /// them, and the predicate must always be checked each time this function + /// returns to protect against spurious wakeups. Additionally, it is + /// typically desirable for the timeout to not exceed some duration in + /// spite of spurious wakes, thus the sleep-duration is decremented by the + /// amount slept. Alternatively, use the `wait_timeout_while` method + /// to wait with a timeout while a predicate is true. + /// + /// The returned [`WaitTimeoutResult`] value indicates if the timeout is + /// known to have elapsed. + /// + /// Like [`wait`], the lock specified will be re-acquired when this function + /// returns, regardless of whether the timeout elapsed or not. + /// + /// [`wait`]: Self::wait + /// [`wait_timeout_while`]: Self::wait_timeout_while + /// + /// # Examples + /// + /// ``` + /// #![feature(nonpoison_mutex)] + /// #![feature(nonpoison_condvar)] + /// + /// use std::sync::nonpoison::{Mutex, Condvar}; + /// use std::sync::Arc; + /// use std::thread; + /// use std::time::Duration; + /// + /// let pair = Arc::new((Mutex::new(false), Condvar::new())); + /// let pair2 = Arc::clone(&pair); + /// + /// thread::spawn(move || { + /// let (lock, cvar) = &*pair2; + /// let mut started = lock.lock(); + /// *started = true; + /// // We notify the condvar that the value has changed. + /// cvar.notify_one(); + /// }); + /// + /// // wait for the thread to start up + /// let (lock, cvar) = &*pair; + /// let mut started = lock.lock(); + /// // as long as the value inside the `Mutex` is `false`, we wait + /// loop { + /// let result = cvar.wait_timeout(started, Duration::from_millis(10)); + /// // 10 milliseconds have passed, or maybe the value changed! + /// started = result.0; + /// if *started == true { + /// // We received the notification and the value has been updated, we can leave. + /// break + /// } + /// } + /// ``` + #[unstable(feature = "nonpoison_condvar", issue = "134645")] + pub fn wait_timeout<'a, T>( + &self, + guard: MutexGuard<'a, T>, + dur: Duration, + ) -> (MutexGuard<'a, T>, WaitTimeoutResult) { + let success = unsafe { + let lock = mutex::guard_lock(&guard); + self.inner.wait_timeout(lock, dur) + }; + (guard, WaitTimeoutResult(!success)) + } + + /// Waits on this condition variable for a notification, timing out after a + /// specified duration. + /// + /// The semantics of this function are equivalent to [`wait_while`] except + /// that the thread will be blocked for roughly no longer than `dur`. This + /// method should not be used for precise timing due to anomalies such as + /// preemption or platform differences that might not cause the maximum + /// amount of time waited to be precisely `dur`. + /// + /// Note that the best effort is made to ensure that the time waited is + /// measured with a monotonic clock, and not affected by the changes made to + /// the system time. + /// + /// The returned [`WaitTimeoutResult`] value indicates if the timeout is + /// known to have elapsed without the condition being met. + /// + /// Like [`wait_while`], the lock specified will be re-acquired when this + /// function returns, regardless of whether the timeout elapsed or not. + /// + /// [`wait_while`]: Self::wait_while + /// [`wait_timeout`]: Self::wait_timeout + /// + /// # Examples + /// + /// ``` + /// #![feature(nonpoison_mutex)] + /// #![feature(nonpoison_condvar)] + /// + /// use std::sync::nonpoison::{Mutex, Condvar}; + /// use std::sync::Arc; + /// use std::thread; + /// use std::time::Duration; + /// + /// let pair = Arc::new((Mutex::new(true), Condvar::new())); + /// let pair2 = Arc::clone(&pair); + /// + /// thread::spawn(move || { + /// let (lock, cvar) = &*pair2; + /// let mut pending = lock.lock(); + /// *pending = false; + /// // We notify the condvar that the value has changed. + /// cvar.notify_one(); + /// }); + /// + /// // wait for the thread to start up + /// let (lock, cvar) = &*pair; + /// let result = cvar.wait_timeout_while( + /// lock.lock(), + /// Duration::from_millis(100), + /// |&mut pending| pending, + /// ); + /// if result.1.timed_out() { + /// // timed-out without the condition ever evaluating to false. + /// } + /// // access the locked mutex via result.0 + /// ``` + #[unstable(feature = "nonpoison_condvar", issue = "134645")] + pub fn wait_timeout_while<'a, T, F>( + &self, + mut guard: MutexGuard<'a, T>, + dur: Duration, + mut condition: F, + ) -> (MutexGuard<'a, T>, WaitTimeoutResult) + where + F: FnMut(&mut T) -> bool, + { + let start = Instant::now(); + loop { + if !condition(&mut *guard) { + return (guard, WaitTimeoutResult(false)); + } + let timeout = match dur.checked_sub(start.elapsed()) { + Some(timeout) => timeout, + None => return (guard, WaitTimeoutResult(true)), + }; + guard = self.wait_timeout(guard, timeout).0; + } + } + + /// Wakes up one blocked thread on this condvar. + /// + /// If there is a blocked thread on this condition variable, then it will + /// be woken up from its call to [`wait`] or [`wait_timeout`]. Calls to + /// `notify_one` are not buffered in any way. + /// + /// To wake up all threads, see [`notify_all`]. + /// + /// [`wait`]: Self::wait + /// [`wait_timeout`]: Self::wait_timeout + /// [`notify_all`]: Self::notify_all + /// + /// # Examples + /// + /// ``` + /// #![feature(nonpoison_mutex)] + /// #![feature(nonpoison_condvar)] + /// + /// use std::sync::nonpoison::{Mutex, Condvar}; + /// use std::sync::Arc; + /// use std::thread; + /// + /// let pair = Arc::new((Mutex::new(false), Condvar::new())); + /// let pair2 = Arc::clone(&pair); + /// + /// thread::spawn(move || { + /// let (lock, cvar) = &*pair2; + /// let mut started = lock.lock(); + /// *started = true; + /// // We notify the condvar that the value has changed. + /// cvar.notify_one(); + /// }); + /// + /// // Wait for the thread to start up. + /// let (lock, cvar) = &*pair; + /// let mut started = lock.lock(); + /// // As long as the value inside the `Mutex` is `false`, we wait. + /// while !*started { + /// started = cvar.wait(started); + /// } + /// ``` + #[unstable(feature = "nonpoison_condvar", issue = "134645")] + pub fn notify_one(&self) { + self.inner.notify_one() + } + + /// Wakes up all blocked threads on this condvar. + /// + /// This method will ensure that any current waiters on the condition + /// variable are awoken. Calls to `notify_all()` are not buffered in any + /// way. + /// + /// To wake up only one thread, see [`notify_one`]. + /// + /// [`notify_one`]: Self::notify_one + /// + /// # Examples + /// + /// ``` + /// #![feature(nonpoison_mutex)] + /// #![feature(nonpoison_condvar)] + /// + /// use std::sync::nonpoison::{Mutex, Condvar}; + /// use std::sync::Arc; + /// use std::thread; + /// + /// let pair = Arc::new((Mutex::new(false), Condvar::new())); + /// let pair2 = Arc::clone(&pair); + /// + /// thread::spawn(move || { + /// let (lock, cvar) = &*pair2; + /// let mut started = lock.lock(); + /// *started = true; + /// // We notify the condvar that the value has changed. + /// cvar.notify_all(); + /// }); + /// + /// // Wait for the thread to start up. + /// let (lock, cvar) = &*pair; + /// let mut started = lock.lock(); + /// // As long as the value inside the `Mutex` is `false`, we wait. + /// while !*started { + /// started = cvar.wait(started); + /// } + /// ``` + #[unstable(feature = "nonpoison_condvar", issue = "134645")] + pub fn notify_all(&self) { + self.inner.notify_all() + } +} + +#[unstable(feature = "nonpoison_condvar", issue = "134645")] +impl fmt::Debug for Condvar { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Condvar").finish_non_exhaustive() + } +} + +#[unstable(feature = "nonpoison_condvar", issue = "134645")] +impl Default for Condvar { + /// Creates a `Condvar` which is ready to be waited on and notified. + fn default() -> Condvar { + Condvar::new() + } +} diff --git a/library/std/src/sync/nonpoison/mutex.rs b/library/std/src/sync/nonpoison/mutex.rs index fd1e671d7a3d..f8af081e79d9 100644 --- a/library/std/src/sync/nonpoison/mutex.rs +++ b/library/std/src/sync/nonpoison/mutex.rs @@ -114,7 +114,6 @@ impl !Send for MutexGuard<'_, T> {} #[unstable(feature = "nonpoison_mutex", issue = "134645")] unsafe impl Sync for MutexGuard<'_, T> {} -// FIXME(nonpoison_condvar): Use this link instead: [`Condvar`]: crate::sync::nonpoison::Condvar /// An RAII mutex guard returned by `MutexGuard::map`, which can point to a /// subfield of the protected data. When this structure is dropped (falls out /// of scope), the lock will be unlocked. @@ -131,7 +130,7 @@ unsafe impl Sync for MutexGuard<'_, T> {} /// /// [`map`]: MutexGuard::map /// [`filter_map`]: MutexGuard::filter_map -/// [`Condvar`]: crate::sync::Condvar +/// [`Condvar`]: crate::sync::nonpoison::Condvar #[must_use = "if unused the Mutex will immediately unlock"] #[must_not_suspend = "holding a MappedMutexGuard across suspend \ points can cause deadlocks, delays, \ @@ -458,6 +457,10 @@ impl fmt::Display for MutexGuard<'_, T> { } } +pub fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex { + &guard.lock.inner +} + impl<'a, T: ?Sized> MutexGuard<'a, T> { /// Makes a [`MappedMutexGuard`] for a component of the borrowed data, e.g. /// an enum variant. From b2380d2bcc429b1ac8d800bc2fdce90de81d1454 Mon Sep 17 00:00:00 2001 From: Connor Tsui Date: Sat, 23 Aug 2025 09:15:33 -0400 Subject: [PATCH 09/68] add nonpoison and poison condvar tests Adds tests for the `nonpoison::Mutex` variant by using a macro to duplicate the existing `poison` tests. Note that all of the tests here are adapted from the existing `poison` tests. Also steals the `test_mutex_arc_condvar` test from `mutex.rs`. Signed-off-by: Connor Tsui --- library/std/tests/sync/condvar.rs | 425 ++++++++++++++++++------------ library/std/tests/sync/lib.rs | 1 + library/std/tests/sync/mutex.rs | 34 --- 3 files changed, 253 insertions(+), 207 deletions(-) diff --git a/library/std/tests/sync/condvar.rs b/library/std/tests/sync/condvar.rs index 834de6bb1c29..1d712a643003 100644 --- a/library/std/tests/sync/condvar.rs +++ b/library/std/tests/sync/condvar.rs @@ -1,190 +1,269 @@ +use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::mpsc::channel; -use std::sync::{Arc, Condvar, Mutex}; use std::thread; use std::time::Duration; -#[test] -fn smoke() { - let c = Condvar::new(); - c.notify_one(); - c.notify_all(); -} +use super::nonpoison_and_poison_unwrap_test; -#[test] -#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads -fn notify_one() { - let m = Arc::new(Mutex::new(())); - let m2 = m.clone(); - let c = Arc::new(Condvar::new()); - let c2 = c.clone(); +nonpoison_and_poison_unwrap_test!( + name: smoke, + test_body: { + use locks::Condvar; - let g = m.lock().unwrap(); - let _t = thread::spawn(move || { - let _g = m2.lock().unwrap(); - c2.notify_one(); - }); - let g = c.wait(g).unwrap(); - drop(g); -} - -#[test] -#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads -fn notify_all() { - const N: usize = 10; - - let data = Arc::new((Mutex::new(0), Condvar::new())); - let (tx, rx) = channel(); - for _ in 0..N { - let data = data.clone(); - let tx = tx.clone(); - thread::spawn(move || { - let &(ref lock, ref cond) = &*data; - let mut cnt = lock.lock().unwrap(); - *cnt += 1; - if *cnt == N { - tx.send(()).unwrap(); - } - while *cnt != 0 { - cnt = cond.wait(cnt).unwrap(); - } - tx.send(()).unwrap(); - }); + let c = Condvar::new(); + c.notify_one(); + c.notify_all(); } - drop(tx); +); - let &(ref lock, ref cond) = &*data; - rx.recv().unwrap(); - let mut cnt = lock.lock().unwrap(); - *cnt = 0; - cond.notify_all(); - drop(cnt); - - for _ in 0..N { - rx.recv().unwrap(); - } -} - -#[test] #[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads -fn wait_while() { - let pair = Arc::new((Mutex::new(false), Condvar::new())); - let pair2 = pair.clone(); +nonpoison_and_poison_unwrap_test!( + name: notify_one, + test_body: { + use locks::{Condvar, Mutex}; - // Inside of our lock, spawn a new thread, and then wait for it to start. - thread::spawn(move || { - let &(ref lock, ref cvar) = &*pair2; - let mut started = lock.lock().unwrap(); - *started = true; - // We notify the condvar that the value has changed. - cvar.notify_one(); - }); - - // Wait for the thread to start up. - let &(ref lock, ref cvar) = &*pair; - let guard = cvar.wait_while(lock.lock().unwrap(), |started| !*started); - assert!(*guard.unwrap()); -} - -#[test] -#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // condvar wait not supported -fn wait_timeout_wait() { - let m = Arc::new(Mutex::new(())); - let c = Arc::new(Condvar::new()); - - loop { - let g = m.lock().unwrap(); - let (_g, no_timeout) = c.wait_timeout(g, Duration::from_millis(1)).unwrap(); - // spurious wakeups mean this isn't necessarily true - // so execute test again, if not timeout - if !no_timeout.timed_out() { - continue; - } - - break; - } -} - -#[test] -#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // condvar wait not supported -fn wait_timeout_while_wait() { - let m = Arc::new(Mutex::new(())); - let c = Arc::new(Condvar::new()); - - let g = m.lock().unwrap(); - let (_g, wait) = c.wait_timeout_while(g, Duration::from_millis(1), |_| true).unwrap(); - // no spurious wakeups. ensure it timed-out - assert!(wait.timed_out()); -} - -#[test] -#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // condvar wait not supported -fn wait_timeout_while_instant_satisfy() { - let m = Arc::new(Mutex::new(())); - let c = Arc::new(Condvar::new()); - - let g = m.lock().unwrap(); - let (_g, wait) = c.wait_timeout_while(g, Duration::from_millis(0), |_| false).unwrap(); - // ensure it didn't time-out even if we were not given any time. - assert!(!wait.timed_out()); -} - -#[test] -#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads -fn wait_timeout_while_wake() { - let pair = Arc::new((Mutex::new(false), Condvar::new())); - let pair_copy = pair.clone(); - - let &(ref m, ref c) = &*pair; - let g = m.lock().unwrap(); - let _t = thread::spawn(move || { - let &(ref lock, ref cvar) = &*pair_copy; - let mut started = lock.lock().unwrap(); - thread::sleep(Duration::from_millis(1)); - *started = true; - cvar.notify_one(); - }); - let (g2, wait) = c - .wait_timeout_while(g, Duration::from_millis(u64::MAX), |&mut notified| !notified) - .unwrap(); - // ensure it didn't time-out even if we were not given any time. - assert!(!wait.timed_out()); - assert!(*g2); -} - -#[test] -#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads -fn wait_timeout_wake() { - let m = Arc::new(Mutex::new(())); - let c = Arc::new(Condvar::new()); - - loop { - let g = m.lock().unwrap(); - - let c2 = c.clone(); + let m = Arc::new(Mutex::new(())); let m2 = m.clone(); + let c = Arc::new(Condvar::new()); + let c2 = c.clone(); - let notified = Arc::new(AtomicBool::new(false)); - let notified_copy = notified.clone(); - - let t = thread::spawn(move || { - let _g = m2.lock().unwrap(); - thread::sleep(Duration::from_millis(1)); - notified_copy.store(true, Ordering::Relaxed); + let g = maybe_unwrap(m.lock()); + let _t = thread::spawn(move || { + let _g = maybe_unwrap(m2.lock()); c2.notify_one(); }); - let (g, timeout_res) = c.wait_timeout(g, Duration::from_millis(u64::MAX)).unwrap(); - assert!(!timeout_res.timed_out()); - // spurious wakeups mean this isn't necessarily true - // so execute test again, if not notified - if !notified.load(Ordering::Relaxed) { - t.join().unwrap(); - continue; - } + let g = maybe_unwrap(c.wait(g)); drop(g); - - t.join().unwrap(); - - break; } -} +); + +#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads +nonpoison_and_poison_unwrap_test!( + name: notify_all, + test_body: { + use locks::{Condvar, Mutex}; + + const N: usize = 10; + + let data = Arc::new((Mutex::new(0), Condvar::new())); + let (tx, rx) = channel(); + for _ in 0..N { + let data = data.clone(); + let tx = tx.clone(); + thread::spawn(move || { + let &(ref lock, ref cond) = &*data; + let mut cnt = maybe_unwrap(lock.lock()); + *cnt += 1; + if *cnt == N { + tx.send(()).unwrap(); + } + while *cnt != 0 { + cnt = maybe_unwrap(cond.wait(cnt)); + } + tx.send(()).unwrap(); + }); + } + drop(tx); + + let &(ref lock, ref cond) = &*data; + rx.recv().unwrap(); + let mut cnt = maybe_unwrap(lock.lock()); + *cnt = 0; + cond.notify_all(); + drop(cnt); + + for _ in 0..N { + rx.recv().unwrap(); + } + } +); + +#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads +nonpoison_and_poison_unwrap_test!( + name: test_mutex_arc_condvar, + test_body: { + use locks::{Condvar, Mutex}; + + struct Packet(Arc<(Mutex, Condvar)>); + + let packet = Packet(Arc::new((Mutex::new(false), Condvar::new()))); + let packet2 = Packet(packet.0.clone()); + + let (tx, rx) = channel(); + + let _t = thread::spawn(move || { + // Wait until our parent has taken the lock. + rx.recv().unwrap(); + let &(ref lock, ref cvar) = &*packet2.0; + + // Set the data to `true` and wake up our parent. + let mut guard = maybe_unwrap(lock.lock()); + *guard = true; + cvar.notify_one(); + }); + + let &(ref lock, ref cvar) = &*packet.0; + let mut guard = maybe_unwrap(lock.lock()); + // Wake up our child. + tx.send(()).unwrap(); + + // Wait until our child has set the data to `true`. + assert!(!*guard); + while !*guard { + guard = maybe_unwrap(cvar.wait(guard)); + } + } +); + +#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads +nonpoison_and_poison_unwrap_test!( + name: wait_while, + test_body: { + use locks::{Condvar, Mutex}; + + let pair = Arc::new((Mutex::new(false), Condvar::new())); + let pair2 = pair.clone(); + + // Inside of our lock, spawn a new thread, and then wait for it to start. + thread::spawn(move || { + let &(ref lock, ref cvar) = &*pair2; + let mut started = maybe_unwrap(lock.lock()); + *started = true; + // We notify the condvar that the value has changed. + cvar.notify_one(); + }); + + // Wait for the thread to start up. + let &(ref lock, ref cvar) = &*pair; + let guard = cvar.wait_while(maybe_unwrap(lock.lock()), |started| !*started); + assert!(*maybe_unwrap(guard)); + } +); + +#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads +nonpoison_and_poison_unwrap_test!( + name: wait_timeout_wait, + test_body: { + use locks::{Condvar, Mutex}; + + let m = Arc::new(Mutex::new(())); + let c = Arc::new(Condvar::new()); + + loop { + let g = maybe_unwrap(m.lock()); + let (_g, no_timeout) = maybe_unwrap(c.wait_timeout(g, Duration::from_millis(1))); + // spurious wakeups mean this isn't necessarily true + // so execute test again, if not timeout + if !no_timeout.timed_out() { + continue; + } + + break; + } + } +); + +#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads +nonpoison_and_poison_unwrap_test!( + name: wait_timeout_while_wait, + test_body: { + use locks::{Condvar, Mutex}; + + let m = Arc::new(Mutex::new(())); + let c = Arc::new(Condvar::new()); + + let g = maybe_unwrap(m.lock()); + let (_g, wait) = maybe_unwrap(c.wait_timeout_while(g, Duration::from_millis(1), |_| true)); + // no spurious wakeups. ensure it timed-out + assert!(wait.timed_out()); + } +); + +#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads +nonpoison_and_poison_unwrap_test!( + name: wait_timeout_while_instant_satisfy, + test_body: { + use locks::{Condvar, Mutex}; + + let m = Arc::new(Mutex::new(())); + let c = Arc::new(Condvar::new()); + + let g = maybe_unwrap(m.lock()); + let (_g, wait) = + maybe_unwrap(c.wait_timeout_while(g, Duration::from_millis(0), |_| false)); + // ensure it didn't time-out even if we were not given any time. + assert!(!wait.timed_out()); + } +); + +#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads +nonpoison_and_poison_unwrap_test!( + name: wait_timeout_while_wake, + test_body: { + use locks::{Condvar, Mutex}; + + let pair = Arc::new((Mutex::new(false), Condvar::new())); + let pair_copy = pair.clone(); + + let &(ref m, ref c) = &*pair; + let g = maybe_unwrap(m.lock()); + let _t = thread::spawn(move || { + let &(ref lock, ref cvar) = &*pair_copy; + let mut started = maybe_unwrap(lock.lock()); + thread::sleep(Duration::from_millis(1)); + *started = true; + cvar.notify_one(); + }); + let (g2, wait) = maybe_unwrap(c.wait_timeout_while( + g, + Duration::from_millis(u64::MAX), + |&mut notified| !notified + )); + // ensure it didn't time-out even if we were not given any time. + assert!(!wait.timed_out()); + assert!(*g2); + } +); + +#[cfg_attr(any(target_os = "emscripten", target_os = "wasi"), ignore)] // no threads +nonpoison_and_poison_unwrap_test!( + name: wait_timeout_wake, + test_body: { + use locks::{Condvar, Mutex}; + + let m = Arc::new(Mutex::new(())); + let c = Arc::new(Condvar::new()); + + loop { + let g = maybe_unwrap(m.lock()); + + let c2 = c.clone(); + let m2 = m.clone(); + + let notified = Arc::new(AtomicBool::new(false)); + let notified_copy = notified.clone(); + + let t = thread::spawn(move || { + let _g = maybe_unwrap(m2.lock()); + thread::sleep(Duration::from_millis(1)); + notified_copy.store(true, Ordering::Relaxed); + c2.notify_one(); + }); + let (g, timeout_res) = + maybe_unwrap(c.wait_timeout(g, Duration::from_millis(u64::MAX))); + assert!(!timeout_res.timed_out()); + // spurious wakeups mean this isn't necessarily true + // so execute test again, if not notified + if !notified.load(Ordering::Relaxed) { + t.join().unwrap(); + continue; + } + drop(g); + + t.join().unwrap(); + + break; + } + } +); diff --git a/library/std/tests/sync/lib.rs b/library/std/tests/sync/lib.rs index f874c2ba3895..ac1dbebcc5cb 100644 --- a/library/std/tests/sync/lib.rs +++ b/library/std/tests/sync/lib.rs @@ -7,6 +7,7 @@ #![feature(rwlock_downgrade)] #![feature(std_internals)] #![feature(sync_nonpoison)] +#![feature(nonpoison_condvar)] #![feature(nonpoison_mutex)] #![feature(nonpoison_rwlock)] #![allow(internal_features)] diff --git a/library/std/tests/sync/mutex.rs b/library/std/tests/sync/mutex.rs index 90cefc0d5946..612c75c7aef5 100644 --- a/library/std/tests/sync/mutex.rs +++ b/library/std/tests/sync/mutex.rs @@ -213,40 +213,6 @@ nonpoison_and_poison_unwrap_test!( } ); -// FIXME(nonpoison_condvar): Move this to the `condvar.rs` test file once `nonpoison::condvar` gets -// implemented. -#[test] -fn test_mutex_arc_condvar() { - struct Packet(Arc<(Mutex, Condvar)>); - - let packet = Packet(Arc::new((Mutex::new(false), Condvar::new()))); - let packet2 = Packet(packet.0.clone()); - - let (tx, rx) = channel(); - - let _t = thread::spawn(move || { - // Wait until our parent has taken the lock. - rx.recv().unwrap(); - let &(ref lock, ref cvar) = &*packet2.0; - - // Set the data to `true` and wake up our parent. - let mut guard = lock.lock().unwrap(); - *guard = true; - cvar.notify_one(); - }); - - let &(ref lock, ref cvar) = &*packet.0; - let mut guard = lock.lock().unwrap(); - // Wake up our child. - tx.send(()).unwrap(); - - // Wait until our child has set the data to `true`. - assert!(!*guard); - while !*guard { - guard = cvar.wait(guard).unwrap(); - } -} - nonpoison_and_poison_unwrap_test!( name: test_mutex_arc_nested, test_body: { From a8163bd6f4f2bc3ba8fb56df9d48622070ba68ab Mon Sep 17 00:00:00 2001 From: Connor Tsui Date: Tue, 22 Jul 2025 13:52:10 +0200 Subject: [PATCH 10/68] change `Barrier` implementation to use `nonpoison::Condvar` Signed-off-by: Connor Tsui --- library/std/src/sync/barrier.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/library/std/src/sync/barrier.rs b/library/std/src/sync/barrier.rs index 067ff66d9af7..712ce03f90b0 100644 --- a/library/std/src/sync/barrier.rs +++ b/library/std/src/sync/barrier.rs @@ -1,6 +1,5 @@ use crate::fmt; -// FIXME(nonpoison_mutex,nonpoison_condvar): switch to nonpoison versions once they are available -use crate::sync::{Condvar, Mutex}; +use crate::sync::nonpoison::{Condvar, Mutex}; /// A barrier enables multiple threads to synchronize the beginning /// of some computation. @@ -118,12 +117,11 @@ impl Barrier { /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn wait(&self) -> BarrierWaitResult { - let mut lock = self.lock.lock().unwrap(); + let mut lock = self.lock.lock(); let local_gen = lock.generation_id; lock.count += 1; if lock.count < self.num_threads { - let _guard = - self.cvar.wait_while(lock, |state| local_gen == state.generation_id).unwrap(); + let _guard = self.cvar.wait_while(lock, |state| local_gen == state.generation_id); BarrierWaitResult(false) } else { lock.count = 0; From b8ee38b79e6bfbb1376785f765a233e96d0c5da2 Mon Sep 17 00:00:00 2001 From: Connor Tsui Date: Thu, 24 Jul 2025 15:24:22 +0200 Subject: [PATCH 11/68] move `WaitTimeoutResult` up to `mod.rs` Since `WaitTimeoutResult` is poison-agnostic, we want to use the same type for both variants of `Condvar`. Signed-off-by: Connor Tsui --- library/std/src/sync/mod.rs | 65 ++++++++++++++++++++- library/std/src/sync/nonpoison.rs | 2 +- library/std/src/sync/nonpoison/condvar.rs | 70 +---------------------- library/std/src/sync/poison.rs | 2 +- library/std/src/sync/poison/condvar.rs | 66 +-------------------- 5 files changed, 68 insertions(+), 137 deletions(-) diff --git a/library/std/src/sync/mod.rs b/library/std/src/sync/mod.rs index 6ef3bf25cf67..97c04d07eaf1 100644 --- a/library/std/src/sync/mod.rs +++ b/library/std/src/sync/mod.rs @@ -209,7 +209,7 @@ pub use self::poison::{LockResult, PoisonError}; #[doc(inline)] pub use self::poison::{ Mutex, MutexGuard, TryLockError, TryLockResult, - Condvar, WaitTimeoutResult, + Condvar, Once, OnceState, RwLock, RwLockReadGuard, RwLockWriteGuard, }; @@ -234,3 +234,66 @@ mod barrier; mod lazy_lock; mod once_lock; mod reentrant_lock; + +/// A type indicating whether a timed wait on a condition variable returned +/// due to a time out or not. +/// +/// It is returned by the [`wait_timeout`] method. +/// +/// [`wait_timeout`]: Condvar::wait_timeout +#[derive(Debug, PartialEq, Eq, Copy, Clone)] +#[stable(feature = "wait_timeout", since = "1.5.0")] +pub struct WaitTimeoutResult(bool); + +impl WaitTimeoutResult { + /// Returns `true` if the wait was known to have timed out. + /// + /// # Examples + /// + /// This example spawns a thread which will sleep 20 milliseconds before + /// updating a boolean value and then notifying the condvar. + /// + /// The main thread will wait with a 10 millisecond timeout on the condvar + /// and will leave the loop upon timeout. + /// + /// ``` + /// use std::sync::{Arc, Condvar, Mutex}; + /// use std::thread; + /// use std::time::Duration; + /// + /// let pair = Arc::new((Mutex::new(false), Condvar::new())); + /// let pair2 = Arc::clone(&pair); + /// + /// # let handle = + /// thread::spawn(move || { + /// let (lock, cvar) = &*pair2; + /// + /// // Let's wait 20 milliseconds before notifying the condvar. + /// thread::sleep(Duration::from_millis(20)); + /// + /// let mut started = lock.lock().unwrap(); + /// // We update the boolean value. + /// *started = true; + /// cvar.notify_one(); + /// }); + /// + /// // Wait for the thread to start up. + /// let (lock, cvar) = &*pair; + /// loop { + /// // Let's put a timeout on the condvar's wait. + /// let result = cvar.wait_timeout(lock.lock().unwrap(), Duration::from_millis(10)).unwrap(); + /// // 10 milliseconds have passed. + /// if result.1.timed_out() { + /// // timed out now and we can leave. + /// break + /// } + /// } + /// # // Prevent leaks for Miri. + /// # let _ = handle.join(); + /// ``` + #[must_use] + #[stable(feature = "wait_timeout", since = "1.5.0")] + pub fn timed_out(&self) -> bool { + self.0 + } +} diff --git a/library/std/src/sync/nonpoison.rs b/library/std/src/sync/nonpoison.rs index 96ecf7084fa9..ec3587263f47 100644 --- a/library/std/src/sync/nonpoison.rs +++ b/library/std/src/sync/nonpoison.rs @@ -30,7 +30,7 @@ impl fmt::Display for WouldBlock { } #[unstable(feature = "nonpoison_condvar", issue = "134645")] -pub use self::condvar::{Condvar, WaitTimeoutResult}; +pub use self::condvar::Condvar; #[unstable(feature = "mapped_lock_guards", issue = "117108")] pub use self::mutex::MappedMutexGuard; #[unstable(feature = "nonpoison_mutex", issue = "134645")] diff --git a/library/std/src/sync/nonpoison/condvar.rs b/library/std/src/sync/nonpoison/condvar.rs index 9744c87d9cc1..49afdd878182 100644 --- a/library/std/src/sync/nonpoison/condvar.rs +++ b/library/std/src/sync/nonpoison/condvar.rs @@ -1,77 +1,9 @@ use crate::fmt; +use crate::sync::WaitTimeoutResult; use crate::sync::nonpoison::{MutexGuard, mutex}; use crate::sys::sync as sys; use crate::time::{Duration, Instant}; -/// A type indicating whether a timed wait on a condition variable returned -/// due to a time out or not. -/// -/// It is returned by the [`wait_timeout`] method. -/// -/// [`wait_timeout`]: Condvar::wait_timeout -#[derive(Debug, PartialEq, Eq, Copy, Clone)] -#[unstable(feature = "nonpoison_condvar", issue = "134645")] -pub struct WaitTimeoutResult(bool); - -// FIXME(nonpoison_condvar) this type is duplicated in `poison`. How do we share types that are -// poison-agnostic? -impl WaitTimeoutResult { - /// Returns `true` if the wait was known to have timed out. - /// - /// # Examples - /// - /// This example spawns a thread which will sleep 20 milliseconds before - /// updating a boolean value and then notifying the condvar. - /// - /// The main thread will wait with a 10 millisecond timeout on the condvar - /// and will leave the loop upon timeout. - /// - /// ``` - /// #![feature(nonpoison_mutex)] - /// #![feature(nonpoison_condvar)] - /// - /// use std::sync::nonpoison::{Mutex, Condvar}; - /// use std::sync::Arc; - /// use std::thread; - /// use std::time::Duration; - /// - /// let pair = Arc::new((Mutex::new(false), Condvar::new())); - /// let pair2 = Arc::clone(&pair); - /// - /// # let handle = - /// thread::spawn(move || { - /// let (lock, cvar) = &*pair2; - /// - /// // Let's wait 20 milliseconds before notifying the condvar. - /// thread::sleep(Duration::from_millis(20)); - /// - /// let mut started = lock.lock(); - /// // We update the boolean value. - /// *started = true; - /// cvar.notify_one(); - /// }); - /// - /// // Wait for the thread to start up. - /// let (lock, cvar) = &*pair; - /// loop { - /// // Let's put a timeout on the condvar's wait. - /// let result = cvar.wait_timeout(lock.lock(), Duration::from_millis(10)); - /// // 10 milliseconds have passed. - /// if result.1.timed_out() { - /// // timed out now and we can leave. - /// break - /// } - /// } - /// # // Prevent leaks for Miri. - /// # let _ = handle.join(); - /// ``` - #[must_use] - #[unstable(feature = "nonpoison_condvar", issue = "134645")] - pub fn timed_out(&self) -> bool { - self.0 - } -} - /// A Condition Variable /// /// For more information about condition variables, check out the documentation for the poisoning diff --git a/library/std/src/sync/poison.rs b/library/std/src/sync/poison.rs index 31889dcc10fa..dc4774dcca18 100644 --- a/library/std/src/sync/poison.rs +++ b/library/std/src/sync/poison.rs @@ -61,7 +61,7 @@ //! then the lock will not be poisoned. #[stable(feature = "rust1", since = "1.0.0")] -pub use self::condvar::{Condvar, WaitTimeoutResult}; +pub use self::condvar::Condvar; #[unstable(feature = "mapped_lock_guards", issue = "117108")] pub use self::mutex::MappedMutexGuard; #[stable(feature = "rust1", since = "1.0.0")] diff --git a/library/std/src/sync/poison/condvar.rs b/library/std/src/sync/poison/condvar.rs index 0e9d4233c657..5dc2b510f3a2 100644 --- a/library/std/src/sync/poison/condvar.rs +++ b/library/std/src/sync/poison/condvar.rs @@ -1,73 +1,9 @@ use crate::fmt; +use crate::sync::WaitTimeoutResult; use crate::sync::poison::{self, LockResult, MutexGuard, PoisonError, mutex}; use crate::sys::sync as sys; use crate::time::{Duration, Instant}; -/// A type indicating whether a timed wait on a condition variable returned -/// due to a time out or not. -/// -/// It is returned by the [`wait_timeout`] method. -/// -/// [`wait_timeout`]: Condvar::wait_timeout -#[derive(Debug, PartialEq, Eq, Copy, Clone)] -#[stable(feature = "wait_timeout", since = "1.5.0")] -pub struct WaitTimeoutResult(bool); - -// FIXME(nonpoison_condvar): `WaitTimeoutResult` is actually poisoning-agnostic, it seems. -// Should we take advantage of this fact? -impl WaitTimeoutResult { - /// Returns `true` if the wait was known to have timed out. - /// - /// # Examples - /// - /// This example spawns a thread which will sleep 20 milliseconds before - /// updating a boolean value and then notifying the condvar. - /// - /// The main thread will wait with a 10 millisecond timeout on the condvar - /// and will leave the loop upon timeout. - /// - /// ``` - /// use std::sync::{Arc, Condvar, Mutex}; - /// use std::thread; - /// use std::time::Duration; - /// - /// let pair = Arc::new((Mutex::new(false), Condvar::new())); - /// let pair2 = Arc::clone(&pair); - /// - /// # let handle = - /// thread::spawn(move || { - /// let (lock, cvar) = &*pair2; - /// - /// // Let's wait 20 milliseconds before notifying the condvar. - /// thread::sleep(Duration::from_millis(20)); - /// - /// let mut started = lock.lock().unwrap(); - /// // We update the boolean value. - /// *started = true; - /// cvar.notify_one(); - /// }); - /// - /// // Wait for the thread to start up. - /// let (lock, cvar) = &*pair; - /// loop { - /// // Let's put a timeout on the condvar's wait. - /// let result = cvar.wait_timeout(lock.lock().unwrap(), Duration::from_millis(10)).unwrap(); - /// // 10 milliseconds have passed. - /// if result.1.timed_out() { - /// // timed out now and we can leave. - /// break - /// } - /// } - /// # // Prevent leaks for Miri. - /// # let _ = handle.join(); - /// ``` - #[must_use] - #[stable(feature = "wait_timeout", since = "1.5.0")] - pub fn timed_out(&self) -> bool { - self.0 - } -} - /// A Condition Variable /// /// Condition variables represent the ability to block a thread such that it From d210ce7dac4acb7653d647c77d56c10068dda82c Mon Sep 17 00:00:00 2001 From: Connor Tsui Date: Sat, 23 Aug 2025 09:39:37 -0400 Subject: [PATCH 12/68] fix visibility of private getters Signed-off-by: Connor Tsui --- library/std/src/sync/nonpoison/mutex.rs | 3 ++- library/std/src/sync/poison/mutex.rs | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/library/std/src/sync/nonpoison/mutex.rs b/library/std/src/sync/nonpoison/mutex.rs index f8af081e79d9..07430ce3a139 100644 --- a/library/std/src/sync/nonpoison/mutex.rs +++ b/library/std/src/sync/nonpoison/mutex.rs @@ -457,7 +457,8 @@ impl fmt::Display for MutexGuard<'_, T> { } } -pub fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex { +/// For use in [`nonpoison::condvar`](super::condvar). +pub(super) fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex { &guard.lock.inner } diff --git a/library/std/src/sync/poison/mutex.rs b/library/std/src/sync/poison/mutex.rs index 720c212c65cf..7e9d920d92f8 100644 --- a/library/std/src/sync/poison/mutex.rs +++ b/library/std/src/sync/poison/mutex.rs @@ -757,11 +757,13 @@ impl fmt::Display for MutexGuard<'_, T> { } } -pub fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex { +/// For use in [`nonpoison::condvar`](super::condvar). +pub(super) fn guard_lock<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a sys::Mutex { &guard.lock.inner } -pub fn guard_poison<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a poison::Flag { +/// For use in [`nonpoison::condvar`](super::condvar). +pub(super) fn guard_poison<'a, T: ?Sized>(guard: &MutexGuard<'a, T>) -> &'a poison::Flag { &guard.lock.poison } From fdbaaac245673d8dbf7bcc21ebedc833693434f6 Mon Sep 17 00:00:00 2001 From: Nilotpal Gupta Date: Tue, 19 Aug 2025 11:10:32 +0530 Subject: [PATCH 13/68] Fix format string grammar in docs and improve alignment error message --- compiler/rustc_parse_format/src/lib.rs | 4 +++- library/alloc/src/fmt.rs | 2 +- tests/ui/fmt/format-string-wrong-order.rs | 8 +++++--- tests/ui/fmt/format-string-wrong-order.stderr | 14 ++++++++++---- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_parse_format/src/lib.rs b/compiler/rustc_parse_format/src/lib.rs index 8e4da7923fcb..5cda0b813d23 100644 --- a/compiler/rustc_parse_format/src/lib.rs +++ b/compiler/rustc_parse_format/src/lib.rs @@ -858,7 +858,9 @@ impl<'input> Parser<'input> { self.errors.insert( 0, ParseError { - description: "expected format parameter to occur after `:`".to_owned(), + description: + "expected alignment specifier after `:` in format string; example: `{:>?}`" + .to_owned(), note: None, label: format!("expected `{}` to occur after `:`", alignment), span: range, diff --git a/library/alloc/src/fmt.rs b/library/alloc/src/fmt.rs index d0ba9c398864..82eaf7d87244 100644 --- a/library/alloc/src/fmt.rs +++ b/library/alloc/src/fmt.rs @@ -354,7 +354,7 @@ //! sign := '+' | '-' //! width := count //! precision := count | '*' -//! type := '?' | 'x?' | 'X?' | identifier +//! type := '?' | 'x?' | 'X?' | 'o' | 'x' | 'X' | 'p' | 'b' | 'e' | 'E' //! count := parameter | integer //! parameter := argument '$' //! ``` diff --git a/tests/ui/fmt/format-string-wrong-order.rs b/tests/ui/fmt/format-string-wrong-order.rs index 891279b97e4d..4d4e04ecd042 100644 --- a/tests/ui/fmt/format-string-wrong-order.rs +++ b/tests/ui/fmt/format-string-wrong-order.rs @@ -13,9 +13,11 @@ fn main() { format!("{?:#?}", bar); //~^ ERROR invalid format string: expected format parameter to occur after `:` format!("Hello {<5:}!", "x"); - //~^ ERROR invalid format string: expected format parameter to occur after `:` + //~^ ERROR invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` format!("Hello {^5:}!", "x"); - //~^ ERROR invalid format string: expected format parameter to occur after `:` + //~^ ERROR invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` format!("Hello {>5:}!", "x"); - //~^ ERROR invalid format string: expected format parameter to occur after `:` + //~^ ERROR invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` + println!("{0:#X>18}", 12345); + //~^ ERROR invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` } diff --git a/tests/ui/fmt/format-string-wrong-order.stderr b/tests/ui/fmt/format-string-wrong-order.stderr index 7f017511761f..441ae6d2e505 100644 --- a/tests/ui/fmt/format-string-wrong-order.stderr +++ b/tests/ui/fmt/format-string-wrong-order.stderr @@ -50,23 +50,29 @@ LL | format!("{?:#?}", bar); | = note: `?` comes after `:`, try `:?` instead -error: invalid format string: expected format parameter to occur after `:` +error: invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` --> $DIR/format-string-wrong-order.rs:15:21 | LL | format!("Hello {<5:}!", "x"); | ^ expected `<` to occur after `:` in format string -error: invalid format string: expected format parameter to occur after `:` +error: invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` --> $DIR/format-string-wrong-order.rs:17:21 | LL | format!("Hello {^5:}!", "x"); | ^ expected `^` to occur after `:` in format string -error: invalid format string: expected format parameter to occur after `:` +error: invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` --> $DIR/format-string-wrong-order.rs:19:21 | LL | format!("Hello {>5:}!", "x"); | ^ expected `>` to occur after `:` in format string -error: aborting due to 9 previous errors +error: invalid format string: expected alignment specifier after `:` in format string; example: `{:>?}` + --> $DIR/format-string-wrong-order.rs:21:20 + | +LL | println!("{0:#X>18}", 12345); + | ^ expected `>` to occur after `:` in format string + +error: aborting due to 10 previous errors From c7e1885075b3ac9c3f3c0553dbf813f0d44d18e7 Mon Sep 17 00:00:00 2001 From: Waffle Lapkin Date: Fri, 25 Jul 2025 11:28:37 +0200 Subject: [PATCH 14/68] allow using `target_val!` with a rename --- compiler/rustc_target/src/spec/json.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/compiler/rustc_target/src/spec/json.rs b/compiler/rustc_target/src/spec/json.rs index f56a65d9c0ca..2ea7c4df881d 100644 --- a/compiler/rustc_target/src/spec/json.rs +++ b/compiler/rustc_target/src/spec/json.rs @@ -245,19 +245,17 @@ impl ToJson for Target { target.update_to_cli(); macro_rules! target_val { - ($attr:ident) => {{ - let name = (stringify!($attr)).replace("_", "-"); - d.insert(name, target.$attr.to_json()); + ($attr:ident) => { + target_val!($attr, (stringify!($attr)).replace("_", "-")) + }; + ($attr:ident, $json_name:expr) => {{ + let name = $json_name; + d.insert(name.into(), target.$attr.to_json()); }}; } macro_rules! target_option_val { - ($attr:ident) => {{ - let name = (stringify!($attr)).replace("_", "-"); - if default.$attr != target.$attr { - d.insert(name, target.$attr.to_json()); - } - }}; + ($attr:ident) => {{ target_option_val!($attr, (stringify!($attr)).replace("_", "-")) }}; ($attr:ident, $json_name:expr) => {{ let name = $json_name; if default.$attr != target.$attr { @@ -290,7 +288,7 @@ impl ToJson for Target { target_val!(llvm_target); target_val!(metadata); - d.insert("target-pointer-width".to_string(), self.pointer_width.to_string().to_json()); + target_val!(pointer_width, "target-pointer-width"); target_val!(arch); target_val!(data_layout); From 1f7efabdc6204b3a1cc9bfe612136f848f643949 Mon Sep 17 00:00:00 2001 From: Waffle Lapkin Date: Fri, 25 Jul 2025 11:28:37 +0200 Subject: [PATCH 15/68] turn pointer width into an integer in target.json --- compiler/rustc_abi/src/lib.rs | 2 +- compiler/rustc_ast_ir/src/lib.rs | 4 ++-- compiler/rustc_target/src/spec/json.rs | 7 ++----- compiler/rustc_target/src/spec/mod.rs | 2 +- tests/run-make/rust-lld-custom-target/custom-target.json | 2 +- tests/run-make/rustdoc-target-spec-json-path/target.json | 2 +- tests/run-make/target-specs/endianness-mismatch.json | 2 +- tests/run-make/target-specs/mismatching-data-layout.json | 2 +- tests/run-make/target-specs/my-awesome-platform.json | 2 +- tests/run-make/target-specs/my-incomplete-platform.json | 2 +- .../target-specs/my-x86_64-unknown-linux-gnu-platform.json | 2 +- tests/run-make/target-specs/require-explicit-cpu.json | 2 +- 12 files changed, 14 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_abi/src/lib.rs b/compiler/rustc_abi/src/lib.rs index 14e256b8045d..a1a0de5aaf8f 100644 --- a/compiler/rustc_abi/src/lib.rs +++ b/compiler/rustc_abi/src/lib.rs @@ -315,7 +315,7 @@ pub enum TargetDataLayoutErrors<'a> { MissingAlignment { cause: &'a str }, InvalidAlignment { cause: &'a str, err: AlignFromBytesError }, InconsistentTargetArchitecture { dl: &'a str, target: &'a str }, - InconsistentTargetPointerWidth { pointer_size: u64, target: u32 }, + InconsistentTargetPointerWidth { pointer_size: u64, target: u16 }, InvalidBitsSize { err: String }, UnknownPointerSpecification { err: String }, } diff --git a/compiler/rustc_ast_ir/src/lib.rs b/compiler/rustc_ast_ir/src/lib.rs index 8f2a37c12108..44837b1b4940 100644 --- a/compiler/rustc_ast_ir/src/lib.rs +++ b/compiler/rustc_ast_ir/src/lib.rs @@ -69,7 +69,7 @@ impl IntTy { }) } - pub fn normalize(&self, target_width: u32) -> Self { + pub fn normalize(&self, target_width: u16) -> Self { match self { IntTy::Isize => match target_width { 16 => IntTy::I16, @@ -148,7 +148,7 @@ impl UintTy { }) } - pub fn normalize(&self, target_width: u32) -> Self { + pub fn normalize(&self, target_width: u16) -> Self { match self { UintTy::Usize => match target_width { 16 => UintTy::U16, diff --git a/compiler/rustc_target/src/spec/json.rs b/compiler/rustc_target/src/spec/json.rs index 2ea7c4df881d..e9ae5734d5b2 100644 --- a/compiler/rustc_target/src/spec/json.rs +++ b/compiler/rustc_target/src/spec/json.rs @@ -25,10 +25,7 @@ impl Target { let mut base = Target { llvm_target: json.llvm_target, metadata: Default::default(), - pointer_width: json - .target_pointer_width - .parse() - .map_err(|err| format!("invalid target-pointer-width: {err}"))?, + pointer_width: json.target_pointer_width, data_layout: json.data_layout, arch: json.arch, options: Default::default(), @@ -461,7 +458,7 @@ struct TargetSpecJsonMetadata { #[serde(deny_unknown_fields)] struct TargetSpecJson { llvm_target: StaticCow, - target_pointer_width: String, + target_pointer_width: u16, data_layout: StaticCow, arch: StaticCow, diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index c53d92bee9d2..7873f03eb296 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -2327,7 +2327,7 @@ pub struct Target { /// Used for generating target documentation. pub metadata: TargetMetadata, /// Number of bits in a pointer. Influences the `target_pointer_width` `cfg` variable. - pub pointer_width: u32, + pub pointer_width: u16, /// Architecture to use for ABI considerations. Valid options include: "x86", /// "x86_64", "arm", "aarch64", "mips", "powerpc", "powerpc64", and others. pub arch: StaticCow, diff --git a/tests/run-make/rust-lld-custom-target/custom-target.json b/tests/run-make/rust-lld-custom-target/custom-target.json index e2c64cbdb43c..28c3dc6f387a 100644 --- a/tests/run-make/rust-lld-custom-target/custom-target.json +++ b/tests/run-make/rust-lld-custom-target/custom-target.json @@ -53,5 +53,5 @@ "target-family": [ "unix" ], - "target-pointer-width": "64" + "target-pointer-width": 64 } diff --git a/tests/run-make/rustdoc-target-spec-json-path/target.json b/tests/run-make/rustdoc-target-spec-json-path/target.json index d7e4cac57ae7..6d8fe8528c80 100644 --- a/tests/run-make/rustdoc-target-spec-json-path/target.json +++ b/tests/run-make/rustdoc-target-spec-json-path/target.json @@ -33,5 +33,5 @@ "thread" ], "target-family": "unix", - "target-pointer-width": "64" + "target-pointer-width": 64 } diff --git a/tests/run-make/target-specs/endianness-mismatch.json b/tests/run-make/target-specs/endianness-mismatch.json index cc03becc59af..d73ea1cbcfec 100644 --- a/tests/run-make/target-specs/endianness-mismatch.json +++ b/tests/run-make/target-specs/endianness-mismatch.json @@ -4,7 +4,7 @@ "linker-flavor": "gcc", "llvm-target": "x86_64-unknown-linux-gnu", "target-endian": "big", - "target-pointer-width": "64", + "target-pointer-width": 64, "arch": "x86_64", "os": "linux" } diff --git a/tests/run-make/target-specs/mismatching-data-layout.json b/tests/run-make/target-specs/mismatching-data-layout.json index d12caaad14a0..e948d4d2f99f 100644 --- a/tests/run-make/target-specs/mismatching-data-layout.json +++ b/tests/run-make/target-specs/mismatching-data-layout.json @@ -2,5 +2,5 @@ "arch": "x86_64", "data-layout": "e-m:e-i64:16:32:64", "llvm-target": "x86_64-unknown-unknown-gnu", - "target-pointer-width": "64" + "target-pointer-width": 64 } diff --git a/tests/run-make/target-specs/my-awesome-platform.json b/tests/run-make/target-specs/my-awesome-platform.json index d41038b84a86..732a6bacd157 100644 --- a/tests/run-make/target-specs/my-awesome-platform.json +++ b/tests/run-make/target-specs/my-awesome-platform.json @@ -3,7 +3,7 @@ "linker-flavor": "gcc", "llvm-target": "i686-unknown-linux-gnu", "target-endian": "little", - "target-pointer-width": "32", + "target-pointer-width": 32, "arch": "x86", "os": "linux" } diff --git a/tests/run-make/target-specs/my-incomplete-platform.json b/tests/run-make/target-specs/my-incomplete-platform.json index 8bdc4108f494..68dbfc62d1c2 100644 --- a/tests/run-make/target-specs/my-incomplete-platform.json +++ b/tests/run-make/target-specs/my-incomplete-platform.json @@ -2,7 +2,7 @@ "data-layout": "e-p:32:32-f64:32:64-i64:32:64-f80:32:32-n8:16:32", "linker-flavor": "gcc", "target-endian": "little", - "target-pointer-width": "32", + "target-pointer-width": 32, "arch": "x86", "os": "foo" } diff --git a/tests/run-make/target-specs/my-x86_64-unknown-linux-gnu-platform.json b/tests/run-make/target-specs/my-x86_64-unknown-linux-gnu-platform.json index 27833f1abdd9..008ea5483831 100644 --- a/tests/run-make/target-specs/my-x86_64-unknown-linux-gnu-platform.json +++ b/tests/run-make/target-specs/my-x86_64-unknown-linux-gnu-platform.json @@ -4,7 +4,7 @@ "linker-flavor": "gcc", "llvm-target": "x86_64-unknown-linux-gnu", "target-endian": "little", - "target-pointer-width": "64", + "target-pointer-width": 64, "arch": "x86_64", "os": "linux" } diff --git a/tests/run-make/target-specs/require-explicit-cpu.json b/tests/run-make/target-specs/require-explicit-cpu.json index 9744bca168e2..4f23b644d8cf 100644 --- a/tests/run-make/target-specs/require-explicit-cpu.json +++ b/tests/run-make/target-specs/require-explicit-cpu.json @@ -3,7 +3,7 @@ "linker-flavor": "gcc", "llvm-target": "i686-unknown-linux-gnu", "target-endian": "little", - "target-pointer-width": "32", + "target-pointer-width": 32, "arch": "x86", "os": "linux", "need-explicit-cpu": true From aa0887b223d3af4570f2a6f31c9d85d792a7b4de Mon Sep 17 00:00:00 2001 From: Waffle Lapkin Date: Fri, 25 Jul 2025 15:12:44 +0200 Subject: [PATCH 16/68] accept integer `target-pointer-width` in compiletest --- src/tools/compiletest/src/common.rs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/tools/compiletest/src/common.rs b/src/tools/compiletest/src/common.rs index 7fc80c1edb15..645ed474b604 100644 --- a/src/tools/compiletest/src/common.rs +++ b/src/tools/compiletest/src/common.rs @@ -6,7 +6,6 @@ use std::sync::OnceLock; use build_helper::git::GitConfig; use camino::{Utf8Path, Utf8PathBuf}; use semver::Version; -use serde::de::{Deserialize, Deserializer, Error as _}; use crate::executor::{ColorConfig, OutputFormat}; use crate::fatal; @@ -1080,7 +1079,7 @@ pub struct TargetCfg { pub(crate) abi: String, #[serde(rename = "target-family", default)] pub(crate) families: Vec, - #[serde(rename = "target-pointer-width", deserialize_with = "serde_parse_u32")] + #[serde(rename = "target-pointer-width")] pub(crate) pointer_width: u32, #[serde(rename = "target-endian", default)] endian: Endian, @@ -1190,11 +1189,6 @@ fn query_rustc_output(config: &Config, args: &[&str], envs: HashMap>(deserializer: D) -> Result { - let string = String::deserialize(deserializer)?; - string.parse().map_err(D::Error::custom) -} - #[derive(Debug, Clone)] pub struct TestPaths { pub file: Utf8PathBuf, // e.g., compile-test/foo/bar/baz.rs From 6a3187af13e59beb00f6eaff8c4c9da19b382d36 Mon Sep 17 00:00:00 2001 From: Waffle Lapkin Date: Thu, 31 Jul 2025 19:03:22 +0200 Subject: [PATCH 17/68] fix target-pointer-width in tests --- .../rustc_codegen_gcc/target_specs/m68k-unknown-linux-gnu.json | 2 +- compiler/rustc_target/src/tests.rs | 2 +- library/compiler-builtins/etc/thumbv7em-none-eabi-renamed.json | 2 +- src/tools/miri/tests/x86_64-unknown-kernel.json | 2 +- tests/ui/check-cfg/my-awesome-platform.json | 2 +- tests/ui/codegen/mismatched-data-layout.json | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_codegen_gcc/target_specs/m68k-unknown-linux-gnu.json b/compiler/rustc_codegen_gcc/target_specs/m68k-unknown-linux-gnu.json index 95ea06106fb1..b13b640a7c7e 100644 --- a/compiler/rustc_codegen_gcc/target_specs/m68k-unknown-linux-gnu.json +++ b/compiler/rustc_codegen_gcc/target_specs/m68k-unknown-linux-gnu.json @@ -22,5 +22,5 @@ "unix" ], "target-mcount": "_mcount", - "target-pointer-width": "32" + "target-pointer-width": 32 } diff --git a/compiler/rustc_target/src/tests.rs b/compiler/rustc_target/src/tests.rs index ee847a84007f..a2692ea6be5e 100644 --- a/compiler/rustc_target/src/tests.rs +++ b/compiler/rustc_target/src/tests.rs @@ -7,7 +7,7 @@ fn report_unused_fields() { "arch": "powerpc64", "data-layout": "e-m:e-i64:64-n32:64", "llvm-target": "powerpc64le-elf", - "target-pointer-width": "64", + "target-pointer-width": 64, "code-mode": "foo" } "#; diff --git a/library/compiler-builtins/etc/thumbv7em-none-eabi-renamed.json b/library/compiler-builtins/etc/thumbv7em-none-eabi-renamed.json index 81273d44e496..6369bbe25477 100644 --- a/library/compiler-builtins/etc/thumbv7em-none-eabi-renamed.json +++ b/library/compiler-builtins/etc/thumbv7em-none-eabi-renamed.json @@ -19,5 +19,5 @@ }, "panic-strategy": "abort", "relocation-model": "static", - "target-pointer-width": "32" + "target-pointer-width": 32 } diff --git a/src/tools/miri/tests/x86_64-unknown-kernel.json b/src/tools/miri/tests/x86_64-unknown-kernel.json index a5eaceb4f68e..0f8032c39d56 100644 --- a/src/tools/miri/tests/x86_64-unknown-kernel.json +++ b/src/tools/miri/tests/x86_64-unknown-kernel.json @@ -1,7 +1,7 @@ { "llvm-target": "x86_64-unknown-none", "target-endian": "little", - "target-pointer-width": "64", + "target-pointer-width": 64, "target-c-int-width": 32, "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", "arch": "x86_64", diff --git a/tests/ui/check-cfg/my-awesome-platform.json b/tests/ui/check-cfg/my-awesome-platform.json index 4c16d06c7b7d..3a1f6b1a54c6 100644 --- a/tests/ui/check-cfg/my-awesome-platform.json +++ b/tests/ui/check-cfg/my-awesome-platform.json @@ -3,7 +3,7 @@ "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128", "arch": "x86_64", "target-endian": "little", - "target-pointer-width": "64", + "target-pointer-width": 64, "os": "ericos", "linker-flavor": "ld.lld", "linker": "rust-lld", diff --git a/tests/ui/codegen/mismatched-data-layout.json b/tests/ui/codegen/mismatched-data-layout.json index f8c510c18636..a1b222010abd 100644 --- a/tests/ui/codegen/mismatched-data-layout.json +++ b/tests/ui/codegen/mismatched-data-layout.json @@ -3,7 +3,7 @@ "data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128", "arch": "x86_64", "target-endian": "little", - "target-pointer-width": "64", + "target-pointer-width": 64, "os": "none", "linker-flavor": "ld.lld", "linker": "rust-lld", From 2d8cb59784711f503170011461c23385042a974d Mon Sep 17 00:00:00 2001 From: Miguel Ojeda Date: Thu, 21 Aug 2025 20:18:00 +0200 Subject: [PATCH 18/68] CI: rfl: support Rust >= 1.91.0 target spec To unblock the Rust CI in PR [1], use a temporary commit from Rust for Linux that supports the future target spec format. Link: https://github.com/rust-lang/rust/pull/144443 [1] Signed-off-by: Miguel Ojeda --- src/ci/docker/scripts/rfl-build.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ci/docker/scripts/rfl-build.sh b/src/ci/docker/scripts/rfl-build.sh index 8acc5040a2fc..70ff5d0d2c72 100755 --- a/src/ci/docker/scripts/rfl-build.sh +++ b/src/ci/docker/scripts/rfl-build.sh @@ -2,7 +2,8 @@ set -euo pipefail -LINUX_VERSION=v6.16-rc1 +# https://github.com/rust-lang/rust/pull/144443 +LINUX_VERSION=7770d51bce622b13195b2d3c85407282fc9c27e5 # Build rustc, rustdoc, cargo, clippy-driver and rustfmt ../x.py build --stage 2 library rustdoc clippy rustfmt From f29327ff6093bc09c445028bd05a172e570e8486 Mon Sep 17 00:00:00 2001 From: gonzalobg <65027571+gonzalobg@users.noreply.github.com> Date: Wed, 27 Aug 2025 16:21:07 +0200 Subject: [PATCH 19/68] Clarify that align_offset overaligns The current documentation is not clear whether adding `a` to a pointer overaligns (align up) or underaligns (align down). It should say this explicitly. --- library/core/src/ptr/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/library/core/src/ptr/mod.rs b/library/core/src/ptr/mod.rs index 6fc85a83e179..6b94088cb567 100644 --- a/library/core/src/ptr/mod.rs +++ b/library/core/src/ptr/mod.rs @@ -2166,10 +2166,9 @@ pub unsafe fn write_volatile(dst: *mut T, src: T) { } } -/// Align pointer `p`. +/// Calculate an element-offset that increases a pointer's alignment. /// -/// Calculate offset (in terms of elements of `size_of::()` stride) that has to be applied -/// to pointer `p` so that pointer `p` would get aligned to `a`. +/// Calculate an element-offset (not byte-offset) that when added to a given pointer `p`, increases `p`'s alignment to at least the given alignment `a`. /// /// # Safety /// `a` must be a power of two. From 60734115899e5658c63965edecab8ec4df570767 Mon Sep 17 00:00:00 2001 From: no92 Date: Thu, 28 Aug 2025 23:15:54 +0200 Subject: [PATCH 20/68] bootstrap: bump cc-rs to 1.2.28 --- src/bootstrap/Cargo.lock | 4 ++-- src/bootstrap/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bootstrap/Cargo.lock b/src/bootstrap/Cargo.lock index be29e77e572a..1535d0870333 100644 --- a/src/bootstrap/Cargo.lock +++ b/src/bootstrap/Cargo.lock @@ -95,9 +95,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.23" +version = "1.2.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f4ac86a9e5bc1e2b3449ab9d7d3a6a405e3d1bb28d7b9be8614f55846ae3766" +checksum = "4ad45f4f74e4e20eaa392913b7b33a7091c87e59628f4dd27888205ad888843c" dependencies = [ "shlex", ] diff --git a/src/bootstrap/Cargo.toml b/src/bootstrap/Cargo.toml index cd5a60187e51..c71755844654 100644 --- a/src/bootstrap/Cargo.toml +++ b/src/bootstrap/Cargo.toml @@ -32,7 +32,7 @@ test = false # Most of the time updating these dependencies requires modifications to the # bootstrap codebase(e.g., https://github.com/rust-lang/rust/issues/124565); # otherwise, some targets will fail. That's why these dependencies are explicitly pinned. -cc = "=1.2.23" +cc = "=1.2.28" cmake = "=0.1.54" build_helper = { path = "../build_helper" } From 6577a0ffdbe20f0c50537c34aa3214e026fe1904 Mon Sep 17 00:00:00 2001 From: no92 Date: Thu, 28 Aug 2025 23:15:55 +0200 Subject: [PATCH 21/68] compiler: Add `{x86_64,aarch64,riscv64gc}-unknown-managarm-mlibc` targets Co-authored-by: Dennis Bonke --- .../src/spec/base/managarm_mlibc.rs | 17 +++++++++++++ compiler/rustc_target/src/spec/base/mod.rs | 1 + compiler/rustc_target/src/spec/mod.rs | 4 ++++ .../targets/aarch64_unknown_managarm_mlibc.rs | 22 +++++++++++++++++ .../riscv64gc_unknown_managarm_mlibc.rs | 24 +++++++++++++++++++ .../targets/x86_64_unknown_managarm_mlibc.rs | 24 +++++++++++++++++++ src/tools/build-manifest/src/main.rs | 3 +++ tests/assembly-llvm/targets/targets-elf.rs | 9 +++++++ .../auxiliary/used_pre_main_constructor.rs | 1 + tests/ui/check-cfg/cfg-crate-features.stderr | 2 +- tests/ui/check-cfg/well-known-values.stderr | 6 ++--- 11 files changed, 109 insertions(+), 4 deletions(-) create mode 100644 compiler/rustc_target/src/spec/base/managarm_mlibc.rs create mode 100644 compiler/rustc_target/src/spec/targets/aarch64_unknown_managarm_mlibc.rs create mode 100644 compiler/rustc_target/src/spec/targets/riscv64gc_unknown_managarm_mlibc.rs create mode 100644 compiler/rustc_target/src/spec/targets/x86_64_unknown_managarm_mlibc.rs diff --git a/compiler/rustc_target/src/spec/base/managarm_mlibc.rs b/compiler/rustc_target/src/spec/base/managarm_mlibc.rs new file mode 100644 index 000000000000..da3856b212d9 --- /dev/null +++ b/compiler/rustc_target/src/spec/base/managarm_mlibc.rs @@ -0,0 +1,17 @@ +use crate::spec::{RelroLevel, TargetOptions, cvs}; + +pub(crate) fn opts() -> TargetOptions { + TargetOptions { + os: "managarm".into(), + env: "mlibc".into(), + dynamic_linking: true, + executables: true, + families: cvs!["unix"], + has_rpath: true, + position_independent_executables: true, + relro_level: RelroLevel::Full, + has_thread_local: true, + crt_static_respected: true, + ..Default::default() + } +} diff --git a/compiler/rustc_target/src/spec/base/mod.rs b/compiler/rustc_target/src/spec/base/mod.rs index b368d93f0072..be15da7329d7 100644 --- a/compiler/rustc_target/src/spec/base/mod.rs +++ b/compiler/rustc_target/src/spec/base/mod.rs @@ -20,6 +20,7 @@ pub(crate) mod linux_ohos; pub(crate) mod linux_uclibc; pub(crate) mod linux_wasm; pub(crate) mod lynxos178; +pub(crate) mod managarm_mlibc; pub(crate) mod msvc; pub(crate) mod netbsd; pub(crate) mod nto_qnx; diff --git a/compiler/rustc_target/src/spec/mod.rs b/compiler/rustc_target/src/spec/mod.rs index c53d92bee9d2..e97a9b9c776d 100644 --- a/compiler/rustc_target/src/spec/mod.rs +++ b/compiler/rustc_target/src/spec/mod.rs @@ -2032,6 +2032,10 @@ supported_targets! { ("i586-unknown-redox", i586_unknown_redox), ("x86_64-unknown-redox", x86_64_unknown_redox), + ("x86_64-unknown-managarm-mlibc", x86_64_unknown_managarm_mlibc), + ("aarch64-unknown-managarm-mlibc", aarch64_unknown_managarm_mlibc), + ("riscv64gc-unknown-managarm-mlibc", riscv64gc_unknown_managarm_mlibc), + ("i386-apple-ios", i386_apple_ios), ("x86_64-apple-ios", x86_64_apple_ios), ("aarch64-apple-ios", aarch64_apple_ios), diff --git a/compiler/rustc_target/src/spec/targets/aarch64_unknown_managarm_mlibc.rs b/compiler/rustc_target/src/spec/targets/aarch64_unknown_managarm_mlibc.rs new file mode 100644 index 000000000000..1fa9d7131c50 --- /dev/null +++ b/compiler/rustc_target/src/spec/targets/aarch64_unknown_managarm_mlibc.rs @@ -0,0 +1,22 @@ +use crate::spec::{StackProbeType, Target, base}; + +pub(crate) fn target() -> Target { + let mut base = base::managarm_mlibc::opts(); + base.max_atomic_width = Some(128); + base.stack_probes = StackProbeType::Inline; + base.features = "+v8a".into(); + + Target { + llvm_target: "aarch64-unknown-managarm-mlibc".into(), + metadata: crate::spec::TargetMetadata { + description: Some("managarm/aarch64".into()), + tier: Some(3), + host_tools: Some(false), + std: Some(false), + }, + pointer_width: 64, + data_layout: "e-m:e-p270:32:32-p271:32:32-p272:64:64-i8:8:32-i16:16:32-i64:64-i128:128-n32:64-S128-Fn32".into(), + arch: "aarch64".into(), + options: base + } +} diff --git a/compiler/rustc_target/src/spec/targets/riscv64gc_unknown_managarm_mlibc.rs b/compiler/rustc_target/src/spec/targets/riscv64gc_unknown_managarm_mlibc.rs new file mode 100644 index 000000000000..abf28310634a --- /dev/null +++ b/compiler/rustc_target/src/spec/targets/riscv64gc_unknown_managarm_mlibc.rs @@ -0,0 +1,24 @@ +use crate::spec::{CodeModel, Target, TargetOptions, base}; + +pub(crate) fn target() -> Target { + Target { + llvm_target: "riscv64-unknown-managarm-mlibc".into(), + metadata: crate::spec::TargetMetadata { + description: Some("managarm/riscv64".into()), + tier: Some(3), + host_tools: Some(false), + std: Some(false), + }, + pointer_width: 64, + data_layout: "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128".into(), + arch: "riscv64".into(), + options: TargetOptions { + code_model: Some(CodeModel::Medium), + cpu: "generic-rv64".into(), + features: "+m,+a,+f,+d,+c".into(), + llvm_abiname: "lp64d".into(), + max_atomic_width: Some(64), + ..base::managarm_mlibc::opts() + }, + } +} diff --git a/compiler/rustc_target/src/spec/targets/x86_64_unknown_managarm_mlibc.rs b/compiler/rustc_target/src/spec/targets/x86_64_unknown_managarm_mlibc.rs new file mode 100644 index 000000000000..359e38cb8007 --- /dev/null +++ b/compiler/rustc_target/src/spec/targets/x86_64_unknown_managarm_mlibc.rs @@ -0,0 +1,24 @@ +use crate::spec::{Cc, LinkerFlavor, Lld, StackProbeType, Target, base}; + +pub(crate) fn target() -> Target { + let mut base = base::managarm_mlibc::opts(); + base.cpu = "x86-64".into(); + base.max_atomic_width = Some(64); + base.add_pre_link_args(LinkerFlavor::Gnu(Cc::Yes, Lld::No), &["-m64"]); + base.stack_probes = StackProbeType::Inline; + + Target { + llvm_target: "x86_64-unknown-managarm-mlibc".into(), + metadata: crate::spec::TargetMetadata { + description: Some("managarm/amd64".into()), + tier: Some(3), + host_tools: Some(false), + std: Some(false), + }, + pointer_width: 64, + data_layout: + "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128".into(), + arch: "x86_64".into(), + options: base, + } +} diff --git a/src/tools/build-manifest/src/main.rs b/src/tools/build-manifest/src/main.rs index 8f88ab10bf76..2890d26d3f44 100644 --- a/src/tools/build-manifest/src/main.rs +++ b/src/tools/build-manifest/src/main.rs @@ -71,6 +71,7 @@ static TARGETS: &[&str] = &[ "aarch64-unknown-none-softfloat", "aarch64-unknown-redox", "aarch64-unknown-uefi", + "aarch64-unknown-managarm-mlibc", "amdgcn-amd-amdhsa", "arm64e-apple-darwin", "arm64e-apple-ios", @@ -155,6 +156,7 @@ static TARGETS: &[&str] = &[ "riscv64gc-unknown-none-elf", "riscv64gc-unknown-linux-gnu", "riscv64gc-unknown-linux-musl", + "riscv64gc-unknown-managarm-mlibc", "s390x-unknown-linux-gnu", "sparc64-unknown-linux-gnu", "sparcv9-sun-solaris", @@ -194,6 +196,7 @@ static TARGETS: &[&str] = &[ "x86_64-unknown-redox", "x86_64-unknown-hermit", "x86_64-unknown-uefi", + "x86_64-unknown-managarm-mlibc", ]; /// This allows the manifest to contain rust-docs for hosts that don't build diff --git a/tests/assembly-llvm/targets/targets-elf.rs b/tests/assembly-llvm/targets/targets-elf.rs index c935a75a6901..b5c116cdfef0 100644 --- a/tests/assembly-llvm/targets/targets-elf.rs +++ b/tests/assembly-llvm/targets/targets-elf.rs @@ -52,6 +52,9 @@ //@ revisions: aarch64_unknown_linux_ohos //@ [aarch64_unknown_linux_ohos] compile-flags: --target aarch64-unknown-linux-ohos //@ [aarch64_unknown_linux_ohos] needs-llvm-components: aarch64 +//@ revisions: aarch64_unknown_managarm_mlibc +//@ [aarch64_unknown_managarm_mlibc] compile-flags: --target aarch64-unknown-managarm-mlibc +//@ [aarch64_unknown_managarm_mlibc] needs-llvm-components: aarch64 //@ revisions: aarch64_unknown_netbsd //@ [aarch64_unknown_netbsd] compile-flags: --target aarch64-unknown-netbsd //@ [aarch64_unknown_netbsd] needs-llvm-components: aarch64 @@ -490,6 +493,9 @@ //@ revisions: riscv64gc_unknown_linux_musl //@ [riscv64gc_unknown_linux_musl] compile-flags: --target riscv64gc-unknown-linux-musl //@ [riscv64gc_unknown_linux_musl] needs-llvm-components: riscv +//@ revisions: riscv64gc_unknown_managarm_mlibc +//@ [riscv64gc_unknown_managarm_mlibc] compile-flags: --target riscv64gc-unknown-managarm-mlibc +//@ [riscv64gc_unknown_managarm_mlibc] needs-llvm-components: riscv //@ revisions: riscv64gc_unknown_netbsd //@ [riscv64gc_unknown_netbsd] compile-flags: --target riscv64gc-unknown-netbsd //@ [riscv64gc_unknown_netbsd] needs-llvm-components: riscv @@ -649,6 +655,9 @@ //@ revisions: x86_64_unknown_linux_none //@ [x86_64_unknown_linux_none] compile-flags: --target x86_64-unknown-linux-none //@ [x86_64_unknown_linux_none] needs-llvm-components: x86 +//@ revisions: x86_64_unknown_managarm_mlibc +//@ [x86_64_unknown_managarm_mlibc] compile-flags: --target x86_64-unknown-managarm-mlibc +//@ [x86_64_unknown_managarm_mlibc] needs-llvm-components: x86 //@ revisions: x86_64_unknown_netbsd //@ [x86_64_unknown_netbsd] compile-flags: --target x86_64-unknown-netbsd //@ [x86_64_unknown_netbsd] needs-llvm-components: x86 diff --git a/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs b/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs index 8ea7a9cb4b5a..f93a2aae5a1e 100644 --- a/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs +++ b/tests/ui/attributes/auxiliary/used_pre_main_constructor.rs @@ -20,6 +20,7 @@ target_os = "nto", target_os = "openbsd", target_os = "fuchsia", + target_os = "managarm", ), link_section = ".init_array" )] diff --git a/tests/ui/check-cfg/cfg-crate-features.stderr b/tests/ui/check-cfg/cfg-crate-features.stderr index d69a24f3f645..6b2e628e12ea 100644 --- a/tests/ui/check-cfg/cfg-crate-features.stderr +++ b/tests/ui/check-cfg/cfg-crate-features.stderr @@ -24,7 +24,7 @@ warning: unexpected `cfg` condition value: `does_not_exist` LL | #![cfg(not(target(os = "does_not_exist")))] | ^^^^^^^^^^^^^^^^^^^^^ | - = note: expected values for `target_os` are: `aix`, `amdhsa`, `android`, `cuda`, `cygwin`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `lynxos178`, `macos`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `psx`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, `tvos`, and `uefi` and 10 more + = note: expected values for `target_os` are: `aix`, `amdhsa`, `android`, `cuda`, `cygwin`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `lynxos178`, `macos`, `managarm`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `psx`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, and `tvos` and 11 more = note: see for more information about checking conditional configuration = note: `#[warn(unexpected_cfgs)]` on by default diff --git a/tests/ui/check-cfg/well-known-values.stderr b/tests/ui/check-cfg/well-known-values.stderr index 18e038a442ee..6490fc63fd76 100644 --- a/tests/ui/check-cfg/well-known-values.stderr +++ b/tests/ui/check-cfg/well-known-values.stderr @@ -156,7 +156,7 @@ warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE` LL | target_env = "_UNEXPECTED_VALUE", | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: expected values for `target_env` are: ``, `gnu`, `macabi`, `msvc`, `musl`, `newlib`, `nto70`, `nto71`, `nto71_iosock`, `nto80`, `ohos`, `p1`, `p2`, `relibc`, `sgx`, `sim`, `uclibc`, and `v5` + = note: expected values for `target_env` are: ``, `gnu`, `macabi`, `mlibc`, `msvc`, `musl`, `newlib`, `nto70`, `nto71`, `nto71_iosock`, `nto80`, `ohos`, `p1`, `p2`, `relibc`, `sgx`, `sim`, `uclibc`, and `v5` = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE` @@ -201,7 +201,7 @@ warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE` LL | target_os = "_UNEXPECTED_VALUE", | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: expected values for `target_os` are: `aix`, `amdhsa`, `android`, `cuda`, `cygwin`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `lynxos178`, `macos`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `psx`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, `tvos`, `uefi`, `unknown`, `vexos`, `visionos`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`, and `zkvm` + = note: expected values for `target_os` are: `aix`, `amdhsa`, `android`, `cuda`, `cygwin`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `lynxos178`, `macos`, `managarm`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `psx`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, `tvos`, `uefi`, `unknown`, `vexos`, `visionos`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`, and `zkvm` = note: see for more information about checking conditional configuration warning: unexpected `cfg` condition value: `_UNEXPECTED_VALUE` @@ -274,7 +274,7 @@ LL | #[cfg(target_os = "linuz")] // testing that we suggest `linux` | | | help: there is a expected value with a similar name: `"linux"` | - = note: expected values for `target_os` are: `aix`, `amdhsa`, `android`, `cuda`, `cygwin`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `lynxos178`, `macos`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `psx`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, `tvos`, `uefi`, `unknown`, `vexos`, `visionos`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`, and `zkvm` + = note: expected values for `target_os` are: `aix`, `amdhsa`, `android`, `cuda`, `cygwin`, `dragonfly`, `emscripten`, `espidf`, `freebsd`, `fuchsia`, `haiku`, `hermit`, `horizon`, `hurd`, `illumos`, `ios`, `l4re`, `linux`, `lynxos178`, `macos`, `managarm`, `netbsd`, `none`, `nto`, `nuttx`, `openbsd`, `psp`, `psx`, `redox`, `rtems`, `solaris`, `solid_asp3`, `teeos`, `trusty`, `tvos`, `uefi`, `unknown`, `vexos`, `visionos`, `vita`, `vxworks`, `wasi`, `watchos`, `windows`, `xous`, and `zkvm` = note: see for more information about checking conditional configuration warning: 28 warnings emitted From 8434d3810b938b2236180a67886cf79ee5d9efe4 Mon Sep 17 00:00:00 2001 From: no92 Date: Thu, 28 Aug 2025 23:15:55 +0200 Subject: [PATCH 22/68] doc: Add `*-unknown-managarm-mlibc` documentation --- src/doc/rustc/src/SUMMARY.md | 1 + src/doc/rustc/src/platform-support.md | 3 ++ .../rustc/src/platform-support/managarm.md | 53 +++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 src/doc/rustc/src/platform-support/managarm.md diff --git a/src/doc/rustc/src/SUMMARY.md b/src/doc/rustc/src/SUMMARY.md index e0d637a2a67f..8e378e53e518 100644 --- a/src/doc/rustc/src/SUMMARY.md +++ b/src/doc/rustc/src/SUMMARY.md @@ -116,6 +116,7 @@ - [\*-unikraft-linux-musl](platform-support/unikraft-linux-musl.md) - [\*-unknown-hermit](platform-support/hermit.md) - [\*-unknown-freebsd](platform-support/freebsd.md) + - [\*-unknown-managarm-mlibc](platform-support/managarm.md) - [\*-unknown-netbsd\*](platform-support/netbsd.md) - [\*-unknown-openbsd](platform-support/openbsd.md) - [\*-unknown-redox](platform-support/redox.md) diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md index edfc2db7d6f6..dfe193e6e8ad 100644 --- a/src/doc/rustc/src/platform-support.md +++ b/src/doc/rustc/src/platform-support.md @@ -258,6 +258,7 @@ target | std | host | notes [`aarch64-unknown-hermit`](platform-support/hermit.md) | ✓ | | ARM64 Hermit [`aarch64-unknown-illumos`](platform-support/illumos.md) | ✓ | ✓ | ARM64 illumos `aarch64-unknown-linux-gnu_ilp32` | ✓ | ✓ | ARM64 Linux (ILP32 ABI) +[`aarch64-unknown-managarm-mlibc`](platform-support/managarm.md) | ? | | ARM64 Managarm [`aarch64-unknown-netbsd`](platform-support/netbsd.md) | ✓ | ✓ | ARM64 NetBSD [`aarch64-unknown-nto-qnx700`](platform-support/nto-qnx.md) | ? | | ARM64 QNX Neutrino 7.0 RTOS | [`aarch64-unknown-nto-qnx710`](platform-support/nto-qnx.md) | ✓ | | ARM64 QNX Neutrino 7.1 RTOS with default network stack (io-pkt) | @@ -388,6 +389,7 @@ target | std | host | notes `riscv64gc-unknown-freebsd` | ? | | RISC-V FreeBSD `riscv64gc-unknown-fuchsia` | ? | | RISC-V Fuchsia [`riscv64gc-unknown-hermit`](platform-support/hermit.md) | ✓ | | RISC-V Hermit +[`riscv64gc-unknown-managarm-mlibc`](platform-support/managarm.md) | ? | | RISC-V Managarm [`riscv64gc-unknown-netbsd`](platform-support/netbsd.md) | ✓ | ✓ | RISC-V NetBSD [`riscv64gc-unknown-nuttx-elf`](platform-support/nuttx.md) | ✓ | | RISC-V 64bit with NuttX [`riscv64gc-unknown-openbsd`](platform-support/openbsd.md) | ✓ | ✓ | OpenBSD/riscv64 @@ -428,6 +430,7 @@ target | std | host | notes [`x86_64-unknown-hurd-gnu`](platform-support/hurd.md) | ✓ | ✓ | 64-bit GNU/Hurd `x86_64-unknown-l4re-uclibc` | ? | | [`x86_64-unknown-linux-none`](platform-support/x86_64-unknown-linux-none.md) | * | | 64-bit Linux with no libc +[`x86_64-unknown-managarm-mlibc`](platform-support/managarm.md) | ? | | x86_64 Managarm [`x86_64-unknown-openbsd`](platform-support/openbsd.md) | ✓ | ✓ | 64-bit OpenBSD [`x86_64-unknown-trusty`](platform-support/trusty.md) | ✓ | | `x86_64-uwp-windows-gnu` | ✓ | | diff --git a/src/doc/rustc/src/platform-support/managarm.md b/src/doc/rustc/src/platform-support/managarm.md new file mode 100644 index 000000000000..aa2d5a7ac23e --- /dev/null +++ b/src/doc/rustc/src/platform-support/managarm.md @@ -0,0 +1,53 @@ +# `*-unknown-managarm-mlibc` + +**Tier: 3** + +## Target Maintainers + +- [@no92](https://github.com/no92) +- [@64](https://github.com/64) +- [@Dennisbonke](https://github.com/Dennisbonke) + +## Requirements + +This target is cross-compiled. There is currently no support for `std` yet. It generates binaries in the ELF format. Currently, we support the `x86_64`, `aarch64` and `riscv64gc` architectures. The examples below `$ARCH` should be substituted for one of the supported architectures. + +## Building the target + +Managarm has upstream support in LLVM since the release of 21.1.0. + +Set up your `bootstrap.toml` like this: + +```toml +change-id = 142379 + +[llvm] +targets = "X86;AArch64;RISCV" +download-ci-llvm = false + +[build] +target = ["$ARCH-unknown-managarm-mlibc", "x86_64-unknown-linux-gnu"] + +[target.x86_64-unknown-linux-gnu] +llvm-config = "/path/to/your/llvm/bin/llvm-config" + +[target.$ARCH-unknown-managarm-mlibc] +llvm-config = "/path/to/your/llvm/bin/llvm-config" +``` + +## Building Rust programs + +Build a `$ARCH-managarm-gcc` using our [gcc fork](https://github.com/managarm/gcc). + +```toml +[build] +rustc = "/path/to/the/rust-prefix/bin/rustc" +target = "$ARCH-unknown-managarm-mlibc" + +[target.$ARCH-unknown-managarm-mlibc] +linker = "/path/to/the/managarm-gcc/bin/$ARCH-managarm-gcc" +``` + +## Testing + +This target does not support running the Rust testsuite yet. From 8134a10ec76e7c357d207ea83b215a15af63519c Mon Sep 17 00:00:00 2001 From: actuallylost Date: Fri, 29 Aug 2025 23:58:32 +0300 Subject: [PATCH 23/68] Add `Duration::from_nanos_u128` Tracking issue: RUST-139201 Co-authored-by: omanirudh --- library/core/src/time.rs | 36 +++++++++++++++++++++++++++++++++ library/coretests/tests/lib.rs | 1 + library/coretests/tests/time.rs | 19 +++++++++++++++++ 3 files changed, 56 insertions(+) diff --git a/library/core/src/time.rs b/library/core/src/time.rs index f37e47f132d5..d205bc376f12 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -308,6 +308,42 @@ impl Duration { Duration { secs, nanos: subsec_nanos } } + /// Creates a new `Duration` from the specified number of nanoseconds. + /// + /// # Panics + /// + /// Panics if the given number of nanoseconds is greater than [`Duration::MAX`]. + /// + /// # Examples + /// + /// ``` + /// #![feature(duration_from_nanos_u128)] + /// use std::time::Duration; + /// + /// let nanos = 10_u128.pow(24) + 321; + /// let duration = Duration::from_nanos_u128(nanos); + /// + /// assert_eq!(10_u64.pow(15), duration.as_secs()); + /// assert_eq!(321, duration.subsec_nanos()); + /// ``` + #[unstable(feature = "duration_from_nanos_u128", issue = "139201")] + // This is necessary because of const `try_from`, but can be removed if a trait-free impl is used instead + #[rustc_const_unstable(feature = "duration_from_nanos_u128", issue = "139201")] + #[must_use] + #[inline] + #[track_caller] + pub const fn from_nanos_u128(nanos: u128) -> Duration { + const NANOS_PER_SEC: u128 = self::NANOS_PER_SEC as u128; + let Ok(secs) = u64::try_from(nanos / NANOS_PER_SEC) else { + panic!("overflow in `Duration::from_nanos_u128`"); + }; + let subsec_nanos = (nanos % NANOS_PER_SEC) as u32; + // SAFETY: x % 1_000_000_000 < 1_000_000_000 also, subsec_nanos >= 0 since u128 >=0 and u32 >=0 + let subsec_nanos = unsafe { Nanoseconds::new_unchecked(subsec_nanos) }; + + Duration { secs: secs as u64, nanos: subsec_nanos } + } + /// Creates a new `Duration` from the specified number of weeks. /// /// # Panics diff --git a/library/coretests/tests/lib.rs b/library/coretests/tests/lib.rs index b9b768f29d78..c95643f0c8e3 100644 --- a/library/coretests/tests/lib.rs +++ b/library/coretests/tests/lib.rs @@ -36,6 +36,7 @@ #![feature(drop_guard)] #![feature(duration_constants)] #![feature(duration_constructors)] +#![feature(duration_from_nanos_u128)] #![feature(error_generic_member_access)] #![feature(exact_div)] #![feature(exact_size_is_empty)] diff --git a/library/coretests/tests/time.rs b/library/coretests/tests/time.rs index bb98e59bf5a2..fb3c50f9bde9 100644 --- a/library/coretests/tests/time.rs +++ b/library/coretests/tests/time.rs @@ -45,6 +45,14 @@ fn from_weeks_overflow() { let _ = Duration::from_weeks(overflow); } +#[test] +#[should_panic] +fn from_nanos_u128_overflow() { + let nanos_per_sec: u128 = 1_000_000_000; + let overflow = (u64::MAX as u128 * nanos_per_sec) + (nanos_per_sec - 1) + 1; + let _ = Duration::from_nanos_u128(overflow); +} + #[test] fn constructor_weeks() { assert_eq!(Duration::from_weeks(1), Duration::from_secs(7 * 24 * 60 * 60)); @@ -81,6 +89,8 @@ fn secs() { assert_eq!(Duration::from_micros(1_000_001).as_secs(), 1); assert_eq!(Duration::from_nanos(999_999_999).as_secs(), 0); assert_eq!(Duration::from_nanos(1_000_000_001).as_secs(), 1); + assert_eq!(Duration::from_nanos_u128(999_999_999).as_secs(), 0); + assert_eq!(Duration::from_nanos_u128(1_000_000_001).as_secs(), 1); } #[test] @@ -95,6 +105,8 @@ fn millis() { assert_eq!(Duration::from_micros(1_001_000).subsec_millis(), 1); assert_eq!(Duration::from_nanos(999_999_999).subsec_millis(), 999); assert_eq!(Duration::from_nanos(1_001_000_000).subsec_millis(), 1); + assert_eq!(Duration::from_nanos_u128(999_999_999).subsec_millis(), 999); + assert_eq!(Duration::from_nanos_u128(1_001_000_001).subsec_millis(), 1); } #[test] @@ -109,6 +121,8 @@ fn micros() { assert_eq!(Duration::from_micros(1_000_001).subsec_micros(), 1); assert_eq!(Duration::from_nanos(999_999_999).subsec_micros(), 999_999); assert_eq!(Duration::from_nanos(1_000_001_000).subsec_micros(), 1); + assert_eq!(Duration::from_nanos_u128(999_999_999).subsec_micros(), 999_999); + assert_eq!(Duration::from_nanos_u128(1_000_001_000).subsec_micros(), 1); } #[test] @@ -123,6 +137,8 @@ fn nanos() { assert_eq!(Duration::from_micros(1_000_001).subsec_nanos(), 1000); assert_eq!(Duration::from_nanos(999_999_999).subsec_nanos(), 999_999_999); assert_eq!(Duration::from_nanos(1_000_000_001).subsec_nanos(), 1); + assert_eq!(Duration::from_nanos_u128(999_999_999).subsec_nanos(), 999_999_999); + assert_eq!(Duration::from_nanos_u128(1_000_000_001).subsec_nanos(), 1); } #[test] @@ -520,6 +536,9 @@ fn duration_const() { const FROM_NANOS: Duration = Duration::from_nanos(1_000_000_000); assert_eq!(FROM_NANOS, Duration::SECOND); + const FROM_NANOS_U128: Duration = Duration::from_nanos_u128(NANOS); + assert_eq!(FROM_NANOS_U128, Duration::SECOND); + const MAX: Duration = Duration::new(u64::MAX, 999_999_999); const CHECKED_ADD: Option = MAX.checked_add(Duration::SECOND); From 2c21b884a59814836cbd9386492d9ba71039fc75 Mon Sep 17 00:00:00 2001 From: "Chai T. Rex" Date: Sat, 23 Aug 2025 17:53:27 -0400 Subject: [PATCH 24/68] Optimize `.ilog({2,10})` to `.ilog{2,10}()` Inform compiler of optimizations when the base is known at compile time and there's a cheaper method available: * `{integer}.checked_ilog(2)` -> `{integer}.checked_ilog2()` * `{integer}.checked_ilog(10)` -> `{integer}.checked_ilog10()` * `{integer}.ilog(2)` -> `{integer}.ilog2()` * `{integer}.ilog(10)` -> `{integer}.ilog10()` --- library/core/src/num/uint_macros.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs index 10d9498d15e4..acf4ca9a287c 100644 --- a/library/core/src/num/uint_macros.rs +++ b/library/core/src/num/uint_macros.rs @@ -1491,6 +1491,20 @@ macro_rules! uint_impl { without modifying the original"] #[inline] pub const fn checked_ilog(self, base: Self) -> Option { + // Inform compiler of optimizations when the base is known at + // compile time and there's a cheaper method available. + // + // Note: Like all optimizations, this is not guaranteed to be + // applied by the compiler. If you want those specific bases, + // use `.checked_ilog2()` or `.checked_ilog10()` directly. + if core::intrinsics::is_val_statically_known(base) { + if base == 2 { + return self.checked_ilog2(); + } else if base == 10 { + return self.checked_ilog10(); + } + } + if self <= 0 || base <= 1 { None } else if self < base { From 539f8400e769ad6092b5fe1cac0ebe885e5543a8 Mon Sep 17 00:00:00 2001 From: Aurelia Molzer <5550310+HeroicKatora@users.noreply.github.com> Date: Sat, 30 Aug 2025 11:50:28 +0200 Subject: [PATCH 25/68] Clarify panic-drop test for select_unpredictable --- library/coretests/tests/hint.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/library/coretests/tests/hint.rs b/library/coretests/tests/hint.rs index 9ef5567681e9..24de27b24b80 100644 --- a/library/coretests/tests/hint.rs +++ b/library/coretests/tests/hint.rs @@ -23,7 +23,7 @@ fn select_unpredictable_drop() { } #[test] -#[should_panic] +#[should_panic = "message canary"] fn select_unpredictable_drop_on_panic() { use core::cell::Cell; @@ -37,7 +37,7 @@ fn select_unpredictable_drop_on_panic() { fn drop(&mut self) { let value = self.cell.get(); self.cell.set(self.write); - assert_eq!(value, self.expect); + assert_eq!(value, self.expect, "message canary"); } } @@ -55,6 +55,5 @@ fn select_unpredictable_drop_on_panic() { // 3. `armed` drops during unwind, writes 0 and does not panic as 0xdead == 0xdead // // If `selected` is not dropped, `armed` panics as 1 != 0xdead - let _unreachable = - core::hint::select_unpredictable(core::hint::black_box(true), selected, unselected); + let _unreachable = core::hint::select_unpredictable(true, selected, unselected); } From 354787b5ba8210070929e7454312c51da8e6f988 Mon Sep 17 00:00:00 2001 From: Aurelia Molzer <5550310+HeroicKatora@users.noreply.github.com> Date: Sat, 30 Aug 2025 11:58:48 +0200 Subject: [PATCH 26/68] Simplify select_unpredictable guard selection Instead of a tuple, select the dropped value and its guard with two separate calls to the intrinsic which makes both calls have a pointer-valued argument that should be simpler in codegen. Use the same condition on all (not an inverted condition) to clarify the intent of parallel selection. This should also be a simpler value-dependency chain if the guard is deduced unused (i.e. drop_in_place a noop for the type). --- library/core/src/hint.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/library/core/src/hint.rs b/library/core/src/hint.rs index 1bb1c5695087..852882aa7f4c 100644 --- a/library/core/src/hint.rs +++ b/library/core/src/hint.rs @@ -788,6 +788,7 @@ pub fn select_unpredictable(condition: bool, true_val: T, false_val: T) -> T let mut false_val = MaybeUninit::new(false_val); struct DropOnPanic { + // Invariant: valid pointer and points to an initialized `MaybeUninit`. inner: *mut MaybeUninit, } @@ -806,12 +807,11 @@ pub fn select_unpredictable(condition: bool, true_val: T, false_val: T) -> T // value that is not selected. unsafe { // Extract the selected value first, ensure it is dropped as well if dropping the unselected - // value panics. - let (guard, drop) = crate::intrinsics::select_unpredictable( - condition, - (true_ptr, false_ptr), - (false_ptr, true_ptr), - ); + // value panics. We construct a temporary by-pointer guard around the selected value while + // dropping the unselected value. Arguments overlap here, so we can not use mutable + // reference for these arguments. + let guard = crate::intrinsics::select_unpredictable(condition, true_ptr, false_ptr); + let drop = crate::intrinsics::select_unpredictable(condition, false_ptr, true_ptr); // SAFETY: both pointers are to valid `MaybeUninit`, in both variants they do not alias but // the two arguments we have selected from did alias each other. From ee9803a244d65a830a999f8066da2a70e1972d5e Mon Sep 17 00:00:00 2001 From: Aurelia Molzer <5550310+HeroicKatora@users.noreply.github.com> Date: Sat, 30 Aug 2025 13:00:02 +0200 Subject: [PATCH 27/68] Switch select_unpredictable guard to raw pointer --- library/core/src/hint.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/library/core/src/hint.rs b/library/core/src/hint.rs index 852882aa7f4c..89c64f6d28fe 100644 --- a/library/core/src/hint.rs +++ b/library/core/src/hint.rs @@ -788,19 +788,20 @@ pub fn select_unpredictable(condition: bool, true_val: T, false_val: T) -> T let mut false_val = MaybeUninit::new(false_val); struct DropOnPanic { - // Invariant: valid pointer and points to an initialized `MaybeUninit`. - inner: *mut MaybeUninit, + // Invariant: valid pointer and points to an initialized value that is not further used, + // i.e. it can be dropped by this guard. + inner: *mut T, } impl Drop for DropOnPanic { fn drop(&mut self) { // SAFETY: Must be guaranteed on construction of local type `DropOnPanic`. - unsafe { (*self.inner).assume_init_drop() } + unsafe { self.inner.drop_in_place() } } } - let true_ptr = (&mut true_val) as *mut _; - let false_ptr = (&mut false_val) as *mut _; + let true_ptr = true_val.as_mut_ptr(); + let false_ptr = false_val.as_mut_ptr(); // SAFETY: The value that is not selected is dropped, and the selected one // is returned. This is necessary because the intrinsic doesn't drop the @@ -813,10 +814,12 @@ pub fn select_unpredictable(condition: bool, true_val: T, false_val: T) -> T let guard = crate::intrinsics::select_unpredictable(condition, true_ptr, false_ptr); let drop = crate::intrinsics::select_unpredictable(condition, false_ptr, true_ptr); - // SAFETY: both pointers are to valid `MaybeUninit`, in both variants they do not alias but - // the two arguments we have selected from did alias each other. + // SAFETY: both pointers are well-aligned and point to initialized values inside a + // `MaybeUninit` each. In both possible values for `condition` the pointer `guard` and + // `drop` do not alias (even though the two argument pairs we have selected from did alias + // each other). let guard = DropOnPanic { inner: guard }; - (*drop).assume_init_drop(); + drop.drop_in_place(); crate::mem::forget(guard); // Note that it is important to use the values here. Reading from the pointer we got makes From fcd6f284a1d10f8b021b0aec90231f22cff20fbe Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Sat, 30 Aug 2025 13:44:13 +0200 Subject: [PATCH 28/68] Remove incorrect FIXME --- compiler/rustc_attr_parsing/src/attributes/deprecation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_attr_parsing/src/attributes/deprecation.rs b/compiler/rustc_attr_parsing/src/attributes/deprecation.rs index 31c698228ef4..f96477e28cd0 100644 --- a/compiler/rustc_attr_parsing/src/attributes/deprecation.rs +++ b/compiler/rustc_attr_parsing/src/attributes/deprecation.rs @@ -58,7 +58,7 @@ impl SingleAttributeParser for DeprecationParser { Allow(Target::AssocTy), Allow(Target::AssocConst), Allow(Target::Variant), - Allow(Target::Impl { of_trait: false }), //FIXME This does not make sense + Allow(Target::Impl { of_trait: false }), Allow(Target::Crate), Error(Target::WherePredicate), ]); From 0858b14e25eb9ed105ac25ac6ae756c94edc469b Mon Sep 17 00:00:00 2001 From: Vladimir Petrzhikovskii Date: Tue, 5 Aug 2025 16:34:11 +0200 Subject: [PATCH 29/68] std: clarify `OpenOptions` error for create without write access Previously, attempting to create/truncate a file without write/append access would result in platform-specific error messages: - Unix: "Invalid argument" - Windows: raw OS error code 87 These error codes look like system errors, which could waste hours of debugging for what is actually an API misuse issue. --- library/std/src/fs.rs | 7 +++- library/std/src/fs/tests.rs | 56 ++++++++++++++++++++++--------- library/std/src/sys/fs/unix.rs | 26 ++++++++++++-- library/std/src/sys/fs/windows.rs | 24 +++++++++++-- 4 files changed, 91 insertions(+), 22 deletions(-) diff --git a/library/std/src/fs.rs b/library/std/src/fs.rs index d9c9606fc1ce..9f90c989cc72 100644 --- a/library/std/src/fs.rs +++ b/library/std/src/fs.rs @@ -1614,6 +1614,10 @@ impl OpenOptions { /// See also [`std::fs::write()`][self::write] for a simple function to /// create a file with some given data. /// + /// # Errors + /// + /// If `.create(true)` is set without `.write(true)` or `.append(true)`, + /// calling [`open`](Self::open) will fail with [`InvalidInput`](io::ErrorKind::InvalidInput) error. /// # Examples /// /// ```no_run @@ -1685,7 +1689,8 @@ impl OpenOptions { /// * [`AlreadyExists`]: `create_new` was specified and the file already /// exists. /// * [`InvalidInput`]: Invalid combinations of open options (truncate - /// without write access, no access mode set, etc.). + /// without write access, create without write or append access, + /// no access mode set, etc.). /// /// The following errors don't match any existing [`io::ErrorKind`] at the moment: /// * One of the directory components of the specified file path diff --git a/library/std/src/fs/tests.rs b/library/std/src/fs/tests.rs index c81e3af2f0d4..5e51d5e52114 100644 --- a/library/std/src/fs/tests.rs +++ b/library/std/src/fs/tests.rs @@ -1265,12 +1265,7 @@ fn open_flavors() { let mut ra = OO::new(); ra.read(true).append(true); - #[cfg(windows)] - let invalid_options = 87; // ERROR_INVALID_PARAMETER - #[cfg(all(unix, not(target_os = "vxworks")))] - let invalid_options = "Invalid argument"; - #[cfg(target_os = "vxworks")] - let invalid_options = "invalid argument"; + let invalid_options = "creating or truncating a file requires write or append access"; // Test various combinations of creation modes and access modes. // @@ -1293,10 +1288,10 @@ fn open_flavors() { check!(c(&w).open(&tmpdir.join("a"))); // read-only - error!(c(&r).create_new(true).open(&tmpdir.join("b")), invalid_options); - error!(c(&r).create(true).truncate(true).open(&tmpdir.join("b")), invalid_options); - error!(c(&r).truncate(true).open(&tmpdir.join("b")), invalid_options); - error!(c(&r).create(true).open(&tmpdir.join("b")), invalid_options); + error_contains!(c(&r).create_new(true).open(&tmpdir.join("b")), invalid_options); + error_contains!(c(&r).create(true).truncate(true).open(&tmpdir.join("b")), invalid_options); + error_contains!(c(&r).truncate(true).open(&tmpdir.join("b")), invalid_options); + error_contains!(c(&r).create(true).open(&tmpdir.join("b")), invalid_options); check!(c(&r).open(&tmpdir.join("a"))); // try opening the file created with write_only // read-write @@ -1308,21 +1303,21 @@ fn open_flavors() { // append check!(c(&a).create_new(true).open(&tmpdir.join("d"))); - error!(c(&a).create(true).truncate(true).open(&tmpdir.join("d")), invalid_options); - error!(c(&a).truncate(true).open(&tmpdir.join("d")), invalid_options); + error_contains!(c(&a).create(true).truncate(true).open(&tmpdir.join("d")), invalid_options); + error_contains!(c(&a).truncate(true).open(&tmpdir.join("d")), invalid_options); check!(c(&a).create(true).open(&tmpdir.join("d"))); check!(c(&a).open(&tmpdir.join("d"))); // read-append check!(c(&ra).create_new(true).open(&tmpdir.join("e"))); - error!(c(&ra).create(true).truncate(true).open(&tmpdir.join("e")), invalid_options); - error!(c(&ra).truncate(true).open(&tmpdir.join("e")), invalid_options); + error_contains!(c(&ra).create(true).truncate(true).open(&tmpdir.join("e")), invalid_options); + error_contains!(c(&ra).truncate(true).open(&tmpdir.join("e")), invalid_options); check!(c(&ra).create(true).open(&tmpdir.join("e"))); check!(c(&ra).open(&tmpdir.join("e"))); // Test opening a file without setting an access mode let mut blank = OO::new(); - error!(blank.create(true).open(&tmpdir.join("f")), invalid_options); + error_contains!(blank.create(true).open(&tmpdir.join("f")), invalid_options); // Test write works check!(check!(File::create(&tmpdir.join("h"))).write("foobar".as_bytes())); @@ -2084,3 +2079,34 @@ fn test_rename_junction() { // Junction links are always absolute so we just check the file name is correct. assert_eq!(fs::read_link(&dest).unwrap().file_name(), Some(not_exist.as_os_str())); } + +#[test] +fn test_open_options_invalid_combinations() { + use crate::fs::OpenOptions as OO; + + let test_cases: &[(fn() -> OO, &str)] = &[ + (|| OO::new().create(true).read(true).clone(), "create without write"), + (|| OO::new().create_new(true).read(true).clone(), "create_new without write"), + (|| OO::new().truncate(true).read(true).clone(), "truncate without write"), + (|| OO::new().truncate(true).append(true).clone(), "truncate with append"), + ]; + + for (make_opts, desc) in test_cases { + let opts = make_opts(); + let result = opts.open("nonexistent.txt"); + assert!(result.is_err(), "{desc} should fail"); + let err = result.unwrap_err(); + assert_eq!(err.kind(), ErrorKind::InvalidInput, "{desc} - wrong error kind"); + assert_eq!( + err.to_string(), + "creating or truncating a file requires write or append access", + "{desc} - wrong error message" + ); + } + + let result = OO::new().open("nonexistent.txt"); + assert!(result.is_err(), "no access mode should fail"); + let err = result.unwrap_err(); + assert_eq!(err.kind(), ErrorKind::InvalidInput); + assert_eq!(err.to_string(), "must specify at least one of read, write, or append access"); +} diff --git a/library/std/src/sys/fs/unix.rs b/library/std/src/sys/fs/unix.rs index 0d710a4b2a6c..a89c3bbacfbf 100644 --- a/library/std/src/sys/fs/unix.rs +++ b/library/std/src/sys/fs/unix.rs @@ -1123,7 +1123,21 @@ impl OpenOptions { (true, true, false) => Ok(libc::O_RDWR), (false, _, true) => Ok(libc::O_WRONLY | libc::O_APPEND), (true, _, true) => Ok(libc::O_RDWR | libc::O_APPEND), - (false, false, false) => Err(Error::from_raw_os_error(libc::EINVAL)), + (false, false, false) => { + // If no access mode is set, check if any creation flags are set + // to provide a more descriptive error message + if self.create || self.create_new || self.truncate { + Err(io::Error::new( + io::ErrorKind::InvalidInput, + "creating or truncating a file requires write or append access", + )) + } else { + Err(io::Error::new( + io::ErrorKind::InvalidInput, + "must specify at least one of read, write, or append access", + )) + } + } } } @@ -1132,12 +1146,18 @@ impl OpenOptions { (true, false) => {} (false, false) => { if self.truncate || self.create || self.create_new { - return Err(Error::from_raw_os_error(libc::EINVAL)); + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "creating or truncating a file requires write or append access", + )); } } (_, true) => { if self.truncate && !self.create_new { - return Err(Error::from_raw_os_error(libc::EINVAL)); + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "creating or truncating a file requires write or append access", + )); } } } diff --git a/library/std/src/sys/fs/windows.rs b/library/std/src/sys/fs/windows.rs index bb3e4bc30ca9..bac278f7c8f5 100644 --- a/library/std/src/sys/fs/windows.rs +++ b/library/std/src/sys/fs/windows.rs @@ -258,7 +258,19 @@ impl OpenOptions { Ok(c::GENERIC_READ | (c::FILE_GENERIC_WRITE & !c::FILE_WRITE_DATA)) } (false, false, false, None) => { - Err(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETER as i32)) + // If no access mode is set, check if any creation flags are set + // to provide a more descriptive error message + if self.create || self.create_new || self.truncate { + Err(io::Error::new( + io::ErrorKind::InvalidInput, + "creating or truncating a file requires write or append access", + )) + } else { + Err(io::Error::new( + io::ErrorKind::InvalidInput, + "must specify at least one of read, write, or append access", + )) + } } } } @@ -268,12 +280,18 @@ impl OpenOptions { (true, false) => {} (false, false) => { if self.truncate || self.create || self.create_new { - return Err(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETER as i32)); + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "creating or truncating a file requires write or append access", + )); } } (_, true) => { if self.truncate && !self.create_new { - return Err(Error::from_raw_os_error(c::ERROR_INVALID_PARAMETER as i32)); + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "creating or truncating a file requires write or append access", + )); } } } From a3ef8178d29b2cb61a3033e83fbba2b2a66d6716 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 8 Aug 2025 11:53:25 +0200 Subject: [PATCH 30/68] Add snapshot test for `tier-check` --- src/bootstrap/src/core/builder/tests.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index b079117c5a7d..d16ab530c1ea 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2116,6 +2116,19 @@ mod snapshot { "); } + #[test] + fn test_tier_check() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("test") + .path("tier-check") + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 1 -> std 1 + "); + } + #[test] fn doc_all() { let ctx = TestCtx::new(); From 65b7cde18ca7c331e323ace7b5f4f78b8cca4c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 8 Aug 2025 11:55:07 +0200 Subject: [PATCH 31/68] Fixup `x test tier-check` --- src/bootstrap/src/core/build_steps/test.rs | 40 +++++++++++----------- src/bootstrap/src/core/builder/tests.rs | 2 +- src/bootstrap/src/lib.rs | 19 ++++++++++ 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 26b4aaa8b5bb..a8002cd293b6 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -3248,9 +3248,15 @@ impl Step for Bootstrap { } } +fn get_compiler_to_test(builder: &Builder<'_>, target: TargetSelection) -> Compiler { + builder.compiler(builder.top_stage, target) +} + +/// Tests the Platform Support page in the rustc book. +/// `test_compiler` is used to query the actual targets that are checked. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TierCheck { - pub compiler: Compiler, + test_compiler: Compiler, } impl Step for TierCheck { @@ -3263,42 +3269,36 @@ impl Step for TierCheck { } fn make_run(run: RunConfig<'_>) { - let compiler = run.builder.compiler_for( - run.builder.top_stage, - run.builder.build.host_target, - run.target, - ); - run.builder.ensure(TierCheck { compiler }); + run.builder + .ensure(TierCheck { test_compiler: get_compiler_to_test(run.builder, run.target) }); } - /// Tests the Platform Support page in the rustc book. fn run(self, builder: &Builder<'_>) { - builder.std(self.compiler, self.compiler.host); + let tool_build_compiler = builder.compiler(0, builder.host_target); + let mut cargo = tool::prepare_tool_cargo( builder, - self.compiler, - Mode::ToolStd, - self.compiler.host, + tool_build_compiler, + Mode::ToolBootstrap, + tool_build_compiler.host, Kind::Run, "src/tools/tier-check", SourceType::InTree, &[], ); cargo.arg(builder.src.join("src/doc/rustc/src/platform-support.md")); - cargo.arg(builder.rustc(self.compiler)); + cargo.arg(builder.rustc(self.test_compiler)); if builder.is_verbose() { cargo.arg("--verbose"); } - let _guard = builder.msg( - Kind::Test, - "platform support check", - None, - self.compiler, - self.compiler.host, - ); + let _guard = builder.msg_test("platform support check", self.test_compiler); BootstrapCommand::from(cargo).delay_failure().run(builder); } + + fn metadata(&self) -> Option { + Some(StepMetadata::test("tier-check", self.test_compiler.host)) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index d16ab530c1ea..e5fff459a236 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2125,7 +2125,7 @@ mod snapshot { .render_steps(), @r" [build] llvm [build] rustc 0 -> rustc 1 - [build] rustc 1 -> std 1 + [test] rustc 0 -> tier-check 1 "); } diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index b8ee83b20e46..1beeb16b44f3 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -1159,6 +1159,25 @@ impl Build { self.group(&msg) } + /// Return a `Group` guard for a [`Step`] that tests `what` with the given `stage` and `target` + /// (determined by `host_and_stage`). + /// Use this instead of [`Builder::msg`] when there is no clear `build_compiler` to be + /// determined. + /// + /// [`Step`]: crate::core::builder::Step + #[must_use = "Groups should not be dropped until the Step finishes running"] + #[track_caller] + fn msg_test( + &self, + what: impl Display, + host_and_stage: impl Into, + ) -> Option { + let HostAndStage { host, stage } = host_and_stage.into(); + let action = Kind::Test.description(); + let msg = format!("{action} stage{stage} {what} ({host})"); + self.group(&msg) + } + /// Return a `Group` guard for a [`Step`] that is only built once and isn't affected by `--stage`. /// /// [`Step`]: crate::core::builder::Step From 542e750e5159ea85e0b7b9bf1cb743011cf286cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 13:46:21 +0200 Subject: [PATCH 32/68] Forbid running tests on stage 0 unless `build.compiletest-allow-stage0` is enabled --- src/bootstrap/src/core/config/config.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index a8eb563015f5..450f325f1a97 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -1002,12 +1002,18 @@ impl Config { (0, Subcommand::Install) => { check_stage0("install"); } + (0, Subcommand::Test { .. }) if build_compiletest_allow_stage0 != Some(true) => { + eprintln!( + "ERROR: cannot test anything on stage 0. Use at least stage 1. If you want to run compiletest with an external stage0 toolchain, enable `build.compiletest-allow-stage0`." + ); + exit!(1); + } _ => {} } if flags_compile_time_deps && !matches!(flags_cmd, Subcommand::Check { .. }) { eprintln!( - "WARNING: Can't use --compile-time-deps with any subcommand other than check." + "ERROR: Can't use --compile-time-deps with any subcommand other than check." ); exit!(1); } From f0d15d33247c9f63671e0801f700ec6f2f73d7ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 13:58:12 +0200 Subject: [PATCH 33/68] Add snapshot test for `x test` --- src/bootstrap/src/core/builder/tests.rs | 72 ++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index e5fff459a236..851e043a1c8e 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2037,6 +2037,76 @@ mod snapshot { .render_steps(), @"[check] rustc 0 -> RunMakeSupport 1 "); } + #[test] + fn test_all() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("test") + .render_steps(), @r" + [build] rustc 0 -> Tidy 1 + [test] tidy <> + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 1 -> std 1 + [build] rustc 0 -> Compiletest 1 + [test] Ui + [test] Crashes + [build] rustc 0 -> CoverageDump 1 + [build] rustc 1 -> std 1 + [test] CodegenLlvm + [test] CodegenUnits + [test] AssemblyLlvm + [test] Incremental + [test] Debuginfo + [test] UiFullDeps + [build] rustdoc 1 + [test] Rustdoc + [test] CoverageRunRustdoc + [test] Pretty + [build] rustc 1 -> std 1 + [test] CrateLibrustc + [build] rustc 1 -> rustc 2 + [build] rustdoc 0 + [build] rustc 0 -> UnstableBookGen 1 + [build] rustc 0 -> Rustbook 1 + [doc] unstable-book (book) + [doc] book (book) + [doc] book/first-edition (book) + [doc] book/second-edition (book) + [doc] book/2018-edition (book) + [doc] rustc 0 -> standalone 1 + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 0 -> error-index 1 + [doc] rustc 0 -> error-index 1 + [doc] nomicon (book) + [doc] rustc 1 -> reference (book) 2 + [doc] rustdoc (book) + [doc] rust-by-example (book) + [build] rustc 0 -> LintDocs 1 + [doc] rustc (book) + [doc] cargo (book) + [doc] clippy (book) + [doc] embedded-book (book) + [doc] edition-guide (book) + [doc] style-guide (book) + [doc] rustc 0 -> releases 1 + [build] rustc 0 -> Linkchecker 1 + [test] tier-check + [doc] rustc (book) + [doc] rustc 1 -> std 1 crates=[] + [build] rustc 0 -> RustdocTheme 1 + [test] RustdocUi + [build] rustc 0 -> JsonDocCk 1 + [build] rustc 0 -> JsonDocLint 1 + [test] RustdocJson + [doc] rustc 0 -> rustc 1 + [build] rustc 0 -> HtmlChecker 1 + [build] rustc 0 -> RunMakeSupport 1 + [build] rustc 1 -> cargo 2 + [test] RunMake + "); + } + #[test] fn test_exclude() { let ctx = TestCtx::new(); @@ -2125,7 +2195,7 @@ mod snapshot { .render_steps(), @r" [build] llvm [build] rustc 0 -> rustc 1 - [test] rustc 0 -> tier-check 1 + [test] tier-check "); } From a347f8dcb6bb783e8b509284ad3bd18ff639da37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 14:12:16 +0200 Subject: [PATCH 34/68] Fix staging of `TestFloatParse` The tool wasn't useful for anything, it was only built as a part of the test, but we can just use `cargo test` and `cargo run` in the test, no need to (pre-)build the tool itself. --- src/bootstrap/src/core/build_steps/check.rs | 6 +- src/bootstrap/src/core/build_steps/test.rs | 92 +++++++++++++-------- src/bootstrap/src/core/build_steps/tool.rs | 37 +-------- 3 files changed, 61 insertions(+), 74 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/check.rs b/src/bootstrap/src/core/build_steps/check.rs index bebae893ee7f..a604e7c05859 100644 --- a/src/bootstrap/src/core/build_steps/check.rs +++ b/src/bootstrap/src/core/build_steps/check.rs @@ -8,8 +8,8 @@ use crate::core::build_steps::compile::{ }; use crate::core::build_steps::tool; use crate::core::build_steps::tool::{ - COMPILETEST_ALLOW_FEATURES, SourceType, ToolTargetBuildMode, get_tool_target_compiler, - prepare_tool_cargo, + COMPILETEST_ALLOW_FEATURES, SourceType, TEST_FLOAT_PARSE_ALLOW_FEATURES, ToolTargetBuildMode, + get_tool_target_compiler, prepare_tool_cargo, }; use crate::core::builder::{ self, Alias, Builder, Cargo, Kind, RunConfig, ShouldRun, Step, StepMetadata, crate_description, @@ -791,7 +791,7 @@ tool_check_step!(MiroptTestTools { tool_check_step!(TestFloatParse { path: "src/tools/test-float-parse", mode: |_builder| Mode::ToolStd, - allow_features: tool::TestFloatParse::ALLOW_FEATURES + allow_features: TEST_FLOAT_PARSE_ALLOW_FEATURES }); tool_check_step!(FeaturesStatusDump { path: "src/tools/features-status-dump", diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index a8002cd293b6..8d07874bbc51 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -18,8 +18,8 @@ use crate::core::build_steps::llvm::get_llvm_version; use crate::core::build_steps::run::get_completion_paths; use crate::core::build_steps::synthetic_targets::MirOptPanicAbortSyntheticTarget; use crate::core::build_steps::tool::{ - self, COMPILETEST_ALLOW_FEATURES, RustcPrivateCompilers, SourceType, Tool, ToolTargetBuildMode, - get_tool_target_compiler, + self, COMPILETEST_ALLOW_FEATURES, RustcPrivateCompilers, SourceType, + TEST_FLOAT_PARSE_ALLOW_FEATURES, Tool, ToolTargetBuildMode, get_tool_target_compiler, }; use crate::core::build_steps::toolstate::ToolState; use crate::core::build_steps::{compile, dist, llvm}; @@ -2886,6 +2886,7 @@ impl Step for Crate { } } +/// Run cargo tests for the rustdoc crate. /// Rustdoc is special in various ways, which is why this step is different from `Crate`. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct CrateRustdoc { @@ -2981,7 +2982,8 @@ impl Step for CrateRustdoc { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct CrateRustdocJsonTypes { - host: TargetSelection, + build_compiler: Compiler, + target: TargetSelection, } impl Step for CrateRustdocJsonTypes { @@ -2996,23 +2998,22 @@ impl Step for CrateRustdocJsonTypes { fn make_run(run: RunConfig<'_>) { let builder = run.builder; - builder.ensure(CrateRustdocJsonTypes { host: run.target }); + builder.ensure(CrateRustdocJsonTypes { + build_compiler: get_tool_target_compiler( + builder, + ToolTargetBuildMode::Build(run.target), + ), + target: run.target, + }); } fn run(self, builder: &Builder<'_>) { - let target = self.host; - - // Use the previous stage compiler to reuse the artifacts that are - // created when running compiletest for tests/rustdoc. If this used - // `compiler`, then it would cause rustdoc to be built *again*, which - // isn't really necessary. - let compiler = builder.compiler_for(builder.top_stage, target, target); - builder.ensure(compile::Rustc::new(compiler, target)); + let target = self.target; let cargo = tool::prepare_tool_cargo( builder, - compiler, - Mode::ToolRustc, + self.build_compiler, + Mode::ToolTarget, target, builder.kind, "src/rustdoc-json-types", @@ -3021,7 +3022,7 @@ impl Step for CrateRustdocJsonTypes { ); // FIXME: this looks very wrong, libtest doesn't accept `-C` arguments and the quotes are fishy. - let libtest_args = if self.host.contains("musl") { + let libtest_args = if target.contains("musl") { ["'-Ctarget-feature=-crt-static'"].as_slice() } else { &[] @@ -3705,14 +3706,35 @@ impl Step for CodegenGCC { } } +/// Get a build compiler that can be used to test the standard library (i.e. its stage will +/// correspond to the stage that we want to test). +fn get_test_build_compiler_for_std(builder: &Builder<'_>) -> Compiler { + if builder.top_stage == 0 { + eprintln!( + "ERROR: cannot run tests on stage 0. `build.compiletest-allow-stage0` only works for compiletest test suites." + ); + exit!(1); + } + builder.compiler(builder.top_stage, builder.host_target) +} + /// Test step that does two things: /// - Runs `cargo test` for the `src/tools/test-float-parse` tool. /// - Invokes the `test-float-parse` tool to test the standard library's /// float parsing routines. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TestFloatParse { - path: PathBuf, - host: TargetSelection, + /// The build compiler which will build and run unit tests of `test-float-parse`, and which will + /// build the `test-float-parse` tool itself. + /// + /// Note that the staging is a bit funny here, because this step essentially tests std, but it + /// also needs to build the tool. So if we test stage1 std, we build: + /// 1) stage1 rustc + /// 2) Use that to build stage1 libstd + /// 3) Use that to build and run *stage2* test-float-parse + build_compiler: Compiler, + /// Target for which we build std and test that std. + target: TargetSelection, } impl Step for TestFloatParse { @@ -3725,47 +3747,47 @@ impl Step for TestFloatParse { } fn make_run(run: RunConfig<'_>) { - for path in run.paths { - let path = path.assert_single_path().path.clone(); - run.builder.ensure(Self { path, host: run.target }); - } + run.builder.ensure(Self { + build_compiler: get_test_build_compiler_for_std(run.builder), + target: run.target, + }); } fn run(self, builder: &Builder<'_>) { - let bootstrap_host = builder.config.host_target; - let compiler = builder.compiler(builder.top_stage, bootstrap_host); - let path = self.path.to_str().unwrap(); - let crate_name = self.path.iter().next_back().unwrap().to_str().unwrap(); + let build_compiler = self.build_compiler; + let target = self.target; - builder.ensure(tool::TestFloatParse { host: self.host }); + // Build the standard library that will be tested, and a stdlib for host code + builder.std(build_compiler, target); + builder.std(build_compiler, builder.host_target); // Run any unit tests in the crate let mut cargo_test = tool::prepare_tool_cargo( builder, - compiler, + build_compiler, Mode::ToolStd, - bootstrap_host, + target, Kind::Test, - path, + "src/tools/test-float-parse", SourceType::InTree, &[], ); - cargo_test.allow_features(tool::TestFloatParse::ALLOW_FEATURES); + cargo_test.allow_features(TEST_FLOAT_PARSE_ALLOW_FEATURES); - run_cargo_test(cargo_test, &[], &[], crate_name, bootstrap_host, builder); + run_cargo_test(cargo_test, &[], &[], "test-float-parse", target, builder); // Run the actual parse tests. let mut cargo_run = tool::prepare_tool_cargo( builder, - compiler, + build_compiler, Mode::ToolStd, - bootstrap_host, + target, Kind::Run, - path, + "src/tools/test-float-parse", SourceType::InTree, &[], ); - cargo_run.allow_features(tool::TestFloatParse::ALLOW_FEATURES); + cargo_run.allow_features(TEST_FLOAT_PARSE_ALLOW_FEATURES); if !matches!(env::var("FLOAT_PARSE_TESTS_NO_SKIP_HUGE").as_deref(), Ok("1") | Ok("true")) { cargo_run.args(["--", "--skip-huge"]); diff --git a/src/bootstrap/src/core/build_steps/tool.rs b/src/bootstrap/src/core/build_steps/tool.rs index b62c9a906b79..e15b570a5a7a 100644 --- a/src/bootstrap/src/core/build_steps/tool.rs +++ b/src/bootstrap/src/core/build_steps/tool.rs @@ -1539,42 +1539,7 @@ tool_rustc_extended!(Rustfmt { add_bins_to_sysroot: ["rustfmt"] }); -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct TestFloatParse { - pub host: TargetSelection, -} - -impl TestFloatParse { - pub const ALLOW_FEATURES: &'static str = "f16,cfg_target_has_reliable_f16_f128"; -} - -impl Step for TestFloatParse { - type Output = ToolBuildResult; - const IS_HOST: bool = true; - const DEFAULT: bool = false; - - fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { - run.path("src/tools/test-float-parse") - } - - fn run(self, builder: &Builder<'_>) -> ToolBuildResult { - let bootstrap_host = builder.config.host_target; - let compiler = builder.compiler(builder.top_stage, bootstrap_host); - - builder.ensure(ToolBuild { - build_compiler: compiler, - target: bootstrap_host, - tool: "test-float-parse", - mode: Mode::ToolStd, - path: "src/tools/test-float-parse", - source_type: SourceType::InTree, - extra_features: Vec::new(), - allow_features: Self::ALLOW_FEATURES, - cargo_args: Vec::new(), - artifact_kind: ToolArtifactKind::Binary, - }) - } -} +pub const TEST_FLOAT_PARSE_ALLOW_FEATURES: &'static str = "f16,cfg_target_has_reliable_f16_f128"; impl Builder<'_> { /// Gets a `BootstrapCommand` which is ready to run `tool` in `stage` built for From 42fb65b5a9bb1ce5e176cdced3c09ce62b37d750 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 14:34:56 +0200 Subject: [PATCH 35/68] Remove `compiler_for` from `test::CodegenGCC` --- src/bootstrap/src/core/build_steps/test.rs | 82 ++++++++-------------- 1 file changed, 31 insertions(+), 51 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 8d07874bbc51..f6b9b6f70efa 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -3578,7 +3578,7 @@ impl Step for CodegenCranelift { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct CodegenGCC { - compiler: Compiler, + compilers: RustcPrivateCompilers, target: TargetSelection, } @@ -3594,7 +3594,7 @@ impl Step for CodegenGCC { fn make_run(run: RunConfig<'_>) { let builder = run.builder; let host = run.build_triple(); - let compiler = run.builder.compiler_for(run.builder.top_stage, host, host); + let compilers = RustcPrivateCompilers::new(run.builder, run.builder.top_stage, host); if builder.doc_tests == DocTests::Only { return; @@ -3623,68 +3623,41 @@ impl Step for CodegenGCC { return; } - builder.ensure(CodegenGCC { compiler, target: run.target }); + builder.ensure(CodegenGCC { compilers, target: run.target }); } fn run(self, builder: &Builder<'_>) { - let compiler = self.compiler; + let compilers = self.compilers; let target = self.target; let gcc = builder.ensure(Gcc { target }); builder.ensure( - compile::Std::new(compiler, target) + compile::Std::new(compilers.build_compiler(), target) .extra_rust_args(&["-Csymbol-mangling-version=v0", "-Cpanic=abort"]), ); - // If we're not doing a full bootstrap but we're testing a stage2 - // version of libstd, then what we're actually testing is the libstd - // produced in stage1. Reflect that here by updating the compiler that - // we're working with automatically. - let compiler = builder.compiler_for(compiler.stage, compiler.host, target); + let _msg = builder.msg_test("rustc_codegen_gcc", compilers.build_compiler()); - let build_cargo = || { - let mut cargo = builder::Cargo::new( - builder, - compiler, - Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works - SourceType::InTree, - target, - Kind::Run, - ); + let mut cargo = builder::Cargo::new( + builder, + compilers.build_compiler(), + Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works + SourceType::InTree, + target, + Kind::Run, + ); - cargo.current_dir(&builder.src.join("compiler/rustc_codegen_gcc")); - cargo - .arg("--manifest-path") - .arg(builder.src.join("compiler/rustc_codegen_gcc/build_system/Cargo.toml")); - compile::rustc_cargo_env(builder, &mut cargo, target); - add_cg_gcc_cargo_flags(&mut cargo, &gcc); + cargo.current_dir(&builder.src.join("compiler/rustc_codegen_gcc")); + cargo + .arg("--manifest-path") + .arg(builder.src.join("compiler/rustc_codegen_gcc/build_system/Cargo.toml")); + compile::rustc_cargo_env(builder, &mut cargo, target); + add_cg_gcc_cargo_flags(&mut cargo, &gcc); - // Avoid incremental cache issues when changing rustc - cargo.env("CARGO_BUILD_INCREMENTAL", "false"); - cargo.rustflag("-Cpanic=abort"); - - cargo - }; - - builder.info(&format!( - "{} GCC stage{} ({} -> {})", - Kind::Test.description(), - compiler.stage, - &compiler.host, - target - )); - let _time = helpers::timeit(builder); - - // FIXME: Uncomment the `prepare` command below once vendoring is implemented. - /* - let mut prepare_cargo = build_cargo(); - prepare_cargo.arg("--").arg("prepare"); - #[expect(deprecated)] - builder.config.try_run(&mut prepare_cargo.into()).unwrap(); - */ - - let mut cargo = build_cargo(); + // Avoid incremental cache issues when changing rustc + cargo.env("CARGO_BUILD_INCREMENTAL", "false"); + cargo.rustflag("-Cpanic=abort"); cargo // cg_gcc's build system ignores RUSTFLAGS. pass some flags through CG_RUSTFLAGS instead. @@ -3696,7 +3669,7 @@ impl Step for CodegenGCC { .arg("--gcc-path") .arg(gcc.libgccjit.parent().unwrap()) .arg("--out-dir") - .arg(builder.stage_out(compiler, Mode::ToolRustc).join("cg_gcc")) + .arg(builder.stage_out(compilers.build_compiler(), Mode::Codegen).join("cg_gcc")) .arg("--release") .arg("--mini-tests") .arg("--std-tests"); @@ -3704,6 +3677,13 @@ impl Step for CodegenGCC { cargo.into_cmd().run(builder); } + + fn metadata(&self) -> Option { + Some( + StepMetadata::test("rustc_codegen_gcc", self.target) + .built_by(self.compilers.build_compiler()), + ) + } } /// Get a build compiler that can be used to test the standard library (i.e. its stage will From 2ca5cb8c1683ad5c8d36e4464b2cfc56537b7bc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 14:59:34 +0200 Subject: [PATCH 36/68] Remove `compiler_for` from `test::CodegenCranelift` --- src/bootstrap/src/core/build_steps/test.rs | 84 +++++++++------------- 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index f6b9b6f70efa..e53638c81bdb 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -3450,7 +3450,7 @@ impl Step for TestHelpers { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct CodegenCranelift { - compiler: Compiler, + compilers: RustcPrivateCompilers, target: TargetSelection, } @@ -3466,7 +3466,7 @@ impl Step for CodegenCranelift { fn make_run(run: RunConfig<'_>) { let builder = run.builder; let host = run.build_triple(); - let compiler = run.builder.compiler_for(run.builder.top_stage, host, host); + let compilers = RustcPrivateCompilers::new(run.builder, run.builder.top_stage, host); if builder.doc_tests == DocTests::Only { return; @@ -3496,71 +3496,50 @@ impl Step for CodegenCranelift { return; } - builder.ensure(CodegenCranelift { compiler, target: run.target }); + builder.ensure(CodegenCranelift { compilers, target: run.target }); } fn run(self, builder: &Builder<'_>) { - let compiler = self.compiler; + let compilers = self.compilers; + let build_compiler = compilers.build_compiler(); + + // We need to run the cranelift tests with the compiler against cranelift links to, not with + // the build compiler. + let target_compiler = compilers.target_compiler(); let target = self.target; - builder.std(compiler, target); + builder.std(target_compiler, target); - // If we're not doing a full bootstrap but we're testing a stage2 - // version of libstd, then what we're actually testing is the libstd - // produced in stage1. Reflect that here by updating the compiler that - // we're working with automatically. - let compiler = builder.compiler_for(compiler.stage, compiler.host, target); + let mut cargo = builder::Cargo::new( + builder, + target_compiler, + Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works + SourceType::InTree, + target, + Kind::Run, + ); - let build_cargo = || { - let mut cargo = builder::Cargo::new( - builder, - compiler, - Mode::Codegen, // Must be codegen to ensure dlopen on compiled dylibs works - SourceType::InTree, - target, - Kind::Run, - ); + cargo.current_dir(&builder.src.join("compiler/rustc_codegen_cranelift")); + cargo + .arg("--manifest-path") + .arg(builder.src.join("compiler/rustc_codegen_cranelift/build_system/Cargo.toml")); + compile::rustc_cargo_env(builder, &mut cargo, target); - cargo.current_dir(&builder.src.join("compiler/rustc_codegen_cranelift")); - cargo - .arg("--manifest-path") - .arg(builder.src.join("compiler/rustc_codegen_cranelift/build_system/Cargo.toml")); - compile::rustc_cargo_env(builder, &mut cargo, target); + // Avoid incremental cache issues when changing rustc + cargo.env("CARGO_BUILD_INCREMENTAL", "false"); - // Avoid incremental cache issues when changing rustc - cargo.env("CARGO_BUILD_INCREMENTAL", "false"); - - cargo - }; - - builder.info(&format!( - "{} cranelift stage{} ({} -> {})", - Kind::Test.description(), - compiler.stage, - &compiler.host, - target - )); - let _time = helpers::timeit(builder); + let _guard = builder.msg_test("rustc_codegen_cranelift", target_compiler); // FIXME handle vendoring for source tarballs before removing the --skip-test below let download_dir = builder.out.join("cg_clif_download"); - // FIXME: Uncomment the `prepare` command below once vendoring is implemented. - /* - let mut prepare_cargo = build_cargo(); - prepare_cargo.arg("--").arg("prepare").arg("--download-dir").arg(&download_dir); - #[expect(deprecated)] - builder.config.try_run(&mut prepare_cargo.into()).unwrap(); - */ - - let mut cargo = build_cargo(); cargo .arg("--") .arg("test") .arg("--download-dir") .arg(&download_dir) .arg("--out-dir") - .arg(builder.stage_out(compiler, Mode::ToolRustc).join("cg_clif")) + .arg(builder.stage_out(build_compiler, Mode::Codegen).join("cg_clif")) .arg("--no-unstable-features") .arg("--use-backend") .arg("cranelift") @@ -3574,6 +3553,13 @@ impl Step for CodegenCranelift { cargo.into_cmd().run(builder); } + + fn metadata(&self) -> Option { + Some( + StepMetadata::test("rustc_codegen_cranelift", self.target) + .built_by(self.compilers.build_compiler()), + ) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -3637,7 +3623,7 @@ impl Step for CodegenGCC { .extra_rust_args(&["-Csymbol-mangling-version=v0", "-Cpanic=abort"]), ); - let _msg = builder.msg_test("rustc_codegen_gcc", compilers.build_compiler()); + let _guard = builder.msg_test("rustc_codegen_gcc", compilers.build_compiler()); let mut cargo = builder::Cargo::new( builder, From e759b97838f10b52163834315bf486d6972df3de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 15:04:47 +0200 Subject: [PATCH 37/68] Refactor `test::LintDocs` --- src/bootstrap/src/core/build_steps/doc.rs | 6 ++++- src/bootstrap/src/core/build_steps/test.rs | 31 +++++++++++++++++----- src/bootstrap/src/core/builder/tests.rs | 1 + 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs index 9ef1fee1fecc..678fe127879b 100644 --- a/src/bootstrap/src/core/build_steps/doc.rs +++ b/src/bootstrap/src/core/build_steps/doc.rs @@ -791,7 +791,11 @@ fn doc_std( } /// Prepare a compiler that will be able to document something for `target` at `stage`. -fn prepare_doc_compiler(builder: &Builder<'_>, target: TargetSelection, stage: u32) -> Compiler { +pub fn prepare_doc_compiler( + builder: &Builder<'_>, + target: TargetSelection, + stage: u32, +) -> Compiler { assert!(stage > 0, "Cannot document anything in stage 0"); let build_compiler = builder.compiler(stage - 1, builder.host_target); builder.std(build_compiler, target); diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index e53638c81bdb..7c4256e9836a 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -12,7 +12,7 @@ use std::{env, fs, iter}; use build_helper::exit; use crate::core::build_steps::compile::{Std, run_cargo}; -use crate::core::build_steps::doc::DocumentationFormat; +use crate::core::build_steps::doc::{DocumentationFormat, prepare_doc_compiler}; use crate::core::build_steps::gcc::{Gcc, add_cg_gcc_cargo_flags}; use crate::core::build_steps::llvm::get_llvm_version; use crate::core::build_steps::run::get_completion_paths; @@ -3304,8 +3304,8 @@ impl Step for TierCheck { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct LintDocs { - pub compiler: Compiler, - pub target: TargetSelection, + build_compiler: Compiler, + target: TargetSelection, } impl Step for LintDocs { @@ -3318,8 +3318,21 @@ impl Step for LintDocs { } fn make_run(run: RunConfig<'_>) { + // Bump the stage to 2, because the rustc book requires an in-tree compiler. + // At the same time, since this step is enabled by default, we don't want `x test` to fail + // in stage 1. + let stage = if run.builder.config.is_explicit_stage() || run.builder.top_stage >= 2 { + run.builder.top_stage + } else { + 2 + }; + run.builder.ensure(LintDocs { - compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.host_target), + build_compiler: prepare_doc_compiler( + run.builder, + run.builder.config.host_target, + stage, + ), target: run.target, }); } @@ -3327,8 +3340,14 @@ impl Step for LintDocs { /// Tests that the lint examples in the rustc book generate the correct /// lints and have the expected format. fn run(self, builder: &Builder<'_>) { - builder - .ensure(crate::core::build_steps::doc::RustcBook::validate(self.compiler, self.target)); + builder.ensure(crate::core::build_steps::doc::RustcBook::validate( + self.build_compiler, + self.target, + )); + } + + fn metadata(&self) -> Option { + Some(StepMetadata::test("lint-docs", self.target).built_by(self.build_compiler)) } } diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 851e043a1c8e..ba6dfdff17a0 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2093,6 +2093,7 @@ mod snapshot { [build] rustc 0 -> Linkchecker 1 [test] tier-check [doc] rustc (book) + [test] rustc 1 -> lint-docs 2 [doc] rustc 1 -> std 1 crates=[] [build] rustc 0 -> RustdocTheme 1 [test] RustdocUi From 0f2fc03068ae60eed918fab13ccd89ff120dd403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 15:21:59 +0200 Subject: [PATCH 38/68] Remove stage0 checking --- src/bootstrap/src/core/build_steps/test.rs | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 7c4256e9836a..9b27f0063924 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -3691,18 +3691,6 @@ impl Step for CodegenGCC { } } -/// Get a build compiler that can be used to test the standard library (i.e. its stage will -/// correspond to the stage that we want to test). -fn get_test_build_compiler_for_std(builder: &Builder<'_>) -> Compiler { - if builder.top_stage == 0 { - eprintln!( - "ERROR: cannot run tests on stage 0. `build.compiletest-allow-stage0` only works for compiletest test suites." - ); - exit!(1); - } - builder.compiler(builder.top_stage, builder.host_target) -} - /// Test step that does two things: /// - Runs `cargo test` for the `src/tools/test-float-parse` tool. /// - Invokes the `test-float-parse` tool to test the standard library's @@ -3732,10 +3720,8 @@ impl Step for TestFloatParse { } fn make_run(run: RunConfig<'_>) { - run.builder.ensure(Self { - build_compiler: get_test_build_compiler_for_std(run.builder), - target: run.target, - }); + run.builder + .ensure(Self { build_compiler: get_compiler_to_test(run.builder), target: run.target }); } fn run(self, builder: &Builder<'_>) { From 1d96ef8dc09ecc0dc6c531b0ba970cd6c4dd7f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 16:30:02 +0200 Subject: [PATCH 39/68] Small refactoring around `RemoteCopyLibs` --- src/bootstrap/src/core/build_steps/test.rs | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 9b27f0063924..46dafc79253a 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -1741,7 +1741,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the builder.std(compiler, target); } - builder.ensure(RemoteCopyLibs { compiler, target }); + builder.ensure(RemoteCopyLibs { build_compiler: compiler, target }); // compiletest currently has... a lot of arguments, so let's just pass all // of them! @@ -2842,7 +2842,7 @@ impl Step for Crate { // Also prepare a sysroot for the target. if !builder.config.is_host_target(target) { builder.ensure(compile::Std::new(compiler, target).force_recompile(true)); - builder.ensure(RemoteCopyLibs { compiler, target }); + builder.ensure(RemoteCopyLibs { build_compiler: compiler, target }); } // Build `cargo test` command @@ -3050,7 +3050,7 @@ impl Step for CrateRustdocJsonTypes { /// the build target (us) and the server is built for the target. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct RemoteCopyLibs { - compiler: Compiler, + build_compiler: Compiler, target: TargetSelection, } @@ -3062,18 +3062,17 @@ impl Step for RemoteCopyLibs { } fn run(self, builder: &Builder<'_>) { - let compiler = self.compiler; + let build_compiler = self.build_compiler; let target = self.target; if !builder.remote_tested(target) { return; } - builder.std(compiler, target); + builder.std(build_compiler, target); builder.info(&format!("REMOTE copy libs to emulator ({target})")); - let remote_test_server = - builder.ensure(tool::RemoteTestServer { build_compiler: compiler, target }); + let remote_test_server = builder.ensure(tool::RemoteTestServer { build_compiler, target }); // Spawn the emulator and wait for it to come online let tool = builder.tool_exe(Tool::RemoteTestClient); @@ -3088,7 +3087,7 @@ impl Step for RemoteCopyLibs { cmd.run(builder); // Push all our dylibs to the emulator - for f in t!(builder.sysroot_target_libdir(compiler, target).read_dir()) { + for f in t!(builder.sysroot_target_libdir(build_compiler, target).read_dir()) { let f = t!(f); if helpers::is_dylib(&f.path()) { command(&tool).arg("push").arg(f.path()).run(builder); @@ -3134,6 +3133,9 @@ impl Step for Distcheck { .map(|args| args.split(" ").map(|s| s.to_string()).collect::>()) .unwrap_or_default(); + // FIXME: unpack the source tarballs into a directory outside the source checkout, to + // ensure that it cannot access any local state + // Also ensure that it doesn't use download-ci-llvm command("tar") .arg("-xf") .arg(plain_src_tarball.tarball()) @@ -3720,8 +3722,10 @@ impl Step for TestFloatParse { } fn make_run(run: RunConfig<'_>) { - run.builder - .ensure(Self { build_compiler: get_compiler_to_test(run.builder), target: run.target }); + run.builder.ensure(Self { + build_compiler: get_compiler_to_test(run.builder, run.target), + target: run.target, + }); } fn run(self, builder: &Builder<'_>) { From 384a044f55f72341aaf472a490f8eb21cfaea5a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 16:48:02 +0200 Subject: [PATCH 40/68] Add metadata to a bunch of steps, rename variables and add comments --- src/bootstrap/src/core/build_steps/test.rs | 119 ++++++++++++++++----- src/bootstrap/src/core/builder/tests.rs | 11 ++ 2 files changed, 102 insertions(+), 28 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 46dafc79253a..8cc13ce7566b 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -98,6 +98,13 @@ impl Step for CrateBootstrap { let crate_name = path.rsplit_once('/').unwrap().1; run_cargo_test(cargo, &[], &[], crate_name, bootstrap_host, builder); } + + fn metadata(&self) -> Option { + Some( + StepMetadata::test("crate-bootstrap", self.host) + .with_metadata(self.path.as_path().to_string_lossy().to_string()), + ) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -173,6 +180,10 @@ You can skip linkcheck with --skip src/tools/linkchecker" fn make_run(run: RunConfig<'_>) { run.builder.ensure(Linkcheck { host: run.target }); } + + fn metadata(&self) -> Option { + Some(StepMetadata::test("link-check", self.host)) + } } fn check_if_tidy_is_installed(builder: &Builder<'_>) -> bool { @@ -221,6 +232,10 @@ impl Step for HtmlCheck { .arg(builder.doc_out(self.target)) .run(builder); } + + fn metadata(&self) -> Option { + Some(StepMetadata::test("html-check", self.target)) + } } /// Builds cargo and then runs the `src/tools/cargotest` tool, which checks out @@ -399,6 +414,10 @@ impl Step for Cargo { let _time = helpers::timeit(builder); add_flags_and_try_run_tests(builder, &mut cargo); } + + fn metadata(&self) -> Option { + Some(StepMetadata::test("cargo", self.host).built_by(self.build_compiler)) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -457,6 +476,13 @@ impl Step for RustAnalyzer { cargo.add_rustc_lib_path(builder); run_cargo_test(cargo, &[], &[], "rust-analyzer", host, builder); } + + fn metadata(&self) -> Option { + Some( + StepMetadata::test("rust-analyzer", self.compilers.target()) + .built_by(self.compilers.build_compiler()), + ) + } } /// Runs `cargo test` for rustfmt. @@ -508,6 +534,13 @@ impl Step for Rustfmt { run_cargo_test(cargo, &[], &[], "rustfmt", target, builder); } + + fn metadata(&self) -> Option { + Some( + StepMetadata::test("rustfmt", self.compilers.target()) + .built_by(self.compilers.build_compiler()), + ) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -887,6 +920,13 @@ impl Step for Clippy { crate::exit!(1); } } + + fn metadata(&self) -> Option { + Some( + StepMetadata::test("clippy", self.compilers.target()) + .built_by(self.compilers.build_compiler()), + ) + } } fn bin_path_for_cargo(builder: &Builder<'_>, compiler: Compiler) -> OsString { @@ -895,9 +935,11 @@ fn bin_path_for_cargo(builder: &Builder<'_>, compiler: Compiler) -> OsString { env::join_paths(iter::once(path).chain(env::split_paths(&old_path))).expect("") } +/// Run the rustdoc-themes tool to test a given compiler. #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct RustdocTheme { - pub compiler: Compiler, + /// The compiler (more accurately, its rustdoc) that we test. + test_compiler: Compiler, } impl Step for RustdocTheme { @@ -910,9 +952,9 @@ impl Step for RustdocTheme { } fn make_run(run: RunConfig<'_>) { - let compiler = run.builder.compiler(run.builder.top_stage, run.target); + let test_compiler = run.builder.compiler(run.builder.top_stage, run.target); - run.builder.ensure(RustdocTheme { compiler }); + run.builder.ensure(RustdocTheme { test_compiler }); } fn run(self, builder: &Builder<'_>) { @@ -920,21 +962,34 @@ impl Step for RustdocTheme { let mut cmd = builder.tool_cmd(Tool::RustdocTheme); cmd.arg(rustdoc.to_str().unwrap()) .arg(builder.src.join("src/librustdoc/html/static/css/rustdoc.css").to_str().unwrap()) - .env("RUSTC_STAGE", self.compiler.stage.to_string()) - .env("RUSTC_SYSROOT", builder.sysroot(self.compiler)) - .env("RUSTDOC_LIBDIR", builder.sysroot_target_libdir(self.compiler, self.compiler.host)) + .env("RUSTC_STAGE", self.test_compiler.stage.to_string()) + .env("RUSTC_SYSROOT", builder.sysroot(self.test_compiler)) + .env( + "RUSTDOC_LIBDIR", + builder.sysroot_target_libdir(self.test_compiler, self.test_compiler.host), + ) .env("CFG_RELEASE_CHANNEL", &builder.config.channel) - .env("RUSTDOC_REAL", builder.rustdoc_for_compiler(self.compiler)) + .env("RUSTDOC_REAL", builder.rustdoc_for_compiler(self.test_compiler)) .env("RUSTC_BOOTSTRAP", "1"); - cmd.args(linker_args(builder, self.compiler.host, LldThreads::No)); + cmd.args(linker_args(builder, self.test_compiler.host, LldThreads::No)); cmd.delay_failure().run(builder); } + + fn metadata(&self) -> Option { + Some( + StepMetadata::test("rustdoc-theme", self.test_compiler.host) + .stage(self.test_compiler.stage), + ) + } } +/// Test rustdoc JS for the standard library. #[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct RustdocJSStd { - pub target: TargetSelection, + /// Compiler that will build the standary library. + build_compiler: Compiler, + target: TargetSelection, } impl Step for RustdocJSStd { @@ -948,7 +1003,10 @@ impl Step for RustdocJSStd { } fn make_run(run: RunConfig<'_>) { - run.builder.ensure(RustdocJSStd { target: run.target }); + run.builder.ensure(RustdocJSStd { + build_compiler: run.builder.compiler(run.builder.top_stage, run.builder.host_target), + target: run.target, + }); } fn run(self, builder: &Builder<'_>) { @@ -976,19 +1034,18 @@ impl Step for RustdocJSStd { } } builder.ensure(crate::core::build_steps::doc::Std::from_build_compiler( - builder.compiler(builder.top_stage, builder.host_target), + self.build_compiler, self.target, DocumentationFormat::Html, )); - let _guard = builder.msg( - Kind::Test, - "rustdoc-js-std", - None, - (builder.config.host_target, builder.top_stage), - self.target, - ); + let _guard = + builder.msg(Kind::Test, "rustdoc-js-std", None, self.build_compiler, self.target); command.run(builder); } + + fn metadata(&self) -> Option { + Some(StepMetadata::test("rustdoc-js-std", self.target).stage(self.build_compiler.stage)) + } } #[derive(Debug, Clone, Hash, PartialEq, Eq)] @@ -1046,10 +1103,12 @@ fn get_browser_ui_test_version(builder: &Builder<'_>, npm: &Path) -> Option) { - let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple()); - run.builder.ensure(RustdocGUI { target: run.target, compiler }); + let test_compiler = run.builder.compiler(run.builder.top_stage, run.build_triple()); + run.builder.ensure(RustdocGUI { test_compiler, target: run.target }); } fn run(self, builder: &Builder<'_>) { - builder.std(self.compiler, self.target); + builder.std(self.test_compiler, self.target); let mut cmd = builder.tool_cmd(Tool::RustdocGUITest); @@ -1086,7 +1145,7 @@ impl Step for RustdocGUI { build_stamp::clear_if_dirty( builder, &out_dir, - &builder.rustdoc_for_compiler(self.compiler), + &builder.rustdoc_for_compiler(self.test_compiler), ); if let Some(src) = builder.config.src.to_str() { @@ -1103,10 +1162,10 @@ impl Step for RustdocGUI { cmd.arg("--jobs").arg(builder.jobs().to_string()); - cmd.env("RUSTDOC", builder.rustdoc_for_compiler(self.compiler)) - .env("RUSTC", builder.rustc(self.compiler)); + cmd.env("RUSTDOC", builder.rustdoc_for_compiler(self.test_compiler)) + .env("RUSTC", builder.rustc(self.test_compiler)); - add_rustdoc_cargo_linker_args(&mut cmd, builder, self.compiler.host, LldThreads::No); + add_rustdoc_cargo_linker_args(&mut cmd, builder, self.test_compiler.host, LldThreads::No); for path in &builder.paths { if let Some(p) = helpers::is_valid_test_suite_arg(path, "tests/rustdoc-gui", builder) { @@ -1133,9 +1192,13 @@ impl Step for RustdocGUI { } let _time = helpers::timeit(builder); - let _guard = builder.msg(Kind::Test, "rustdoc-gui", None, self.compiler, self.target); + let _guard = builder.msg_test("rustdoc-gui", (self.target, self.test_compiler.stage)); try_run_tests(builder, &mut cmd, true); } + + fn metadata(&self) -> Option { + Some(StepMetadata::test("rustdoc-gui", self.target).stage(self.test_compiler.stage)) + } } /// Runs `src/tools/tidy` and `cargo fmt --check` to detect various style diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index ba6dfdff17a0..921c40166541 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2067,6 +2067,10 @@ mod snapshot { [test] CrateLibrustc [build] rustc 1 -> rustc 2 [build] rustdoc 0 + [test] crate-bootstrap src/tools/coverage-dump + [test] crate-bootstrap src/tools/jsondoclint + [test] crate-bootstrap src/tools/replace-version-placeholder + [test] crate-bootstrap tidyselftest [build] rustc 0 -> UnstableBookGen 1 [build] rustc 0 -> Rustbook 1 [doc] unstable-book (book) @@ -2091,17 +2095,22 @@ mod snapshot { [doc] style-guide (book) [doc] rustc 0 -> releases 1 [build] rustc 0 -> Linkchecker 1 + [test] link-check [test] tier-check + [test] rustc 0 -> rust-analyzer 1 [doc] rustc (book) [test] rustc 1 -> lint-docs 2 [doc] rustc 1 -> std 1 crates=[] + [test] rustc 1 -> rustdoc-js-std 2 [build] rustc 0 -> RustdocTheme 1 + [test] rustdoc-theme 1 [test] RustdocUi [build] rustc 0 -> JsonDocCk 1 [build] rustc 0 -> JsonDocLint 1 [test] RustdocJson [doc] rustc 0 -> rustc 1 [build] rustc 0 -> HtmlChecker 1 + [test] html-check [build] rustc 0 -> RunMakeSupport 1 [build] rustc 1 -> cargo 2 [test] RunMake @@ -2148,6 +2157,7 @@ mod snapshot { [build] rustc 1 -> std 1 [build] rustdoc 1 [build] rustdoc 0 + [test] rustc 0 -> cargo 1 "); } @@ -2167,6 +2177,7 @@ mod snapshot { [build] rustc 2 -> std 2 [build] rustdoc 2 [build] rustdoc 1 + [test] rustc 1 -> cargo 2 "); } From 6651686949d5cb2d85be57e1563e24f31ecbd813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 16:48:49 +0200 Subject: [PATCH 41/68] Move the `test!` macro closer to its usages --- src/bootstrap/src/core/build_steps/test.rs | 140 ++++++++++----------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 8cc13ce7566b..878a649207b3 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -1316,76 +1316,6 @@ HELP: to skip test's attempt to check tidiness, pass `--skip src/tools/tidy` to } } -fn testdir(builder: &Builder<'_>, host: TargetSelection) -> PathBuf { - builder.out.join(host).join("test") -} - -/// Declares a test step that invokes compiletest on a particular test suite. -macro_rules! test { - ( - $( #[$attr:meta] )* // allow docstrings and attributes - $name:ident { - path: $path:expr, - mode: $mode:expr, - suite: $suite:expr, - default: $default:expr - $( , IS_HOST: $IS_HOST:expr )? // default: false - $( , compare_mode: $compare_mode:expr )? // default: None - $( , )? // optional trailing comma - } - ) => { - $( #[$attr] )* - #[derive(Debug, Clone, PartialEq, Eq, Hash)] - pub struct $name { - pub compiler: Compiler, - pub target: TargetSelection, - } - - impl Step for $name { - type Output = (); - const DEFAULT: bool = $default; - const IS_HOST: bool = (const { - #[allow(unused_assignments, unused_mut)] - let mut value = false; - $( value = $IS_HOST; )? - value - }); - - fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { - run.suite_path($path) - } - - fn make_run(run: RunConfig<'_>) { - let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple()); - - run.builder.ensure($name { compiler, target: run.target }); - } - - fn run(self, builder: &Builder<'_>) { - builder.ensure(Compiletest { - compiler: self.compiler, - target: self.target, - mode: $mode, - suite: $suite, - path: $path, - compare_mode: (const { - #[allow(unused_assignments, unused_mut)] - let mut value = None; - $( value = $compare_mode; )? - value - }), - }) - } - - fn metadata(&self) -> Option { - Some( - StepMetadata::test(stringify!($name), self.target) - ) - } - } - }; -} - /// Runs `cargo test` on the `src/tools/run-make-support` crate. /// That crate is used by run-make tests. #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1462,6 +1392,76 @@ impl Step for CrateBuildHelper { } } +fn testdir(builder: &Builder<'_>, host: TargetSelection) -> PathBuf { + builder.out.join(host).join("test") +} + +/// Declares a test step that invokes compiletest on a particular test suite. +macro_rules! test { + ( + $( #[$attr:meta] )* // allow docstrings and attributes + $name:ident { + path: $path:expr, + mode: $mode:expr, + suite: $suite:expr, + default: $default:expr + $( , IS_HOST: $IS_HOST:expr )? // default: false + $( , compare_mode: $compare_mode:expr )? // default: None + $( , )? // optional trailing comma + } + ) => { + $( #[$attr] )* + #[derive(Debug, Clone, PartialEq, Eq, Hash)] + pub struct $name { + pub compiler: Compiler, + pub target: TargetSelection, + } + + impl Step for $name { + type Output = (); + const DEFAULT: bool = $default; + const IS_HOST: bool = (const { + #[allow(unused_assignments, unused_mut)] + let mut value = false; + $( value = $IS_HOST; )? + value + }); + + fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { + run.suite_path($path) + } + + fn make_run(run: RunConfig<'_>) { + let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple()); + + run.builder.ensure($name { compiler, target: run.target }); + } + + fn run(self, builder: &Builder<'_>) { + builder.ensure(Compiletest { + compiler: self.compiler, + target: self.target, + mode: $mode, + suite: $suite, + path: $path, + compare_mode: (const { + #[allow(unused_assignments, unused_mut)] + let mut value = None; + $( value = $compare_mode; )? + value + }), + }) + } + + fn metadata(&self) -> Option { + Some( + StepMetadata::test(stringify!($name), self.target) + ) + } + } + }; +} + test!(Ui { path: "tests/ui", mode: "ui", suite: "ui", default: true }); test!(Crashes { path: "tests/crashes", mode: "crashes", suite: "crashes", default: true }); From 0b3a13c9c05d09b93e6c82163e14108485d6c609 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 17:04:58 +0200 Subject: [PATCH 42/68] Fix staging for compiler/std crate tests --- src/bootstrap/src/core/build_steps/test.rs | 162 ++++++++++++++------- 1 file changed, 106 insertions(+), 56 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 878a649207b3..ad17a5576585 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -96,7 +96,7 @@ impl Step for CrateBootstrap { ); let crate_name = path.rsplit_once('/').unwrap().1; - run_cargo_test(cargo, &[], &[], crate_name, bootstrap_host, builder); + run_cargo_test(cargo, &[], &[], crate_name, bootstrap_host, builder, Mode::ToolBootstrap); } fn metadata(&self) -> Option { @@ -153,7 +153,15 @@ You can skip linkcheck with --skip src/tools/linkchecker" SourceType::InTree, &[], ); - run_cargo_test(cargo, &[], &[], "linkchecker self tests", bootstrap_host, builder); + run_cargo_test( + cargo, + &[], + &[], + "linkchecker self tests", + bootstrap_host, + builder, + Mode::ToolBootstrap, + ); if builder.doc_tests == DocTests::No { return; @@ -474,7 +482,7 @@ impl Step for RustAnalyzer { cargo.env("SKIP_SLOW_TESTS", "1"); cargo.add_rustc_lib_path(builder); - run_cargo_test(cargo, &[], &[], "rust-analyzer", host, builder); + run_cargo_test(cargo, &[], &[], "rust-analyzer", host, builder, Mode::ToolRustc); } fn metadata(&self) -> Option { @@ -532,7 +540,7 @@ impl Step for Rustfmt { cargo.add_rustc_lib_path(builder); - run_cargo_test(cargo, &[], &[], "rustfmt", target, builder); + run_cargo_test(cargo, &[], &[], "rustfmt", target, builder, Mode::ToolRustc); } fn metadata(&self) -> Option { @@ -825,7 +833,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the cargo.env("TEST_RUSTC", builder.rustc(compiler)); cargo.allow_features(COMPILETEST_ALLOW_FEATURES); - run_cargo_test(cargo, &[], &[], "compiletest self test", host, builder); + run_cargo_test(cargo, &[], &[], "compiletest self test", host, builder, Mode::ToolStd); } } @@ -1351,7 +1359,15 @@ impl Step for CrateRunMakeSupport { &[], ); cargo.allow_features("test"); - run_cargo_test(cargo, &[], &[], "run-make-support self test", host, builder); + run_cargo_test( + cargo, + &[], + &[], + "run-make-support self test", + host, + builder, + Mode::ToolBootstrap, + ); } } @@ -1388,7 +1404,15 @@ impl Step for CrateBuildHelper { &[], ); cargo.allow_features("test"); - run_cargo_test(cargo, &[], &[], "build_helper self test", host, builder); + run_cargo_test( + cargo, + &[], + &[], + "build_helper self test", + host, + builder, + Mode::ToolBootstrap, + ); } } @@ -2340,9 +2364,10 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} } } +/// Runs the documentation tests for a book in `src/doc` using the `rustdoc` of `test_compiler`. #[derive(Debug, Clone, PartialEq, Eq, Hash)] struct BookTest { - compiler: Compiler, + test_compiler: Compiler, path: PathBuf, name: &'static str, is_ext_doc: bool, @@ -2357,9 +2382,6 @@ impl Step for BookTest { run.never() } - /// Runs the documentation tests for a book in `src/doc`. - /// - /// This uses the `rustdoc` that sits next to `compiler`. fn run(self, builder: &Builder<'_>) { // External docs are different from local because: // - Some books need pre-processing by mdbook before being tested. @@ -2382,13 +2404,13 @@ impl BookTest { /// This runs the equivalent of `mdbook test` (via the rustbook wrapper) /// which in turn runs `rustdoc --test` on each file in the book. fn run_ext_doc(self, builder: &Builder<'_>) { - let compiler = self.compiler; + let test_compiler = self.test_compiler; - builder.std(compiler, compiler.host); + builder.std(test_compiler, test_compiler.host); // mdbook just executes a binary named "rustdoc", so we need to update // PATH so that it points to our rustdoc. - let mut rustdoc_path = builder.rustdoc_for_compiler(compiler); + let mut rustdoc_path = builder.rustdoc_for_compiler(test_compiler); rustdoc_path.pop(); let old_path = env::var_os("PATH").unwrap_or_default(); let new_path = env::join_paths(iter::once(rustdoc_path).chain(env::split_paths(&old_path))) @@ -2411,7 +2433,7 @@ impl BookTest { let target = builder.config.host_target; let cargo = tool::prepare_tool_cargo( builder, - compiler, + test_compiler, mode, target, Kind::Build, @@ -2420,7 +2442,7 @@ impl BookTest { &[], ); - let stamp = BuildStamp::new(&builder.cargo_out(compiler, mode, target)) + let stamp = BuildStamp::new(&builder.cargo_out(test_compiler, mode, target)) .with_prefix(PathBuf::from(dep).file_name().and_then(|v| v.to_str()).unwrap()); let output_paths = run_cargo(builder, cargo, vec![], &stamp, vec![], false, false); @@ -2453,8 +2475,8 @@ impl BookTest { Kind::Test, format_args!("mdbook {}", self.path.display()), None, - compiler, - compiler.host, + test_compiler, + test_compiler.host, ); let _time = helpers::timeit(builder); let toolstate = if rustbook_cmd.delay_failure().run(builder) { @@ -2467,12 +2489,18 @@ impl BookTest { /// This runs `rustdoc --test` on all `.md` files in the path. fn run_local_doc(self, builder: &Builder<'_>) { - let compiler = self.compiler; - let host = self.compiler.host; + let test_compiler = self.test_compiler; + let host = self.test_compiler.host; - builder.std(compiler, host); + builder.std(test_compiler, host); - let _guard = builder.msg(Kind::Test, format!("book {}", self.name), None, compiler, host); + let _guard = builder.msg( + Kind::Test, + format!("book {}", self.name), + None, + (test_compiler.host, test_compiler.stage - 1), + host, + ); // Do a breadth-first traversal of the `src/doc` directory and just run // tests for all files that end in `*.md` @@ -2495,7 +2523,7 @@ impl BookTest { files.sort(); for file in files { - markdown_test(builder, compiler, &file); + markdown_test(builder, test_compiler, &file); } } } @@ -2511,7 +2539,7 @@ macro_rules! test_book { $( #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct $name { - compiler: Compiler, + test_compiler: Compiler, } impl Step for $name { @@ -2525,7 +2553,7 @@ macro_rules! test_book { fn make_run(run: RunConfig<'_>) { run.builder.ensure($name { - compiler: run.builder.compiler(run.builder.top_stage, run.target), + test_compiler: run.builder.compiler(run.builder.top_stage, run.target), }); } @@ -2545,7 +2573,7 @@ macro_rules! test_book { )? builder.ensure(BookTest { - compiler: self.compiler, + test_compiler: self.test_compiler, path: PathBuf::from($path), name: $book_name, is_ext_doc: !$default, @@ -2665,7 +2693,8 @@ fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) -> /// which have their own separate test steps.) #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct CrateLibrustc { - compiler: Compiler, + /// The compiler that will *build* rustc in test mode. + build_compiler: Compiler, target: TargetSelection, crates: Vec, } @@ -2682,18 +2711,18 @@ impl Step for CrateLibrustc { fn make_run(run: RunConfig<'_>) { let builder = run.builder; let host = run.build_triple(); - let compiler = builder.compiler_for(builder.top_stage, host, host); + let build_compiler = builder.compiler(builder.top_stage - 1, host); let crates = run.make_run_crates(Alias::Compiler); - builder.ensure(CrateLibrustc { compiler, target: run.target, crates }); + builder.ensure(CrateLibrustc { build_compiler, target: run.target, crates }); } fn run(self, builder: &Builder<'_>) { - builder.std(self.compiler, self.target); + builder.std(self.build_compiler, self.target); // To actually run the tests, delegate to a copy of the `Crate` step. builder.ensure(Crate { - compiler: self.compiler, + build_compiler: self.build_compiler, target: self.target, mode: Mode::Rustc, crates: self.crates, @@ -2715,12 +2744,14 @@ fn run_cargo_test<'a>( description: impl Into>, target: TargetSelection, builder: &Builder<'_>, + mode: impl Into>, ) -> bool { + let mode = mode.into(); let compiler = cargo.compiler(); let mut cargo = prepare_cargo_test(cargo, libtest_args, crates, target, builder); let _time = helpers::timeit(builder); let _group = - description.into().and_then(|what| builder.msg(Kind::Test, what, None, compiler, target)); + description.into().and_then(|what| builder.msg(Kind::Test, what, mode, compiler, target)); #[cfg(feature = "build-metrics")] builder.metrics.begin_test_suite( @@ -2818,10 +2849,11 @@ fn prepare_cargo_test( /// library crates and compiler crates. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct Crate { - pub compiler: Compiler, - pub target: TargetSelection, - pub mode: Mode, - pub crates: Vec, + /// The compiler that will *build* libstd or rustc in test mode. + build_compiler: Compiler, + target: TargetSelection, + mode: Mode, + crates: Vec, } impl Step for Crate { @@ -2835,14 +2867,14 @@ impl Step for Crate { fn make_run(run: RunConfig<'_>) { let builder = run.builder; let host = run.build_triple(); - let compiler = builder.compiler_for(builder.top_stage, host, host); + let build_compiler = builder.compiler(builder.top_stage, host); let crates = run .paths .iter() .map(|p| builder.crate_paths[&p.assert_single_path().path].clone()) .collect(); - builder.ensure(Crate { compiler, target: run.target, mode: Mode::Std, crates }); + builder.ensure(Crate { build_compiler, target: run.target, mode: Mode::Std, crates }); } /// Runs all unit tests plus documentation tests for a given crate defined @@ -2854,19 +2886,13 @@ impl Step for Crate { /// Currently this runs all tests for a DAG by passing a bunch of `-p foo` /// arguments, and those arguments are discovered from `cargo metadata`. fn run(self, builder: &Builder<'_>) { - let compiler = self.compiler; + let build_compiler = self.build_compiler; let target = self.target; let mode = self.mode; // Prepare sysroot // See [field@compile::Std::force_recompile]. - builder.ensure(Std::new(compiler, compiler.host).force_recompile(true)); - - // If we're not doing a full bootstrap but we're testing a stage2 - // version of libstd, then what we're actually testing is the libstd - // produced in stage1. Reflect that here by updating the compiler that - // we're working with automatically. - let compiler = builder.compiler_for(compiler.stage, compiler.host, target); + builder.ensure(Std::new(build_compiler, build_compiler.host).force_recompile(true)); let mut cargo = if builder.kind == Kind::Miri { if builder.top_stage == 0 { @@ -2878,7 +2904,7 @@ impl Step for Crate { // (Implicitly prepares target sysroot) let mut cargo = builder::Cargo::new( builder, - compiler, + build_compiler, mode, SourceType::InTree, target, @@ -2904,12 +2930,19 @@ impl Step for Crate { } else { // Also prepare a sysroot for the target. if !builder.config.is_host_target(target) { - builder.ensure(compile::Std::new(compiler, target).force_recompile(true)); - builder.ensure(RemoteCopyLibs { build_compiler: compiler, target }); + builder.ensure(compile::Std::new(build_compiler, target).force_recompile(true)); + builder.ensure(RemoteCopyLibs { build_compiler, target }); } // Build `cargo test` command - builder::Cargo::new(builder, compiler, mode, SourceType::InTree, target, builder.kind) + builder::Cargo::new( + builder, + build_compiler, + mode, + SourceType::InTree, + target, + builder.kind, + ) }; match mode { @@ -2928,7 +2961,7 @@ impl Step for Crate { } } Mode::Rustc => { - compile::rustc_cargo(builder, &mut cargo, target, &compiler, &self.crates); + compile::rustc_cargo(builder, &mut cargo, target, &build_compiler, &self.crates); } _ => panic!("can only test libraries"), }; @@ -2945,7 +2978,15 @@ impl Step for Crate { crates.push("alloctests".to_owned()); } - run_cargo_test(cargo, &[], &crates, &*crate_description(&self.crates), target, builder); + run_cargo_test( + cargo, + &[], + &crates, + &*crate_description(&self.crates), + target, + builder, + mode, + ); } } @@ -3039,7 +3080,15 @@ impl Step for CrateRustdoc { dylib_path.insert(0, PathBuf::from(&*libdir)); cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap()); - run_cargo_test(cargo, &[], &["rustdoc:0.0.0".to_string()], "rustdoc", target, builder); + run_cargo_test( + cargo, + &[], + &["rustdoc:0.0.0".to_string()], + "rustdoc", + target, + builder, + Mode::ToolRustc, + ); } } @@ -3098,6 +3147,7 @@ impl Step for CrateRustdocJsonTypes { "rustdoc-json-types", target, builder, + Mode::ToolTarget, ); } } @@ -3298,7 +3348,7 @@ impl Step for Bootstrap { // bootstrap tests are racy on directory creation so just run them one at a time. // Since there's not many this shouldn't be a problem. - run_cargo_test(cargo, &["--test-threads=1"], &[], None, host, builder); + run_cargo_test(cargo, &["--test-threads=1"], &[], None, host, builder, Mode::ToolBootstrap); } fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -3441,7 +3491,7 @@ impl Step for RustInstaller { let _guard = builder.msg(Kind::Test, "rust-installer", None, build_compiler, bootstrap_host); - run_cargo_test(cargo, &[], &[], None, bootstrap_host, builder); + run_cargo_test(cargo, &[], &[], None, bootstrap_host, builder, Mode::ToolBootstrap); // We currently don't support running the test.sh script outside linux(?) environments. // Eventually this should likely migrate to #[test]s in rust-installer proper rather than a @@ -3812,7 +3862,7 @@ impl Step for TestFloatParse { ); cargo_test.allow_features(TEST_FLOAT_PARSE_ALLOW_FEATURES); - run_cargo_test(cargo_test, &[], &[], "test-float-parse", target, builder); + run_cargo_test(cargo_test, &[], &[], "test-float-parse", target, builder, Mode::ToolStd); // Run the actual parse tests. let mut cargo_run = tool::prepare_tool_cargo( From ef569d3ce36d18bbca01a42087f070da0cc58271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 17:09:02 +0200 Subject: [PATCH 43/68] Fix spacing when testing individual crates --- src/bootstrap/src/core/builder/mod.rs | 2 +- src/bootstrap/src/core/builder/tests.rs | 5 +++-- src/bootstrap/src/lib.rs | 10 +++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/bootstrap/src/core/builder/mod.rs b/src/bootstrap/src/core/builder/mod.rs index b224a7e73221..627085df812c 100644 --- a/src/bootstrap/src/core/builder/mod.rs +++ b/src/bootstrap/src/core/builder/mod.rs @@ -295,7 +295,7 @@ pub fn crate_description(crates: &[impl AsRef]) -> String { return "".into(); } - let mut descr = String::from(" {"); + let mut descr = String::from("{"); descr.push_str(crates[0].as_ref()); for krate in &crates[1..] { descr.push_str(", "); diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 921c40166541..687c2fdd7ba2 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2064,9 +2064,10 @@ mod snapshot { [test] CoverageRunRustdoc [test] Pretty [build] rustc 1 -> std 1 + [build] rustc 0 -> std 0 + [build] rustdoc 0 [test] CrateLibrustc [build] rustc 1 -> rustc 2 - [build] rustdoc 0 [test] crate-bootstrap src/tools/coverage-dump [test] crate-bootstrap src/tools/jsondoclint [test] crate-bootstrap src/tools/replace-version-placeholder @@ -2101,7 +2102,7 @@ mod snapshot { [doc] rustc (book) [test] rustc 1 -> lint-docs 2 [doc] rustc 1 -> std 1 crates=[] - [test] rustc 1 -> rustdoc-js-std 2 + [test] rustdoc-js-std 1 [build] rustc 0 -> RustdocTheme 1 [test] rustdoc-theme 1 [test] RustdocUi diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 1beeb16b44f3..29d0eac670aa 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -1144,14 +1144,18 @@ impl Build { }; let action = action.into().description(); - let msg = |fmt| format!("{action} stage{actual_stage} {what}{fmt}"); + let what = what.to_string(); + let msg = |fmt| { + let space = if !what.is_empty() { " " } else { "" }; + format!("{action} stage{actual_stage} {what}{space}{fmt}") + }; let msg = if let Some(target) = target.into() { let build_stage = host_and_stage.stage; let host = host_and_stage.host; if host == target { - msg(format_args!(" (stage{build_stage} -> stage{actual_stage}, {target})")) + msg(format_args!("(stage{build_stage} -> stage{actual_stage}, {target})")) } else { - msg(format_args!(" (stage{build_stage}:{host} -> stage{actual_stage}:{target})")) + msg(format_args!("(stage{build_stage}:{host} -> stage{actual_stage}:{target})")) } } else { msg(format_args!("")) From dda8e1ddc167264ef5b0cdff0c4a3e6c7fcdca43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 17:30:30 +0200 Subject: [PATCH 44/68] Do not run tests on CI in stage 0 --- src/ci/docker/host-x86_64/tidy/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ci/docker/host-x86_64/tidy/Dockerfile b/src/ci/docker/host-x86_64/tidy/Dockerfile index c8558689d3ba..2dda51b155e9 100644 --- a/src/ci/docker/host-x86_64/tidy/Dockerfile +++ b/src/ci/docker/host-x86_64/tidy/Dockerfile @@ -44,5 +44,5 @@ RUN bash -c 'npm install -g eslint@$(cat /tmp/eslint.version)' # NOTE: intentionally uses python2 for x.py so we can test it still works. # validate-toolstate only runs in our CI, so it's ok for it to only support python3. -ENV SCRIPT TIDY_PRINT_DIFF=1 python2.7 ../x.py test --stage 0 \ +ENV SCRIPT TIDY_PRINT_DIFF=1 python2.7 ../x.py test \ src/tools/tidy tidyselftest --extra-checks=py,cpp,js,spellcheck From b53d532a3874517354afc3fd0bcf596b1e00fa71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 17:18:19 +0200 Subject: [PATCH 45/68] Add change tracker entry --- src/bootstrap/src/utils/change_tracker.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/bootstrap/src/utils/change_tracker.rs b/src/bootstrap/src/utils/change_tracker.rs index 606d88d3db44..2cc2fb486fad 100644 --- a/src/bootstrap/src/utils/change_tracker.rs +++ b/src/bootstrap/src/utils/change_tracker.rs @@ -531,4 +531,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[ severity: ChangeSeverity::Info, summary: "It is now possible to `check/build/dist` the standard stage 0 library if you use a stage0 rustc built from in-tree sources. This is useful for quickly cross-compiling the standard library. You have to enable build.local-rebuild for this to work.", }, + ChangeInfo { + change_id: 145663, + severity: ChangeSeverity::Warning, + summary: "It is no longer possible to `x test` with stage 0, except for running compiletest and opting into `build.compiletest-allow-stage0`.", + }, ]; From 425c66ecdc7f37db3f4c4d134fd01105bc9c8a3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 18:05:22 +0200 Subject: [PATCH 46/68] Skip bootstrap tests on CI --- src/bootstrap/src/core/build_steps/tool.rs | 2 +- src/bootstrap/src/core/builder/tests.rs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/build_steps/tool.rs b/src/bootstrap/src/core/build_steps/tool.rs index e15b570a5a7a..65c4c4990865 100644 --- a/src/bootstrap/src/core/build_steps/tool.rs +++ b/src/bootstrap/src/core/build_steps/tool.rs @@ -1539,7 +1539,7 @@ tool_rustc_extended!(Rustfmt { add_bins_to_sysroot: ["rustfmt"] }); -pub const TEST_FLOAT_PARSE_ALLOW_FEATURES: &'static str = "f16,cfg_target_has_reliable_f16_f128"; +pub const TEST_FLOAT_PARSE_ALLOW_FEATURES: &str = "f16,cfg_target_has_reliable_f16_f128"; impl Builder<'_> { /// Gets a `BootstrapCommand` which is ready to run `tool` in `stage` built for diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 687c2fdd7ba2..11cbf9d38dd3 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2042,6 +2042,10 @@ mod snapshot { let ctx = TestCtx::new(); insta::assert_snapshot!( ctx.config("test") + // Skip bootstrap tests, as for some reason the recursive nature of running + // bootstrap tests under bootstrap tests causes non-deterministic snapshot diffs + // on CI. + .args(&["--skip", "bootstrap"]) .render_steps(), @r" [build] rustc 0 -> Tidy 1 [test] tidy <> From 1591901e2f2cae2c1b79f330e9d392b594db4cc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 19:37:45 +0200 Subject: [PATCH 47/68] Fix doclink --- src/bootstrap/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 29d0eac670aa..0584c53f549e 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -1165,7 +1165,7 @@ impl Build { /// Return a `Group` guard for a [`Step`] that tests `what` with the given `stage` and `target` /// (determined by `host_and_stage`). - /// Use this instead of [`Builder::msg`] when there is no clear `build_compiler` to be + /// Use this instead of [`builder::Builder::msg`] when there is no clear `build_compiler` to be /// determined. /// /// [`Step`]: crate::core::builder::Step From 1c1d630c8c3a74837d30846a8280da0e14ca87e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 19:53:24 +0200 Subject: [PATCH 48/68] Fake nodejs in snapshot test --- src/bootstrap/src/core/builder/tests.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 11cbf9d38dd3..67e89c1fb02e 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2046,6 +2046,8 @@ mod snapshot { // bootstrap tests under bootstrap tests causes non-deterministic snapshot diffs // on CI. .args(&["--skip", "bootstrap"]) + // rustdoc-js-std requires nodejs to be present + .args(&["--set", "build.nodejs=/bin/nodejs"]) .render_steps(), @r" [build] rustc 0 -> Tidy 1 [test] tidy <> From daaf0c9eb5b98aba6317980782259c17416bdaf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 21:01:31 +0200 Subject: [PATCH 49/68] Fix tests and doclink --- src/bootstrap/src/core/builder/tests.rs | 18 +++++++++--------- src/bootstrap/src/lib.rs | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 67e89c1fb02e..b3c13f22814a 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2042,15 +2042,18 @@ mod snapshot { let ctx = TestCtx::new(); insta::assert_snapshot!( ctx.config("test") - // Skip bootstrap tests, as for some reason the recursive nature of running - // bootstrap tests under bootstrap tests causes non-deterministic snapshot diffs - // on CI. - .args(&["--skip", "bootstrap"]) - // rustdoc-js-std requires nodejs to be present - .args(&["--set", "build.nodejs=/bin/nodejs"]) + // Bootstrap only run by default on CI, so we have to emulate that also locally. + .args(&["--ci", "true"]) + // These rustdoc tests requires nodejs to be present. + // We can't easily opt out of it, so if it is present on the local PC, the test + // would have different result on CI, where nodejs might be missing. + .args(&["--skip", "rustdoc-js-std"]) + .args(&["--skip", "rustdoc-js"]) + .args(&["--skip", "rustdoc-gui"]) .render_steps(), @r" [build] rustc 0 -> Tidy 1 [test] tidy <> + [build] rustdoc 0 [build] llvm [build] rustc 0 -> rustc 1 [build] rustc 1 -> std 1 @@ -2071,7 +2074,6 @@ mod snapshot { [test] Pretty [build] rustc 1 -> std 1 [build] rustc 0 -> std 0 - [build] rustdoc 0 [test] CrateLibrustc [build] rustc 1 -> rustc 2 [test] crate-bootstrap src/tools/coverage-dump @@ -2107,8 +2109,6 @@ mod snapshot { [test] rustc 0 -> rust-analyzer 1 [doc] rustc (book) [test] rustc 1 -> lint-docs 2 - [doc] rustc 1 -> std 1 crates=[] - [test] rustdoc-js-std 1 [build] rustc 0 -> RustdocTheme 1 [test] rustdoc-theme 1 [test] RustdocUi diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 0584c53f549e..9a882eae08ed 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -1165,7 +1165,7 @@ impl Build { /// Return a `Group` guard for a [`Step`] that tests `what` with the given `stage` and `target` /// (determined by `host_and_stage`). - /// Use this instead of [`builder::Builder::msg`] when there is no clear `build_compiler` to be + /// Use this instead of [`Build::msg`] when there is no clear `build_compiler` to be /// determined. /// /// [`Step`]: crate::core::builder::Step From d2195c483c2c0593b1b6cde7a9f4097ec9c17c10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 27 Aug 2025 11:35:10 +0200 Subject: [PATCH 50/68] Add test stage 2 snapshot tests --- src/bootstrap/src/core/builder/tests.rs | 107 +++++++++++++++++++++--- 1 file changed, 97 insertions(+), 10 deletions(-) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index b3c13f22814a..31f863f240ff 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2037,19 +2037,23 @@ mod snapshot { .render_steps(), @"[check] rustc 0 -> RunMakeSupport 1 "); } + fn prepare_test_config(ctx: &TestCtx) -> ConfigBuilder { + ctx.config("test") + // Bootstrap only runs by default on CI, so we have to emulate that also locally. + .args(&["--ci", "true"]) + // These rustdoc tests requires nodejs to be present. + // We can't easily opt out of it, so if it is present on the local PC, the test + // would have different result on CI, where nodejs might be missing. + .args(&["--skip", "rustdoc-js-std"]) + .args(&["--skip", "rustdoc-js"]) + .args(&["--skip", "rustdoc-gui"]) + } + #[test] - fn test_all() { + fn test_all_stage_1() { let ctx = TestCtx::new(); insta::assert_snapshot!( - ctx.config("test") - // Bootstrap only run by default on CI, so we have to emulate that also locally. - .args(&["--ci", "true"]) - // These rustdoc tests requires nodejs to be present. - // We can't easily opt out of it, so if it is present on the local PC, the test - // would have different result on CI, where nodejs might be missing. - .args(&["--skip", "rustdoc-js-std"]) - .args(&["--skip", "rustdoc-js"]) - .args(&["--skip", "rustdoc-gui"]) + prepare_test_config(&ctx) .render_steps(), @r" [build] rustc 0 -> Tidy 1 [test] tidy <> @@ -2124,6 +2128,89 @@ mod snapshot { "); } + #[test] + fn test_all_stage_2() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + prepare_test_config(&ctx) + .stage(2) + .render_steps(), @r" + [build] rustc 0 -> Tidy 1 + [test] tidy <> + [build] rustdoc 0 + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 1 -> std 1 + [build] rustc 1 -> rustc 2 + [build] rustc 2 -> std 2 + [build] rustc 0 -> Compiletest 1 + [test] Ui + [test] Crashes + [build] rustc 0 -> CoverageDump 1 + [build] rustc 2 -> std 2 + [test] CodegenLlvm + [test] CodegenUnits + [test] AssemblyLlvm + [test] Incremental + [test] Debuginfo + [build] rustc 2 -> rustc 3 + [test] UiFullDeps + [build] rustdoc 2 + [test] Rustdoc + [test] CoverageRunRustdoc + [test] Pretty + [build] rustc 2 -> std 2 + [build] rustc 1 -> std 1 + [build] rustdoc 1 + [test] rustc 1 -> CrateLibrustc 2 + [test] crate-bootstrap src/tools/coverage-dump + [test] crate-bootstrap src/tools/jsondoclint + [test] crate-bootstrap src/tools/replace-version-placeholder + [test] crate-bootstrap tidyselftest + [build] rustc 0 -> UnstableBookGen 1 + [build] rustc 0 -> Rustbook 1 + [doc] unstable-book (book) + [doc] book (book) + [doc] book/first-edition (book) + [doc] book/second-edition (book) + [doc] book/2018-edition (book) + [doc] rustc 1 -> standalone 2 + [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [build] rustc 1 -> error-index 2 + [doc] rustc 1 -> error-index 2 + [doc] nomicon (book) + [doc] rustc 1 -> reference (book) 2 + [doc] rustdoc (book) + [doc] rust-by-example (book) + [build] rustc 0 -> LintDocs 1 + [doc] rustc (book) + [doc] cargo (book) + [doc] clippy (book) + [doc] embedded-book (book) + [doc] edition-guide (book) + [doc] style-guide (book) + [doc] rustc 1 -> releases 2 + [build] rustc 0 -> Linkchecker 1 + [test] link-check + [test] tier-check + [test] rustc 1 -> rust-analyzer 2 + [doc] rustc (book) + [test] rustc 1 -> lint-docs 2 + [build] rustc 0 -> RustdocTheme 1 + [test] rustdoc-theme 2 + [test] RustdocUi + [build] rustc 0 -> JsonDocCk 1 + [build] rustc 0 -> JsonDocLint 1 + [test] RustdocJson + [doc] rustc 1 -> rustc 2 + [build] rustc 0 -> HtmlChecker 1 + [test] html-check + [build] rustc 0 -> RunMakeSupport 1 + [build] rustc 2 -> cargo 3 + [test] RunMake + "); + } + #[test] fn test_exclude() { let ctx = TestCtx::new(); From 9de09ac101f5e46b7eda48261a6757dc78b25bd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Mon, 25 Aug 2025 17:40:01 +0200 Subject: [PATCH 51/68] Fix stage 1 compiler tests --- compiler/rustc_lint_defs/src/builtin.rs | 11 +++--- src/bootstrap/src/core/build_steps/doc.rs | 2 ++ src/bootstrap/src/core/build_steps/test.rs | 2 +- src/bootstrap/src/core/builder/tests.rs | 42 ++++++++++++++++++++-- src/bootstrap/src/core/config/config.rs | 4 +-- src/tools/lint-docs/src/lib.rs | 42 +++++++++++++++++----- src/tools/lint-docs/src/main.rs | 13 +++++++ 7 files changed, 97 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 97aa10659679..ce92263babd8 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -2309,10 +2309,10 @@ declare_lint! { /// ### Example /// /// ```rust - /// #![feature(sanitize)] + /// #![cfg_attr(not(bootstrap), feature(sanitize))] /// /// #[inline(always)] - /// #[sanitize(address = "off")] + /// #[cfg_attr(not(bootstrap), sanitize(address = "off"))] /// fn x() {} /// /// fn main() { @@ -4832,13 +4832,16 @@ declare_lint! { /// /// ### Example /// - /// ```rust,compile_fail + #[cfg_attr(not(bootstrap), doc = "```rust,compile_fail")] + #[cfg_attr(bootstrap, doc = "```rust")] /// #![doc = in_root!()] /// /// macro_rules! in_root { () => { "" } } /// /// fn main() {} - /// ``` + #[cfg_attr(not(bootstrap), doc = "```")] + #[cfg_attr(bootstrap, doc = "```")] + // ^ Needed to avoid tidy warning about odd number of backticks /// /// {{produces}} /// diff --git a/src/bootstrap/src/core/build_steps/doc.rs b/src/bootstrap/src/core/build_steps/doc.rs index 678fe127879b..0789eefa8946 100644 --- a/src/bootstrap/src/core/build_steps/doc.rs +++ b/src/bootstrap/src/core/build_steps/doc.rs @@ -1293,6 +1293,8 @@ impl Step for RustcBook { // functional sysroot. builder.std(self.build_compiler, self.target); let mut cmd = builder.tool_cmd(Tool::LintDocs); + cmd.arg("--build-rustc-stage"); + cmd.arg(self.build_compiler.stage.to_string()); cmd.arg("--src"); cmd.arg(builder.src.join("compiler")); cmd.arg("--out"); diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index ad17a5576585..1786e8d17b6e 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -2730,7 +2730,7 @@ impl Step for CrateLibrustc { } fn metadata(&self) -> Option { - Some(StepMetadata::test("CrateLibrustc", self.target)) + Some(StepMetadata::test("CrateLibrustc", self.target).built_by(self.build_compiler)) } } diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 31f863f240ff..f14c897cab49 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2078,7 +2078,7 @@ mod snapshot { [test] Pretty [build] rustc 1 -> std 1 [build] rustc 0 -> std 0 - [test] CrateLibrustc + [test] rustc 0 -> CrateLibrustc 1 [build] rustc 1 -> rustc 2 [test] crate-bootstrap src/tools/coverage-dump [test] crate-bootstrap src/tools/jsondoclint @@ -2211,6 +2211,40 @@ mod snapshot { "); } + #[test] + fn test_compiler_stage_1() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("test") + .path("compiler") + .stage(1) + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 0 -> std 0 + [build] rustdoc 0 + [test] rustc 0 -> CrateLibrustc 1 + "); + } + + #[test] + fn test_compiler_stage_2() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("test") + .path("compiler") + .stage(2) + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 1 -> std 1 + [build] rustc 1 -> rustc 2 + [build] rustc 1 -> std 1 + [build] rustdoc 1 + [test] rustc 1 -> CrateLibrustc 2 + "); + } + #[test] fn test_exclude() { let ctx = TestCtx::new(); @@ -2228,13 +2262,15 @@ mod snapshot { let get_steps = |args: &[&str]| ctx.config("test").args(args).get_steps(); + let rustc_metadata = + || StepMetadata::test("CrateLibrustc", host).built_by(Compiler::new(0, host)); // Ensure our test is valid, and `test::Rustc` would be run without the exclude. - get_steps(&[]).assert_contains(StepMetadata::test("CrateLibrustc", host)); + get_steps(&[]).assert_contains(rustc_metadata()); let steps = get_steps(&["--skip", "compiler/rustc_data_structures"]); // Ensure tests for rustc are not skipped. - steps.assert_contains(StepMetadata::test("CrateLibrustc", host)); + steps.assert_contains(rustc_metadata()); steps.assert_contains_fuzzy(StepMetadata::build("rustc", host)); } diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 450f325f1a97..d12cc962187f 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -1012,9 +1012,7 @@ impl Config { } if flags_compile_time_deps && !matches!(flags_cmd, Subcommand::Check { .. }) { - eprintln!( - "ERROR: Can't use --compile-time-deps with any subcommand other than check." - ); + eprintln!("ERROR: Can't use --compile-time-deps with any subcommand other than check."); exit!(1); } diff --git a/src/tools/lint-docs/src/lib.rs b/src/tools/lint-docs/src/lib.rs index b33344ca5dda..bc38e931fe51 100644 --- a/src/tools/lint-docs/src/lib.rs +++ b/src/tools/lint-docs/src/lib.rs @@ -59,6 +59,8 @@ pub struct LintExtractor<'a> { pub rustc_target: &'a str, /// The target linker overriding `rustc`'s default pub rustc_linker: Option<&'a str>, + /// Stage of the compiler that builds the docs (the stage of `rustc_path`). + pub build_rustc_stage: u32, /// Verbose output. pub verbose: bool, /// Validate the style and the code example. @@ -216,14 +218,7 @@ impl<'a> LintExtractor<'a> { if let Some(text) = line.strip_prefix("/// ") { doc_lines.push(text.to_string()); } else if let Some(text) = line.strip_prefix("#[doc = \"") { - let escaped = text.strip_suffix("\"]").unwrap(); - let mut buf = String::new(); - unescape_str(escaped, |_, res| match res { - Ok(c) => buf.push(c), - Err(err) => { - assert!(!err.is_fatal(), "failed to unescape string literal") - } - }); + let buf = parse_doc_string(text); doc_lines.push(buf); } else if line == "///" { doc_lines.push("".to_string()); @@ -234,6 +229,20 @@ impl<'a> LintExtractor<'a> { // Ignore allow of lints (useful for // invalid_rust_codeblocks). continue; + } else if let Some(text) = + line.strip_prefix("#[cfg_attr(not(bootstrap), doc = \"") + { + if self.build_rustc_stage >= 1 { + let buf = parse_doc_string(text); + doc_lines.push(buf); + } + } else if let Some(text) = + line.strip_prefix("#[cfg_attr(bootstrap, doc = \"") + { + if self.build_rustc_stage == 0 { + let buf = parse_doc_string(text); + doc_lines.push(buf); + } } else { let name = lint_name(line).map_err(|e| { format!( @@ -580,6 +589,23 @@ impl<'a> LintExtractor<'a> { } } +/// Parses a doc string that follows `#[doc = "`. +fn parse_doc_string(text: &str) -> String { + let escaped = text.strip_suffix("]").unwrap_or(text); + let escaped = escaped.strip_suffix(")").unwrap_or(escaped).strip_suffix("\""); + let Some(escaped) = escaped else { + panic!("Cannot extract docstring content from {text}"); + }; + let mut buf = String::new(); + unescape_str(escaped, |_, res| match res { + Ok(c) => buf.push(c), + Err(err) => { + assert!(!err.is_fatal(), "failed to unescape string literal") + } + }); + buf +} + /// Adds `Lint`s that have been renamed. fn add_renamed_lints(lints: &mut Vec) { for (level, names) in RENAMES { diff --git a/src/tools/lint-docs/src/main.rs b/src/tools/lint-docs/src/main.rs index e377283b1a45..1933ce4d2f1e 100644 --- a/src/tools/lint-docs/src/main.rs +++ b/src/tools/lint-docs/src/main.rs @@ -25,6 +25,7 @@ fn doit() -> Result<(), Box> { let mut args = std::env::args().skip(1); let mut src_path = None; let mut out_path = None; + let mut build_rustc_stage = None; let mut rustc_path = None; let mut rustc_target = None; let mut rustc_linker = None; @@ -32,6 +33,14 @@ fn doit() -> Result<(), Box> { let mut validate = false; while let Some(arg) = args.next() { match arg.as_str() { + "--build-rustc-stage" => { + build_rustc_stage = match args.next() { + Some(s) => { + Some(s.parse::().expect("build rustc stage has to be an integer")) + } + None => return Err("--build-rustc-stage requires a value".into()), + }; + } "--src" => { src_path = match args.next() { Some(s) => Some(PathBuf::from(s)), @@ -67,6 +76,9 @@ fn doit() -> Result<(), Box> { s => return Err(format!("unexpected argument `{}`", s).into()), } } + if build_rustc_stage.is_none() { + return Err("--build-rustc-stage must be specified to the stage of the compiler that generates the docs".into()); + } if src_path.is_none() { return Err("--src must be specified to the directory with the compiler source".into()); } @@ -85,6 +97,7 @@ fn doit() -> Result<(), Box> { rustc_path: &rustc_path.unwrap(), rustc_target: &rustc_target.unwrap(), rustc_linker: rustc_linker.as_deref(), + build_rustc_stage: build_rustc_stage.unwrap(), verbose, validate, }; From 5bc9308471ce452003bfebd97db401ae145bffda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 27 Aug 2025 11:38:39 +0200 Subject: [PATCH 52/68] Do not run `lint-docs` tests in stage 1 `x test` by default --- src/bootstrap/src/core/build_steps/test.rs | 20 +++++++++----------- src/bootstrap/src/core/builder/tests.rs | 4 +--- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 1786e8d17b6e..95be5360b0bb 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -2693,7 +2693,7 @@ fn markdown_test(builder: &Builder<'_>, compiler: Compiler, markdown: &Path) -> /// which have their own separate test steps.) #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct CrateLibrustc { - /// The compiler that will *build* rustc in test mode. + /// The compiler that will run unit tests and doctests on the in-tree rustc source. build_compiler: Compiler, target: TargetSelection, crates: Vec, @@ -3429,24 +3429,22 @@ impl Step for LintDocs { const IS_HOST: bool = true; fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { - run.path("src/tools/lint-docs") + let stage = run.builder.top_stage; + // Lint docs tests might not work with stage 1, so do not run this test by default in + // `x test` below stage 2. + run.path("src/tools/lint-docs").default_condition(stage > 1) } fn make_run(run: RunConfig<'_>) { - // Bump the stage to 2, because the rustc book requires an in-tree compiler. - // At the same time, since this step is enabled by default, we don't want `x test` to fail - // in stage 1. - let stage = if run.builder.config.is_explicit_stage() || run.builder.top_stage >= 2 { - run.builder.top_stage - } else { - 2 - }; + if run.builder.top_stage < 2 { + eprintln!("WARNING: lint-docs tests might not work below stage 2"); + } run.builder.ensure(LintDocs { build_compiler: prepare_doc_compiler( run.builder, run.builder.config.host_target, - stage, + run.builder.top_stage, ), target: run.target, }); diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index f14c897cab49..43e67756e742 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2111,8 +2111,6 @@ mod snapshot { [test] link-check [test] tier-check [test] rustc 0 -> rust-analyzer 1 - [doc] rustc (book) - [test] rustc 1 -> lint-docs 2 [build] rustc 0 -> RustdocTheme 1 [test] rustdoc-theme 1 [test] RustdocUi @@ -2175,7 +2173,7 @@ mod snapshot { [doc] book/second-edition (book) [doc] book/2018-edition (book) [doc] rustc 1 -> standalone 2 - [doc] rustc 2 -> std 2 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] + [doc] rustc 1 -> std 1 crates=[alloc,compiler_builtins,core,panic_abort,panic_unwind,proc_macro,rustc-std-workspace-core,std,std_detect,sysroot,test,unwind] [build] rustc 1 -> error-index 2 [doc] rustc 1 -> error-index 2 [doc] nomicon (book) From 9e8d9078078cf84cee394303d7d801cb204d7a96 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 30 Aug 2025 17:28:14 +0200 Subject: [PATCH 53/68] Update `browser-ui-test` version to `0.21.3` --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index dbdd9c644f14..44e8b65c28d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,7 +5,7 @@ "packages": { "": { "dependencies": { - "browser-ui-test": "^0.21.1", + "browser-ui-test": "^0.21.3", "es-check": "^6.2.1", "eslint": "^8.57.1", "eslint-js": "github:eslint/js", @@ -555,7 +555,7 @@ } }, "node_modules/browser-ui-test": { - "version": "0.21.1", + "version": "0.21.3", "resolved": "https://registry.npmjs.org/browser-ui-test/-/browser-ui-test-0.21.1.tgz", "integrity": "sha512-b3a9mhALAmbP+GifoN/c295RPyuyfIUFMz0DtlBHbeDW5PYQTMHZZJtm7R2UyP6JiIQSkR+7227sS3maMGMUTg==", "license": "MIT", diff --git a/package.json b/package.json index 945f9a3ee87d..04f4c501d1fb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "dependencies": { - "browser-ui-test": "^0.21.1", + "browser-ui-test": "^0.21.3", "es-check": "^6.2.1", "eslint": "^8.57.1", "eslint-js": "github:eslint/js", From 2dc57526c276c12de478194dfef0f5af5fbe30cb Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 30 Aug 2025 13:11:21 -0400 Subject: [PATCH 54/68] Use move_file for rename in tracing This avoids panicking when the source and destinations are on different filesystems. --- src/bootstrap/src/utils/tracing.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/utils/tracing.rs b/src/bootstrap/src/utils/tracing.rs index 472781ffa73a..b1226ed7de7b 100644 --- a/src/bootstrap/src/utils/tracing.rs +++ b/src/bootstrap/src/utils/tracing.rs @@ -168,7 +168,11 @@ mod inner { impl TracingGuard { pub fn copy_to_dir(self, dir: &std::path::Path) { drop(self.guard); - std::fs::rename(&self.chrome_tracing_path, dir.join("chrome-trace.json")).unwrap(); + crate::utils::helpers::move_file( + &self.chrome_tracing_path, + dir.join("chrome-trace.json"), + ) + .unwrap(); } } From 3516e25eedcf7438e04b6c1f5a7a926ac2c893bc Mon Sep 17 00:00:00 2001 From: Chris Denton Date: Sat, 30 Aug 2025 12:07:25 +0000 Subject: [PATCH 55/68] Fix spurious test timeout --- library/std/src/sys/process/windows/tests.rs | 24 +++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/library/std/src/sys/process/windows/tests.rs b/library/std/src/sys/process/windows/tests.rs index 1377e12162f2..a21afe3363c5 100644 --- a/library/std/src/sys/process/windows/tests.rs +++ b/library/std/src/sys/process/windows/tests.rs @@ -1,7 +1,8 @@ use super::{Arg, make_command_line}; use crate::env; use crate::ffi::{OsStr, OsString}; -use crate::process::Command; +use crate::os::windows::io::AsHandle; +use crate::process::{Command, Stdio}; #[test] fn test_raw_args() { @@ -29,19 +30,30 @@ fn test_thread_handle() { use crate::os::windows::process::{ChildExt, CommandExt}; const CREATE_SUSPENDED: u32 = 0x00000004; - let p = Command::new("cmd").args(&["/C", "exit 0"]).creation_flags(CREATE_SUSPENDED).spawn(); + let p = Command::new("whoami").stdout(Stdio::null()).creation_flags(CREATE_SUSPENDED).spawn(); assert!(p.is_ok()); - let mut p = p.unwrap(); + + // Ensure the process is killed in the event something goes wrong. + struct DropGuard(crate::process::Child); + impl Drop for DropGuard { + fn drop(&mut self) { + let _ = self.0.kill(); + } + } + let mut p = DropGuard(p.unwrap()); + let p = &mut p.0; unsafe extern "system" { - fn ResumeThread(_: BorrowedHandle<'_>) -> u32; + unsafe fn ResumeThread(hHandle: BorrowedHandle<'_>) -> u32; + unsafe fn WaitForSingleObject(hHandle: BorrowedHandle<'_>, dwMilliseconds: u32) -> u32; } unsafe { ResumeThread(p.main_thread_handle()); + // Wait until the process exits or 1 minute passes. + // We don't bother checking the result here as that's done below using `try_wait`. + WaitForSingleObject(p.as_handle(), 1000 * 60); } - crate::thread::sleep(crate::time::Duration::from_millis(100)); - let res = p.try_wait(); assert!(res.is_ok()); assert!(res.unwrap().is_some()); From 3af81cf0b7bd394dac89cbacec303b5937b5519a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 30 Aug 2025 18:42:07 +0000 Subject: [PATCH 56/68] review comment: move `Visitor` --- compiler/rustc_parse/src/parser/stmt.rs | 56 +++++++++++++------------ 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_parse/src/parser/stmt.rs b/compiler/rustc_parse/src/parser/stmt.rs index 732c653e4bc8..ad5ab6e6b779 100644 --- a/compiler/rustc_parse/src/parser/stmt.rs +++ b/compiler/rustc_parse/src/parser/stmt.rs @@ -806,6 +806,35 @@ impl<'a> Parser<'a> { } true }); + + struct IdentFinder { + idents: Vec, + /// If a block references one of the bindings introduced by the let pattern, + /// we likely meant to use `if let`. + /// This is pre-expansion, so if we encounter + /// `let Some(x) = foo() { println!("{x}") }` we won't find it. + references_ident: bool = false, + /// If a block has a `return`, then we know with high certainty that it was + /// meant to be let-else. + has_return: bool = false, + } + + impl<'a> Visitor<'a> for IdentFinder { + fn visit_ident(&mut self, ident: &Ident) { + for i in &self.idents { + if ident.name == i.name { + self.references_ident = true; + } + } + } + fn visit_expr(&mut self, node: &'a Expr) { + if let ExprKind::Ret(..) = node.kind { + self.has_return = true; + } + walk_expr(self, node); + } + } + // Collect all bindings in pattern and see if they appear in the block. Likely meant // to write `if let`. See if the block has a return. Likely meant to write // `let else`. @@ -1132,30 +1161,3 @@ impl<'a> Parser<'a> { self.mk_block(thin_vec![self.mk_stmt_err(span, guar)], BlockCheckMode::Default, span) } } - -struct IdentFinder { - idents: Vec, - /// If a block references one of the bindings introduced by the let pattern, we likely meant to - /// use `if let`. - /// This is pre-expansion, so if we encounter `let Some(x) = foo() { println!("{x}") }` we won't - /// find it. - references_ident: bool = false, - /// If a block has a `return`, then we know with high certainty that the - has_return: bool = false, -} - -impl<'a> Visitor<'a> for IdentFinder { - fn visit_ident(&mut self, ident: &Ident) { - for i in &self.idents { - if ident.name == i.name { - self.references_ident = true; - } - } - } - fn visit_expr(&mut self, node: &'a Expr) { - if let ExprKind::Ret(..) = node.kind { - self.has_return = true; - } - walk_expr(self, node); - } -} From 0becce400b73dc54754d005e1e4513811043466b Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 30 Aug 2025 11:11:37 -0700 Subject: [PATCH 57/68] rustdoc-search: split function inverted index by input/output With a patch applied to count the number of unifications, and running the query `Option, (T -> U) -> Option` before: performed unifyFunctionType on 17484 functions after: performed unifyFunctionType on 3055 functions --- src/librustdoc/html/render/search_index.rs | 255 +++++++++++++++++---- src/librustdoc/html/static/js/rustdoc.d.ts | 5 +- src/librustdoc/html/static/js/search.js | 63 +++-- 3 files changed, 256 insertions(+), 67 deletions(-) diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index dddc087d124d..2984f3ab50ee 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -289,8 +289,26 @@ impl SerializedSearchIndex { (Some(self_type_data), None) => Some(self_type_data), (None, Some(other_type_data)) => Some(TypeData { search_unbox: other_type_data.search_unbox, - inverted_function_signature_index: other_type_data - .inverted_function_signature_index + inverted_function_inputs_index: other_type_data + .inverted_function_inputs_index + .iter() + .cloned() + .map(|mut list: Vec| { + for fnid in &mut list { + assert!( + other.function_data + [usize::try_from(*fnid).unwrap()] + .is_some(), + ); + // this is valid because we call `self.push()` once, exactly, for every entry, + // even if we're just pushing a tombstone + *fnid += u32::try_from(other_entryid_offset).unwrap(); + } + list + }) + .collect(), + inverted_function_output_index: other_type_data + .inverted_function_output_index .iter() .cloned() .map(|mut list: Vec| { @@ -310,18 +328,42 @@ impl SerializedSearchIndex { }), (Some(mut self_type_data), Some(other_type_data)) => { for (size, other_list) in other_type_data - .inverted_function_signature_index + .inverted_function_inputs_index .iter() .enumerate() { - while self_type_data.inverted_function_signature_index.len() + while self_type_data.inverted_function_inputs_index.len() <= size { self_type_data - .inverted_function_signature_index + .inverted_function_inputs_index .push(Vec::new()); } - self_type_data.inverted_function_signature_index[size].extend( + self_type_data.inverted_function_inputs_index[size].extend( + other_list.iter().copied().map(|fnid| { + assert!( + other.function_data[usize::try_from(fnid).unwrap()] + .is_some(), + ); + // this is valid because we call `self.push()` once, exactly, for every entry, + // even if we're just pushing a tombstone + fnid + u32::try_from(other_entryid_offset).unwrap() + }), + ) + } + for (size, other_list) in other_type_data + .inverted_function_output_index + .iter() + .enumerate() + { + while self_type_data.inverted_function_output_index.len() + <= size + { + self_type_data + .inverted_function_output_index + .push(Vec::new()); + } + self_type_data.inverted_function_output_index[size].extend( other_list.iter().copied().map(|fnid| { assert!( other.function_data[usize::try_from(fnid).unwrap()] @@ -443,8 +485,25 @@ impl SerializedSearchIndex { param_names: function_data.param_names.clone(), }), other.type_data[other_entryid].as_ref().map(|type_data| TypeData { - inverted_function_signature_index: type_data - .inverted_function_signature_index + inverted_function_inputs_index: type_data + .inverted_function_inputs_index + .iter() + .cloned() + .map(|mut list| { + for fnid in &mut list { + assert!( + other.function_data[usize::try_from(*fnid).unwrap()] + .is_some(), + ); + // this is valid because we call `self.push()` once, exactly, for every entry, + // even if we're just pushing a tombstone + *fnid += u32::try_from(other_entryid_offset).unwrap(); + } + list + }) + .collect(), + inverted_function_output_index: type_data + .inverted_function_output_index .iter() .cloned() .map(|mut list| { @@ -599,9 +658,13 @@ impl SerializedSearchIndex { }, ), self.type_data[id].as_ref().map( - |TypeData { search_unbox, inverted_function_signature_index }| { - let inverted_function_signature_index: Vec> = - inverted_function_signature_index + |TypeData { + search_unbox, + inverted_function_inputs_index, + inverted_function_output_index, + }| { + let inverted_function_inputs_index: Vec> = + inverted_function_inputs_index .iter() .cloned() .map(|mut list| { @@ -615,7 +678,26 @@ impl SerializedSearchIndex { list }) .collect(); - TypeData { search_unbox: *search_unbox, inverted_function_signature_index } + let inverted_function_output_index: Vec> = + inverted_function_output_index + .iter() + .cloned() + .map(|mut list| { + for id in &mut list { + *id = u32::try_from( + *map.get(&usize::try_from(*id).unwrap()).unwrap(), + ) + .unwrap(); + } + list.sort(); + list + }) + .collect(); + TypeData { + search_unbox: *search_unbox, + inverted_function_inputs_index, + inverted_function_output_index, + } }, ), self.alias_pointers[id].and_then(|alias| map.get(&alias).copied()), @@ -934,18 +1016,20 @@ struct TypeData { /// | `Unboxable` | yes | no | no | /// | `Inner` | no | no | yes | search_unbox: bool, - /// List of functions that mention this type in their type signature. + /// List of functions that mention this type in their type signature, + /// on the left side of the `->` arrow. /// - /// - The outermost list has one entry per alpha-normalized generic. - /// - /// - The second layer is sorted by number of types that appear in the + /// - The outer layer is sorted by number of types that appear in the /// type signature. The search engine iterates over these in order from /// smallest to largest. Functions with less stuff in their type /// signature are more likely to be what the user wants, because we never /// show functions that are *missing* parts of the query, so removing.. /// - /// - The final layer is the list of functions. - inverted_function_signature_index: Vec>, + /// - The inner layer is the list of functions. + inverted_function_inputs_index: Vec>, + /// List of functions that mention this type in their type signature, + /// on the right side of the `->` arrow. + inverted_function_output_index: Vec>, } impl Serialize for TypeData { @@ -953,15 +1037,21 @@ impl Serialize for TypeData { where S: Serializer, { - if self.search_unbox || !self.inverted_function_signature_index.is_empty() { + if self.search_unbox + || !self.inverted_function_inputs_index.is_empty() + || !self.inverted_function_output_index.is_empty() + { let mut seq = serializer.serialize_seq(None)?; - if !self.inverted_function_signature_index.is_empty() { - let mut buf = Vec::new(); - encode::write_postings_to_string(&self.inverted_function_signature_index, &mut buf); - let mut serialized_result = Vec::new(); - stringdex_internals::encode::write_base64_to_bytes(&buf, &mut serialized_result); - seq.serialize_element(&String::from_utf8(serialized_result).unwrap())?; - } + let mut buf = Vec::new(); + encode::write_postings_to_string(&self.inverted_function_inputs_index, &mut buf); + let mut serialized_result = Vec::new(); + stringdex_internals::encode::write_base64_to_bytes(&buf, &mut serialized_result); + seq.serialize_element(&str::from_utf8(&serialized_result).unwrap())?; + buf.clear(); + serialized_result.clear(); + encode::write_postings_to_string(&self.inverted_function_output_index, &mut buf); + stringdex_internals::encode::write_base64_to_bytes(&buf, &mut serialized_result); + seq.serialize_element(&str::from_utf8(&serialized_result).unwrap())?; if self.search_unbox { seq.serialize_element(&1)?; } @@ -984,21 +1074,39 @@ impl<'de> Deserialize<'de> for TypeData { write!(formatter, "type data") } fn visit_none(self) -> Result { - Ok(TypeData { inverted_function_signature_index: vec![], search_unbox: false }) + Ok(TypeData { + inverted_function_inputs_index: vec![], + inverted_function_output_index: vec![], + search_unbox: false, + }) } fn visit_seq>(self, mut v: A) -> Result { - let inverted_function_signature_index: String = + let inverted_function_inputs_index: String = + v.next_element()?.unwrap_or(String::new()); + let inverted_function_output_index: String = v.next_element()?.unwrap_or(String::new()); let search_unbox: u32 = v.next_element()?.unwrap_or(0); let mut idx: Vec = Vec::new(); stringdex_internals::decode::read_base64_from_bytes( - inverted_function_signature_index.as_bytes(), + inverted_function_inputs_index.as_bytes(), &mut idx, ) .unwrap(); - let mut inverted_function_signature_index = Vec::new(); - encode::read_postings_from_string(&mut inverted_function_signature_index, &idx); - Ok(TypeData { inverted_function_signature_index, search_unbox: search_unbox == 1 }) + let mut inverted_function_inputs_index = Vec::new(); + encode::read_postings_from_string(&mut inverted_function_inputs_index, &idx); + idx.clear(); + stringdex_internals::decode::read_base64_from_bytes( + inverted_function_output_index.as_bytes(), + &mut idx, + ) + .unwrap(); + let mut inverted_function_output_index = Vec::new(); + encode::read_postings_from_string(&mut inverted_function_output_index, &idx); + Ok(TypeData { + inverted_function_inputs_index, + inverted_function_output_index, + search_unbox: search_unbox == 1, + }) } } deserializer.deserialize_any(TypeDataVisitor) @@ -1222,8 +1330,16 @@ pub(crate) fn build_index( let index = *index.get(); serialized_index.descs[index] = crate_doc; for type_data in serialized_index.type_data.iter_mut() { - if let Some(TypeData { inverted_function_signature_index, .. }) = type_data { - for list in &mut inverted_function_signature_index[..] { + if let Some(TypeData { + inverted_function_inputs_index, + inverted_function_output_index, + .. + }) = type_data + { + for list in inverted_function_inputs_index + .iter_mut() + .chain(inverted_function_output_index.iter_mut()) + { list.retain(|fnid| { serialized_index.entry_data[usize::try_from(*fnid).unwrap()] .as_ref() @@ -1449,7 +1565,8 @@ pub(crate) fn build_index( if serialized_index.type_data[id].as_mut().is_none() { serialized_index.type_data[id] = Some(TypeData { search_unbox, - inverted_function_signature_index: Vec::new(), + inverted_function_inputs_index: Vec::new(), + inverted_function_output_index: Vec::new(), }); } else if search_unbox { serialized_index.type_data[id].as_mut().unwrap().search_unbox = true; @@ -1473,7 +1590,11 @@ pub(crate) fn build_index( None }, }, - TypeData { search_unbox, inverted_function_signature_index: Vec::new() }, + TypeData { + inverted_function_inputs_index: Vec::new(), + inverted_function_output_index: Vec::new(), + search_unbox, + }, ); pathid } @@ -1695,13 +1816,14 @@ pub(crate) fn build_index( } } if let Some(search_type) = &mut item.search_type { - let mut used_in_function_signature = BTreeSet::new(); + let mut used_in_function_inputs = BTreeSet::new(); + let mut used_in_function_output = BTreeSet::new(); for item in &mut search_type.inputs { convert_render_type( item, cache, &mut serialized_index, - &mut used_in_function_signature, + &mut used_in_function_inputs, tcx, ); } @@ -1710,20 +1832,44 @@ pub(crate) fn build_index( item, cache, &mut serialized_index, - &mut used_in_function_signature, + &mut used_in_function_output, tcx, ); } + let mut used_in_constraints = Vec::new(); for constraint in &mut search_type.where_clause { + let mut used_in_constraint = BTreeSet::new(); for trait_ in &mut constraint[..] { convert_render_type( trait_, cache, &mut serialized_index, - &mut used_in_function_signature, + &mut used_in_constraint, tcx, ); } + used_in_constraints.push(used_in_constraint); + } + loop { + let mut inserted_any = false; + for (i, used_in_constraint) in used_in_constraints.iter().enumerate() { + let id = !(i as isize); + if used_in_function_inputs.contains(&id) + && !used_in_function_inputs.is_superset(&used_in_constraint) + { + used_in_function_inputs.extend(used_in_constraint.iter().copied()); + inserted_any = true; + } + if used_in_function_output.contains(&id) + && !used_in_function_output.is_superset(&used_in_constraint) + { + used_in_function_output.extend(used_in_constraint.iter().copied()); + inserted_any = true; + } + } + if !inserted_any { + break; + } } let search_type_size = search_type.size() + // Artificially give struct fields a size of 8 instead of their real @@ -1746,13 +1892,13 @@ pub(crate) fn build_index( .map(|sym| sym.map(|sym| sym.to_string()).unwrap_or(String::new())) .collect::>(), }); - for index in used_in_function_signature { + for index in used_in_function_inputs { let postings = if index >= 0 { assert!(serialized_index.path_data[index as usize].is_some()); &mut serialized_index.type_data[index as usize] .as_mut() .unwrap() - .inverted_function_signature_index + .inverted_function_inputs_index } else { let generic_id = usize::try_from(-index).unwrap() - 1; for _ in serialized_index.generic_inverted_index.len()..=generic_id { @@ -1763,7 +1909,30 @@ pub(crate) fn build_index( while postings.len() <= search_type_size { postings.push(Vec::new()); } - postings[search_type_size].push(new_entry_id as u32); + if postings[search_type_size].last() != Some(&(new_entry_id as u32)) { + postings[search_type_size].push(new_entry_id as u32); + } + } + for index in used_in_function_output { + let postings = if index >= 0 { + assert!(serialized_index.path_data[index as usize].is_some()); + &mut serialized_index.type_data[index as usize] + .as_mut() + .unwrap() + .inverted_function_output_index + } else { + let generic_id = usize::try_from(-index).unwrap() - 1; + for _ in serialized_index.generic_inverted_index.len()..=generic_id { + serialized_index.generic_inverted_index.push(Vec::new()); + } + &mut serialized_index.generic_inverted_index[generic_id] + }; + while postings.len() <= search_type_size { + postings.push(Vec::new()); + } + if postings[search_type_size].last() != Some(&(new_entry_id as u32)) { + postings[search_type_size].push(new_entry_id as u32); + } } } } diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index 3ac10742e415..74f646008ebd 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -270,9 +270,12 @@ declare namespace rustdoc { */ interface TypeData { searchUnbox: boolean, - invertedFunctionSignatureIndex: RoaringBitmap[], + invertedFunctionInputsIndex: RoaringBitmap[], + invertedFunctionOutputIndex: RoaringBitmap[], } + type TypeInvertedIndexPolarity = "invertedFunctionInputsIndex" | "invertedFunctionOutputIndex"; + /** * A search entry of some sort. */ diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index b003bcc7bf9c..2854f65b5992 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -1696,7 +1696,7 @@ class DocSearch { } /** * function_signature, param_names - * @type {[string, number] | [number] | [string] | [] | null} + * @type {[string, string, number] | [string, string] | [] | null} */ const raw = JSON.parse(encoded); @@ -1705,32 +1705,46 @@ class DocSearch { } let searchUnbox = false; - const invertedFunctionSignatureIndex = []; + const invertedFunctionInputsIndex = []; + const invertedFunctionOutputIndex = []; if (typeof raw[0] === "string") { - if (raw[1]) { + if (raw[2]) { searchUnbox = true; } // the inverted function signature index is a list of bitmaps, // by number of types that appear in the function let i = 0; - const pb = makeUint8ArrayFromBase64(raw[0]); - const l = pb.length; + let pb = makeUint8ArrayFromBase64(raw[0]); + let l = pb.length; while (i < l) { if (pb[i] === 0) { - invertedFunctionSignatureIndex.push(RoaringBitmap.empty()); + invertedFunctionInputsIndex.push(RoaringBitmap.empty()); i += 1; } else { const bitmap = new RoaringBitmap(pb, i); i += bitmap.consumed_len_bytes; - invertedFunctionSignatureIndex.push(bitmap); + invertedFunctionInputsIndex.push(bitmap); + } + } + i = 0; + pb = makeUint8ArrayFromBase64(raw[1]); + l = pb.length; + while (i < l) { + if (pb[i] === 0) { + invertedFunctionOutputIndex.push(RoaringBitmap.empty()); + i += 1; + } else { + const bitmap = new RoaringBitmap(pb, i); + i += bitmap.consumed_len_bytes; + invertedFunctionOutputIndex.push(bitmap); } } } else if (raw[0]) { searchUnbox = true; } - return { searchUnbox, invertedFunctionSignatureIndex }; + return { searchUnbox, invertedFunctionInputsIndex, invertedFunctionOutputIndex }; } /** @@ -4009,9 +4023,10 @@ class DocSearch { * or anything else. This function returns all possible permutations. * * @param {rustdoc.ParserQueryElement|null} elem + * @param {rustdoc.TypeInvertedIndexPolarity} polarity * @returns {Promise[]>} */ - const unpackPostingsList = async elem => { + const unpackPostingsList = async(elem, polarity) => { if (!elem) { return empty_postings_list; } @@ -4039,7 +4054,7 @@ class DocSearch { const types = (await Promise.all(typePromises)) .filter(([_id, name, ty, path]) => name !== null && name.toLowerCase() === elem.pathLast && - ty && !ty.invertedFunctionSignatureIndex.every(bitmap => { + ty && !ty[polarity].every(bitmap => { return bitmap.isEmpty(); }) && path && path.ty !== TY_ASSOCTYPE && @@ -4078,7 +4093,7 @@ class DocSearch { this.getPathData(id), ]); if (name !== null && ty !== null && path !== null && - !ty.invertedFunctionSignatureIndex.every(bitmap => { + !ty[polarity].every(bitmap => { return bitmap.isEmpty(); }) && path.ty !== TY_ASSOCTYPE @@ -4176,18 +4191,18 @@ class DocSearch { /** @type {PostingsList[]} */ const results = []; for (const [id, _name, typeData] of types) { - if (!typeData || typeData.invertedFunctionSignatureIndex.every(bitmap => { + if (!typeData || typeData[polarity].every(bitmap => { return bitmap.isEmpty(); })) { continue; } - const upla = await unpackPostingsListAll(elem.generics); - const uplb = await unpackPostingsListBindings(elem.bindings); + const upla = await unpackPostingsListAll(elem.generics, polarity); + const uplb = await unpackPostingsListBindings(elem.bindings, polarity); for (const {invertedIndex: genericsIdx, queryElem: generics} of upla) { for (const {invertedIndex: bindingsIdx, queryElem: bindings} of uplb) { results.push({ invertedIndex: intersectInvertedIndexes( - typeData.invertedFunctionSignatureIndex, + typeData[polarity], genericsIdx, bindingsIdx, ), @@ -4219,15 +4234,16 @@ class DocSearch { * take the intersection of this bitmap. * * @param {(rustdoc.ParserQueryElement|null)[]|null} elems + * @param {rustdoc.TypeInvertedIndexPolarity} polarity * @returns {Promise[]>} */ - const unpackPostingsListAll = async elems => { + const unpackPostingsListAll = async(elems, polarity) => { if (!elems || elems.length === 0) { return nested_everything_postings_list; } const [firstPostingsList, remainingAll] = await Promise.all([ - unpackPostingsList(elems[0]), - unpackPostingsListAll(elems.slice(1)), + unpackPostingsList(elems[0], polarity), + unpackPostingsListAll(elems.slice(1), polarity), ]); /** @type {PostingsList[]} */ const results = []; @@ -4261,11 +4277,12 @@ class DocSearch { * Before passing an actual parser item to it, make sure to clone the map. * * @param {Map} elems + * @param {rustdoc.TypeInvertedIndexPolarity} polarity * @returns {Promise, * >[]>} */ - const unpackPostingsListBindings = async elems => { + const unpackPostingsListBindings = async(elems, polarity) => { if (!elems) { return [{ invertedIndex: everything_inverted_index, @@ -4296,8 +4313,8 @@ class DocSearch { } elems.delete(firstKey); const [firstPostingsList, remainingAll] = await Promise.all([ - unpackPostingsListAll(firstList), - unpackPostingsListBindings(elems), + unpackPostingsListAll(firstList, polarity), + unpackPostingsListBindings(elems, polarity), ]); /** @type {PostingsList>[]} */ const results = []; @@ -4335,8 +4352,8 @@ class DocSearch { // finally, we can do the actual unification loop const [allInputs, allOutput] = await Promise.all([ - unpackPostingsListAll(inputs), - unpackPostingsListAll(output), + unpackPostingsListAll(inputs, "invertedFunctionInputsIndex"), + unpackPostingsListAll(output, "invertedFunctionOutputIndex"), ]); let checkCounter = 0; /** From 199d2d4615ffb207ba6a5a4664800e63e6076f21 Mon Sep 17 00:00:00 2001 From: Mark Rousskov Date: Sat, 30 Aug 2025 14:06:06 -0400 Subject: [PATCH 58/68] Use absolute path to llvm-bolt, merge-fdata rather than PATH This unconditionally uses the provided LLVM toolchain's BOLT. I'm not sure that makes sense, but since we don't build BOLT as part of Rust's build of LLVM today, it's probably the right option for now. This avoids breaking the build on not being able to find the llvm-bolt executable. --- src/tools/opt-dist/src/bolt.rs | 5 +++-- src/tools/opt-dist/src/environment.rs | 8 ++++++++ src/tools/opt-dist/src/main.rs | 4 ++-- src/tools/opt-dist/src/training.rs | 3 ++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/tools/opt-dist/src/bolt.rs b/src/tools/opt-dist/src/bolt.rs index a06e59fcc412..3ee9912b8c26 100644 --- a/src/tools/opt-dist/src/bolt.rs +++ b/src/tools/opt-dist/src/bolt.rs @@ -9,6 +9,7 @@ use crate::utils::io::copy_file; /// Instruments an artifact at the given `path` (in-place) with BOLT and then calls `func`. /// After this function finishes, the original file will be restored. pub fn with_bolt_instrumented anyhow::Result, R>( + env: &Environment, path: &Utf8Path, func: F, ) -> anyhow::Result { @@ -26,7 +27,7 @@ pub fn with_bolt_instrumented anyhow::Result, R>( let profile_prefix = Utf8Path::from_path(&profile_prefix).unwrap(); // Instrument the original file with BOLT, saving the result into `instrumented_path` - cmd(&["llvm-bolt"]) + cmd(&[env.llvm_bolt().as_str()]) .arg("-instrument") .arg(path) .arg(&format!("--instrumentation-file={profile_prefix}")) @@ -61,7 +62,7 @@ pub fn bolt_optimize( let split_strategy = if env.host_tuple().starts_with("aarch64") { "profile2" } else { "cdsplit" }; - cmd(&["llvm-bolt"]) + cmd(&[env.llvm_bolt().as_str()]) .arg(temp_path.display()) .arg("-data") .arg(&profile.0) diff --git a/src/tools/opt-dist/src/environment.rs b/src/tools/opt-dist/src/environment.rs index 2cae0785f33b..7cc51901a83c 100644 --- a/src/tools/opt-dist/src/environment.rs +++ b/src/tools/opt-dist/src/environment.rs @@ -116,6 +116,14 @@ impl Environment { pub fn stage0(&self) -> Utf8PathBuf { self.stage0_root.clone().unwrap_or_else(|| self.build_artifacts().join("stage0")) } + + pub fn llvm_bolt(&self) -> Utf8PathBuf { + self.host_llvm_dir().join(format!("bin/llvm-bolt{}", executable_extension())) + } + + pub fn merge_fdata(&self) -> Utf8PathBuf { + self.host_llvm_dir().join(format!("bin/merge-fdata{}", executable_extension())) + } } /// What is the extension of binary executables on this platform? diff --git a/src/tools/opt-dist/src/main.rs b/src/tools/opt-dist/src/main.rs index 339c25552ada..48b25f235dd6 100644 --- a/src/tools/opt-dist/src/main.rs +++ b/src/tools/opt-dist/src/main.rs @@ -329,7 +329,7 @@ fn execute_pipeline( // FIXME(kobzol): try gather profiles together, at once for LLVM and rustc // Instrument the libraries and gather profiles - let llvm_profile = with_bolt_instrumented(&llvm_lib, |llvm_profile_dir| { + let llvm_profile = with_bolt_instrumented(env, &llvm_lib, |llvm_profile_dir| { stage.section("Gather profiles", |_| { gather_bolt_profiles(env, "LLVM", llvm_benchmarks(env), llvm_profile_dir) }) @@ -354,7 +354,7 @@ fn execute_pipeline( log::info!("Optimizing {rustc_lib} with BOLT"); // Instrument it and gather profiles - let rustc_profile = with_bolt_instrumented(&rustc_lib, |rustc_profile_dir| { + let rustc_profile = with_bolt_instrumented(env, &rustc_lib, |rustc_profile_dir| { stage.section("Gather profiles", |_| { gather_bolt_profiles(env, "rustc", rustc_benchmarks(env), rustc_profile_dir) }) diff --git a/src/tools/opt-dist/src/training.rs b/src/tools/opt-dist/src/training.rs index ae062d5c60c6..4f9352d11b12 100644 --- a/src/tools/opt-dist/src/training.rs +++ b/src/tools/opt-dist/src/training.rs @@ -195,7 +195,8 @@ pub fn gather_bolt_profiles( let profiles: Vec<_> = glob::glob(&format!("{profile_prefix}*"))?.collect::, _>>()?; - let mut merge_args = vec!["merge-fdata"]; + let fdata = env.merge_fdata(); + let mut merge_args = vec![fdata.as_str()]; merge_args.extend(profiles.iter().map(|p| p.to_str().unwrap())); with_log_group("Merging BOLT profiles", || { From 6ef0bfdb5cd874502aeaa7339fbe059b91ed35c3 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 30 Aug 2025 12:30:02 -0700 Subject: [PATCH 59/68] rustdoc-search: improve concurrency at type search --- src/librustdoc/html/static/js/search.js | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 2854f65b5992..fa812a2b67b5 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -4031,7 +4031,11 @@ class DocSearch { return empty_postings_list; } const typeFilter = itemTypeFromName(elem.typeFilter); - const searchResults = await index.search(elem.normalizedPathLast); + const [searchResults, upla, uplb] = await Promise.all([ + index.search(elem.normalizedPathLast), + unpackPostingsListAll(elem.generics, polarity), + unpackPostingsListBindings(elem.bindings, polarity), + ]); /** * @type {Promise<[ * number, @@ -4196,8 +4200,6 @@ class DocSearch { })) { continue; } - const upla = await unpackPostingsListAll(elem.generics, polarity); - const uplb = await unpackPostingsListBindings(elem.bindings, polarity); for (const {invertedIndex: genericsIdx, queryElem: generics} of upla) { for (const {invertedIndex: bindingsIdx, queryElem: bindings} of uplb) { results.push({ @@ -4303,19 +4305,23 @@ class DocSearch { queryElem: new Map(), }]; } - const firstKeyIds = await index.search(firstKey); + // HEADS UP! + // We must put this map back the way we found it before returning, + // otherwise things break. + elems.delete(firstKey); + const [firstKeyIds, firstPostingsList, remainingAll] = await Promise.all([ + index.search(firstKey), + unpackPostingsListAll(firstList, polarity), + unpackPostingsListBindings(elems, polarity), + ]); if (!firstKeyIds) { + elems.set(firstKey, firstList); // User specified a non-existent key. return [{ invertedIndex: empty_inverted_index, queryElem: new Map(), }]; } - elems.delete(firstKey); - const [firstPostingsList, remainingAll] = await Promise.all([ - unpackPostingsListAll(firstList, polarity), - unpackPostingsListBindings(elems, polarity), - ]); /** @type {PostingsList>[]} */ const results = []; for (const keyId of firstKeyIds.matches().entries()) { From 6d8d952e48689065931566209b5c992434fe0e82 Mon Sep 17 00:00:00 2001 From: joboet Date: Sun, 31 Aug 2025 13:46:52 +0200 Subject: [PATCH 60/68] std: fix `SplitPaths` regression --- library/std/src/sys/pal/unix/os.rs | 20 ++++++++++++++----- .../split-paths-may-dangle.rs | 11 ++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 tests/ui/type-alias-impl-trait/split-paths-may-dangle.rs diff --git a/library/std/src/sys/pal/unix/os.rs b/library/std/src/sys/pal/unix/os.rs index aec089f7e5c2..f0b6068e06c0 100644 --- a/library/std/src/sys/pal/unix/os.rs +++ b/library/std/src/sys/pal/unix/os.rs @@ -186,14 +186,24 @@ pub fn chdir(p: &path::Path) -> io::Result<()> { if result == 0 { Ok(()) } else { Err(io::Error::last_os_error()) } } -pub type SplitPaths<'a> = impl Iterator; +// This can't just be `impl Iterator` because that requires `'a` to be live on +// drop (see #146045). +pub type SplitPaths<'a> = iter::Map< + slice::Split<'a, u8, impl FnMut(&u8) -> bool + 'static>, + impl FnMut(&[u8]) -> PathBuf + 'static, +>; #[define_opaque(SplitPaths)] pub fn split_paths(unparsed: &OsStr) -> SplitPaths<'_> { - unparsed - .as_bytes() - .split(|&b| b == PATH_SEPARATOR) - .map(|part| PathBuf::from(OsStr::from_bytes(part))) + fn is_separator(&b: &u8) -> bool { + b == PATH_SEPARATOR + } + + fn into_pathbuf(part: &[u8]) -> PathBuf { + PathBuf::from(OsStr::from_bytes(part)) + } + + unparsed.as_bytes().split(is_separator).map(into_pathbuf) } #[derive(Debug)] diff --git a/tests/ui/type-alias-impl-trait/split-paths-may-dangle.rs b/tests/ui/type-alias-impl-trait/split-paths-may-dangle.rs new file mode 100644 index 000000000000..61c5342cdbb9 --- /dev/null +++ b/tests/ui/type-alias-impl-trait/split-paths-may-dangle.rs @@ -0,0 +1,11 @@ +// Regression test for issue #146045 - ensure that the TAIT `SplitPaths` does not +// require the borrowed string to be live. +//@ check-pass +//@ edition:2015 + +pub fn repro() -> Option { + let unparsed = std::ffi::OsString::new(); + std::env::split_paths(&unparsed).next() +} + +fn main() {} From 988c077b068b9e6fb4e58650d7e921993a650983 Mon Sep 17 00:00:00 2001 From: The Miri Cronjob Bot Date: Mon, 1 Sep 2025 04:55:23 +0000 Subject: [PATCH 61/68] Prepare for merging from rust-lang/rust This updates the rust-version file to 828e45ad11ce4ab56dd64e93f1fb5dd8f0c0ae93. --- src/tools/miri/rust-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/miri/rust-version b/src/tools/miri/rust-version index 695959e2e68a..594b47b888f9 100644 --- a/src/tools/miri/rust-version +++ b/src/tools/miri/rust-version @@ -1 +1 @@ -e004014d1bf4c29928a0f0f9f7d0964d43606cbd +828e45ad11ce4ab56dd64e93f1fb5dd8f0c0ae93 From 99583d1aeca1bdc8630e75e2cf6475b8386c711d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 16:30:02 +0200 Subject: [PATCH 62/68] Remove outdated comment --- src/bootstrap/src/core/build_steps/test.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 95be5360b0bb..f6a912f31900 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -3246,9 +3246,6 @@ impl Step for Distcheck { .map(|args| args.split(" ").map(|s| s.to_string()).collect::>()) .unwrap_or_default(); - // FIXME: unpack the source tarballs into a directory outside the source checkout, to - // ensure that it cannot access any local state - // Also ensure that it doesn't use download-ci-llvm command("tar") .arg("-xf") .arg(plain_src_tarball.tarball()) From 2bfff320bd7e0714fd70a7d8e4c2081a697180d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 20:46:54 +0200 Subject: [PATCH 63/68] Add some compiletest suites snapshot tests --- src/bootstrap/src/core/build_steps/test.rs | 13 +- src/bootstrap/src/core/builder/tests.rs | 155 +++++++++++++++++---- 2 files changed, 133 insertions(+), 35 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index f6a912f31900..e61be236c021 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -1476,12 +1476,6 @@ macro_rules! test { }), }) } - - fn metadata(&self) -> Option { - Some( - StepMetadata::test(stringify!($name), self.target) - ) - } } }; } @@ -2362,6 +2356,13 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} try_run_tests(builder, &mut cmd, false); } } + + fn metadata(&self) -> Option { + Some( + StepMetadata::test(&format!("compiletest-{}", self.suite), self.target) + .stage(self.compiler.stage), + ) + } } /// Runs the documentation tests for a book in `src/doc` using the `rustdoc` of `test_compiler`. diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index 43e67756e742..e6cf34ed499f 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2062,20 +2062,23 @@ mod snapshot { [build] rustc 0 -> rustc 1 [build] rustc 1 -> std 1 [build] rustc 0 -> Compiletest 1 - [test] Ui - [test] Crashes + [test] compiletest-ui 1 + [test] compiletest-crashes 1 [build] rustc 0 -> CoverageDump 1 + [test] compiletest-coverage 1 + [test] compiletest-coverage 1 [build] rustc 1 -> std 1 - [test] CodegenLlvm - [test] CodegenUnits - [test] AssemblyLlvm - [test] Incremental - [test] Debuginfo - [test] UiFullDeps + [test] compiletest-mir-opt 1 + [test] compiletest-codegen-llvm 1 + [test] compiletest-codegen-units 1 + [test] compiletest-assembly-llvm 1 + [test] compiletest-incremental 1 + [test] compiletest-debuginfo 1 + [test] compiletest-ui-fulldeps 1 [build] rustdoc 1 - [test] Rustdoc - [test] CoverageRunRustdoc - [test] Pretty + [test] compiletest-rustdoc 1 + [test] compiletest-coverage-run-rustdoc 1 + [test] compiletest-pretty 1 [build] rustc 1 -> std 1 [build] rustc 0 -> std 0 [test] rustc 0 -> CrateLibrustc 1 @@ -2113,16 +2116,107 @@ mod snapshot { [test] rustc 0 -> rust-analyzer 1 [build] rustc 0 -> RustdocTheme 1 [test] rustdoc-theme 1 - [test] RustdocUi + [test] compiletest-rustdoc-ui 1 [build] rustc 0 -> JsonDocCk 1 [build] rustc 0 -> JsonDocLint 1 - [test] RustdocJson + [test] compiletest-rustdoc-json 1 [doc] rustc 0 -> rustc 1 [build] rustc 0 -> HtmlChecker 1 [test] html-check [build] rustc 0 -> RunMakeSupport 1 [build] rustc 1 -> cargo 2 - [test] RunMake + [test] compiletest-run-make 1 + "); + } + + #[test] + fn test_compiletest_suites_stage1() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("test") + .args(&["ui", "ui-fulldeps", "run-make", "rustdoc", "rustdoc-gui", "incremental"]) + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 1 -> std 1 + [build] rustc 0 -> Compiletest 1 + [test] compiletest-ui 1 + [test] compiletest-ui-fulldeps 1 + [build] rustc 0 -> RunMakeSupport 1 + [build] rustc 1 -> cargo 2 + [build] rustdoc 1 + [test] compiletest-run-make 1 + [test] compiletest-rustdoc 1 + [build] rustc 0 -> RustdocGUITest 1 + [test] rustdoc-gui 1 + [test] compiletest-incremental 1 + [build] rustc 1 -> rustc 2 + "); + } + + #[test] + fn test_compiletest_suites_stage2() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("test") + .args(&["ui", "ui-fulldeps", "run-make", "rustdoc", "rustdoc-gui", "incremental"]) + .stage(2) + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 1 -> std 1 + [build] rustc 1 -> rustc 2 + [build] rustc 2 -> std 2 + [build] rustc 0 -> Compiletest 1 + [test] compiletest-ui 2 + [build] rustc 2 -> rustc 3 + [test] compiletest-ui-fulldeps 2 + [build] rustc 0 -> RunMakeSupport 1 + [build] rustc 2 -> cargo 3 + [build] rustdoc 2 + [test] compiletest-run-make 2 + [test] compiletest-rustdoc 2 + [build] rustc 0 -> RustdocGUITest 1 + [test] rustdoc-gui 2 + [test] compiletest-incremental 2 + [build] rustdoc 1 + "); + } + + #[test] + fn test_compiletest_suites_stage2_cross() { + let ctx = TestCtx::new(); + insta::assert_snapshot!( + ctx.config("test") + .hosts(&[TEST_TRIPLE_1]) + .targets(&[TEST_TRIPLE_1]) + .args(&["ui", "ui-fulldeps", "run-make", "rustdoc", "rustdoc-gui", "incremental"]) + .stage(2) + .render_steps(), @r" + [build] llvm + [build] rustc 0 -> rustc 1 + [build] rustc 1 -> std 1 + [build] rustc 1 -> rustc 2 + [build] rustc 2 -> std 2 + [build] rustc 0 -> Compiletest 1 + [build] rustc 1 -> std 1 + [build] rustc 2 -> std 2 + [test] compiletest-ui 2 + [build] llvm + [build] rustc 2 -> rustc 3 + [test] compiletest-ui-fulldeps 2 + [build] rustc 0 -> RunMakeSupport 1 + [build] rustc 2 -> cargo 3 + [build] rustdoc 2 + [test] compiletest-run-make 2 + [test] compiletest-rustdoc 2 + [build] rustc 0 -> RustdocGUITest 1 + [test] rustdoc-gui 2 + [test] compiletest-incremental 2 + [build] rustc 1 -> rustc 2 + [build] rustdoc 1 + [build] rustc 2 -> std 2 + [build] rustdoc 2 "); } @@ -2142,21 +2236,24 @@ mod snapshot { [build] rustc 1 -> rustc 2 [build] rustc 2 -> std 2 [build] rustc 0 -> Compiletest 1 - [test] Ui - [test] Crashes + [test] compiletest-ui 2 + [test] compiletest-crashes 2 [build] rustc 0 -> CoverageDump 1 + [test] compiletest-coverage 2 + [test] compiletest-coverage 2 [build] rustc 2 -> std 2 - [test] CodegenLlvm - [test] CodegenUnits - [test] AssemblyLlvm - [test] Incremental - [test] Debuginfo + [test] compiletest-mir-opt 2 + [test] compiletest-codegen-llvm 2 + [test] compiletest-codegen-units 2 + [test] compiletest-assembly-llvm 2 + [test] compiletest-incremental 2 + [test] compiletest-debuginfo 2 [build] rustc 2 -> rustc 3 - [test] UiFullDeps + [test] compiletest-ui-fulldeps 2 [build] rustdoc 2 - [test] Rustdoc - [test] CoverageRunRustdoc - [test] Pretty + [test] compiletest-rustdoc 2 + [test] compiletest-coverage-run-rustdoc 2 + [test] compiletest-pretty 2 [build] rustc 2 -> std 2 [build] rustc 1 -> std 1 [build] rustdoc 1 @@ -2196,16 +2293,16 @@ mod snapshot { [test] rustc 1 -> lint-docs 2 [build] rustc 0 -> RustdocTheme 1 [test] rustdoc-theme 2 - [test] RustdocUi + [test] compiletest-rustdoc-ui 2 [build] rustc 0 -> JsonDocCk 1 [build] rustc 0 -> JsonDocLint 1 - [test] RustdocJson + [test] compiletest-rustdoc-json 2 [doc] rustc 1 -> rustc 2 [build] rustc 0 -> HtmlChecker 1 [test] html-check [build] rustc 0 -> RunMakeSupport 1 [build] rustc 2 -> cargo 3 - [test] RunMake + [test] compiletest-run-make 2 "); } @@ -2249,7 +2346,7 @@ mod snapshot { let steps = ctx.config("test").args(&["--skip", "src/tools/tidy"]).get_steps(); let host = TargetSelection::from_user(&host_target()); - steps.assert_contains(StepMetadata::test("RustdocUi", host)); + steps.assert_contains(StepMetadata::test("compiletest-rustdoc-ui", host).stage(1)); steps.assert_not_contains(test::Tidy); } From 72167b726c49776b01cd84552ed90761a839ee7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 20:39:08 +0200 Subject: [PATCH 64/68] Rename `compiler` to `test_compiler` --- src/bootstrap/src/core/build_steps/test.rs | 101 +++++++++++---------- 1 file changed, 54 insertions(+), 47 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index e61be236c021..295ebfc3638d 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -1079,7 +1079,7 @@ impl Step for RustdocJSNotStd { fn run(self, builder: &Builder<'_>) { builder.ensure(Compiletest { - compiler: self.compiler, + test_compiler: self.compiler, target: self.target, mode: "rustdoc-js", suite: "rustdoc-js", @@ -1437,8 +1437,8 @@ macro_rules! test { $( #[$attr] )* #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct $name { - pub compiler: Compiler, - pub target: TargetSelection, + test_compiler: Compiler, + target: TargetSelection, } impl Step for $name { @@ -1456,14 +1456,14 @@ macro_rules! test { } fn make_run(run: RunConfig<'_>) { - let compiler = run.builder.compiler(run.builder.top_stage, run.build_triple()); + let test_compiler = run.builder.compiler(run.builder.top_stage, run.build_triple()); - run.builder.ensure($name { compiler, target: run.target }); + run.builder.ensure($name { test_compiler, target: run.target }); } fn run(self, builder: &Builder<'_>) { builder.ensure(Compiletest { - compiler: self.compiler, + test_compiler: self.test_compiler, target: self.target, mode: $mode, suite: $suite, @@ -1644,7 +1644,7 @@ impl Step for Coverage { // Like other compiletest suite test steps, delegate to an internal // compiletest task to actually run the tests. builder.ensure(Compiletest { - compiler, + test_compiler: compiler, target, mode, suite: Self::SUITE, @@ -1685,7 +1685,7 @@ impl Step for MirOpt { fn run(self, builder: &Builder<'_>) { let run = |target| { builder.ensure(Compiletest { - compiler: self.compiler, + test_compiler: self.compiler, target, mode: "mir-opt", suite: "mir-opt", @@ -1720,9 +1720,15 @@ impl Step for MirOpt { } } +/// Executes the `compiletest` tool to run a suite of tests. +/// +/// Compiles all tests with `test_compiler` for `target` with the specified +/// compiletest `mode` and `suite` arguments. For example `mode` can be +/// "run-pass" or `suite` can be something like `debuginfo`. #[derive(Debug, Clone, PartialEq, Eq, Hash)] struct Compiletest { - compiler: Compiler, + /// The compiler that we're testing. + test_compiler: Compiler, target: TargetSelection, mode: &'static str, suite: &'static str, @@ -1737,11 +1743,6 @@ impl Step for Compiletest { run.never() } - /// Executes the `compiletest` tool to run a suite of tests. - /// - /// Compiles all tests with `compiler` for `target` with the specified - /// compiletest `mode` and `suite` arguments. For example `mode` can be - /// "run-pass" or `suite` can be something like `debuginfo`. fn run(self, builder: &Builder<'_>) { if builder.doc_tests == DocTests::Only { return; @@ -1756,7 +1757,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the crate::exit!(1); } - let mut compiler = self.compiler; + let mut test_compiler = self.test_compiler; let target = self.target; let mode = self.mode; let suite = self.suite; @@ -1776,30 +1777,30 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the // NOTE: Only stage 1 is special cased because we need the rustc_private artifacts to match the // running compiler in stage 2 when plugins run. let query_compiler; - let (stage, stage_id) = if suite == "ui-fulldeps" && compiler.stage == 1 { + let (stage, stage_id) = if suite == "ui-fulldeps" && test_compiler.stage == 1 { // Even when using the stage 0 compiler, we also need to provide the stage 1 compiler // so that compiletest can query it for target information. - query_compiler = Some(compiler); + query_compiler = Some(test_compiler); // At stage 0 (stage - 1) we are using the stage0 compiler. Using `self.target` can lead // finding an incorrect compiler path on cross-targets, as the stage 0 is always equal to // `build.build` in the configuration. let build = builder.build.host_target; - compiler = builder.compiler(compiler.stage - 1, build); - let test_stage = compiler.stage + 1; + test_compiler = builder.compiler(test_compiler.stage - 1, build); + let test_stage = test_compiler.stage + 1; (test_stage, format!("stage{test_stage}-{build}")) } else { query_compiler = None; - let stage = compiler.stage; + let stage = test_compiler.stage; (stage, format!("stage{stage}-{target}")) }; if suite.ends_with("fulldeps") { - builder.ensure(compile::Rustc::new(compiler, target)); + builder.ensure(compile::Rustc::new(test_compiler, target)); } if suite == "debuginfo" { builder.ensure(dist::DebuggerScripts { - sysroot: builder.sysroot(compiler).to_path_buf(), + sysroot: builder.sysroot(test_compiler).to_path_buf(), target, }); } @@ -1809,20 +1810,22 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the // ensure that `libproc_macro` is available on the host. if suite == "mir-opt" { - builder.ensure(compile::Std::new(compiler, compiler.host).is_for_mir_opt_tests(true)); + builder.ensure( + compile::Std::new(test_compiler, test_compiler.host).is_for_mir_opt_tests(true), + ); } else { - builder.std(compiler, compiler.host); + builder.std(test_compiler, test_compiler.host); } let mut cmd = builder.tool_cmd(Tool::Compiletest); if suite == "mir-opt" { - builder.ensure(compile::Std::new(compiler, target).is_for_mir_opt_tests(true)); + builder.ensure(compile::Std::new(test_compiler, target).is_for_mir_opt_tests(true)); } else { - builder.std(compiler, target); + builder.std(test_compiler, target); } - builder.ensure(RemoteCopyLibs { build_compiler: compiler, target }); + builder.ensure(RemoteCopyLibs { build_compiler: test_compiler, target }); // compiletest currently has... a lot of arguments, so let's just pass all // of them! @@ -1830,9 +1833,9 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the cmd.arg("--stage").arg(stage.to_string()); cmd.arg("--stage-id").arg(stage_id); - cmd.arg("--compile-lib-path").arg(builder.rustc_libdir(compiler)); - cmd.arg("--run-lib-path").arg(builder.sysroot_target_libdir(compiler, target)); - cmd.arg("--rustc-path").arg(builder.rustc(compiler)); + cmd.arg("--compile-lib-path").arg(builder.rustc_libdir(test_compiler)); + cmd.arg("--run-lib-path").arg(builder.sysroot_target_libdir(test_compiler, target)); + cmd.arg("--rustc-path").arg(builder.rustc(test_compiler)); if let Some(query_compiler) = query_compiler { cmd.arg("--query-rustc-path").arg(builder.rustc(query_compiler)); } @@ -1849,14 +1852,16 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the // If we're using `--stage 0`, we should provide the bootstrap cargo. builder.initial_cargo.clone() } else { - builder.ensure(tool::Cargo::from_build_compiler(compiler, compiler.host)).tool_path + builder + .ensure(tool::Cargo::from_build_compiler(test_compiler, test_compiler.host)) + .tool_path }; cmd.arg("--cargo-path").arg(cargo_path); // We need to pass the compiler that was used to compile run-make-support, // because we have to use the same compiler to compile rmake.rs recipes. - let stage0_rustc_path = builder.compiler(0, compiler.host); + let stage0_rustc_path = builder.compiler(0, test_compiler.host); cmd.arg("--stage0-rustc-path").arg(builder.rustc(stage0_rustc_path)); } @@ -1868,7 +1873,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the || mode == "rustdoc-json" || suite == "coverage-run-rustdoc" { - cmd.arg("--rustdoc-path").arg(builder.rustdoc_for_compiler(compiler)); + cmd.arg("--rustdoc-path").arg(builder.rustdoc_for_compiler(test_compiler)); } if mode == "rustdoc-json" { @@ -1893,14 +1898,14 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the // directory immediately under the root build directory, and the test-suite-specific build // directory. cmd.arg("--build-root").arg(&builder.out); - cmd.arg("--build-test-suite-root").arg(testdir(builder, compiler.host).join(suite)); + cmd.arg("--build-test-suite-root").arg(testdir(builder, test_compiler.host).join(suite)); // When top stage is 0, that means that we're testing an externally provided compiler. // In that case we need to use its specific sysroot for tests to pass. let sysroot = if builder.top_stage == 0 { builder.initial_sysroot.clone() } else { - builder.sysroot(compiler) + builder.sysroot(test_compiler) }; cmd.arg("--sysroot-base").arg(sysroot); @@ -1908,11 +1913,11 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the cmd.arg("--suite").arg(suite); cmd.arg("--mode").arg(mode); cmd.arg("--target").arg(target.rustc_target_arg()); - cmd.arg("--host").arg(&*compiler.host.triple); + cmd.arg("--host").arg(&*test_compiler.host.triple); cmd.arg("--llvm-filecheck").arg(builder.llvm_filecheck(builder.config.host_target)); if let Some(codegen_backend) = builder.config.cmd.test_codegen_backend() { - if !builder.config.enabled_codegen_backends(compiler.host).contains(codegen_backend) { + if !builder.config.enabled_codegen_backends(test_compiler.host).contains(codegen_backend) { eprintln!( "\ ERROR: No configured backend named `{name}` @@ -1931,7 +1936,7 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} // Tells compiletest which codegen backend to use. // It is used to e.g. ignore tests that don't support that codegen backend. cmd.arg("--default-codegen-backend") - .arg(builder.config.default_codegen_backend(compiler.host).name()); + .arg(builder.config.default_codegen_backend(test_compiler.host).name()); } if builder.build.config.llvm_enzyme { @@ -2011,7 +2016,7 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} if let Some(linker) = builder.linker(target) { cmd.arg("--target-linker").arg(linker); } - if let Some(linker) = builder.linker(compiler.host) { + if let Some(linker) = builder.linker(test_compiler.host) { cmd.arg("--host-linker").arg(linker); } } @@ -2022,16 +2027,18 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} } let mut hostflags = flags.clone(); - hostflags.extend(linker_flags(builder, compiler.host, LldThreads::No)); + hostflags.extend(linker_flags(builder, test_compiler.host, LldThreads::No)); let mut targetflags = flags; // Provide `rust_test_helpers` for both host and target. if suite == "ui" || suite == "incremental" { - builder.ensure(TestHelpers { target: compiler.host }); + builder.ensure(TestHelpers { target: test_compiler.host }); builder.ensure(TestHelpers { target }); - hostflags - .push(format!("-Lnative={}", builder.test_helpers_out(compiler.host).display())); + hostflags.push(format!( + "-Lnative={}", + builder.test_helpers_out(test_compiler.host).display() + )); targetflags.push(format!("-Lnative={}", builder.test_helpers_out(target).display())); } @@ -2116,7 +2123,7 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} let mut llvm_components_passed = false; let mut copts_passed = false; - if builder.config.llvm_enabled(compiler.host) { + if builder.config.llvm_enabled(test_compiler.host) { let llvm::LlvmResult { host_llvm_config, .. } = builder.ensure(llvm::Llvm { target: builder.config.host_target }); if !builder.config.dry_run() { @@ -2327,7 +2334,7 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} format!("compiletest suite={suite} mode={mode}"), // FIXME: compiletest sometimes behaves as ToolStd, we could expose that difference here Mode::ToolBootstrap, - compiler, + test_compiler, target, ); try_run_tests(builder, &mut cmd, false); @@ -2350,7 +2357,7 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} builder.info(&format!( "Check compiletest suite={} mode={} compare_mode={} ({} -> {})", - suite, mode, compare_mode, &compiler.host, target + suite, mode, compare_mode, &test_compiler.host, target )); let _time = helpers::timeit(builder); try_run_tests(builder, &mut cmd, false); @@ -2360,7 +2367,7 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} fn metadata(&self) -> Option { Some( StepMetadata::test(&format!("compiletest-{}", self.suite), self.target) - .stage(self.compiler.stage), + .stage(self.test_compiler.stage), ) } } From 8e97112ba83c97891bcaafa13f099bd22ccd29f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 20:39:15 +0200 Subject: [PATCH 65/68] Remove `Compiler::with_stage` --- src/bootstrap/src/core/build_steps/test.rs | 2 +- src/bootstrap/src/lib.rs | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 295ebfc3638d..3938c6403213 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -1878,7 +1878,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the if mode == "rustdoc-json" { // Use the stage0 compiler for jsondocck - let json_compiler = compiler.with_stage(0); + let json_compiler = builder.compiler(0, builder.host_target); cmd.arg("--jsondocck-path") .arg(builder.ensure(tool::JsonDocCk { compiler: json_compiler, target }).tool_path); cmd.arg("--jsondoclint-path").arg( diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 9a882eae08ed..2108f3b8cf5d 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -2109,11 +2109,6 @@ impl Compiler { self.forced_compiler = forced_compiler; } - pub fn with_stage(mut self, stage: u32) -> Compiler { - self.stage = stage; - self - } - /// Returns `true` if this is a snapshot compiler for `build`'s configuration pub fn is_snapshot(&self, build: &Build) -> bool { self.stage == 0 && self.host == build.host_target From 6c3a86b73b6ce93f392768f5cdc9da9bd6283b32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Wed, 20 Aug 2025 20:42:08 +0200 Subject: [PATCH 66/68] Do not run `lint-docs` tests in stage 1 `x test` by default --- src/bootstrap/src/core/build_steps/test.rs | 9 ++++++--- src/bootstrap/src/core/builder/tests.rs | 10 +++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 3938c6403213..97db9bf536fd 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -1848,12 +1848,15 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the let is_rustdoc = suite == "rustdoc-ui" || suite == "rustdoc-js"; if mode == "run-make" { - let cargo_path = if builder.top_stage == 0 { + let cargo_path = if test_compiler.stage == 0 { // If we're using `--stage 0`, we should provide the bootstrap cargo. builder.initial_cargo.clone() } else { builder - .ensure(tool::Cargo::from_build_compiler(test_compiler, test_compiler.host)) + .ensure(tool::Cargo::from_build_compiler( + builder.compiler(test_compiler.stage - 1, test_compiler.host), + test_compiler.host, + )) .tool_path }; @@ -1902,7 +1905,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the // When top stage is 0, that means that we're testing an externally provided compiler. // In that case we need to use its specific sysroot for tests to pass. - let sysroot = if builder.top_stage == 0 { + let sysroot = if test_compiler.stage == 0 { builder.initial_sysroot.clone() } else { builder.sysroot(test_compiler) diff --git a/src/bootstrap/src/core/builder/tests.rs b/src/bootstrap/src/core/builder/tests.rs index e6cf34ed499f..ef01f14f5e9a 100644 --- a/src/bootstrap/src/core/builder/tests.rs +++ b/src/bootstrap/src/core/builder/tests.rs @@ -2124,7 +2124,7 @@ mod snapshot { [build] rustc 0 -> HtmlChecker 1 [test] html-check [build] rustc 0 -> RunMakeSupport 1 - [build] rustc 1 -> cargo 2 + [build] rustc 0 -> cargo 1 [test] compiletest-run-make 1 "); } @@ -2143,7 +2143,7 @@ mod snapshot { [test] compiletest-ui 1 [test] compiletest-ui-fulldeps 1 [build] rustc 0 -> RunMakeSupport 1 - [build] rustc 1 -> cargo 2 + [build] rustc 0 -> cargo 1 [build] rustdoc 1 [test] compiletest-run-make 1 [test] compiletest-rustdoc 1 @@ -2172,7 +2172,7 @@ mod snapshot { [build] rustc 2 -> rustc 3 [test] compiletest-ui-fulldeps 2 [build] rustc 0 -> RunMakeSupport 1 - [build] rustc 2 -> cargo 3 + [build] rustc 1 -> cargo 2 [build] rustdoc 2 [test] compiletest-run-make 2 [test] compiletest-rustdoc 2 @@ -2206,7 +2206,7 @@ mod snapshot { [build] rustc 2 -> rustc 3 [test] compiletest-ui-fulldeps 2 [build] rustc 0 -> RunMakeSupport 1 - [build] rustc 2 -> cargo 3 + [build] rustc 1 -> cargo 2 [build] rustdoc 2 [test] compiletest-run-make 2 [test] compiletest-rustdoc 2 @@ -2301,7 +2301,7 @@ mod snapshot { [build] rustc 0 -> HtmlChecker 1 [test] html-check [build] rustc 0 -> RunMakeSupport 1 - [build] rustc 2 -> cargo 3 + [build] rustc 1 -> cargo 2 [test] compiletest-run-make 2 "); } From 6f5fc69c7ebca3e48073ba81fdd3c3ccfe1f2bf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Mon, 1 Sep 2025 09:14:27 +0200 Subject: [PATCH 67/68] Consolidate staging of test steps in message groups --- src/bootstrap/src/core/build_steps/test.rs | 163 +++++++-------------- src/bootstrap/src/lib.rs | 58 ++++---- 2 files changed, 88 insertions(+), 133 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index 97db9bf536fd..cdcbe29795df 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -96,7 +96,7 @@ impl Step for CrateBootstrap { ); let crate_name = path.rsplit_once('/').unwrap().1; - run_cargo_test(cargo, &[], &[], crate_name, bootstrap_host, builder, Mode::ToolBootstrap); + run_cargo_test(cargo, &[], &[], crate_name, bootstrap_host, builder); } fn metadata(&self) -> Option { @@ -153,15 +153,7 @@ You can skip linkcheck with --skip src/tools/linkchecker" SourceType::InTree, &[], ); - run_cargo_test( - cargo, - &[], - &[], - "linkchecker self tests", - bootstrap_host, - builder, - Mode::ToolBootstrap, - ); + run_cargo_test(cargo, &[], &[], "linkchecker self tests", bootstrap_host, builder); if builder.doc_tests == DocTests::No { return; @@ -174,7 +166,7 @@ You can skip linkcheck with --skip src/tools/linkchecker" let linkchecker = builder.tool_cmd(Tool::Linkchecker); // Run the linkchecker. - let _guard = builder.msg(Kind::Test, "Linkcheck", None, compiler, bootstrap_host); + let _guard = builder.msg_test("Linkcheck", bootstrap_host, 1); let _time = helpers::timeit(builder); linkchecker.delay_failure().arg(builder.out.join(host).join("doc")).run(builder); } @@ -482,7 +474,7 @@ impl Step for RustAnalyzer { cargo.env("SKIP_SLOW_TESTS", "1"); cargo.add_rustc_lib_path(builder); - run_cargo_test(cargo, &[], &[], "rust-analyzer", host, builder, Mode::ToolRustc); + run_cargo_test(cargo, &[], &[], "rust-analyzer", host, builder); } fn metadata(&self) -> Option { @@ -540,7 +532,7 @@ impl Step for Rustfmt { cargo.add_rustc_lib_path(builder); - run_cargo_test(cargo, &[], &[], "rustfmt", target, builder, Mode::ToolRustc); + run_cargo_test(cargo, &[], &[], "rustfmt", target, builder); } fn metadata(&self) -> Option { @@ -679,8 +671,7 @@ impl Step for Miri { cargo.env("MIRI_TEST_TARGET", target.rustc_target_arg()); { - let _guard = - builder.msg(Kind::Test, "miri", Mode::ToolRustc, miri.build_compiler, target); + let _guard = builder.msg_test("miri", target, target_compiler.stage); let _time = helpers::timeit(builder); cargo.run(builder); } @@ -696,13 +687,8 @@ impl Step for Miri { cargo.args(["tests/pass", "tests/panic"]); { - let _guard = builder.msg( - Kind::Test, - "miri (mir-opt-level 4)", - Mode::ToolRustc, - miri.build_compiler, - target, - ); + let _guard = + builder.msg_test("miri (mir-opt-level 4)", target, target_compiler.stage); let _time = helpers::timeit(builder); cargo.run(builder); } @@ -772,8 +758,7 @@ impl Step for CargoMiri { // Finally, run everything. let mut cargo = BootstrapCommand::from(cargo); { - let _guard = - builder.msg(Kind::Test, "cargo-miri", Mode::ToolRustc, (host, stage), target); + let _guard = builder.msg_test("cargo-miri", target, stage); let _time = helpers::timeit(builder); cargo.run(builder); } @@ -833,7 +818,7 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the cargo.env("TEST_RUSTC", builder.rustc(compiler)); cargo.allow_features(COMPILETEST_ALLOW_FEATURES); - run_cargo_test(cargo, &[], &[], "compiletest self test", host, builder, Mode::ToolStd); + run_cargo_test(cargo, &[], &[], "compiletest self test", host, builder); } } @@ -916,7 +901,7 @@ impl Step for Clippy { cargo.add_rustc_lib_path(builder); let cargo = prepare_cargo_test(cargo, &[], &[], target, builder); - let _guard = builder.msg(Kind::Test, "clippy", Mode::ToolRustc, build_compiler, target); + let _guard = builder.msg_test("clippy", target, target_compiler.stage); // Clippy reports errors if it blessed the outputs if cargo.allow_failure().run(builder) { @@ -1046,8 +1031,7 @@ impl Step for RustdocJSStd { self.target, DocumentationFormat::Html, )); - let _guard = - builder.msg(Kind::Test, "rustdoc-js-std", None, self.build_compiler, self.target); + let _guard = builder.msg_test("rustdoc-js-std", self.target, self.build_compiler.stage); command.run(builder); } @@ -1200,7 +1184,7 @@ impl Step for RustdocGUI { } let _time = helpers::timeit(builder); - let _guard = builder.msg_test("rustdoc-gui", (self.target, self.test_compiler.stage)); + let _guard = builder.msg_test("rustdoc-gui", self.target, self.test_compiler.stage); try_run_tests(builder, &mut cmd, true); } @@ -1359,15 +1343,7 @@ impl Step for CrateRunMakeSupport { &[], ); cargo.allow_features("test"); - run_cargo_test( - cargo, - &[], - &[], - "run-make-support self test", - host, - builder, - Mode::ToolBootstrap, - ); + run_cargo_test(cargo, &[], &[], "run-make-support self test", host, builder); } } @@ -1404,15 +1380,7 @@ impl Step for CrateBuildHelper { &[], ); cargo.allow_features("test"); - run_cargo_test( - cargo, - &[], - &[], - "build_helper self test", - host, - builder, - Mode::ToolBootstrap, - ); + run_cargo_test(cargo, &[], &[], "build_helper self test", host, builder); } } @@ -1724,7 +1692,7 @@ impl Step for MirOpt { /// /// Compiles all tests with `test_compiler` for `target` with the specified /// compiletest `mode` and `suite` arguments. For example `mode` can be -/// "run-pass" or `suite` can be something like `debuginfo`. +/// "mir-opt" and `suite` can be something like "debuginfo". #[derive(Debug, Clone, PartialEq, Eq, Hash)] struct Compiletest { /// The compiler that we're testing. @@ -1920,7 +1888,11 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the cmd.arg("--llvm-filecheck").arg(builder.llvm_filecheck(builder.config.host_target)); if let Some(codegen_backend) = builder.config.cmd.test_codegen_backend() { - if !builder.config.enabled_codegen_backends(test_compiler.host).contains(codegen_backend) { + if !builder + .config + .enabled_codegen_backends(test_compiler.host) + .contains(codegen_backend) + { eprintln!( "\ ERROR: No configured backend named `{name}` @@ -2326,19 +2298,16 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} mode: mode.into(), compare_mode: None, target: self.target.triple.to_string(), - host: self.compiler.host.triple.to_string(), - stage: self.compiler.stage, + host: self.test_compiler.host.triple.to_string(), + stage: self.test_compiler.stage, }, builder, ); - let _group = builder.msg( - Kind::Test, - format!("compiletest suite={suite} mode={mode}"), - // FIXME: compiletest sometimes behaves as ToolStd, we could expose that difference here - Mode::ToolBootstrap, - test_compiler, + let _group = builder.msg_test( + format!("with compiletest suite={suite} mode={mode}"), target, + test_compiler.stage, ); try_run_tests(builder, &mut cmd, false); @@ -2352,8 +2321,8 @@ HELP: You can add it into `bootstrap.toml` in `rust.codegen-backends = [{name:?} mode: mode.into(), compare_mode: Some(compare_mode.into()), target: self.target.triple.to_string(), - host: self.compiler.host.triple.to_string(), - stage: self.compiler.stage, + host: self.test_compiler.host.triple.to_string(), + stage: self.test_compiler.stage, }, builder, ); @@ -2482,12 +2451,10 @@ impl BookTest { } builder.add_rust_test_threads(&mut rustbook_cmd); - let _guard = builder.msg( - Kind::Test, + let _guard = builder.msg_test( format_args!("mdbook {}", self.path.display()), - None, - test_compiler, test_compiler.host, + test_compiler.stage, ); let _time = helpers::timeit(builder); let toolstate = if rustbook_cmd.delay_failure().run(builder) { @@ -2505,12 +2472,10 @@ impl BookTest { builder.std(test_compiler, host); - let _guard = builder.msg( - Kind::Test, + let _guard = builder.msg_test( format!("book {}", self.name), - None, - (test_compiler.host, test_compiler.stage - 1), - host, + test_compiler.host, + test_compiler.stage, ); // Do a breadth-first traversal of the `src/doc` directory and just run @@ -2653,13 +2618,7 @@ impl Step for ErrorIndex { let mut tool = tool::ErrorIndex::command(builder, self.compilers); tool.arg("markdown").arg(&output); - let guard = builder.msg( - Kind::Test, - "error-index", - None, - self.compilers.build_compiler(), - target_compiler.host, - ); + let guard = builder.msg_test("error-index", target_compiler.host, target_compiler.stage); let _time = helpers::timeit(builder); tool.run_capture(builder); drop(guard); @@ -2755,14 +2714,12 @@ fn run_cargo_test<'a>( description: impl Into>, target: TargetSelection, builder: &Builder<'_>, - mode: impl Into>, ) -> bool { - let mode = mode.into(); let compiler = cargo.compiler(); let mut cargo = prepare_cargo_test(cargo, libtest_args, crates, target, builder); let _time = helpers::timeit(builder); let _group = - description.into().and_then(|what| builder.msg(Kind::Test, what, mode, compiler, target)); + description.into().and_then(|what| builder.msg_test(what, target, compiler.stage + 1)); #[cfg(feature = "build-metrics")] builder.metrics.begin_test_suite( @@ -2989,15 +2946,7 @@ impl Step for Crate { crates.push("alloctests".to_owned()); } - run_cargo_test( - cargo, - &[], - &crates, - &*crate_description(&self.crates), - target, - builder, - mode, - ); + run_cargo_test(cargo, &[], &crates, &*crate_description(&self.crates), target, builder); } } @@ -3091,15 +3040,7 @@ impl Step for CrateRustdoc { dylib_path.insert(0, PathBuf::from(&*libdir)); cargo.env(dylib_path_var(), env::join_paths(&dylib_path).unwrap()); - run_cargo_test( - cargo, - &[], - &["rustdoc:0.0.0".to_string()], - "rustdoc", - target, - builder, - Mode::ToolRustc, - ); + run_cargo_test(cargo, &[], &["rustdoc:0.0.0".to_string()], "rustdoc", target, builder); } } @@ -3158,7 +3099,6 @@ impl Step for CrateRustdocJsonTypes { "rustdoc-json-types", target, builder, - Mode::ToolTarget, ); } } @@ -3316,8 +3256,6 @@ impl Step for Bootstrap { fn run(self, builder: &Builder<'_>) { let host = builder.config.host_target; let build_compiler = builder.compiler(0, host); - let _guard = - builder.msg(Kind::Test, "bootstrap", Mode::ToolBootstrap, build_compiler, host); // Some tests require cargo submodule to be present. builder.build.require_submodule("src/tools/cargo", None); @@ -3356,7 +3294,7 @@ impl Step for Bootstrap { // bootstrap tests are racy on directory creation so just run them one at a time. // Since there's not many this shouldn't be a problem. - run_cargo_test(cargo, &["--test-threads=1"], &[], None, host, builder, Mode::ToolBootstrap); + run_cargo_test(cargo, &["--test-threads=1"], &[], None, host, builder); } fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { @@ -3416,7 +3354,11 @@ impl Step for TierCheck { cargo.arg("--verbose"); } - let _guard = builder.msg_test("platform support check", self.test_compiler); + let _guard = builder.msg_test( + "platform support check", + self.test_compiler.host, + self.test_compiler.stage, + ); BootstrapCommand::from(cargo).delay_failure().run(builder); } @@ -3495,9 +3437,8 @@ impl Step for RustInstaller { &[], ); - let _guard = - builder.msg(Kind::Test, "rust-installer", None, build_compiler, bootstrap_host); - run_cargo_test(cargo, &[], &[], None, bootstrap_host, builder, Mode::ToolBootstrap); + let _guard = builder.msg_test("rust-installer", bootstrap_host, 1); + run_cargo_test(cargo, &[], &[], None, bootstrap_host, builder); // We currently don't support running the test.sh script outside linux(?) environments. // Eventually this should likely migrate to #[test]s in rust-installer proper rather than a @@ -3668,7 +3609,11 @@ impl Step for CodegenCranelift { // Avoid incremental cache issues when changing rustc cargo.env("CARGO_BUILD_INCREMENTAL", "false"); - let _guard = builder.msg_test("rustc_codegen_cranelift", target_compiler); + let _guard = builder.msg_test( + "rustc_codegen_cranelift", + target_compiler.host, + target_compiler.stage, + ); // FIXME handle vendoring for source tarballs before removing the --skip-test below let download_dir = builder.out.join("cg_clif_download"); @@ -3763,7 +3708,11 @@ impl Step for CodegenGCC { .extra_rust_args(&["-Csymbol-mangling-version=v0", "-Cpanic=abort"]), ); - let _guard = builder.msg_test("rustc_codegen_gcc", compilers.build_compiler()); + let _guard = builder.msg_test( + "rustc_codegen_gcc", + compilers.target(), + compilers.target_compiler().stage, + ); let mut cargo = builder::Cargo::new( builder, @@ -3868,7 +3817,7 @@ impl Step for TestFloatParse { ); cargo_test.allow_features(TEST_FLOAT_PARSE_ALLOW_FEATURES); - run_cargo_test(cargo_test, &[], &[], "test-float-parse", target, builder, Mode::ToolStd); + run_cargo_test(cargo_test, &[], &[], "test-float-parse", target, builder); // Run the actual parse tests. let mut cargo_run = tool::prepare_tool_cargo( diff --git a/src/bootstrap/src/lib.rs b/src/bootstrap/src/lib.rs index 2108f3b8cf5d..59c0f9faacac 100644 --- a/src/bootstrap/src/lib.rs +++ b/src/bootstrap/src/lib.rs @@ -426,22 +426,22 @@ forward! { download_rustc() -> bool, } -/// A mostly temporary helper struct before we can migrate everything in bootstrap to use -/// the concept of a build compiler. -struct HostAndStage { - host: TargetSelection, +/// An alternative way of specifying what target and stage is involved in some bootstrap activity. +/// Ideally using a `Compiler` directly should be preferred. +struct TargetAndStage { + target: TargetSelection, stage: u32, } -impl From<(TargetSelection, u32)> for HostAndStage { - fn from((host, stage): (TargetSelection, u32)) -> Self { - Self { host, stage } +impl From<(TargetSelection, u32)> for TargetAndStage { + fn from((target, stage): (TargetSelection, u32)) -> Self { + Self { target, stage } } } -impl From for HostAndStage { +impl From for TargetAndStage { fn from(compiler: Compiler) -> Self { - Self { host: compiler.host, stage: compiler.stage } + Self { target: compiler.host, stage: compiler.stage } } } @@ -1109,11 +1109,12 @@ impl Build { /// Return a `Group` guard for a [`Step`] that: /// - Performs `action` + /// - If the action is `Kind::Test`, use [`Build::msg_test`] instead. /// - On `what` /// - Where `what` possibly corresponds to a `mode` - /// - `action` is performed using the given build compiler (`host_and_stage`). - /// - Since some steps do not use the concept of a build compiler yet, it is also possible - /// to pass the host and stage explicitly. + /// - `action` is performed with/on the given compiler (`target_and_stage`). + /// - Since for some steps it is not possible to pass a single compiler here, it is also + /// possible to pass the host and stage explicitly. /// - With a given `target`. /// /// [`Step`]: crate::core::builder::Step @@ -1124,13 +1125,19 @@ impl Build { action: impl Into, what: impl Display, mode: impl Into>, - host_and_stage: impl Into, + target_and_stage: impl Into, target: impl Into>, ) -> Option { - let host_and_stage = host_and_stage.into(); + let target_and_stage = target_and_stage.into(); + let action = action.into(); + assert!( + action != Kind::Test, + "Please use `Build::msg_test` instead of `Build::msg(Kind::Test)`" + ); + let actual_stage = match mode.into() { // Std has the same stage as the compiler that builds it - Some(Mode::Std) => host_and_stage.stage, + Some(Mode::Std) => target_and_stage.stage, // Other things have stage corresponding to their build compiler + 1 Some( Mode::Rustc @@ -1140,18 +1147,18 @@ impl Build { | Mode::ToolStd | Mode::ToolRustc, ) - | None => host_and_stage.stage + 1, + | None => target_and_stage.stage + 1, }; - let action = action.into().description(); + let action = action.description(); let what = what.to_string(); let msg = |fmt| { let space = if !what.is_empty() { " " } else { "" }; format!("{action} stage{actual_stage} {what}{space}{fmt}") }; let msg = if let Some(target) = target.into() { - let build_stage = host_and_stage.stage; - let host = host_and_stage.host; + let build_stage = target_and_stage.stage; + let host = target_and_stage.target; if host == target { msg(format_args!("(stage{build_stage} -> stage{actual_stage}, {target})")) } else { @@ -1163,10 +1170,9 @@ impl Build { self.group(&msg) } - /// Return a `Group` guard for a [`Step`] that tests `what` with the given `stage` and `target` - /// (determined by `host_and_stage`). - /// Use this instead of [`Build::msg`] when there is no clear `build_compiler` to be - /// determined. + /// Return a `Group` guard for a [`Step`] that tests `what` with the given `stage` and `target`. + /// Use this instead of [`Build::msg`] for test steps, because for them it is not always clear + /// what exactly is a build compiler. /// /// [`Step`]: crate::core::builder::Step #[must_use = "Groups should not be dropped until the Step finishes running"] @@ -1174,11 +1180,11 @@ impl Build { fn msg_test( &self, what: impl Display, - host_and_stage: impl Into, + target: TargetSelection, + stage: u32, ) -> Option { - let HostAndStage { host, stage } = host_and_stage.into(); let action = Kind::Test.description(); - let msg = format!("{action} stage{stage} {what} ({host})"); + let msg = format!("{action} stage{stage} {what} ({target})"); self.group(&msg) } From af627f557ad4806e1de1b4800fd1a7e5a42420a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Mon, 1 Sep 2025 18:39:44 +0200 Subject: [PATCH 68/68] Fix ui-fulldeps --- src/bootstrap/src/core/build_steps/test.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/bootstrap/src/core/build_steps/test.rs b/src/bootstrap/src/core/build_steps/test.rs index cdcbe29795df..ee2cbe9385e4 100644 --- a/src/bootstrap/src/core/build_steps/test.rs +++ b/src/bootstrap/src/core/build_steps/test.rs @@ -1873,7 +1873,9 @@ NOTE: if you're sure you want to do this, please open an issue as to why. In the // When top stage is 0, that means that we're testing an externally provided compiler. // In that case we need to use its specific sysroot for tests to pass. - let sysroot = if test_compiler.stage == 0 { + // Note: DO NOT check if test_compiler.stage is 0, because the test compiler can be stage 0 + // even if the top stage is 1 (when we run the ui-fulldeps suite). + let sysroot = if builder.top_stage == 0 { builder.initial_sysroot.clone() } else { builder.sysroot(test_compiler)