From a1062b9bdcf691fc09f8683ae36d2b31c06c3d8f Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Thu, 22 Sep 2022 23:41:38 +0800 Subject: [PATCH 1/8] recover from struct nested in struct --- compiler/rustc_parse/src/parser/item.rs | 17 +++++++++++++++++ src/test/ui/parser/issues/issue-101540.rs | 7 +++++++ src/test/ui/parser/issues/issue-101540.stderr | 10 ++++++++++ 3 files changed, 34 insertions(+) create mode 100644 src/test/ui/parser/issues/issue-101540.rs create mode 100644 src/test/ui/parser/issues/issue-101540.stderr diff --git a/compiler/rustc_parse/src/parser/item.rs b/compiler/rustc_parse/src/parser/item.rs index e55b5ce71cded..e385ac44113a6 100644 --- a/compiler/rustc_parse/src/parser/item.rs +++ b/compiler/rustc_parse/src/parser/item.rs @@ -1715,6 +1715,7 @@ impl<'a> Parser<'a> { fn parse_field_ident(&mut self, adt_ty: &str, lo: Span) -> PResult<'a, Ident> { let (ident, is_raw) = self.ident_or_err()?; if !is_raw && ident.is_reserved() { + let snapshot = self.create_snapshot_for_diagnostic(); let err = if self.check_fn_front_matter(false) { let inherited_vis = Visibility { span: rustc_span::DUMMY_SP, @@ -1735,6 +1736,22 @@ impl<'a> Parser<'a> { err.help("unlike in C++, Java, and C#, functions are declared in `impl` blocks"); err.help("see https://doc.rust-lang.org/book/ch05-03-method-syntax.html for more information"); err + } else if self.eat_keyword(kw::Struct) { + match self.parse_item_struct() { + Ok((ident, _)) => { + let mut err = self.struct_span_err( + lo.with_hi(ident.span.hi()), + &format!("structs are not allowed in {adt_ty} definitions"), + ); + err.help("consider creating a new `struct` definition instead of nesting"); + err + } + Err(err) => { + err.cancel(); + self.restore_snapshot(snapshot); + self.expected_ident_found() + } + } } else { self.expected_ident_found() }; diff --git a/src/test/ui/parser/issues/issue-101540.rs b/src/test/ui/parser/issues/issue-101540.rs new file mode 100644 index 0000000000000..328ec6f906b1f --- /dev/null +++ b/src/test/ui/parser/issues/issue-101540.rs @@ -0,0 +1,7 @@ +struct S1 { + struct S2 { + //~^ ERROR structs are not allowed in struct definitions + } +} + +fn main() {} diff --git a/src/test/ui/parser/issues/issue-101540.stderr b/src/test/ui/parser/issues/issue-101540.stderr new file mode 100644 index 0000000000000..42e471cc13504 --- /dev/null +++ b/src/test/ui/parser/issues/issue-101540.stderr @@ -0,0 +1,10 @@ +error: structs are not allowed in struct definitions + --> $DIR/issue-101540.rs:2:5 + | +LL | struct S2 { + | _____-^^^^^ +LL | | +LL | | } + | |_____- help: consider creating a new `struct` definition instead of nesting + +error: aborting due to previous error From f4170b38c8735daa9e764ea395c6c32cd7b9ffa3 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Fri, 23 Sep 2022 00:02:50 +0800 Subject: [PATCH 2/8] Change UI test stderr --- src/test/ui/parser/issues/issue-101540.stderr | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/ui/parser/issues/issue-101540.stderr b/src/test/ui/parser/issues/issue-101540.stderr index 42e471cc13504..d4e861b6061fa 100644 --- a/src/test/ui/parser/issues/issue-101540.stderr +++ b/src/test/ui/parser/issues/issue-101540.stderr @@ -2,9 +2,8 @@ error: structs are not allowed in struct definitions --> $DIR/issue-101540.rs:2:5 | LL | struct S2 { - | _____-^^^^^ -LL | | -LL | | } - | |_____- help: consider creating a new `struct` definition instead of nesting + | ^^^^^^^^^ + | + = help: consider creating a new `struct` definition instead of nesting error: aborting due to previous error From c66c2e82c1a405ed3fff99953ab96de03806ca09 Mon Sep 17 00:00:00 2001 From: Rageking8 <106309953+Rageking8@users.noreply.github.com> Date: Fri, 23 Sep 2022 00:24:30 +0800 Subject: [PATCH 3/8] Fix slight issues with stderr --- src/test/ui/parser/issues/issue-101540.stderr | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/ui/parser/issues/issue-101540.stderr b/src/test/ui/parser/issues/issue-101540.stderr index d4e861b6061fa..53c7c9590e6ca 100644 --- a/src/test/ui/parser/issues/issue-101540.stderr +++ b/src/test/ui/parser/issues/issue-101540.stderr @@ -1,9 +1,10 @@ error: structs are not allowed in struct definitions --> $DIR/issue-101540.rs:2:5 | -LL | struct S2 { - | ^^^^^^^^^ +LL | struct S2 { + | ^^^^^^^^^ | = help: consider creating a new `struct` definition instead of nesting error: aborting due to previous error + From c8d346e527fc556e3d3715c963c98a474a543320 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 23 Sep 2022 11:06:30 +0200 Subject: [PATCH 4/8] bootstrap: the backtrace feature is stable, no need to allow it any more --- src/bootstrap/builder.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/bootstrap/builder.rs b/src/bootstrap/builder.rs index bc6283ef467d6..742e614d0fff9 100644 --- a/src/bootstrap/builder.rs +++ b/src/bootstrap/builder.rs @@ -1556,13 +1556,12 @@ impl<'a> Builder<'a> { match mode { Mode::ToolBootstrap => { // Restrict the allowed features to those passed by rustbuild, so we don't depend on nightly accidentally. - // HACK: because anyhow does feature detection in build.rs, we need to allow the backtrace feature too. - rustflags.arg("-Zallow-features=binary-dep-depinfo,backtrace"); + rustflags.arg("-Zallow-features=binary-dep-depinfo"); } Mode::ToolStd => { // Right now this is just compiletest and a few other tools that build on stable. // Allow them to use `feature(test)`, but nothing else. - rustflags.arg("-Zallow-features=binary-dep-depinfo,test,backtrace,proc_macro_internals,proc_macro_diagnostic,proc_macro_span"); + rustflags.arg("-Zallow-features=binary-dep-depinfo,test,proc_macro_internals,proc_macro_diagnostic,proc_macro_span"); } Mode::Std | Mode::Rustc | Mode::Codegen | Mode::ToolRustc => {} } From aa35ab81ea4bd427a86e0c781c342835a020e6fe Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Fri, 23 Sep 2022 18:03:44 +0200 Subject: [PATCH 5/8] Stabilize const `BTree{Map,Set}::new` Since `len` and `is_empty` are not const stable yet, this also creates a new feature for them since they previously used the same `const_btree_new` feature. --- compiler/rustc_hir/src/lib.rs | 2 +- library/alloc/src/collections/btree/map.rs | 6 +++--- library/alloc/src/collections/btree/set.rs | 6 +++--- library/alloc/src/lib.rs | 2 +- library/alloc/tests/lib.rs | 2 +- src/test/ui/consts/issue-88071.rs | 2 -- src/tools/clippy/tests/ui/crashes/ice-7126.rs | 6 +++--- 7 files changed, 12 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_hir/src/lib.rs b/compiler/rustc_hir/src/lib.rs index 946da9265ba09..556ff1cc48e4d 100644 --- a/compiler/rustc_hir/src/lib.rs +++ b/compiler/rustc_hir/src/lib.rs @@ -4,7 +4,7 @@ #![feature(associated_type_defaults)] #![feature(closure_track_caller)] -#![feature(const_btree_new)] +#![feature(const_btree_len)] #![cfg_attr(bootstrap, feature(let_else))] #![feature(once_cell)] #![feature(min_specialization)] diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index cacbd54b6c246..3018d1c9125bf 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -580,7 +580,7 @@ impl BTreeMap { /// map.insert(1, "a"); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + #[rustc_const_stable(feature = "const_btree_new", since = "CURRENT_RUSTC_VERSION")] #[must_use] pub const fn new() -> BTreeMap { BTreeMap { root: None, length: 0, alloc: ManuallyDrop::new(Global), _marker: PhantomData } @@ -2392,7 +2392,7 @@ impl BTreeMap { /// ``` #[must_use] #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + #[rustc_const_unstable(feature = "const_btree_len", issue = "71835")] pub const fn len(&self) -> usize { self.length } @@ -2413,7 +2413,7 @@ impl BTreeMap { /// ``` #[must_use] #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + #[rustc_const_unstable(feature = "const_btree_len", issue = "71835")] pub const fn is_empty(&self) -> bool { self.len() == 0 } diff --git a/library/alloc/src/collections/btree/set.rs b/library/alloc/src/collections/btree/set.rs index 2cfc080740921..3caaf521240d2 100644 --- a/library/alloc/src/collections/btree/set.rs +++ b/library/alloc/src/collections/btree/set.rs @@ -343,7 +343,7 @@ impl BTreeSet { /// let mut set: BTreeSet = BTreeSet::new(); /// ``` #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + #[rustc_const_stable(feature = "const_btree_new", since = "CURRENT_RUSTC_VERSION")] #[must_use] pub const fn new() -> BTreeSet { BTreeSet { map: BTreeMap::new() } @@ -1174,7 +1174,7 @@ impl BTreeSet { /// ``` #[must_use] #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + #[rustc_const_unstable(feature = "const_btree_len", issue = "71835")] pub const fn len(&self) -> usize { self.map.len() } @@ -1193,7 +1193,7 @@ impl BTreeSet { /// ``` #[must_use] #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_const_unstable(feature = "const_btree_new", issue = "71835")] + #[rustc_const_unstable(feature = "const_btree_len", issue = "71835")] pub const fn is_empty(&self) -> bool { self.len() == 0 } diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 6b3b1c222229c..fe89394dda928 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -99,7 +99,7 @@ #![feature(coerce_unsized)] #![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))] #![feature(const_box)] -#![cfg_attr(not(no_global_oom_handling), feature(const_btree_new))] +#![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))] #![feature(const_cow_is_borrowed)] #![feature(const_convert)] #![feature(const_size_of_val)] diff --git a/library/alloc/tests/lib.rs b/library/alloc/tests/lib.rs index 490c0d8f76cda..55aced5106c94 100644 --- a/library/alloc/tests/lib.rs +++ b/library/alloc/tests/lib.rs @@ -32,7 +32,7 @@ #![feature(slice_group_by)] #![feature(slice_partition_dedup)] #![feature(string_remove_matches)] -#![feature(const_btree_new)] +#![feature(const_btree_len)] #![feature(const_default_impls)] #![feature(const_trait_impl)] #![feature(const_str_from_utf8)] diff --git a/src/test/ui/consts/issue-88071.rs b/src/test/ui/consts/issue-88071.rs index 1c38c43e6c0c5..f58cdb5945e53 100644 --- a/src/test/ui/consts/issue-88071.rs +++ b/src/test/ui/consts/issue-88071.rs @@ -2,8 +2,6 @@ // // regression test for #88071 -#![feature(const_btree_new)] - use std::collections::BTreeMap; pub struct CustomMap(BTreeMap); diff --git a/src/tools/clippy/tests/ui/crashes/ice-7126.rs b/src/tools/clippy/tests/ui/crashes/ice-7126.rs index ca563ba097851..b2dc2248b5562 100644 --- a/src/tools/clippy/tests/ui/crashes/ice-7126.rs +++ b/src/tools/clippy/tests/ui/crashes/ice-7126.rs @@ -1,13 +1,13 @@ // This test requires a feature gated const fn and will stop working in the future. -#![feature(const_btree_new)] +#![feature(const_btree_len)] use std::collections::BTreeMap; -struct Foo(BTreeMap); +struct Foo(usize); impl Foo { fn new() -> Self { - Self(BTreeMap::new()) + Self(BTreeMap::len(&BTreeMap::::new())) } } From 66484c0a21b840b30f865e92fdd5b3bad877816f Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Fri, 23 Sep 2022 21:04:54 +0200 Subject: [PATCH 6/8] Fix clippy's const fn stability check for CURRENT_RUSTC_VERSION Since clippy can use a projects MSRV for its lints, it might not want to consider functions as const stable if they have been added lately. Functions that have been stabilized this version use CURRENT_RUSTC_VERSION as their version, which gets then turned into the current version, which might be something like `1.66.0-dev`. The version parser cannot deal with this version, so it has to be stripped off. --- .../clippy/clippy_utils/src/qualify_min_const_fn.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs b/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs index 405f022868395..f7ce719177268 100644 --- a/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs +++ b/src/tools/clippy/clippy_utils/src/qualify_min_const_fn.rs @@ -367,10 +367,21 @@ fn is_const_fn(tcx: TyCtxt<'_>, def_id: DefId, msrv: Option) -> bo // Checking MSRV is manually necessary because `rustc` has no such concept. This entire // function could be removed if `rustc` provided a MSRV-aware version of `is_const_fn`. // as a part of an unimplemented MSRV check https://github.com/rust-lang/rust/issues/65262. + + // HACK(nilstrieb): CURRENT_RUSTC_VERSION can return versions like 1.66.0-dev. `rustc-semver` doesn't accept + // the `-dev` version number so we have to strip it off. + let short_version = since + .as_str() + .split('-') + .next() + .expect("rustc_attr::StabilityLevel::Stable::since` is empty"); + + let since = rustc_span::Symbol::intern(short_version); + crate::meets_msrv( msrv, RustcVersion::parse(since.as_str()) - .expect("`rustc_attr::StabilityLevel::Stable::since` is ill-formatted"), + .unwrap_or_else(|err| panic!("`rustc_attr::StabilityLevel::Stable::since` is ill-formatted: `{since}`, {err:?}")), ) } else { // Unstable const fn with the feature enabled. From 41ac87dd38e47fef2de46045ea9c1cb2eba93a8a Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 25 Sep 2022 09:42:01 -0500 Subject: [PATCH 7/8] Don't set RUSTC in the bootstrap build script We no longer use this for anything since https://github.com/rust-lang/rust/pull/98483/files#diff-7eddc76f1be9eca2599a9ae58c65ffe247fbdff9b02ef687439894cab9afe749L781. Remove it, so that we spuriously rebuild bootstrap fewer times on Windows (where PATH changes often). --- src/bootstrap/build.rs | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/src/bootstrap/build.rs b/src/bootstrap/build.rs index ab34d5c1e5504..cd1f418028c62 100644 --- a/src/bootstrap/build.rs +++ b/src/bootstrap/build.rs @@ -1,43 +1,7 @@ -use env::consts::{EXE_EXTENSION, EXE_SUFFIX}; use std::env; -use std::ffi::OsString; -use std::path::PathBuf; - -/// Given an executable called `name`, return the filename for the -/// executable for a particular target. -pub fn exe(name: &PathBuf) -> PathBuf { - if EXE_EXTENSION != "" && name.extension() != Some(EXE_EXTENSION.as_ref()) { - let mut name: OsString = name.clone().into(); - name.push(EXE_SUFFIX); - name.into() - } else { - name.clone() - } -} fn main() { let host = env::var("HOST").unwrap(); println!("cargo:rerun-if-changed=build.rs"); - println!("cargo:rerun-if-env-changed=RUSTC"); println!("cargo:rustc-env=BUILD_TRIPLE={}", host); - - // This may not be a canonicalized path. - let mut rustc = PathBuf::from(env::var_os("RUSTC").unwrap()); - - if rustc.is_relative() { - println!("cargo:rerun-if-env-changed=PATH"); - for dir in env::split_paths(&env::var_os("PATH").unwrap_or_default()) { - let absolute = dir.join(&exe(&rustc)); - if absolute.exists() { - rustc = absolute; - break; - } - } - } - assert!(rustc.is_absolute()); - - // FIXME: if the path is not utf-8, this is going to break. Unfortunately - // Cargo doesn't have a way for us to specify non-utf-8 paths easily, so - // we'll need to invent some encoding scheme if this becomes a problem. - println!("cargo:rustc-env=RUSTC={}", rustc.to_str().unwrap()); } From ddf79827418eea02baf6652eac5bbcf60c061dbb Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sun, 25 Sep 2022 18:35:43 +0200 Subject: [PATCH 8/8] Remove benches from `rustc_middle` These benches benchmark rust langauge features and not the compiler, so they seem to be in the wrong place here. They also all take <1ns, making them pretty useless. Looking at their git history, they just seem to have been carried around for many, many years. This commit ends their journey. --- compiler/rustc_middle/benches/lib.rs | 54 ---------------------------- 1 file changed, 54 deletions(-) delete mode 100644 compiler/rustc_middle/benches/lib.rs diff --git a/compiler/rustc_middle/benches/lib.rs b/compiler/rustc_middle/benches/lib.rs deleted file mode 100644 index 237751bcbd787..0000000000000 --- a/compiler/rustc_middle/benches/lib.rs +++ /dev/null @@ -1,54 +0,0 @@ -#![feature(test)] - -extern crate test; - -use test::Bencher; - -// Static/dynamic method dispatch - -struct Struct { - field: isize, -} - -trait Trait { - fn method(&self) -> isize; -} - -impl Trait for Struct { - fn method(&self) -> isize { - self.field - } -} - -#[bench] -fn trait_vtable_method_call(b: &mut Bencher) { - let s = Struct { field: 10 }; - let t = &s as &dyn Trait; - b.iter(|| t.method()); -} - -#[bench] -fn trait_static_method_call(b: &mut Bencher) { - let s = Struct { field: 10 }; - b.iter(|| s.method()); -} - -// Overhead of various match forms - -#[bench] -fn option_some(b: &mut Bencher) { - let x = Some(10); - b.iter(|| match x { - Some(y) => y, - None => 11, - }); -} - -#[bench] -fn vec_pattern(b: &mut Bencher) { - let x = [1, 2, 3, 4, 5, 6]; - b.iter(|| match x { - [1, 2, 3, ..] => 10, - _ => 11, - }); -}