Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit f8ab56b

Browse files
committed
Auto merge of rust-lang#79576 - m-ou-se:2021, r=Mark-Simulacrum
Add edition 2021. :fireworks: Happy new ~~year~~ Rust. :champagne: This adds --edition=2021, and updates suggestions about 2018 to say "2018 *or later*". Related Cargo PR: rust-lang/cargo#8922 --- Edit: This adds the new edition as *unstable*. Without `-Z unstable-options`, `--edition=2021` results in: ``` $ rustc --edition=2021 error: edition 2021 is unstable and only available with -Z unstable-options. ```
2 parents 99ad5a1 + 3cbdbe8 commit f8ab56b

File tree

17 files changed

+87
-59
lines changed

17 files changed

+87
-59
lines changed

compiler/rustc_ast/src/token.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,7 @@ impl NonterminalKind {
726726
Edition::Edition2015 | Edition::Edition2018 => {
727727
NonterminalKind::Pat2018 { inferred: true }
728728
}
729-
// FIXME(mark-i-m): uncomment when 2021 machinery is available.
730-
//Edition::Edition2021 => NonterminalKind::Pat2021{inferred:true},
729+
Edition::Edition2021 => NonterminalKind::Pat2021 { inferred: true },
731730
},
732731
sym::pat2018 => NonterminalKind::Pat2018 { inferred: false },
733732
sym::pat2021 => NonterminalKind::Pat2021 { inferred: false },

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_ast::{AnonConst, BinOp, BinOpKind, FnDecl, FnRetTy, MacCall, Param, Ty
1515
use rustc_ast::{Arm, Async, BlockCheckMode, Expr, ExprKind, Label, Movability, RangeLimits};
1616
use rustc_ast_pretty::pprust;
1717
use rustc_errors::{Applicability, DiagnosticBuilder, PResult};
18+
use rustc_span::edition::LATEST_STABLE_EDITION;
1819
use rustc_span::source_map::{self, Span, Spanned};
1920
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2021
use rustc_span::{BytePos, Pos};
@@ -2108,8 +2109,8 @@ impl<'a> Parser<'a> {
21082109

21092110
let mut async_block_err = |e: &mut DiagnosticBuilder<'_>, span: Span| {
21102111
recover_async = true;
2111-
e.span_label(span, "`async` blocks are only allowed in the 2018 edition");
2112-
e.help("set `edition = \"2018\"` in `Cargo.toml`");
2112+
e.span_label(span, "`async` blocks are only allowed in Rust 2018 or later");
2113+
e.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
21132114
e.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
21142115
};
21152116

compiler/rustc_parse/src/parser/item.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_ast::{FnHeader, ForeignItem, Path, PathSegment, Visibility, Visibility
1616
use rustc_ast::{MacArgs, MacCall, MacDelimiter};
1717
use rustc_ast_pretty::pprust;
1818
use rustc_errors::{struct_span_err, Applicability, PResult, StashKey};
19-
use rustc_span::edition::Edition;
19+
use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
2020
use rustc_span::source_map::{self, Span};
2121
use rustc_span::symbol::{kw, sym, Ident, Symbol};
2222

@@ -1667,9 +1667,9 @@ impl<'a> Parser<'a> {
16671667
fn ban_async_in_2015(&self, span: Span) {
16681668
if span.rust_2015() {
16691669
let diag = self.diagnostic();
1670-
struct_span_err!(diag, span, E0670, "`async fn` is not permitted in the 2015 edition")
1671-
.span_label(span, "to use `async fn`, switch to Rust 2018")
1672-
.help("set `edition = \"2018\"` in `Cargo.toml`")
1670+
struct_span_err!(diag, span, E0670, "`async fn` is not permitted in Rust 2015")
1671+
.span_label(span, "to use `async fn`, switch to Rust 2018 or later")
1672+
.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION))
16731673
.note("for more on editions, read https://doc.rust-lang.org/edition-guide")
16741674
.emit();
16751675
}

compiler/rustc_resolve/src/late/diagnostics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
180180
(
181181
format!("cannot find {} `{}` in {}{}", expected, item_str, mod_prefix, mod_str),
182182
if path_str == "async" && expected.starts_with("struct") {
183-
"`async` blocks are only allowed in the 2018 edition".to_string()
183+
"`async` blocks are only allowed in Rust 2018 or later".to_string()
184184
} else {
185185
format!("not found in {}", mod_str)
186186
},
@@ -904,7 +904,7 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
904904
Applicability::MaybeIncorrect,
905905
);
906906
if path_str == "try" && span.rust_2015() {
907-
err.note("if you want the `try` keyword, you need to be in the 2018 edition");
907+
err.note("if you want the `try` keyword, you need Rust 2018 or later");
908908
}
909909
}
910910
(Res::Def(DefKind::TyAlias, def_id), PathSource::Trait(_)) => {

compiler/rustc_session/src/config.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,12 +1308,11 @@ fn parse_crate_edition(matches: &getopts::Matches) -> Edition {
13081308
None => DEFAULT_EDITION,
13091309
};
13101310

1311-
if !edition.is_stable() && !nightly_options::match_is_nightly_build(matches) {
1311+
if !edition.is_stable() && !nightly_options::is_unstable_enabled(matches) {
13121312
early_error(
13131313
ErrorOutputType::default(),
13141314
&format!(
1315-
"edition {} is unstable and only \
1316-
available for nightly builds of rustc.",
1315+
"edition {} is unstable and only available with -Z unstable-options.",
13171316
edition,
13181317
),
13191318
)

compiler/rustc_session/src/session.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,11 @@ impl Session {
10761076
self.opts.edition >= Edition::Edition2018
10771077
}
10781078

1079+
/// Are we allowed to use features from the Rust 2021 edition?
1080+
pub fn rust_2021(&self) -> bool {
1081+
self.opts.edition >= Edition::Edition2021
1082+
}
1083+
10791084
pub fn edition(&self) -> Edition {
10801085
self.opts.edition
10811086
}

compiler/rustc_span/src/edition.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,26 @@ pub enum Edition {
2020
Edition2015,
2121
/// The 2018 edition
2222
Edition2018,
23+
/// The 2021 ediiton
24+
Edition2021,
2325
}
2426

2527
// Must be in order from oldest to newest.
26-
pub const ALL_EDITIONS: &[Edition] = &[Edition::Edition2015, Edition::Edition2018];
28+
pub const ALL_EDITIONS: &[Edition] =
29+
&[Edition::Edition2015, Edition::Edition2018, Edition::Edition2021];
2730

28-
pub const EDITION_NAME_LIST: &str = "2015|2018";
31+
pub const EDITION_NAME_LIST: &str = "2015|2018|2021";
2932

3033
pub const DEFAULT_EDITION: Edition = Edition::Edition2015;
3134

35+
pub const LATEST_STABLE_EDITION: Edition = Edition::Edition2018;
36+
3237
impl fmt::Display for Edition {
3338
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3439
let s = match *self {
3540
Edition::Edition2015 => "2015",
3641
Edition::Edition2018 => "2018",
42+
Edition::Edition2021 => "2021",
3743
};
3844
write!(f, "{}", s)
3945
}
@@ -44,20 +50,23 @@ impl Edition {
4450
match *self {
4551
Edition::Edition2015 => "rust_2015_compatibility",
4652
Edition::Edition2018 => "rust_2018_compatibility",
53+
Edition::Edition2021 => "rust_2021_compatibility",
4754
}
4855
}
4956

5057
pub fn feature_name(&self) -> Symbol {
5158
match *self {
5259
Edition::Edition2015 => sym::rust_2015_preview,
5360
Edition::Edition2018 => sym::rust_2018_preview,
61+
Edition::Edition2021 => sym::rust_2021_preview,
5462
}
5563
}
5664

5765
pub fn is_stable(&self) -> bool {
5866
match *self {
5967
Edition::Edition2015 => true,
6068
Edition::Edition2018 => true,
69+
Edition::Edition2021 => false,
6170
}
6271
}
6372
}
@@ -68,6 +77,7 @@ impl FromStr for Edition {
6877
match s {
6978
"2015" => Ok(Edition::Edition2015),
7079
"2018" => Ok(Edition::Edition2018),
80+
"2021" => Ok(Edition::Edition2021),
7181
_ => Err(()),
7282
}
7383
}

compiler/rustc_span/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,11 @@ impl Span {
481481
self.edition() >= edition::Edition::Edition2018
482482
}
483483

484+
#[inline]
485+
pub fn rust_2021(&self) -> bool {
486+
self.edition() >= edition::Edition::Edition2021
487+
}
488+
484489
/// Returns the source callee.
485490
///
486491
/// Returns `None` if the supplied span has no expansion trace,

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ symbols! {
923923
rust,
924924
rust_2015_preview,
925925
rust_2018_preview,
926+
rust_2021_preview,
926927
rust_begin_unwind,
927928
rust_eh_catch_typeinfo,
928929
rust_eh_personality,

compiler/rustc_typeck/src/check/expr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
3838
use rustc_middle::ty::Ty;
3939
use rustc_middle::ty::TypeFoldable;
4040
use rustc_middle::ty::{AdtKind, Visibility};
41+
use rustc_span::edition::LATEST_STABLE_EDITION;
4142
use rustc_span::hygiene::DesugaringKind;
4243
use rustc_span::lev_distance::find_best_match_for_name;
4344
use rustc_span::source_map::Span;
@@ -1637,8 +1638,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16371638
if field.name == kw::Await {
16381639
// We know by construction that `<expr>.await` is either on Rust 2015
16391640
// or results in `ExprKind::Await`. Suggest switching the edition to 2018.
1640-
err.note("to `.await` a `Future`, switch to Rust 2018");
1641-
err.help("set `edition = \"2018\"` in `Cargo.toml`");
1641+
err.note("to `.await` a `Future`, switch to Rust 2018 or later");
1642+
err.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
16421643
err.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
16431644
}
16441645

0 commit comments

Comments
 (0)