From a8bff87cfb15396b4ddf80743652b7e234bd0100 Mon Sep 17 00:00:00 2001 From: Taylor Cramer Date: Fri, 28 Feb 2025 14:04:54 -0800 Subject: [PATCH 01/29] Stabilize [T]::split_off... methods This was previously known as the slice_take feature. --- library/core/src/slice/mod.rs | 34 +++++++--------------------------- library/coretests/tests/lib.rs | 1 - 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/library/core/src/slice/mod.rs b/library/core/src/slice/mod.rs index 7a2764206e8db..f991cc4ae2dbb 100644 --- a/library/core/src/slice/mod.rs +++ b/library/core/src/slice/mod.rs @@ -78,7 +78,7 @@ pub use raw::{from_raw_parts, from_raw_parts_mut}; /// Calculates the direction and split point of a one-sided range. /// -/// This is a helper function for `take` and `take_mut` that returns +/// This is a helper function for `split_off` and `split_off_mut` that returns /// the direction of the split (front or back) as well as the index at /// which to split. Returns `None` if the split index would overflow. #[inline] @@ -4313,8 +4313,6 @@ impl [T] { /// Splitting off the first three elements of a slice: /// /// ``` - /// #![feature(slice_take)] - /// /// let mut slice: &[_] = &['a', 'b', 'c', 'd']; /// let mut first_three = slice.split_off(..3).unwrap(); /// @@ -4325,8 +4323,6 @@ impl [T] { /// Splitting off the last two elements of a slice: /// /// ``` - /// #![feature(slice_take)] - /// /// let mut slice: &[_] = &['a', 'b', 'c', 'd']; /// let mut tail = slice.split_off(2..).unwrap(); /// @@ -4337,8 +4333,6 @@ impl [T] { /// Getting `None` when `range` is out of bounds: /// /// ``` - /// #![feature(slice_take)] - /// /// let mut slice: &[_] = &['a', 'b', 'c', 'd']; /// /// assert_eq!(None, slice.split_off(5..)); @@ -4349,7 +4343,7 @@ impl [T] { /// ``` #[inline] #[must_use = "method does not modify the slice if the range is out of bounds"] - #[unstable(feature = "slice_take", issue = "62280")] + #[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")] pub fn split_off<'a, R: OneSidedRange>( self: &mut &'a Self, range: R, @@ -4385,8 +4379,6 @@ impl [T] { /// Splitting off the first three elements of a slice: /// /// ``` - /// #![feature(slice_take)] - /// /// let mut slice: &mut [_] = &mut ['a', 'b', 'c', 'd']; /// let mut first_three = slice.split_off_mut(..3).unwrap(); /// @@ -4397,8 +4389,6 @@ impl [T] { /// Taking the last two elements of a slice: /// /// ``` - /// #![feature(slice_take)] - /// /// let mut slice: &mut [_] = &mut ['a', 'b', 'c', 'd']; /// let mut tail = slice.split_off_mut(2..).unwrap(); /// @@ -4409,8 +4399,6 @@ impl [T] { /// Getting `None` when `range` is out of bounds: /// /// ``` - /// #![feature(slice_take)] - /// /// let mut slice: &mut [_] = &mut ['a', 'b', 'c', 'd']; /// /// assert_eq!(None, slice.split_off_mut(5..)); @@ -4421,7 +4409,7 @@ impl [T] { /// ``` #[inline] #[must_use = "method does not modify the slice if the range is out of bounds"] - #[unstable(feature = "slice_take", issue = "62280")] + #[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")] pub fn split_off_mut<'a, R: OneSidedRange>( self: &mut &'a mut Self, range: R, @@ -4451,8 +4439,6 @@ impl [T] { /// # Examples /// /// ``` - /// #![feature(slice_take)] - /// /// let mut slice: &[_] = &['a', 'b', 'c']; /// let first = slice.split_off_first().unwrap(); /// @@ -4460,7 +4446,7 @@ impl [T] { /// assert_eq!(first, &'a'); /// ``` #[inline] - #[unstable(feature = "slice_take", issue = "62280")] + #[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")] pub fn split_off_first<'a>(self: &mut &'a Self) -> Option<&'a T> { let (first, rem) = self.split_first()?; *self = rem; @@ -4475,8 +4461,6 @@ impl [T] { /// # Examples /// /// ``` - /// #![feature(slice_take)] - /// /// let mut slice: &mut [_] = &mut ['a', 'b', 'c']; /// let first = slice.split_off_first_mut().unwrap(); /// *first = 'd'; @@ -4485,7 +4469,7 @@ impl [T] { /// assert_eq!(first, &'d'); /// ``` #[inline] - #[unstable(feature = "slice_take", issue = "62280")] + #[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")] pub fn split_off_first_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> { let (first, rem) = mem::take(self).split_first_mut()?; *self = rem; @@ -4500,8 +4484,6 @@ impl [T] { /// # Examples /// /// ``` - /// #![feature(slice_take)] - /// /// let mut slice: &[_] = &['a', 'b', 'c']; /// let last = slice.split_off_last().unwrap(); /// @@ -4509,7 +4491,7 @@ impl [T] { /// assert_eq!(last, &'c'); /// ``` #[inline] - #[unstable(feature = "slice_take", issue = "62280")] + #[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")] pub fn split_off_last<'a>(self: &mut &'a Self) -> Option<&'a T> { let (last, rem) = self.split_last()?; *self = rem; @@ -4524,8 +4506,6 @@ impl [T] { /// # Examples /// /// ``` - /// #![feature(slice_take)] - /// /// let mut slice: &mut [_] = &mut ['a', 'b', 'c']; /// let last = slice.split_off_last_mut().unwrap(); /// *last = 'd'; @@ -4534,7 +4514,7 @@ impl [T] { /// assert_eq!(last, &'d'); /// ``` #[inline] - #[unstable(feature = "slice_take", issue = "62280")] + #[stable(feature = "slice_take", since = "CURRENT_RUSTC_VERSION")] pub fn split_off_last_mut<'a>(self: &mut &'a mut Self) -> Option<&'a mut T> { let (last, rem) = mem::take(self).split_last_mut()?; *self = rem; diff --git a/library/coretests/tests/lib.rs b/library/coretests/tests/lib.rs index 4f21ae5013b66..b3c8e94806d48 100644 --- a/library/coretests/tests/lib.rs +++ b/library/coretests/tests/lib.rs @@ -73,7 +73,6 @@ #![feature(slice_internals)] #![feature(slice_partition_dedup)] #![feature(slice_split_once)] -#![feature(slice_take)] #![feature(split_array)] #![feature(split_as_slice)] #![feature(std_internals)] From 3786a7a90825d2edf34c1a4c08047559eaf402cc Mon Sep 17 00:00:00 2001 From: Slanterns Date: Sat, 1 Mar 2025 19:59:43 +0800 Subject: [PATCH 02/29] stabilize `box_uninit_write` --- library/alloc/src/boxed.rs | 4 +--- library/alloc/src/lib.rs | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index c3f5806e1aa90..9b19ab74edfd5 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -937,8 +937,6 @@ impl Box, A> { /// # Examples /// /// ``` - /// #![feature(box_uninit_write)] - /// /// let big_box = Box::<[usize; 1024]>::new_uninit(); /// /// let mut array = [0; 1024]; @@ -954,7 +952,7 @@ impl Box, A> { /// assert_eq!(*x, i); /// } /// ``` - #[unstable(feature = "box_uninit_write", issue = "129397")] + #[stable(feature = "box_uninit_write", since = "CURRENT_RUSTC_VERSION")] #[inline] pub fn write(mut boxed: Self, value: T) -> Box { unsafe { diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 2e9dd98571537..cb93100f56cf0 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -102,7 +102,6 @@ #![feature(assert_matches)] #![feature(async_fn_traits)] #![feature(async_iterator)] -#![feature(box_uninit_write)] #![feature(bstr)] #![feature(bstr_internals)] #![feature(char_max_len)] From 0ad48b47e65b414c5a47c0955e0ec582c01a61ad Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 3 Mar 2025 05:14:52 +0000 Subject: [PATCH 03/29] Do not recover missing lifetime with random in-scope lifetime --- .../rustc_resolve/src/late/diagnostics.rs | 6 --- tests/ui/async-await/issues/issue-63388-2.rs | 1 - .../async-await/issues/issue-63388-2.stderr | 14 +------ .../in-trait/opaque-and-lifetime-mismatch.rs | 2 - .../opaque-and-lifetime-mismatch.stderr | 42 ++++--------------- .../opaque-used-in-extraneous-argument.stderr | 2 +- .../precise-capturing/bad-lifetimes.rs | 1 - .../precise-capturing/bad-lifetimes.stderr | 12 ++---- tests/ui/issues/issue-13497.rs | 1 - tests/ui/issues/issue-13497.stderr | 14 +------ tests/ui/lifetimes/issue-26638.rs | 1 - tests/ui/lifetimes/issue-26638.stderr | 17 ++------ .../do-not-ice-on-invalid-lifetime.rs | 2 +- .../do-not-ice-on-invalid-lifetime.stderr | 13 +----- .../impl-trait-missing-lifetime-gated.rs | 2 - .../impl-trait-missing-lifetime-gated.stderr | 40 ++++++------------ .../impl-trait-missing-lifetime.rs | 2 - .../impl-trait-missing-lifetime.stderr | 18 +------- tests/ui/suggestions/missing-lt-for-hrtb.rs | 2 - .../ui/suggestions/missing-lt-for-hrtb.stderr | 24 +---------- 20 files changed, 36 insertions(+), 180 deletions(-) diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 0e14e7671b176..42be92c0f8fe5 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -3515,12 +3515,6 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { } } } - - // Record as using the suggested resolution. - let (_, (_, res)) = in_scope_lifetimes[0]; - for < in &lifetime_refs { - self.r.lifetimes_res_map.insert(lt.id, res); - } } _ => { let lifetime_spans: Vec<_> = diff --git a/tests/ui/async-await/issues/issue-63388-2.rs b/tests/ui/async-await/issues/issue-63388-2.rs index 8bb5cfa4a594a..d9dc602bd1c6d 100644 --- a/tests/ui/async-await/issues/issue-63388-2.rs +++ b/tests/ui/async-await/issues/issue-63388-2.rs @@ -12,7 +12,6 @@ impl Xyz { ) -> &dyn Foo //~ ERROR missing lifetime specifier { foo - //~^ ERROR explicit lifetime required in the type of `foo` [E0621] } } diff --git a/tests/ui/async-await/issues/issue-63388-2.stderr b/tests/ui/async-await/issues/issue-63388-2.stderr index 7e3c0a1227de8..45843bc72cd81 100644 --- a/tests/ui/async-await/issues/issue-63388-2.stderr +++ b/tests/ui/async-await/issues/issue-63388-2.stderr @@ -12,16 +12,6 @@ help: consider using the `'a` lifetime LL | ) -> &'a dyn Foo | ++ -error[E0621]: explicit lifetime required in the type of `foo` - --> $DIR/issue-63388-2.rs:14:9 - | -LL | foo: &dyn Foo, bar: &'a dyn Foo - | -------- help: add explicit lifetime `'a` to the type of `foo`: `&'a (dyn Foo + 'a)` -... -LL | foo - | ^^^ lifetime `'a` required - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error -Some errors have detailed explanations: E0106, E0621. -For more information about an error, try `rustc --explain E0106`. +For more information about this error, try `rustc --explain E0106`. diff --git a/tests/ui/impl-trait/in-trait/opaque-and-lifetime-mismatch.rs b/tests/ui/impl-trait/in-trait/opaque-and-lifetime-mismatch.rs index 21e2fda1c3af9..b9b551c1b23d2 100644 --- a/tests/ui/impl-trait/in-trait/opaque-and-lifetime-mismatch.rs +++ b/tests/ui/impl-trait/in-trait/opaque-and-lifetime-mismatch.rs @@ -8,8 +8,6 @@ trait Foo { impl Foo for () { fn bar() -> i32 { - //~^ ERROR method `bar` has an incompatible type for trait - //~| ERROR method `bar` has an incompatible return type for trait 0 } } diff --git a/tests/ui/impl-trait/in-trait/opaque-and-lifetime-mismatch.stderr b/tests/ui/impl-trait/in-trait/opaque-and-lifetime-mismatch.stderr index d575fedbb58de..24896151bedb8 100644 --- a/tests/ui/impl-trait/in-trait/opaque-and-lifetime-mismatch.stderr +++ b/tests/ui/impl-trait/in-trait/opaque-and-lifetime-mismatch.stderr @@ -11,7 +11,7 @@ LL | fn bar() -> Wrapper<'static, impl Sized>; | ++++++++ error[E0106]: missing lifetime specifier - --> $DIR/opaque-and-lifetime-mismatch.rs:18:24 + --> $DIR/opaque-and-lifetime-mismatch.rs:16:24 | LL | fn foo() -> Wrapper; | ^ expected named lifetime parameter @@ -23,7 +23,7 @@ LL | fn foo() -> Wrapper<'static, impl Sized>; | ++++++++ error[E0106]: missing lifetime specifier - --> $DIR/opaque-and-lifetime-mismatch.rs:24:24 + --> $DIR/opaque-and-lifetime-mismatch.rs:22:24 | LL | fn foo() -> Wrapper { | ^ expected named lifetime parameter @@ -49,7 +49,7 @@ LL | struct Wrapper<'rom>(&'rom ()); | ^^^^^^^ error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied - --> $DIR/opaque-and-lifetime-mismatch.rs:18:17 + --> $DIR/opaque-and-lifetime-mismatch.rs:16:17 | LL | fn foo() -> Wrapper; | ^^^^^^^ ---------- help: remove the unnecessary generic argument @@ -62,36 +62,8 @@ note: struct defined here, with 0 generic parameters LL | struct Wrapper<'rom>(&'rom ()); | ^^^^^^^ -error[E0053]: method `bar` has an incompatible return type for trait - --> $DIR/opaque-and-lifetime-mismatch.rs:10:17 - | -LL | fn bar() -> i32 { - | ^^^ - | | - | expected `Wrapper<'static>`, found `i32` - | return type in trait - -error[E0053]: method `bar` has an incompatible type for trait - --> $DIR/opaque-and-lifetime-mismatch.rs:10:17 - | -LL | fn bar() -> i32 { - | ^^^ expected `Wrapper<'static>`, found `i32` - | -note: type in trait - --> $DIR/opaque-and-lifetime-mismatch.rs:4:17 - | -LL | fn bar() -> Wrapper; - | ^^^^^^^^^^^^^^^^^^^ - = note: expected signature `fn() -> Wrapper<'static>` - found signature `fn() -> i32` -help: change the output type to match the trait - | -LL - fn bar() -> i32 { -LL + fn bar() -> Wrapper<'static> { - | - error[E0107]: struct takes 0 generic arguments but 1 generic argument was supplied - --> $DIR/opaque-and-lifetime-mismatch.rs:24:17 + --> $DIR/opaque-and-lifetime-mismatch.rs:22:17 | LL | fn foo() -> Wrapper { | ^^^^^^^ ---------- help: remove the unnecessary generic argument @@ -104,7 +76,7 @@ note: struct defined here, with 0 generic parameters LL | struct Wrapper<'rom>(&'rom ()); | ^^^^^^^ -error: aborting due to 8 previous errors +error: aborting due to 6 previous errors -Some errors have detailed explanations: E0053, E0106, E0107. -For more information about an error, try `rustc --explain E0053`. +Some errors have detailed explanations: E0106, E0107. +For more information about an error, try `rustc --explain E0106`. diff --git a/tests/ui/impl-trait/opaque-used-in-extraneous-argument.stderr b/tests/ui/impl-trait/opaque-used-in-extraneous-argument.stderr index 4198095db65ff..73e7890f91fd1 100644 --- a/tests/ui/impl-trait/opaque-used-in-extraneous-argument.stderr +++ b/tests/ui/impl-trait/opaque-used-in-extraneous-argument.stderr @@ -75,7 +75,7 @@ error[E0061]: this function takes 0 arguments but 1 argument was supplied --> $DIR/opaque-used-in-extraneous-argument.rs:20:5 | LL | open_parent(&old_path) - | ^^^^^^^^^^^ --------- unexpected argument of type `&impl Fn<{type error}> + FnOnce<{type error}, Output = {type error}> + 'static` + | ^^^^^^^^^^^ --------- unexpected argument of type `&impl Fn<{type error}> + FnOnce<{type error}, Output = {type error}> + '_` | note: function defined here --> $DIR/opaque-used-in-extraneous-argument.rs:12:4 diff --git a/tests/ui/impl-trait/precise-capturing/bad-lifetimes.rs b/tests/ui/impl-trait/precise-capturing/bad-lifetimes.rs index 161fe23c89995..8e30973cd3a18 100644 --- a/tests/ui/impl-trait/precise-capturing/bad-lifetimes.rs +++ b/tests/ui/impl-trait/precise-capturing/bad-lifetimes.rs @@ -1,6 +1,5 @@ fn no_elided_lt() -> impl Sized + use<'_> {} //~^ ERROR missing lifetime specifier -//~| ERROR expected lifetime parameter in `use<...>` precise captures list, found `'_` fn static_lt() -> impl Sized + use<'static> {} //~^ ERROR expected lifetime parameter in `use<...>` precise captures list, found `'static` diff --git a/tests/ui/impl-trait/precise-capturing/bad-lifetimes.stderr b/tests/ui/impl-trait/precise-capturing/bad-lifetimes.stderr index c8dac3a69cd98..98f629f52cf34 100644 --- a/tests/ui/impl-trait/precise-capturing/bad-lifetimes.stderr +++ b/tests/ui/impl-trait/precise-capturing/bad-lifetimes.stderr @@ -12,26 +12,20 @@ LL + fn no_elided_lt() -> impl Sized + use<'static> {} | error[E0261]: use of undeclared lifetime name `'missing` - --> $DIR/bad-lifetimes.rs:8:37 + --> $DIR/bad-lifetimes.rs:7:37 | LL | fn missing_lt() -> impl Sized + use<'missing> {} | - ^^^^^^^^ undeclared lifetime | | | help: consider introducing lifetime `'missing` here: `<'missing>` -error: expected lifetime parameter in `use<...>` precise captures list, found `'_` - --> $DIR/bad-lifetimes.rs:1:39 - | -LL | fn no_elided_lt() -> impl Sized + use<'_> {} - | ^^ - error: expected lifetime parameter in `use<...>` precise captures list, found `'static` - --> $DIR/bad-lifetimes.rs:5:36 + --> $DIR/bad-lifetimes.rs:4:36 | LL | fn static_lt() -> impl Sized + use<'static> {} | ^^^^^^^ -error: aborting due to 4 previous errors +error: aborting due to 3 previous errors Some errors have detailed explanations: E0106, E0261. For more information about an error, try `rustc --explain E0106`. diff --git a/tests/ui/issues/issue-13497.rs b/tests/ui/issues/issue-13497.rs index 7f786a54b9f71..4b2795aa841e8 100644 --- a/tests/ui/issues/issue-13497.rs +++ b/tests/ui/issues/issue-13497.rs @@ -3,7 +3,6 @@ fn read_lines_borrowed1() -> Vec< > { let rawLines: Vec = vec!["foo ".to_string(), " bar".to_string()]; rawLines.iter().map(|l| l.trim()).collect() - //~^ ERROR: cannot return value referencing } fn main() {} diff --git a/tests/ui/issues/issue-13497.stderr b/tests/ui/issues/issue-13497.stderr index 8016b55d6aadc..ee111f1d262e0 100644 --- a/tests/ui/issues/issue-13497.stderr +++ b/tests/ui/issues/issue-13497.stderr @@ -15,16 +15,6 @@ LL - &str LL + String | -error[E0515]: cannot return value referencing local variable `rawLines` - --> $DIR/issue-13497.rs:5:5 - | -LL | rawLines.iter().map(|l| l.trim()).collect() - | --------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | returns a value referencing data owned by the current function - | `rawLines` is borrowed here - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error -Some errors have detailed explanations: E0106, E0515. -For more information about an error, try `rustc --explain E0106`. +For more information about this error, try `rustc --explain E0106`. diff --git a/tests/ui/lifetimes/issue-26638.rs b/tests/ui/lifetimes/issue-26638.rs index 11c730165f228..e73cf21bd6cfe 100644 --- a/tests/ui/lifetimes/issue-26638.rs +++ b/tests/ui/lifetimes/issue-26638.rs @@ -3,7 +3,6 @@ fn parse_type(iter: Box+'static>) -> &str { iter.next() fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() } //~^ ERROR missing lifetime specifier [E0106] -//~| ERROR mismatched types //~| ERROR function takes 1 argument but 0 arguments were supplied fn parse_type_3() -> &str { unimplemented!() } diff --git a/tests/ui/lifetimes/issue-26638.stderr b/tests/ui/lifetimes/issue-26638.stderr index bdf911367653b..74a1676f68481 100644 --- a/tests/ui/lifetimes/issue-26638.stderr +++ b/tests/ui/lifetimes/issue-26638.stderr @@ -32,7 +32,7 @@ LL + fn parse_type_2(iter: fn(&u8)->&u8) -> String { iter() } | error[E0106]: missing lifetime specifier - --> $DIR/issue-26638.rs:9:22 + --> $DIR/issue-26638.rs:8:22 | LL | fn parse_type_3() -> &str { unimplemented!() } | ^ expected named lifetime parameter @@ -59,18 +59,7 @@ help: provide the argument LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter(/* &u8 */) } | +++++++++ -error[E0308]: mismatched types - --> $DIR/issue-26638.rs:4:47 - | -LL | fn parse_type_2(iter: fn(&u8)->&u8) -> &str { iter() } - | ---- ^^^^^^ expected `&str`, found `&u8` - | | - | expected `&'static str` because of return type - | - = note: expected reference `&'static str` - found reference `&u8` - -error: aborting due to 5 previous errors +error: aborting due to 4 previous errors -Some errors have detailed explanations: E0061, E0106, E0308. +Some errors have detailed explanations: E0061, E0106. For more information about an error, try `rustc --explain E0061`. diff --git a/tests/ui/structs/default-field-values/do-not-ice-on-invalid-lifetime.rs b/tests/ui/structs/default-field-values/do-not-ice-on-invalid-lifetime.rs index 71d90ddd935ff..ba99b03539cf6 100644 --- a/tests/ui/structs/default-field-values/do-not-ice-on-invalid-lifetime.rs +++ b/tests/ui/structs/default-field-values/do-not-ice-on-invalid-lifetime.rs @@ -1,5 +1,5 @@ #![feature(default_field_values)] -struct A<'a> { //~ ERROR lifetime parameter `'a` is never used +struct A<'a> { x: Vec = Vec::new(), //~ ERROR missing lifetime specifier } diff --git a/tests/ui/structs/default-field-values/do-not-ice-on-invalid-lifetime.stderr b/tests/ui/structs/default-field-values/do-not-ice-on-invalid-lifetime.stderr index 20b9afe80cdc8..7100efc695cef 100644 --- a/tests/ui/structs/default-field-values/do-not-ice-on-invalid-lifetime.stderr +++ b/tests/ui/structs/default-field-values/do-not-ice-on-invalid-lifetime.stderr @@ -9,15 +9,6 @@ help: consider using the `'a` lifetime LL | x: Vec> = Vec::new(), | ++++ -error[E0392]: lifetime parameter `'a` is never used - --> $DIR/do-not-ice-on-invalid-lifetime.rs:2:10 - | -LL | struct A<'a> { - | ^^ unused lifetime parameter - | - = help: consider removing `'a`, referring to it in a field, or using a marker such as `PhantomData` - -error: aborting due to 2 previous errors +error: aborting due to 1 previous error -Some errors have detailed explanations: E0106, E0392. -For more information about an error, try `rustc --explain E0106`. +For more information about this error, try `rustc --explain E0106`. diff --git a/tests/ui/suggestions/impl-trait-missing-lifetime-gated.rs b/tests/ui/suggestions/impl-trait-missing-lifetime-gated.rs index daec66709b654..443a7e3835e3e 100644 --- a/tests/ui/suggestions/impl-trait-missing-lifetime-gated.rs +++ b/tests/ui/suggestions/impl-trait-missing-lifetime-gated.rs @@ -18,7 +18,6 @@ mod elided { // But that lifetime does not participate in resolution. async fn i(mut x: impl Iterator) -> Option<&()> { x.next() } //~^ ERROR missing lifetime specifier - //~| ERROR lifetime may not live long enough } mod underscore { @@ -37,7 +36,6 @@ mod underscore { // But that lifetime does not participate in resolution. async fn i(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } //~^ ERROR missing lifetime specifier - //~| ERROR lifetime may not live long enough } mod alone_in_path { diff --git a/tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr b/tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr index 204209179adf3..24013c85c8758 100644 --- a/tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr +++ b/tests/ui/suggestions/impl-trait-missing-lifetime-gated.stderr @@ -41,7 +41,7 @@ LL + async fn i(mut x: impl Iterator) -> Option<()> { x.next() } | error[E0106]: missing lifetime specifier - --> $DIR/impl-trait-missing-lifetime-gated.rs:28:58 + --> $DIR/impl-trait-missing-lifetime-gated.rs:27:58 | LL | fn g(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } | ^^ expected named lifetime parameter @@ -64,7 +64,7 @@ LL + fn g(mut x: impl Iterator) -> Option<()> { x.next() } | error[E0106]: missing lifetime specifier - --> $DIR/impl-trait-missing-lifetime-gated.rs:38:64 + --> $DIR/impl-trait-missing-lifetime-gated.rs:37:64 | LL | async fn i(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } | ^^ expected named lifetime parameter @@ -87,7 +87,7 @@ LL + async fn i(mut x: impl Iterator) -> Option<()> { x.next( | error[E0106]: missing lifetime specifier - --> $DIR/impl-trait-missing-lifetime-gated.rs:49:37 + --> $DIR/impl-trait-missing-lifetime-gated.rs:47:37 | LL | fn g(mut x: impl Foo) -> Option<&()> { x.next() } | ^ expected named lifetime parameter @@ -108,7 +108,7 @@ LL + fn g(mut x: impl Foo) -> Option<()> { x.next() } | error[E0106]: missing lifetime specifier - --> $DIR/impl-trait-missing-lifetime-gated.rs:60:41 + --> $DIR/impl-trait-missing-lifetime-gated.rs:58:41 | LL | fn g(mut x: impl Foo<()>) -> Option<&()> { x.next() } | ^ expected named lifetime parameter @@ -129,7 +129,7 @@ LL + fn g(mut x: impl Foo<()>) -> Option<()> { x.next() } | warning: elided lifetime has a name - --> $DIR/impl-trait-missing-lifetime-gated.rs:66:57 + --> $DIR/impl-trait-missing-lifetime-gated.rs:64:57 | LL | fn resolved_anonymous<'a, T: 'a>(f: impl Fn(&'a str) -> &T) { | -- lifetime `'a` declared here ^ this elided lifetime gets resolved as `'a` @@ -162,16 +162,8 @@ help: consider introducing a named lifetime parameter LL | fn g<'a>(mut x: impl Iterator) -> Option<&()> { x.next() } | ++++ ++ -error: lifetime may not live long enough - --> $DIR/impl-trait-missing-lifetime-gated.rs:19:67 - | -LL | async fn i(mut x: impl Iterator) -> Option<&()> { x.next() } - | ----------------------------------------------------------- ^^^^^^^^ returning this value requires that `'1` must outlive `'static` - | | - | return type `impl Future>` contains a lifetime `'1` - error[E0658]: anonymous lifetimes in `impl Trait` are unstable - --> $DIR/impl-trait-missing-lifetime-gated.rs:25:35 + --> $DIR/impl-trait-missing-lifetime-gated.rs:24:35 | LL | fn f(_: impl Iterator) {} | ^^ expected named lifetime parameter @@ -185,7 +177,7 @@ LL + fn f<'a>(_: impl Iterator) {} | error[E0658]: anonymous lifetimes in `impl Trait` are unstable - --> $DIR/impl-trait-missing-lifetime-gated.rs:28:39 + --> $DIR/impl-trait-missing-lifetime-gated.rs:27:39 | LL | fn g(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } | ^^ expected named lifetime parameter @@ -198,16 +190,8 @@ LL - fn g(mut x: impl Iterator) -> Option<&'_ ()> { x.next() LL + fn g<'a>(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } | -error: lifetime may not live long enough - --> $DIR/impl-trait-missing-lifetime-gated.rs:38:73 - | -LL | async fn i(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } - | ----------------------------------------------------------------- ^^^^^^^^ returning this value requires that `'1` must outlive `'static` - | | - | return type `impl Future>` contains a lifetime `'1` - error[E0658]: anonymous lifetimes in `impl Trait` are unstable - --> $DIR/impl-trait-missing-lifetime-gated.rs:46:18 + --> $DIR/impl-trait-missing-lifetime-gated.rs:44:18 | LL | fn f(_: impl Foo) {} | ^^^ expected named lifetime parameter @@ -220,7 +204,7 @@ LL | fn f<'a>(_: impl Foo<'a>) {} | ++++ ++++ error[E0658]: anonymous lifetimes in `impl Trait` are unstable - --> $DIR/impl-trait-missing-lifetime-gated.rs:49:22 + --> $DIR/impl-trait-missing-lifetime-gated.rs:47:22 | LL | fn g(mut x: impl Foo) -> Option<&()> { x.next() } | ^^^ expected named lifetime parameter @@ -233,7 +217,7 @@ LL | fn g<'a>(mut x: impl Foo<'a>) -> Option<&()> { x.next() } | ++++ ++++ error[E0658]: anonymous lifetimes in `impl Trait` are unstable - --> $DIR/impl-trait-missing-lifetime-gated.rs:57:22 + --> $DIR/impl-trait-missing-lifetime-gated.rs:55:22 | LL | fn f(_: impl Foo<()>) {} | ^ expected named lifetime parameter @@ -246,7 +230,7 @@ LL | fn f<'a>(_: impl Foo<'a, ()>) {} | ++++ +++ error[E0658]: anonymous lifetimes in `impl Trait` are unstable - --> $DIR/impl-trait-missing-lifetime-gated.rs:60:26 + --> $DIR/impl-trait-missing-lifetime-gated.rs:58:26 | LL | fn g(mut x: impl Foo<()>) -> Option<&()> { x.next() } | ^ expected named lifetime parameter @@ -258,7 +242,7 @@ help: consider introducing a named lifetime parameter LL | fn g<'a>(mut x: impl Foo<'a, ()>) -> Option<&()> { x.next() } | ++++ +++ -error: aborting due to 16 previous errors; 1 warning emitted +error: aborting due to 14 previous errors; 1 warning emitted Some errors have detailed explanations: E0106, E0658. For more information about an error, try `rustc --explain E0106`. diff --git a/tests/ui/suggestions/impl-trait-missing-lifetime.rs b/tests/ui/suggestions/impl-trait-missing-lifetime.rs index 12dc0e8216b7d..27f03431d098a 100644 --- a/tests/ui/suggestions/impl-trait-missing-lifetime.rs +++ b/tests/ui/suggestions/impl-trait-missing-lifetime.rs @@ -8,7 +8,6 @@ fn f(_: impl Iterator) {} // But that lifetime does not participate in resolution. fn g(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } //~^ ERROR missing lifetime specifier -//~| ERROR lifetime may not live long enough // This is understood as `fn foo<'_1>(_: impl Iterator) {}`. async fn h(_: impl Iterator) {} @@ -16,6 +15,5 @@ async fn h(_: impl Iterator) {} // But that lifetime does not participate in resolution. async fn i(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } //~^ ERROR missing lifetime specifier -//~| ERROR lifetime may not live long enough fn main() {} diff --git a/tests/ui/suggestions/impl-trait-missing-lifetime.stderr b/tests/ui/suggestions/impl-trait-missing-lifetime.stderr index dfbc883680b23..c09c575c147f3 100644 --- a/tests/ui/suggestions/impl-trait-missing-lifetime.stderr +++ b/tests/ui/suggestions/impl-trait-missing-lifetime.stderr @@ -22,7 +22,7 @@ LL + fn g(mut x: impl Iterator) -> Option<()> { x.next() } | error[E0106]: missing lifetime specifier - --> $DIR/impl-trait-missing-lifetime.rs:17:60 + --> $DIR/impl-trait-missing-lifetime.rs:16:60 | LL | async fn i(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } | ^^ expected named lifetime parameter @@ -44,20 +44,6 @@ LL - async fn i(mut x: impl Iterator) -> Option<&'_ ()> { x.next( LL + async fn i(mut x: impl Iterator) -> Option<()> { x.next() } | -error: lifetime may not live long enough - --> $DIR/impl-trait-missing-lifetime.rs:17:69 - | -LL | async fn i(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } - | ----------------------------------------------------------------- ^^^^^^^^ returning this value requires that `'1` must outlive `'static` - | | - | return type `impl Future>` contains a lifetime `'1` - -error: lifetime may not live long enough - --> $DIR/impl-trait-missing-lifetime.rs:9:63 - | -LL | fn g(mut x: impl Iterator) -> Option<&'_ ()> { x.next() } - | ----- has type `x` ^^^^^^^^ returning this value requires that `'1` must outlive `'static` - -error: aborting due to 4 previous errors +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0106`. diff --git a/tests/ui/suggestions/missing-lt-for-hrtb.rs b/tests/ui/suggestions/missing-lt-for-hrtb.rs index a48c5665d67a4..04ea3d831c93a 100644 --- a/tests/ui/suggestions/missing-lt-for-hrtb.rs +++ b/tests/ui/suggestions/missing-lt-for-hrtb.rs @@ -8,8 +8,6 @@ fn main() { let x = S(&|x| { println!("hi"); x - //~^ ERROR lifetime may not live long enough - //~| ERROR lifetime may not live long enough }); x.0(&X(&())); } diff --git a/tests/ui/suggestions/missing-lt-for-hrtb.stderr b/tests/ui/suggestions/missing-lt-for-hrtb.stderr index e8c536ac47d50..fa515644431ac 100644 --- a/tests/ui/suggestions/missing-lt-for-hrtb.stderr +++ b/tests/ui/suggestions/missing-lt-for-hrtb.stderr @@ -31,28 +31,6 @@ help: consider using one of the available lifetimes here LL | struct V<'a>(&'a dyn for<'b> Fn(&X) -> &'lifetime X<'lifetime>); | +++++++++ +++++++++++ -error: lifetime may not live long enough - --> $DIR/missing-lt-for-hrtb.rs:10:9 - | -LL | let x = S(&|x| { - | -- return type of closure is &'2 X<'_> - | | - | has type `&'1 X<'_>` -LL | println!("hi"); -LL | x - | ^ returning this value requires that `'1` must outlive `'2` - -error: lifetime may not live long enough - --> $DIR/missing-lt-for-hrtb.rs:10:9 - | -LL | let x = S(&|x| { - | -- return type of closure is &X<'4> - | | - | has type `&X<'3>` -LL | println!("hi"); -LL | x - | ^ returning this value requires that `'3` must outlive `'4` - -error: aborting due to 4 previous errors +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0106`. From e403654c8b49f7bf62e551c2dc5aaaca9ba71d76 Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Sun, 2 Mar 2025 23:47:24 -0800 Subject: [PATCH 04/29] Simplify `::size_hint` The current version is wrong (cc 137919); let's see if we can get away with a loose but trivially-correct one. --- compiler/rustc_middle/src/mir/traversal.rs | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_middle/src/mir/traversal.rs b/compiler/rustc_middle/src/mir/traversal.rs index 5950ac295af19..5fb3c648d5a26 100644 --- a/compiler/rustc_middle/src/mir/traversal.rs +++ b/compiler/rustc_middle/src/mir/traversal.rs @@ -108,7 +108,6 @@ pub struct Postorder<'a, 'tcx> { basic_blocks: &'a IndexSlice>, visited: DenseBitSet, visit_stack: Vec<(BasicBlock, Successors<'a>)>, - root_is_start_block: bool, /// A non-empty `extra` allows for a precise calculation of the successors. extra: Option<(TyCtxt<'tcx>, Instance<'tcx>)>, } @@ -123,7 +122,6 @@ impl<'a, 'tcx> Postorder<'a, 'tcx> { basic_blocks, visited: DenseBitSet::new_empty(basic_blocks.len()), visit_stack: Vec::new(), - root_is_start_block: root == START_BLOCK, extra, }; @@ -211,16 +209,13 @@ impl<'tcx> Iterator for Postorder<'_, 'tcx> { } fn size_hint(&self) -> (usize, Option) { - // All the blocks, minus the number of blocks we've visited. - let upper = self.basic_blocks.len() - self.visited.count(); - - let lower = if self.root_is_start_block { - // We will visit all remaining blocks exactly once. - upper - } else { - self.visit_stack.len() - }; + // These bounds are not at all tight, but that's fine. + // It's not worth a popcnt loop in `DenseBitSet` to improve the upper, + // and in mono-reachable we can't be precise anyway. + // Leaning on amortized growth is fine. + let lower = self.visit_stack.len(); + let upper = self.basic_blocks.len(); (lower, Some(upper)) } } From 53ee6968a3664c36fbc78530cb7795f2259e6a0c Mon Sep 17 00:00:00 2001 From: Jieyou Xu Date: Tue, 4 Mar 2025 00:27:22 +0800 Subject: [PATCH 05/29] Update MSVC INSTALL.md instructions to recommend VS 2022 + recent Windows 10/11 SDK --- INSTALL.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/INSTALL.md b/INSTALL.md index 74fcc58348b43..a46d3d70093f1 100644 --- a/INSTALL.md +++ b/INSTALL.md @@ -210,9 +210,13 @@ itself back on after some time). ### MSVC -MSVC builds of Rust additionally require an installation of Visual Studio 2017 -(or later) so `rustc` can use its linker. The simplest way is to get -[Visual Studio], check the "C++ build tools" and "Windows 10 SDK" workload. +MSVC builds of Rust additionally requires an installation of: + +- Visual Studio 2022 (or later) build tools so `rustc` can use its linker. Older + Visual Studio versions such as 2019 *may* work but aren't actively tested. +- A recent Windows 10 or 11 SDK. + +The simplest way is to get [Visual Studio], check the "C++ build tools". [Visual Studio]: https://visualstudio.microsoft.com/downloads/ From a89cddb2be47b52ec8373165e0d69448df80a23f Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Mon, 3 Mar 2025 22:58:03 +0200 Subject: [PATCH 06/29] Add ``dyn`` keyword --- compiler/rustc_error_codes/src/error_codes/E0373.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0373.md b/compiler/rustc_error_codes/src/error_codes/E0373.md index d4d26007aa50e..b9db807259728 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0373.md +++ b/compiler/rustc_error_codes/src/error_codes/E0373.md @@ -3,7 +3,7 @@ A captured variable in a closure may not live long enough. Erroneous code example: ```compile_fail,E0373 -fn foo() -> Box u32> { +fn foo() -> Box u32> { let x = 0u32; Box::new(|y| x + y) } @@ -42,7 +42,7 @@ This approach moves (or copies, where possible) data into the closure, rather than taking references to it. For example: ``` -fn foo() -> Box u32> { +fn foo() -> Box u32> { let x = 0u32; Box::new(move |y| x + y) } From 3e5fddc95ea2d8851def90b940030d1c97bd5b00 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Mon, 3 Mar 2025 05:25:46 +0000 Subject: [PATCH 07/29] Allow struct field default values to reference struct's generics --- .../src/collect/generics_of.rs | 2 ++ compiler/rustc_resolve/src/late.rs | 4 ++- .../structs/default-field-values/failures.rs | 4 +-- .../default-field-values/failures.stderr | 17 +---------- .../field-references-param.rs | 29 +++++++++++++++++++ .../post-mono.direct.stderr | 23 +++++++++++++++ .../post-mono.indirect.stderr | 29 +++++++++++++++++++ .../structs/default-field-values/post-mono.rs | 23 +++++++++++++++ 8 files changed, 112 insertions(+), 19 deletions(-) create mode 100644 tests/ui/structs/default-field-values/field-references-param.rs create mode 100644 tests/ui/structs/default-field-values/post-mono.direct.stderr create mode 100644 tests/ui/structs/default-field-values/post-mono.indirect.stderr create mode 100644 tests/ui/structs/default-field-values/post-mono.rs diff --git a/compiler/rustc_hir_analysis/src/collect/generics_of.rs b/compiler/rustc_hir_analysis/src/collect/generics_of.rs index af1338e50d007..a153ce8ea902d 100644 --- a/compiler/rustc_hir_analysis/src/collect/generics_of.rs +++ b/compiler/rustc_hir_analysis/src/collect/generics_of.rs @@ -187,6 +187,8 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics { Some(parent_did) } Node::TyPat(_) => Some(parent_did), + // Field default values inherit the ADT's generics. + Node::Field(_) => Some(parent_did), _ => None, } } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index f119ed55e7d1b..3e8946d9291c6 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -78,6 +78,7 @@ struct IsNeverPattern; #[derive(Copy, Clone, Debug, PartialEq, Eq)] enum AnonConstKind { EnumDiscriminant, + FieldDefaultValue, InlineConst, ConstArg(IsRepeatExpr), } @@ -1406,7 +1407,7 @@ impl<'ra: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'r visit_opt!(self, visit_ident, ident); try_visit!(self.visit_ty(ty)); if let Some(v) = &default { - self.resolve_anon_const(v, AnonConstKind::ConstArg(IsRepeatExpr::No)); + self.resolve_anon_const(v, AnonConstKind::FieldDefaultValue); } } } @@ -4658,6 +4659,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> { AnonConstKind::EnumDiscriminant => { ConstantHasGenerics::No(NoConstantGenericsReason::IsEnumDiscriminant) } + AnonConstKind::FieldDefaultValue => ConstantHasGenerics::Yes, AnonConstKind::InlineConst => ConstantHasGenerics::Yes, AnonConstKind::ConstArg(_) => { if self.r.tcx.features().generic_const_exprs() || is_trivial_const_arg { diff --git a/tests/ui/structs/default-field-values/failures.rs b/tests/ui/structs/default-field-values/failures.rs index 0ac071d91d65e..1e94eecb4f870 100644 --- a/tests/ui/structs/default-field-values/failures.rs +++ b/tests/ui/structs/default-field-values/failures.rs @@ -17,9 +17,9 @@ pub struct Bar { #[derive(Default)] pub struct Qux { - bar: S = Self::S, //~ ERROR generic `Self` types are currently not permitted in anonymous constants + bar: S = Self::S, baz: i32 = foo(), - bat: i32 = as T>::K, //~ ERROR generic parameters may not be used in const operations + bat: i32 = as T>::K, bay: i32 = C, } diff --git a/tests/ui/structs/default-field-values/failures.stderr b/tests/ui/structs/default-field-values/failures.stderr index 65ec100fe2ea3..50553816462d1 100644 --- a/tests/ui/structs/default-field-values/failures.stderr +++ b/tests/ui/structs/default-field-values/failures.stderr @@ -6,27 +6,12 @@ LL | Variant {} | = help: consider a manual implementation of `Default` -error: generic parameters may not be used in const operations - --> $DIR/failures.rs:22:23 - | -LL | bat: i32 = as T>::K, - | ^ cannot perform const operation using `C` - | - = help: const parameters may only be used as standalone arguments, i.e. `C` - = help: add `#![feature(generic_const_exprs)]` to allow generic const expressions - error: default fields are not supported in tuple structs --> $DIR/failures.rs:26:22 | LL | pub struct Rak(i32 = 42); | ^^ default fields are only supported on structs -error: generic `Self` types are currently not permitted in anonymous constants - --> $DIR/failures.rs:20:14 - | -LL | bar: S = Self::S, - | ^^^^ - error[E0277]: the trait bound `S: Default` is not satisfied --> $DIR/failures.rs:14:5 | @@ -112,7 +97,7 @@ LL - let _ = Rak(.., 0); LL + let _ = Rak(0); | -error: aborting due to 9 previous errors +error: aborting due to 7 previous errors Some errors have detailed explanations: E0061, E0277, E0308. For more information about an error, try `rustc --explain E0061`. diff --git a/tests/ui/structs/default-field-values/field-references-param.rs b/tests/ui/structs/default-field-values/field-references-param.rs new file mode 100644 index 0000000000000..ecee37edd42c0 --- /dev/null +++ b/tests/ui/structs/default-field-values/field-references-param.rs @@ -0,0 +1,29 @@ +//@ build-pass + +#![feature(default_field_values)] + +struct W; + +impl W { + const fn new() -> Self { W } +} + +struct Z { + // No inference. + one: W = W::::new(), + + // Inference works too. + two: W = W::new(), + + // An anon const that is too generic before substitution. + too_generic: usize = X + 1, +} + +fn use_generically() { + let x: Z = Z { .. }; +} + +fn main() { + let x: Z<0> = Z { .. }; + use_generically::<0>(); +} diff --git a/tests/ui/structs/default-field-values/post-mono.direct.stderr b/tests/ui/structs/default-field-values/post-mono.direct.stderr new file mode 100644 index 0000000000000..cdd80620c48a9 --- /dev/null +++ b/tests/ui/structs/default-field-values/post-mono.direct.stderr @@ -0,0 +1,23 @@ +error[E0080]: evaluation of `Z::<1>::post_mono::{constant#0}` failed + --> $DIR/post-mono.rs:7:24 + | +LL | post_mono: usize = X / 0, + | ^^^^^ attempt to divide `1_usize` by zero + +note: erroneous constant encountered + --> $DIR/post-mono.rs:17:19 + | +LL | let x: Z<1> = Z { .. }; + | ^^^^^^^^ + +note: erroneous constant encountered + --> $DIR/post-mono.rs:17:19 + | +LL | let x: Z<1> = Z { .. }; + | ^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/structs/default-field-values/post-mono.indirect.stderr b/tests/ui/structs/default-field-values/post-mono.indirect.stderr new file mode 100644 index 0000000000000..56c27a6e5dc81 --- /dev/null +++ b/tests/ui/structs/default-field-values/post-mono.indirect.stderr @@ -0,0 +1,29 @@ +error[E0080]: evaluation of `Z::<1>::post_mono::{constant#0}` failed + --> $DIR/post-mono.rs:7:24 + | +LL | post_mono: usize = X / 0, + | ^^^^^ attempt to divide `1_usize` by zero + +note: erroneous constant encountered + --> $DIR/post-mono.rs:12:19 + | +LL | let x: Z = Z { .. }; + | ^^^^^^^^ + +note: erroneous constant encountered + --> $DIR/post-mono.rs:12:19 + | +LL | let x: Z = Z { .. }; + | ^^^^^^^^ + | + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` + +note: the above error was encountered while instantiating `fn indirect::<1>` + --> $DIR/post-mono.rs:22:5 + | +LL | indirect::<1>(); + | ^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0080`. diff --git a/tests/ui/structs/default-field-values/post-mono.rs b/tests/ui/structs/default-field-values/post-mono.rs new file mode 100644 index 0000000000000..4de31f6e2fbac --- /dev/null +++ b/tests/ui/structs/default-field-values/post-mono.rs @@ -0,0 +1,23 @@ +//@ build-fail +//@ revisions: direct indirect + +#![feature(default_field_values)] + +struct Z { + post_mono: usize = X / 0, + //~^ ERROR evaluation of `Z::<1>::post_mono::{constant#0}` failed +} + +fn indirect() { + let x: Z = Z { .. }; +} + +#[cfg(direct)] +fn main() { + let x: Z<1> = Z { .. }; +} + +#[cfg(indirect)] +fn main() { + indirect::<1>(); +} From 12cc2b969d4f45423bff1771af7dcbd2f97707ff Mon Sep 17 00:00:00 2001 From: Zalathar Date: Tue, 4 Mar 2025 13:23:40 +1100 Subject: [PATCH 08/29] Remove unused `PpMode::needs_hir` --- compiler/rustc_session/src/config.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/compiler/rustc_session/src/config.rs b/compiler/rustc_session/src/config.rs index 7586c5766b585..671d9741e2f28 100644 --- a/compiler/rustc_session/src/config.rs +++ b/compiler/rustc_session/src/config.rs @@ -2883,14 +2883,6 @@ impl PpMode { | StableMir => true, } } - pub fn needs_hir(&self) -> bool { - use PpMode::*; - match *self { - Source(_) | AstTree | AstTreeExpanded => false, - - Hir(_) | HirTree | ThirTree | ThirFlat | Mir | MirCFG | StableMir => true, - } - } pub fn needs_analysis(&self) -> bool { use PpMode::*; From fe6cf341479c91ef90d8c809aea70193ed42036a Mon Sep 17 00:00:00 2001 From: Scott McMurray Date: Mon, 3 Mar 2025 21:07:55 -0800 Subject: [PATCH 09/29] Also simplify `Preorder`'s `size_hint` --- compiler/rustc_middle/src/mir/traversal.rs | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_middle/src/mir/traversal.rs b/compiler/rustc_middle/src/mir/traversal.rs index 5fb3c648d5a26..9308570d89d18 100644 --- a/compiler/rustc_middle/src/mir/traversal.rs +++ b/compiler/rustc_middle/src/mir/traversal.rs @@ -23,19 +23,13 @@ pub struct Preorder<'a, 'tcx> { body: &'a Body<'tcx>, visited: DenseBitSet, worklist: Vec, - root_is_start_block: bool, } impl<'a, 'tcx> Preorder<'a, 'tcx> { pub fn new(body: &'a Body<'tcx>, root: BasicBlock) -> Preorder<'a, 'tcx> { let worklist = vec![root]; - Preorder { - body, - visited: DenseBitSet::new_empty(body.basic_blocks.len()), - worklist, - root_is_start_block: root == START_BLOCK, - } + Preorder { body, visited: DenseBitSet::new_empty(body.basic_blocks.len()), worklist } } } @@ -71,15 +65,11 @@ impl<'a, 'tcx> Iterator for Preorder<'a, 'tcx> { } fn size_hint(&self) -> (usize, Option) { - // All the blocks, minus the number of blocks we've visited. - let upper = self.body.basic_blocks.len() - self.visited.count(); + // The worklist might be only things already visited. + let lower = 0; - let lower = if self.root_is_start_block { - // We will visit all remaining blocks exactly once. - upper - } else { - self.worklist.len() - }; + // This is extremely loose, but it's not worth a popcnt loop to do better. + let upper = self.body.basic_blocks.len(); (lower, Some(upper)) } From a8c0b3bfff36c5b7dbd429891b7fd2093e25a9ad Mon Sep 17 00:00:00 2001 From: binarycat Date: Mon, 3 Mar 2025 23:36:12 -0600 Subject: [PATCH 10/29] rustdoc search: increase strictness of typechecking --- src/librustdoc/html/static/js/rustdoc.d.ts | 10 ++++++++-- src/librustdoc/html/static/js/search.js | 7 +++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index acea7828e86a9..c7b7a46808f1b 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -123,7 +123,7 @@ declare namespace rustdoc { * Same as QueryElement, but bindings and typeFilter support strings */ interface ParserQueryElement { - name: string, + name: string|null, id: number|null, fullPath: Array, pathWithoutLast: Array, @@ -131,10 +131,16 @@ declare namespace rustdoc { normalizedPathLast: string, generics: Array, bindings: Map>, - bindingName: {name: string, generics: ParserQueryElement[]}|null, + bindingName: {name: string|null, generics: ParserQueryElement[]}|null, typeFilter: string|null, } + /** + * Same as ParserQueryElement, but all fields are optional. + */ + type ParserQueryElementFields = { + [K in keyof ParserQueryElement]?: ParserQueryElement[T] + } /** * Intermediate parser state. Discarded when parsing is done. */ diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index ccbd6811b0712..4073bb389c069 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -638,7 +638,6 @@ function getNextElem(query, parserState, elems, isInGenerics) { getFilteredNextElem(query, parserState, generics, isInGenerics); generics[generics.length - 1].bindingName = makePrimitiveElement("output"); } else { - // @ts-expect-error generics.push(makePrimitiveElement(null, { bindingName: makePrimitiveElement("output"), typeFilter: null, @@ -791,7 +790,7 @@ function createQueryElement(query, parserState, name, generics, isInGenerics) { generics: generics.filter(gen => { // Syntactically, bindings are parsed as generics, // but the query engine treats them differently. - if (gen.bindingName !== null) { + if (gen.bindingName !== null && gen.bindingName.name !== null) { if (gen.name !== null) { gen.bindingName.generics.unshift(gen); } @@ -811,8 +810,8 @@ function createQueryElement(query, parserState, name, generics, isInGenerics) { /** * - * @param {string} name - * @param {Object=} extra + * @param {string|null} name + * @param {rustdoc.ParserQueryElementFields=} extra * @returns {rustdoc.ParserQueryElement} */ function makePrimitiveElement(name, extra) { From 2f9ea8a55ad6397bae7d97f653bbe02915017f03 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 00:27:27 -0600 Subject: [PATCH 11/29] rustdoc search: give buildTypeMapIndex a conditional return type we encode "if the argument is a non-empty string, this will never return null" into the type signature. --- src/librustdoc/html/static/js/search.js | 26 ++++++++++--------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 4073bb389c069..bd0dab97f0184 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -1477,73 +1477,61 @@ class DocSearch { * Special type name IDs for searching by array. * @type {number} */ - // @ts-expect-error this.typeNameIdOfArray = this.buildTypeMapIndex("array"); /** * Special type name IDs for searching by slice. * @type {number} */ - // @ts-expect-error this.typeNameIdOfSlice = this.buildTypeMapIndex("slice"); /** * Special type name IDs for searching by both array and slice (`[]` syntax). * @type {number} */ - // @ts-expect-error this.typeNameIdOfArrayOrSlice = this.buildTypeMapIndex("[]"); /** * Special type name IDs for searching by tuple. * @type {number} */ - // @ts-expect-error this.typeNameIdOfTuple = this.buildTypeMapIndex("tuple"); /** * Special type name IDs for searching by unit. * @type {number} */ - // @ts-expect-error this.typeNameIdOfUnit = this.buildTypeMapIndex("unit"); /** * Special type name IDs for searching by both tuple and unit (`()` syntax). * @type {number} */ - // @ts-expect-error this.typeNameIdOfTupleOrUnit = this.buildTypeMapIndex("()"); /** * Special type name IDs for searching `fn`. * @type {number} */ - // @ts-expect-error this.typeNameIdOfFn = this.buildTypeMapIndex("fn"); /** * Special type name IDs for searching `fnmut`. * @type {number} */ - // @ts-expect-error this.typeNameIdOfFnMut = this.buildTypeMapIndex("fnmut"); /** * Special type name IDs for searching `fnonce`. * @type {number} */ - // @ts-expect-error this.typeNameIdOfFnOnce = this.buildTypeMapIndex("fnonce"); /** * Special type name IDs for searching higher order functions (`->` syntax). * @type {number} */ - // @ts-expect-error this.typeNameIdOfHof = this.buildTypeMapIndex("->"); /** * Special type name IDs the output assoc type. * @type {number} */ - // @ts-expect-error this.typeNameIdOfOutput = this.buildTypeMapIndex("output", true); /** * Special type name IDs for searching by reference. * @type {number} */ - // @ts-expect-error this.typeNameIdOfReference = this.buildTypeMapIndex("reference"); /** @@ -1596,11 +1584,17 @@ class DocSearch { * This is effectively string interning, so that function matching can be * done more quickly. Two types with the same name but different item kinds * get the same ID. - * - * @param {string} name + * + * @template T extends string + * @overload + * @param {T} name * @param {boolean=} isAssocType - True if this is an assoc type - * - * @returns {number?} + * @returns {T extends "" ? null : number} + * + * @param {string} name + * @param {boolean=} isAssocType + * @returns {number | null} + * */ buildTypeMapIndex(name, isAssocType) { if (name === "" || name === null) { From 60c5855346576dea6666adb9e00479156242b495 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 00:43:56 -0600 Subject: [PATCH 12/29] ParserQueryElement.typeFilter MAY be a string, OR number. ideally we would encode that it is a string before convertTypeFilterOnElem is called, and a number after, but i'm not sure that's possible without significant refactoring. --- src/librustdoc/html/static/js/rustdoc.d.ts | 2 +- src/librustdoc/html/static/js/search.js | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index c7b7a46808f1b..bc4c9a12ac0a1 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -132,7 +132,7 @@ declare namespace rustdoc { generics: Array, bindings: Map>, bindingName: {name: string|null, generics: ParserQueryElement[]}|null, - typeFilter: string|null, + typeFilter: number|string|null, } /** diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index bd0dab97f0184..45077f7920ba4 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -2319,15 +2319,13 @@ class DocSearch { * @param {rustdoc.ParserQueryElement} elem */ function convertTypeFilterOnElem(elem) { - if (elem.typeFilter !== null) { + if (typeof elem.typeFilter === 'string') { let typeFilter = elem.typeFilter; if (typeFilter === "const") { typeFilter = "constant"; } - // @ts-expect-error elem.typeFilter = itemTypeFromName(typeFilter); } else { - // @ts-expect-error elem.typeFilter = NO_TYPE_FILTER; } for (const elem2 of elem.generics) { From 4febd273e5a82a9c5aa648e8d843baa184c30929 Mon Sep 17 00:00:00 2001 From: fuyangpengqi <995764973@qq.com> Date: Tue, 4 Mar 2025 16:05:32 +0800 Subject: [PATCH 13/29] Fix some typos Signed-off-by: fuyangpengqi <995764973@qq.com> --- CONTRIBUTING.md | 2 +- library/std/src/sys/pal/uefi/process.rs | 2 +- .../rustc/src/platform-support/riscv64gc-unknown-linux-gnu.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a5ddff595f5d6..e155e253784aa 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -18,7 +18,7 @@ For submodules, changes need to be made against the repository corresponding the submodule, and not the main `rust-lang/rust` repository. For subtrees, prefer sending a PR against the subtree's repository if it does -not need to be made against the main `rust-lang/rust` repostory (e.g. a +not need to be made against the main `rust-lang/rust` repository (e.g. a rustc-dev-guide change that does not accompany a compiler change). ## About the [rustc-dev-guide] diff --git a/library/std/src/sys/pal/uefi/process.rs b/library/std/src/sys/pal/uefi/process.rs index a47c8dbcaaafc..d4c09dc2ca0e1 100644 --- a/library/std/src/sys/pal/uefi/process.rs +++ b/library/std/src/sys/pal/uefi/process.rs @@ -757,7 +757,7 @@ mod uefi_command_internal { } /// Create a map of environment variable changes. Allows efficient setting and rolling back of -/// enviroment variable changes. +/// environment variable changes. /// /// Entry: (Old Value, New Value) fn env_changes(env: &CommandEnv) -> Option, Option)>> { diff --git a/src/doc/rustc/src/platform-support/riscv64gc-unknown-linux-gnu.md b/src/doc/rustc/src/platform-support/riscv64gc-unknown-linux-gnu.md index b57083980d275..dda2a50c33d7e 100644 --- a/src/doc/rustc/src/platform-support/riscv64gc-unknown-linux-gnu.md +++ b/src/doc/rustc/src/platform-support/riscv64gc-unknown-linux-gnu.md @@ -65,7 +65,7 @@ section below. A RISC-V toolchain can be obtained for Windows/Mac/Linux from the [`riscv-gnu-toolchain`](https://github.com/riscv-collab/riscv-gnu-toolchain) -repostory. Binaries are available via +repository. Binaries are available via [embecosm](https://www.embecosm.com/resources/tool-chain-downloads/#riscv-linux), and may also be available from your OS's package manager. From 2816f0302265e999f3c09099830d029315acb4a8 Mon Sep 17 00:00:00 2001 From: Trevor Gross Date: Tue, 4 Mar 2025 09:56:25 +0000 Subject: [PATCH 14/29] Add `avr-none` to SUMMARY.md and platform-support.md This was missed this in the implementation PR, so update the links here. --- src/doc/rustc/src/SUMMARY.md | 1 + src/doc/rustc/src/platform-support.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/doc/rustc/src/SUMMARY.md b/src/doc/rustc/src/SUMMARY.md index 6c7cdec348024..d9c2f6b28a79d 100644 --- a/src/doc/rustc/src/SUMMARY.md +++ b/src/doc/rustc/src/SUMMARY.md @@ -50,6 +50,7 @@ - [\*-linux-ohos](platform-support/openharmony.md) - [\*-hurd-gnu](platform-support/hurd.md) - [aarch64-unknown-teeos](platform-support/aarch64-unknown-teeos.md) + - [avr-none](platform-support/avr-none.md) - [\*-espidf](platform-support/esp-idf.md) - [\*-unknown-fuchsia](platform-support/fuchsia.md) - [\*-unknown-trusty](platform-support/trusty.md) diff --git a/src/doc/rustc/src/platform-support.md b/src/doc/rustc/src/platform-support.md index c4e5c1aac2f5d..dd085f916b911 100644 --- a/src/doc/rustc/src/platform-support.md +++ b/src/doc/rustc/src/platform-support.md @@ -301,7 +301,7 @@ target | std | host | notes [`armv8r-none-eabihf`](platform-support/armv8r-none-eabihf.md) | * | | Bare Armv8-R, hardfloat [`armv7a-nuttx-eabi`](platform-support/nuttx.md) | ✓ | | ARMv7-A with NuttX [`armv7a-nuttx-eabihf`](platform-support/nuttx.md) | ✓ | | ARMv7-A with NuttX, hardfloat -`avr-none` | * | | AVR; requires `-Zbuild-std=core` and `-Ctarget-cpu=...` +[`avr-none`](platform-support/avr-none.md) | * | | AVR; requires `-Zbuild-std=core` and `-Ctarget-cpu=...` `bpfeb-unknown-none` | * | | BPF (big endian) `bpfel-unknown-none` | * | | BPF (little endian) `csky-unknown-linux-gnuabiv2` | ✓ | | C-SKY abiv2 Linux (little endian) From 84dd2a77769be235c19302d69b3bd1916718819e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Mi=C4=85sko?= Date: Tue, 4 Mar 2025 12:24:09 +0100 Subject: [PATCH 15/29] Remove obsolete comment from DeduceReadOnly The situation described in the comment does arise in practice now and described panic is long gone. --- .../src/deduce_param_attrs.rs | 29 ------------------- 1 file changed, 29 deletions(-) diff --git a/compiler/rustc_mir_transform/src/deduce_param_attrs.rs b/compiler/rustc_mir_transform/src/deduce_param_attrs.rs index 049f13ce96d74..a0db8bdb7ed88 100644 --- a/compiler/rustc_mir_transform/src/deduce_param_attrs.rs +++ b/compiler/rustc_mir_transform/src/deduce_param_attrs.rs @@ -80,35 +80,6 @@ impl<'tcx> Visitor<'tcx> for DeduceReadOnly { // `f` passes. Note that function arguments are the only situation in which this problem can // arise: every other use of `move` in MIR doesn't actually write to the value it moves // from. - // - // Anyway, right now this situation doesn't actually arise in practice. Instead, the MIR for - // that function looks like this: - // - // fn f(_1: BigStruct) -> () { - // let mut _0: (); - // let mut _2: BigStruct; - // bb0: { - // _2 = move _1; - // _0 = g(move _2) -> bb1; - // } - // ... - // } - // - // Because of that extra move that MIR construction inserts, `x` (i.e. `_1`) can *in - // practice* safely be marked `readonly`. - // - // To handle the possibility that other optimizations (for example, destination propagation) - // might someday generate MIR like the first example above, we panic upon seeing an argument - // to *our* function that is directly moved into *another* function as an argument. Having - // eliminated that problematic case, we can safely treat moves as copies in this analysis. - // - // In the future, if MIR optimizations cause arguments of a caller to be directly moved into - // the argument of a callee, we can just add that argument to `mutated_args` instead of - // panicking. - // - // Note that, because the problematic MIR is never actually generated, we can't add a test - // case for this. - if let TerminatorKind::Call { ref args, .. } = terminator.kind { for arg in args { if let Operand::Move(place) = arg.node { From c6c4ea40dc645239ebcb0535be6b96a29983fd5b Mon Sep 17 00:00:00 2001 From: Askar Safin Date: Tue, 4 Mar 2025 15:32:55 +0300 Subject: [PATCH 16/29] Revert "compiler/rustc_data_structures/src/sync/worker_local.rs: delete "unsafe impl Sync"" This reverts commit 02406903b0c26440428580a4bbaf30da973c5b23. --- .../rustc_data_structures/src/sync/worker_local.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_data_structures/src/sync/worker_local.rs b/compiler/rustc_data_structures/src/sync/worker_local.rs index 402ec9827bbaa..d75af00985047 100644 --- a/compiler/rustc_data_structures/src/sync/worker_local.rs +++ b/compiler/rustc_data_structures/src/sync/worker_local.rs @@ -106,6 +106,12 @@ pub struct WorkerLocal { registry: Registry, } +// This is safe because the `deref` call will return a reference to a `T` unique to each thread +// or it will panic for threads without an associated local. So there isn't a need for `T` to do +// it's own synchronization. The `verify` method on `RegistryId` has an issue where the id +// can be reused, but `WorkerLocal` has a reference to `Registry` which will prevent any reuse. +unsafe impl Sync for WorkerLocal {} + impl WorkerLocal { /// Creates a new worker local where the `initial` closure computes the /// value this worker local should take for each thread in the registry. @@ -132,11 +138,6 @@ impl Deref for WorkerLocal { fn deref(&self) -> &T { // This is safe because `verify` will only return values less than // `self.registry.thread_limit` which is the size of the `self.locals` array. - - // The `deref` call will return a reference to a `T` unique to each thread - // or it will panic for threads without an associated local. So there isn't a need for `T` to do - // it's own synchronization. The `verify` method on `RegistryId` has an issue where the id - // can be reused, but `WorkerLocal` has a reference to `Registry` which will prevent any reuse. unsafe { &self.locals.get_unchecked(self.registry.id().verify()).0 } } } From 7635f2bfc61b353b83c244cf758459486a2ea442 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 11:03:55 -0600 Subject: [PATCH 17/29] fix whitespace --- src/librustdoc/html/static/js/search.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 45077f7920ba4..cc8c7a3e43a4a 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -1584,17 +1584,17 @@ class DocSearch { * This is effectively string interning, so that function matching can be * done more quickly. Two types with the same name but different item kinds * get the same ID. - * - * @template T extends string - * @overload + * + * @template T extends string + * @overload * @param {T} name * @param {boolean=} isAssocType - True if this is an assoc type * @returns {T extends "" ? null : number} - * - * @param {string} name + * + * @param {string} name * @param {boolean=} isAssocType * @returns {number | null} - * + * */ buildTypeMapIndex(name, isAssocType) { if (name === "" || name === null) { From 32ebe8ed04db275e68b4f1e08d7f6c8ddc806993 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 13:37:56 -0600 Subject: [PATCH 18/29] don't use double quotes --- src/librustdoc/html/static/js/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index cc8c7a3e43a4a..5daaddd50fb61 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -2319,7 +2319,7 @@ class DocSearch { * @param {rustdoc.ParserQueryElement} elem */ function convertTypeFilterOnElem(elem) { - if (typeof elem.typeFilter === 'string') { + if (typeof elem.typeFilter === "string") { let typeFilter = elem.typeFilter; if (typeFilter === "const") { typeFilter = "constant"; From d130e2545510a10373a147192d8c19cdd9bedc00 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 13:54:56 -0600 Subject: [PATCH 19/29] search.js: local variable `extra` is an array, annotate it as such. --- src/librustdoc/html/static/js/search.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 5daaddd50fb61..b4f3bcde4d4d3 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -2398,9 +2398,9 @@ class DocSearch { continue; } if (!foundStopChar) { - let extra = ""; + /** @type String[] */ + let extra = []; if (isLastElemGeneric(query.elems, parserState)) { - // @ts-expect-error extra = [" after ", ">"]; } else if (prevIs(parserState, "\"")) { throw ["Cannot have more than one element if you use quotes"]; From 78b92f08f9d72fd44e8d1a641c23a25a75f7f25d Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 13:58:35 -0600 Subject: [PATCH 20/29] search.js: second argument of convertNameToId is optional --- src/librustdoc/html/static/js/search.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index b4f3bcde4d4d3..57e776deb5c53 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -2538,7 +2538,7 @@ class DocSearch { * See `buildTypeMapIndex` for more information. * * @param {rustdoc.QueryElement} elem - * @param {boolean} isAssocType + * @param {boolean=} isAssocType */ const convertNameToId = (elem, isAssocType) => { const loweredName = elem.pathLast.toLowerCase(); @@ -2618,7 +2618,6 @@ class DocSearch { ]; } for (const elem2 of elem.generics) { - // @ts-expect-error convertNameToId(elem2); } elem.bindings = new Map(Array.from(elem.bindings.entries()) From 9581f1935bd7a8c66e17c6d65ee286f03ca2c60a Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 14:25:04 -0600 Subject: [PATCH 21/29] search.js: give pathSplitter a type signature --- src/librustdoc/html/static/js/search.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 57e776deb5c53..fb481638061db 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -2740,7 +2740,11 @@ class DocSearch { return [displayPath, href, `${exactPath}::${name}`]; }; - // @ts-expect-error + /** + * + * @param {string} path + * @returns {string} + */ function pathSplitter(path) { const tmp = "" + path.replace(/::/g, "::"); if (tmp.endsWith("")) { From 4f6772d2ac31cdf5ca4fb0b3087a0d884a1f2c5c Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 14:28:45 -0600 Subject: [PATCH 22/29] give local mgens a type --- src/librustdoc/html/static/js/search.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index fb481638061db..335e35b211bf1 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -2834,7 +2834,7 @@ class DocSearch { } let fnInputs = null; let fnOutput = null; - // @ts-expect-error + /** @type {Map | null} */ let mgens = null; if (typeInfo !== "elems" && typeInfo !== "returned") { fnInputs = unifyFunctionTypes( @@ -3047,7 +3047,6 @@ class DocSearch { writeFn(nested, result); } return; - // @ts-expect-error } else if (mgens) { for (const [queryId, fnId] of mgens) { if (fnId === fnType.id) { From c05f6bf98d08cfb159051caaae850590758f6c15 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 14:31:35 -0600 Subject: [PATCH 23/29] search.js: give local `where` a type --- src/librustdoc/html/static/js/search.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 335e35b211bf1..9d740cef3eb9b 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -3062,7 +3062,7 @@ class DocSearch { name: fnParamNames[-1 - fnType.id], highlighted: !!fnType.highlighted, }, result); - // @ts-expect-error + /** @type{string[]} */ const where = []; onEachBtwn( fnType.generics, @@ -3072,7 +3072,6 @@ class DocSearch { () => pushText({ name: " + ", highlighted: false }, where), ); if (where.length > 0) { - // @ts-expect-error whereClause.set(fnParamNames[-1 - fnType.id], where); } } else { From 374de12dad8e42e1595f1af5f8e43b8b2b71c61c Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 15:19:20 -0600 Subject: [PATCH 24/29] search.js: give buildIndex a proper return type some of the fields of rustdoc.Row were confusing null and undefined. --- src/librustdoc/html/static/js/rustdoc.d.ts | 3 ++- src/librustdoc/html/static/js/search.js | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index bc4c9a12ac0a1..060d82056587d 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -182,10 +182,11 @@ declare namespace rustdoc { name: string, normalizedName: string, word: string, + paramNames: string[], parent: ({ty: number, name: string, path: string, exactPath: string}|null|undefined), path: string, ty: number, - type?: FunctionSearchType + type: FunctionSearchType | null, } /** diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 9d740cef3eb9b..ccaa8237ff893 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -1573,7 +1573,6 @@ class DocSearch { /** * @type {Array} */ - // @ts-expect-error this.searchIndex = this.buildIndex(rawSearchIndex); } @@ -1902,6 +1901,7 @@ class DocSearch { * Convert raw search index into in-memory search index. * * @param {Map} rawSearchIndex + * @returns {rustdoc.Row[]} */ buildIndex(rawSearchIndex) { /** @@ -2001,6 +2001,7 @@ class DocSearch { return cb; }; + /** @type {rustdoc.Row[]} */ const searchIndex = []; let currentIndex = 0; let id = 0; @@ -2225,6 +2226,7 @@ class DocSearch { // object defined above. const itemParentIdx = itemParentIdxDecoder.next(); normalizedName = word.indexOf("_") === -1 ? word : word.replace(/_/g, ""); + /** @type {rustdoc.Row} */ const row = { crate, ty: itemTypes.charCodeAt(i) - 65, // 65 = "A" From 5f390265409018a653ba6aef7ee7f2ce56afe972 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 15:26:46 -0600 Subject: [PATCH 25/29] search.js: allow empty result arrays --- src/librustdoc/html/static/js/search.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index ccaa8237ff893..8954cefa31639 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -2759,9 +2759,9 @@ class DocSearch { * Add extra data to result objects, and filter items that have been * marked for removal. * - * @param {[rustdoc.ResultObject]} results + * @param {rustdoc.ResultObject[]} results * @param {"sig"|"elems"|"returned"|null} typeInfo - * @returns {[rustdoc.ResultObject]} + * @returns {rustdoc.ResultObject[]} */ const transformResults = (results, typeInfo) => { const duplicates = new Set(); @@ -3175,7 +3175,7 @@ class DocSearch { * @param {rustdoc.Results} results * @param {"sig"|"elems"|"returned"|null} typeInfo * @param {string} preferredCrate - * @returns {Promise<[rustdoc.ResultObject]>} + * @returns {Promise} */ const sortResults = async(results, typeInfo, preferredCrate) => { const userQuery = parsedQuery.userQuery; @@ -3331,7 +3331,6 @@ class DocSearch { return 0; }); - // @ts-expect-error return transformResults(result_list, typeInfo); }; From a9a52ddc04d45dcf06622f360ad9e784adfedc65 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 15:53:35 -0600 Subject: [PATCH 26/29] search.js: give RawSearchIndexCrate.a an index signature --- src/librustdoc/html/static/js/rustdoc.d.ts | 2 +- src/librustdoc/html/static/js/search.js | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/html/static/js/rustdoc.d.ts b/src/librustdoc/html/static/js/rustdoc.d.ts index 060d82056587d..1554c045a3271 100644 --- a/src/librustdoc/html/static/js/rustdoc.d.ts +++ b/src/librustdoc/html/static/js/rustdoc.d.ts @@ -397,7 +397,7 @@ declare namespace rustdoc { */ type RawSearchIndexCrate = { doc: string, - a: Object, + a: { [key: string]: number[] }, n: Array, t: string, D: string, diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 8954cefa31639..8deeff7f973cf 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -2269,16 +2269,14 @@ class DocSearch { continue; } - // @ts-expect-error + /** @type{number[]} */ let currentNameAliases; if (currentCrateAliases.has(alias_name)) { currentNameAliases = currentCrateAliases.get(alias_name); } else { currentNameAliases = []; - // @ts-expect-error currentCrateAliases.set(alias_name, currentNameAliases); } - // @ts-expect-error for (const local_alias of aliases[alias_name]) { currentNameAliases.push(local_alias + currentIndex); } From 892016766a70e929f9325f315d0a9fd953f54853 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 4 Mar 2025 15:54:48 -0600 Subject: [PATCH 27/29] search.js: remove incorrect outdated comment --- src/librustdoc/html/static/js/search.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js index 8deeff7f973cf..c275127997ab1 100644 --- a/src/librustdoc/html/static/js/search.js +++ b/src/librustdoc/html/static/js/search.js @@ -2102,8 +2102,6 @@ class DocSearch { // an array of [(Number) item type, // (String) name] const rawPaths = crateCorpus.p; - // an array of [(String) alias name - // [Number] index to items] const aliases = crateCorpus.a; // an array of [(Number) item index, // (String) comma-separated list of function generic param names] From 4d75c4f8f3e2b186da379358b09f6c347657e1c2 Mon Sep 17 00:00:00 2001 From: "Celina G. Val" Date: Tue, 4 Mar 2025 17:39:02 -0800 Subject: [PATCH 28/29] Make CrateItem::body() function return an option When we initially created `CrateItem`, it would only represent items that contain a body. That is no longer the case, for now, make this explicit by expanding the APIs to retrieve the item body. This is related to https://github.com/rust-lang/project-stable-mir/issues/34 --- compiler/stable_mir/src/lib.rs | 21 ++++++++++++---- tests/ui-fulldeps/stable-mir/check_ty_fold.rs | 12 ++++++---- tests/ui-fulldeps/stable-mir/crate-info.rs | 23 ++++++++++-------- tests/ui-fulldeps/stable-mir/projections.rs | 10 ++++---- tests/ui-fulldeps/stable-mir/smir_internal.rs | 2 +- tests/ui-fulldeps/stable-mir/smir_serde.rs | 24 ++++++++----------- tests/ui-fulldeps/stable-mir/smir_visitor.rs | 12 +++++----- 7 files changed, 57 insertions(+), 47 deletions(-) diff --git a/compiler/stable_mir/src/lib.rs b/compiler/stable_mir/src/lib.rs index 8df36e23c4a21..70d42dfbfcb9a 100644 --- a/compiler/stable_mir/src/lib.rs +++ b/compiler/stable_mir/src/lib.rs @@ -129,13 +129,21 @@ crate_def_with_ty! { } impl CrateItem { - /// This will return the body of an item. - /// - /// This will panic if no body is available. - pub fn body(&self) -> mir::Body { + /// This will return the body of an item or panic if it's not available. + pub fn expect_body(&self) -> mir::Body { with(|cx| cx.mir_body(self.0)) } + /// Return the body of an item if available. + pub fn body(&self) -> Option { + with(|cx| cx.has_body(self.0).then(|| cx.mir_body(self.0))) + } + + /// Check if a body is available for this item. + pub fn has_body(&self) -> bool { + with(|cx| cx.has_body(self.0)) + } + pub fn span(&self) -> Span { with(|cx| cx.span_of_an_item(self.0)) } @@ -156,8 +164,11 @@ impl CrateItem { with(|cx| cx.is_foreign_item(self.0)) } + /// Emit MIR for this item body. pub fn emit_mir(&self, w: &mut W) -> io::Result<()> { - self.body().dump(w, &self.name()) + self.body() + .ok_or_else(|| io::Error::other(format!("No body found for `{}`", self.name())))? + .dump(w, &self.name()) } } diff --git a/tests/ui-fulldeps/stable-mir/check_ty_fold.rs b/tests/ui-fulldeps/stable-mir/check_ty_fold.rs index 9fa4929d68e27..9d71697178e58 100644 --- a/tests/ui-fulldeps/stable-mir/check_ty_fold.rs +++ b/tests/ui-fulldeps/stable-mir/check_ty_fold.rs @@ -18,9 +18,11 @@ extern crate rustc_interface; extern crate stable_mir; use rustc_smir::rustc_internal; -use stable_mir::ty::{RigidTy, TyKind, Ty, }; -use stable_mir::mir::{Body, MirVisitor, FieldIdx, Place, ProjectionElem, visit::{Location, - PlaceContext}}; +use stable_mir::mir::{ + Body, FieldIdx, MirVisitor, Place, ProjectionElem, + visit::{Location, PlaceContext}, +}; +use stable_mir::ty::{RigidTy, Ty, TyKind}; use std::io::Write; use std::ops::ControlFlow; @@ -29,8 +31,8 @@ const CRATE_NAME: &str = "input"; /// This function uses the Stable MIR APIs to get information about the test crate. fn test_stable_mir() -> ControlFlow<()> { let main_fn = stable_mir::entry_fn(); - let body = main_fn.unwrap().body(); - let mut visitor = PlaceVisitor{ body: &body, tested: false}; + let body = main_fn.unwrap().expect_body(); + let mut visitor = PlaceVisitor { body: &body, tested: false }; visitor.visit_body(&body); assert!(visitor.tested); ControlFlow::Continue(()) diff --git a/tests/ui-fulldeps/stable-mir/crate-info.rs b/tests/ui-fulldeps/stable-mir/crate-info.rs index 7f0e9b50335d3..4d2d7e26276b5 100644 --- a/tests/ui-fulldeps/stable-mir/crate-info.rs +++ b/tests/ui-fulldeps/stable-mir/crate-info.rs @@ -45,7 +45,7 @@ fn test_stable_mir() -> ControlFlow<()> { assert!(stable_mir::find_crates("std").len() == 1); let bar = get_item(&items, (DefKind::Fn, "bar")).unwrap(); - let body = bar.body(); + let body = bar.expect_body(); assert_eq!(body.locals().len(), 2); assert_eq!(body.blocks.len(), 1); let block = &body.blocks[0]; @@ -60,7 +60,7 @@ fn test_stable_mir() -> ControlFlow<()> { } let foo_bar = get_item(&items, (DefKind::Fn, "foo_bar")).unwrap(); - let body = foo_bar.body(); + let body = foo_bar.expect_body(); assert_eq!(body.locals().len(), 5); assert_eq!(body.blocks.len(), 4); let block = &body.blocks[0]; @@ -70,7 +70,7 @@ fn test_stable_mir() -> ControlFlow<()> { } let types = get_item(&items, (DefKind::Fn, "types")).unwrap(); - let body = types.body(); + let body = types.expect_body(); assert_eq!(body.locals().len(), 6); assert_matches!( body.locals()[0].ty.kind(), @@ -100,7 +100,7 @@ fn test_stable_mir() -> ControlFlow<()> { ); let drop = get_item(&items, (DefKind::Fn, "drop")).unwrap(); - let body = drop.body(); + let body = drop.expect_body(); assert_eq!(body.blocks.len(), 2); let block = &body.blocks[0]; match &block.terminator.kind { @@ -109,7 +109,7 @@ fn test_stable_mir() -> ControlFlow<()> { } let assert = get_item(&items, (DefKind::Fn, "assert")).unwrap(); - let body = assert.body(); + let body = assert.expect_body(); assert_eq!(body.blocks.len(), 2); let block = &body.blocks[0]; match &block.terminator.kind { @@ -123,7 +123,8 @@ fn test_stable_mir() -> ControlFlow<()> { match &block.terminator.kind { stable_mir::mir::TerminatorKind::Call { func, .. } => { let TyKind::RigidTy(ty) = func.ty(&body.locals()).unwrap().kind() else { - unreachable!() }; + unreachable!() + }; let RigidTy::FnDef(def, args) = ty else { unreachable!() }; let next_func = Instance::resolve(def, &args).unwrap(); match next_func.body().unwrap().locals()[1].ty.kind() { @@ -138,10 +139,10 @@ fn test_stable_mir() -> ControlFlow<()> { let foo_const = get_item(&items, (DefKind::Const, "FOO")).unwrap(); // Ensure we don't panic trying to get the body of a constant. - foo_const.body(); + foo_const.expect_body(); let locals_fn = get_item(&items, (DefKind::Fn, "locals")).unwrap(); - let body = locals_fn.body(); + let body = locals_fn.expect_body(); assert_eq!(body.locals().len(), 4); assert_matches!( body.ret_local().ty.kind(), @@ -172,8 +173,10 @@ fn get_item<'a>( item: (DefKind, &str), ) -> Option<&'a stable_mir::CrateItem> { items.iter().find(|crate_item| { - matches!((item.0, crate_item.kind()), (DefKind::Fn, ItemKind::Fn) | (DefKind::Const, - ItemKind::Const)) && crate_item.name() == item.1 + matches!( + (item.0, crate_item.kind()), + (DefKind::Fn, ItemKind::Fn) | (DefKind::Const, ItemKind::Const) + ) && crate_item.name() == item.1 }) } diff --git a/tests/ui-fulldeps/stable-mir/projections.rs b/tests/ui-fulldeps/stable-mir/projections.rs index 3cc71247e6744..6f82eba61fcea 100644 --- a/tests/ui-fulldeps/stable-mir/projections.rs +++ b/tests/ui-fulldeps/stable-mir/projections.rs @@ -18,10 +18,10 @@ extern crate rustc_interface; extern crate stable_mir; use rustc_smir::rustc_internal; +use stable_mir::ItemKind; use stable_mir::crate_def::CrateDef; use stable_mir::mir::{ProjectionElem, Rvalue, StatementKind}; use stable_mir::ty::{RigidTy, TyKind, UintTy}; -use stable_mir::ItemKind; use std::assert_matches::assert_matches; use std::io::Write; use std::ops::ControlFlow; @@ -31,7 +31,7 @@ const CRATE_NAME: &str = "input"; /// Tests projections within Place objects fn test_place_projections() -> ControlFlow<()> { let items = stable_mir::all_local_items(); - let body = get_item(&items, (ItemKind::Fn, "projections")).unwrap().body(); + let body = get_item(&items, (ItemKind::Fn, "projections")).unwrap().expect_body(); assert_eq!(body.blocks.len(), 4); // The first statement assigns `&s.c` to a local. The projections include a deref for `s`, since // `s` is passed as a reference argument, and a field access for field `c`. @@ -53,7 +53,7 @@ fn test_place_projections() -> ControlFlow<()> { ); let ty = place.ty(body.locals()).unwrap(); assert_matches!(ty.kind().rigid(), Some(RigidTy::Ref(..))); - }, + } other => panic!( "Unable to match against expected rvalue projection. Expected the projection \ for `s.c`, which is a Deref and u8 Field. Got: {:?}", @@ -137,9 +137,7 @@ fn get_item<'a>( items: &'a stable_mir::CrateItems, item: (ItemKind, &str), ) -> Option<&'a stable_mir::CrateItem> { - items.iter().find(|crate_item| { - crate_item.kind() == item.0 && crate_item.name() == item.1 - }) + items.iter().find(|crate_item| crate_item.kind() == item.0 && crate_item.name() == item.1) } /// This test will generate and analyze a dummy crate using the stable mir. diff --git a/tests/ui-fulldeps/stable-mir/smir_internal.rs b/tests/ui-fulldeps/stable-mir/smir_internal.rs index 453e5372de4f8..f9972dc27e3ff 100644 --- a/tests/ui-fulldeps/stable-mir/smir_internal.rs +++ b/tests/ui-fulldeps/stable-mir/smir_internal.rs @@ -26,7 +26,7 @@ const CRATE_NAME: &str = "input"; fn test_translation(tcx: TyCtxt<'_>) -> ControlFlow<()> { let main_fn = stable_mir::entry_fn().unwrap(); - let body = main_fn.body(); + let body = main_fn.expect_body(); let orig_ty = body.locals()[0].ty; let rustc_ty = rustc_internal::internal(tcx, &orig_ty); assert!(rustc_ty.is_unit()); diff --git a/tests/ui-fulldeps/stable-mir/smir_serde.rs b/tests/ui-fulldeps/stable-mir/smir_serde.rs index 2c74276d550cf..9b3638a9f2f40 100644 --- a/tests/ui-fulldeps/stable-mir/smir_serde.rs +++ b/tests/ui-fulldeps/stable-mir/smir_serde.rs @@ -14,33 +14,29 @@ extern crate rustc_smir; extern crate rustc_driver; extern crate rustc_interface; extern crate rustc_middle; -extern crate stable_mir; extern crate serde; extern crate serde_json; +extern crate stable_mir; use rustc_middle::ty::TyCtxt; use rustc_smir::rustc_internal; +use serde_json::to_string; use stable_mir::mir::Body; -use std::io::{Write, BufWriter}; +use std::io::{BufWriter, Write}; use std::ops::ControlFlow; -use serde_json::to_string; - const CRATE_NAME: &str = "input"; fn serialize_to_json(_tcx: TyCtxt<'_>) -> ControlFlow<()> { let path = "output.json"; - let mut writer = BufWriter::new(std::fs::File::create(path) - .expect("Failed to create path")); + let mut writer = BufWriter::new(std::fs::File::create(path).expect("Failed to create path")); let local_crate = stable_mir::local_crate(); - let items: Vec = stable_mir::all_local_items() - .iter() - .map(|item| { item.body() }) - .collect(); - let crate_data = ( local_crate.name, items ); - writer.write_all(to_string(&crate_data) - .expect("serde_json failed") - .as_bytes()).expect("JSON serialization failed"); + let items: Vec = + stable_mir::all_local_items().iter().map(|item| item.expect_body()).collect(); + let crate_data = (local_crate.name, items); + writer + .write_all(to_string(&crate_data).expect("serde_json failed").as_bytes()) + .expect("JSON serialization failed"); ControlFlow::Continue(()) } diff --git a/tests/ui-fulldeps/stable-mir/smir_visitor.rs b/tests/ui-fulldeps/stable-mir/smir_visitor.rs index 0a6415d490e4b..cffb41742b4e4 100644 --- a/tests/ui-fulldeps/stable-mir/smir_visitor.rs +++ b/tests/ui-fulldeps/stable-mir/smir_visitor.rs @@ -16,10 +16,10 @@ extern crate rustc_driver; extern crate rustc_interface; extern crate stable_mir; -use std::collections::HashSet; use rustc_smir::rustc_internal; -use stable_mir::*; use stable_mir::mir::MirVisitor; +use stable_mir::*; +use std::collections::HashSet; use std::io::Write; use std::ops::ControlFlow; @@ -27,7 +27,7 @@ const CRATE_NAME: &str = "input"; fn test_visitor() -> ControlFlow<()> { let main_fn = stable_mir::entry_fn(); - let main_body = main_fn.unwrap().body(); + let main_body = main_fn.unwrap().expect_body(); let main_visitor = TestVisitor::collect(&main_body); assert!(main_visitor.ret_val.is_some()); assert!(main_visitor.args.is_empty()); @@ -51,7 +51,7 @@ struct TestVisitor<'a> { pub tys: HashSet, pub ret_val: Option, pub args: Vec, - pub calls: Vec + pub calls: Vec, } impl<'a> TestVisitor<'a> { @@ -90,8 +90,8 @@ impl<'a> mir::MirVisitor for TestVisitor<'a> { fn visit_terminator(&mut self, term: &mir::Terminator, location: mir::visit::Location) { if let mir::TerminatorKind::Call { func, .. } = &term.kind { let ty::TyKind::RigidTy(ty) = func.ty(self.body.locals()).unwrap().kind() else { - unreachable! - () }; + unreachable!() + }; let ty::RigidTy::FnDef(def, args) = ty else { unreachable!() }; self.calls.push(mir::mono::Instance::resolve(def, &args).unwrap()); } From 55f8d3f628e674d6874c96c9da425db406f15d09 Mon Sep 17 00:00:00 2001 From: Predrag Gruevski Date: Tue, 4 Mar 2025 22:03:28 +0000 Subject: [PATCH 29/29] Pretty-print `#[deprecated]` attribute in HIR. --- compiler/rustc_hir_pretty/src/lib.rs | 74 ++++++++++++++++++++++++ tests/ui/unpretty/deprecated-attr.rs | 17 ++++++ tests/ui/unpretty/deprecated-attr.stdout | 21 +++++++ tests/ui/unpretty/diagnostic-attr.rs | 13 +++++ tests/ui/unpretty/diagnostic-attr.stdout | 16 +++++ 5 files changed, 141 insertions(+) create mode 100644 tests/ui/unpretty/deprecated-attr.rs create mode 100644 tests/ui/unpretty/deprecated-attr.stdout create mode 100644 tests/ui/unpretty/diagnostic-attr.rs create mode 100644 tests/ui/unpretty/diagnostic-attr.stdout diff --git a/compiler/rustc_hir_pretty/src/lib.rs b/compiler/rustc_hir_pretty/src/lib.rs index 22edc18d71ca3..1658c8dac6768 100644 --- a/compiler/rustc_hir_pretty/src/lib.rs +++ b/compiler/rustc_hir_pretty/src/lib.rs @@ -117,6 +117,80 @@ impl<'a> State<'a> { )); self.hardbreak() } + hir::Attribute::Parsed(AttributeKind::Deprecation { deprecation, .. }) => { + self.word("#[deprecated"); + + // There are three possible forms here: + // 1. a form with explicit components like + // `#[deprecated(since = "1.2.3", note = "some note", suggestion = "something")]` + // where each component may be present or absent. + // 2. `#[deprecated = "message"]` + // 3. `#[deprecated]` + // + // Let's figure out which we need. + // If there's a `since` or `suggestion` value, we're definitely in form 1. + if matches!( + deprecation.since, + rustc_attr_parsing::DeprecatedSince::RustcVersion(..) + | rustc_attr_parsing::DeprecatedSince::Future + | rustc_attr_parsing::DeprecatedSince::NonStandard(..) + ) || deprecation.suggestion.is_some() + { + self.word("("); + let mut use_comma = false; + + match &deprecation.since { + rustc_attr_parsing::DeprecatedSince::RustcVersion(rustc_version) => { + self.word("since = \""); + self.word(format!( + "{}.{}.{}", + rustc_version.major, rustc_version.minor, rustc_version.patch + )); + self.word("\""); + use_comma = true; + } + rustc_attr_parsing::DeprecatedSince::Future => { + self.word("since = \"future\""); + use_comma = true; + } + rustc_attr_parsing::DeprecatedSince::NonStandard(symbol) => { + self.word("since = \""); + self.word(symbol.to_ident_string()); + self.word("\""); + use_comma = true; + } + _ => {} + } + + if let Some(note) = &deprecation.note { + if use_comma { + self.word(", "); + } + self.word("note = \""); + self.word(note.to_ident_string()); + self.word("\""); + use_comma = true; + } + + if let Some(suggestion) = &deprecation.suggestion { + if use_comma { + self.word(", "); + } + self.word("suggestion = \""); + self.word(suggestion.to_ident_string()); + self.word("\""); + } + } else if let Some(note) = &deprecation.note { + // We're in form 2: `#[deprecated = "message"]`. + self.word(" = \""); + self.word(note.to_ident_string()); + self.word("\""); + } else { + // We're in form 3: `#[deprecated]`. Nothing to do here. + } + + self.word("]"); + } hir::Attribute::Parsed(pa) => { self.word("#[attr=\""); pa.print_attribute(self); diff --git a/tests/ui/unpretty/deprecated-attr.rs b/tests/ui/unpretty/deprecated-attr.rs new file mode 100644 index 0000000000000..dda362a595e24 --- /dev/null +++ b/tests/ui/unpretty/deprecated-attr.rs @@ -0,0 +1,17 @@ +//@ compile-flags: -Zunpretty=hir +//@ check-pass + +#[deprecated] +pub struct PlainDeprecated; + +#[deprecated = "here's why this is deprecated"] +pub struct DirectNote; + +#[deprecated(note = "here's why this is deprecated")] +pub struct ExplicitNote; + +#[deprecated(since = "1.2.3", note = "here's why this is deprecated")] +pub struct SinceAndNote; + +#[deprecated(note = "here's why this is deprecated", since = "1.2.3")] +pub struct FlippedOrder; diff --git a/tests/ui/unpretty/deprecated-attr.stdout b/tests/ui/unpretty/deprecated-attr.stdout new file mode 100644 index 0000000000000..60dbac1072b9f --- /dev/null +++ b/tests/ui/unpretty/deprecated-attr.stdout @@ -0,0 +1,21 @@ +#[prelude_import] +use ::std::prelude::rust_2015::*; +#[macro_use] +extern crate std; +//@ compile-flags: -Zunpretty=hir +//@ check-pass + +#[deprecated] +struct PlainDeprecated; + +#[deprecated = "here's why this is deprecated"] +struct DirectNote; + +#[deprecated = "here's why this is deprecated"] +struct ExplicitNote; + +#[deprecated(since = "1.2.3", note = "here's why this is deprecated"] +struct SinceAndNote; + +#[deprecated(since = "1.2.3", note = "here's why this is deprecated"] +struct FlippedOrder; diff --git a/tests/ui/unpretty/diagnostic-attr.rs b/tests/ui/unpretty/diagnostic-attr.rs new file mode 100644 index 0000000000000..27f5b693e691c --- /dev/null +++ b/tests/ui/unpretty/diagnostic-attr.rs @@ -0,0 +1,13 @@ +//@ compile-flags: -Zunpretty=hir +//@ check-pass + +#[diagnostic::on_unimplemented( + message = "My Message for `ImportantTrait<{A}>` implemented for `{Self}`", + label = "My Label", + note = "Note 1", + note = "Note 2" +)] +pub trait ImportantTrait {} + +#[diagnostic::do_not_recommend] +impl ImportantTrait for T where T: Clone {} diff --git a/tests/ui/unpretty/diagnostic-attr.stdout b/tests/ui/unpretty/diagnostic-attr.stdout new file mode 100644 index 0000000000000..e8696d04d38ab --- /dev/null +++ b/tests/ui/unpretty/diagnostic-attr.stdout @@ -0,0 +1,16 @@ +#[prelude_import] +use ::std::prelude::rust_2015::*; +#[macro_use] +extern crate std; +//@ compile-flags: -Zunpretty=hir +//@ check-pass + +#[diagnostic::on_unimplemented(message = +"My Message for `ImportantTrait<{A}>` implemented for `{Self}`", label = +"My Label", note = "Note 1", note = "Note 2")] +trait ImportantTrait { } + +#[diagnostic::do_not_recommend] +impl ImportantTrait for T where T: Clone + {#![diagnostic::do_not_recommend] +}