Skip to content

Commit

Permalink
Rollup merge of rust-lang#94461 - jhpratt:2024-edition, r=pnkfelix
Browse files Browse the repository at this point in the history
Create (unstable) 2024 edition

[On Zulip](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/Deprecating.20macro.20scoping.20shenanigans/near/272860652), there was a small aside regarding creating the 2024 edition now as opposed to later. There was a reasonable amount of support and no stated opposition.

This change creates the 2024 edition in the compiler and creates a prelude for the 2024 edition. There is no current difference between the 2021 and 2024 editions. Cargo and other tools will need to be updated separately, as it's not in the same repository. This change permits the vast majority of work towards the next edition to proceed _now_ instead of waiting until 2024.

For sanity purposes, I've merged the "hello" UI tests into a single file with multiple revisions. Otherwise we'd end up with a file per edition, despite them being essentially identical.

````@rustbot```` label +T-lang +S-waiting-on-review

Not sure on the relevant team, to be honest.
  • Loading branch information
Dylan-DPC authored Apr 15, 2022
2 parents 27e2d81 + 6b75406 commit 20bf34f
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 12 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ impl NonterminalKind {
Edition::Edition2015 | Edition::Edition2018 => {
NonterminalKind::PatParam { inferred: true }
}
Edition::Edition2021 => NonterminalKind::PatWithOr,
Edition::Edition2021 | Edition::Edition2024 => NonterminalKind::PatWithOr,
},
sym::pat_param => NonterminalKind::PatParam { inferred: false },
sym::expr => NonterminalKind::Expr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub fn inject(
Edition2015 => sym::rust_2015,
Edition2018 => sym::rust_2018,
Edition2021 => sym::rust_2021,
Edition2024 => sym::rust_2024,
}])
.map(|&symbol| Ident::new(symbol, span))
.collect();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ fn check_matcher_core<'tt>(
err.span_label(sp, format!("not allowed after `{}` fragments", kind));

if kind == NonterminalKind::PatWithOr
&& sess.edition == Edition::Edition2021
&& sess.edition.rust_2021()
&& next_token.is_token(&BinOp(token::BinOpToken::Or))
{
let suggestion = quoted_tt_to_string(&TokenTree::MetaVarDecl(
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,11 @@ impl Session {
self.opts.edition >= Edition::Edition2021
}

/// Are we allowed to use features from the Rust 2024 edition?
pub fn rust_2024(&self) -> bool {
self.opts.edition >= Edition::Edition2024
}

pub fn edition(&self) -> Edition {
self.opts.edition
}
Expand Down
30 changes: 28 additions & 2 deletions compiler/rustc_span/src/edition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ pub enum Edition {
Edition2018,
/// The 2021 edition
Edition2021,
/// The 2024 edition
Edition2024,
}

// Must be in order from oldest to newest.
pub const ALL_EDITIONS: &[Edition] =
&[Edition::Edition2015, Edition::Edition2018, Edition::Edition2021];
&[Edition::Edition2015, Edition::Edition2018, Edition::Edition2021, Edition::Edition2024];

pub const EDITION_NAME_LIST: &str = "2015|2018|2021";
pub const EDITION_NAME_LIST: &str = "2015|2018|2021|2024";

pub const DEFAULT_EDITION: Edition = Edition::Edition2015;

Expand All @@ -40,6 +42,7 @@ impl fmt::Display for Edition {
Edition::Edition2015 => "2015",
Edition::Edition2018 => "2018",
Edition::Edition2021 => "2021",
Edition::Edition2024 => "2024",
};
write!(f, "{}", s)
}
Expand All @@ -51,6 +54,7 @@ impl Edition {
Edition::Edition2015 => "rust_2015_compatibility",
Edition::Edition2018 => "rust_2018_compatibility",
Edition::Edition2021 => "rust_2021_compatibility",
Edition::Edition2024 => "rust_2024_compatibility",
}
}

Expand All @@ -59,6 +63,7 @@ impl Edition {
Edition::Edition2015 => sym::rust_2015_preview,
Edition::Edition2018 => sym::rust_2018_preview,
Edition::Edition2021 => sym::rust_2021_preview,
Edition::Edition2024 => sym::rust_2024_preview,
}
}

Expand All @@ -67,8 +72,28 @@ impl Edition {
Edition::Edition2015 => true,
Edition::Edition2018 => true,
Edition::Edition2021 => true,
Edition::Edition2024 => false,
}
}

pub fn rust_2015(&self) -> bool {
*self == Edition::Edition2015
}

/// Are we allowed to use features from the Rust 2018 edition?
pub fn rust_2018(&self) -> bool {
*self >= Edition::Edition2018
}

/// Are we allowed to use features from the Rust 2021 edition?
pub fn rust_2021(&self) -> bool {
*self >= Edition::Edition2021
}

/// Are we allowed to use features from the Rust 2024 edition?
pub fn rust_2024(&self) -> bool {
*self >= Edition::Edition2024
}
}

impl FromStr for Edition {
Expand All @@ -78,6 +103,7 @@ impl FromStr for Edition {
"2015" => Ok(Edition::Edition2015),
"2018" => Ok(Edition::Edition2018),
"2021" => Ok(Edition::Edition2021),
"2024" => Ok(Edition::Edition2024),
_ => Err(()),
}
}
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_span/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,11 @@ impl Span {
self.edition() >= edition::Edition::Edition2021
}

#[inline]
pub fn rust_2024(self) -> bool {
self.edition() >= edition::Edition::Edition2024
}

/// Returns the source callee.
///
/// Returns `None` if the supplied span has no expansion trace,
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,6 +1149,8 @@ symbols! {
rust_2018_preview,
rust_2021,
rust_2021_preview,
rust_2024,
rust_2024_preview,
rust_begin_unwind,
rust_eh_catch_typeinfo,
rust_eh_personality,
Expand Down
10 changes: 10 additions & 0 deletions library/core/src/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ pub mod rust_2021 {
#[doc(no_inline)]
pub use crate::convert::{TryFrom, TryInto};
}

/// The 2024 edition of the core prelude.
///
/// See the [module-level documentation](self) for more.
#[unstable(feature = "prelude_2024", issue = "none")]
pub mod rust_2024 {
#[unstable(feature = "prelude_2024", issue = "none")]
#[doc(no_inline)]
pub use super::rust_2021::*;
}
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@
#![feature(panic_info_message)]
#![feature(panic_internals)]
#![feature(portable_simd)]
#![feature(prelude_2024)]
#![feature(ptr_as_uninit)]
#![feature(raw_os_nonzero)]
#![feature(slice_internals)]
Expand Down
14 changes: 14 additions & 0 deletions library/std/src/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,17 @@ pub mod rust_2021 {
#[doc(no_inline)]
pub use core::prelude::rust_2021::*;
}

/// The 2024 version of the prelude of The Rust Standard Library.
///
/// See the [module-level documentation](self) for more.
#[unstable(feature = "prelude_2024", issue = "none")]
pub mod rust_2024 {
#[unstable(feature = "prelude_2024", issue = "none")]
#[doc(no_inline)]
pub use super::v1::*;

#[unstable(feature = "prelude_2024", issue = "none")]
#[doc(no_inline)]
pub use core::prelude::rust_2024::*;
}
11 changes: 9 additions & 2 deletions src/test/ui/hello.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// run-pass
// revisions: e2015 e2018 e2021 e2024

pub fn main() {
println!("hello, world");
//[e2018] edition:2018
//[e2021] edition:2021
//[e2024] edition:2024

//[e2024] compile-flags: -Zunstable-options

fn main() {
println!("hello");
}
6 changes: 0 additions & 6 deletions src/test/ui/hello2021.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/tools/rustfmt/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,7 @@ fn edition_from_edition_str(edition_str: &str) -> Result<Edition> {
"2015" => Ok(Edition::Edition2015),
"2018" => Ok(Edition::Edition2018),
"2021" => Ok(Edition::Edition2021),
"2024" => Ok(Edition::Edition2024),
_ => Err(format_err!("Invalid value for `--edition`")),
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/tools/rustfmt/src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ pub enum Edition {
#[doc_hint = "2021"]
/// Edition 2021.
Edition2021,
#[value = "2024"]
#[doc_hint = "2024"]
/// Edition 2024.
Edition2024,
}

impl Default for Edition {
Expand All @@ -437,6 +441,7 @@ impl From<Edition> for rustc_span::edition::Edition {
Edition::Edition2015 => Self::Edition2015,
Edition::Edition2018 => Self::Edition2018,
Edition::Edition2021 => Self::Edition2021,
Edition::Edition2024 => Self::Edition2024,
}
}
}
Expand Down

0 comments on commit 20bf34f

Please sign in to comment.