Skip to content

Commit 3bb0968

Browse files
committed
Auto merge of #8922 - m-ou-se:2021, r=alexcrichton
Add support for Rust edition 2021. This adds support for `ediiton = "2021"`. Related `rustc` PR: rust-lang/rust#79576
2 parents 4aa5223 + bc0c820 commit 3bb0968

File tree

12 files changed

+38
-38
lines changed

12 files changed

+38
-38
lines changed

src/cargo/core/features.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,16 @@ pub enum Edition {
6666
Edition2015,
6767
/// The 2018 edition
6868
Edition2018,
69+
/// The 2021 edition
70+
Edition2021,
6971
}
7072

7173
impl fmt::Display for Edition {
7274
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7375
match *self {
7476
Edition::Edition2015 => f.write_str("2015"),
7577
Edition::Edition2018 => f.write_str("2018"),
78+
Edition::Edition2021 => f.write_str("2021"),
7679
}
7780
}
7881
}
@@ -82,14 +85,15 @@ impl FromStr for Edition {
8285
match s {
8386
"2015" => Ok(Edition::Edition2015),
8487
"2018" => Ok(Edition::Edition2018),
85-
s if s.parse().map_or(false, |y: u16| y > 2020 && y < 2050) => bail!(
88+
"2021" => Ok(Edition::Edition2021),
89+
s if s.parse().map_or(false, |y: u16| y > 2021 && y < 2050) => bail!(
8690
"this version of Cargo is older than the `{}` edition, \
87-
and only supports `2015` and `2018` editions.",
91+
and only supports `2015`, `2018`, and `2021` editions.",
8892
s
8993
),
9094
s => bail!(
91-
"supported edition values are `2015` or `2018`, but `{}` \
92-
is unknown",
95+
"supported edition values are `2015`, `2018`, or `2021`, \
96+
but `{}` is unknown",
9397
s
9498
),
9599
}

src/cargo/ops/fix.rs

+19-23
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use log::{debug, trace, warn};
5050
use rustfix::diagnostics::Diagnostic;
5151
use rustfix::{self, CodeFix};
5252

53-
use crate::core::Workspace;
53+
use crate::core::{Edition, Workspace};
5454
use crate::ops::{self, CompileOptions};
5555
use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer};
5656
use crate::util::errors::CargoResult;
@@ -203,7 +203,7 @@ pub fn fix_maybe_exec_rustc() -> CargoResult<bool> {
203203
Err(_) => return Ok(false),
204204
};
205205

206-
let args = FixArgs::get();
206+
let args = FixArgs::get()?;
207207
trace!("cargo-fix as rustc got file {:?}", args.file);
208208

209209
let rustc = args.rustc.as_ref().expect("fix wrapper rustc was not set");
@@ -583,15 +583,15 @@ struct FixArgs {
583583
file: Option<PathBuf>,
584584
prepare_for_edition: PrepareFor,
585585
idioms: bool,
586-
enabled_edition: Option<String>,
586+
enabled_edition: Option<Edition>,
587587
other: Vec<OsString>,
588588
rustc: Option<PathBuf>,
589589
format_args: Vec<String>,
590590
}
591591

592592
enum PrepareFor {
593593
Next,
594-
Edition(String),
594+
Edition(Edition),
595595
None,
596596
}
597597

@@ -602,7 +602,7 @@ impl Default for PrepareFor {
602602
}
603603

604604
impl FixArgs {
605-
fn get() -> FixArgs {
605+
fn get() -> Result<FixArgs, Error> {
606606
let mut ret = FixArgs::default();
607607

608608
ret.rustc = env::args_os().nth(1).map(PathBuf::from);
@@ -615,7 +615,7 @@ impl FixArgs {
615615
}
616616
if let Some(s) = path.to_str() {
617617
if let Some(edition) = s.strip_prefix("--edition=") {
618-
ret.enabled_edition = Some(edition.to_string());
618+
ret.enabled_edition = Some(edition.parse()?);
619619
continue;
620620
}
621621
if s.starts_with("--error-format=") || s.starts_with("--json=") {
@@ -628,13 +628,13 @@ impl FixArgs {
628628
ret.other.push(path.into());
629629
}
630630
if let Ok(s) = env::var(PREPARE_FOR_ENV) {
631-
ret.prepare_for_edition = PrepareFor::Edition(s);
631+
ret.prepare_for_edition = PrepareFor::Edition(s.parse()?);
632632
} else if env::var(EDITION_ENV).is_ok() {
633633
ret.prepare_for_edition = PrepareFor::Next;
634634
}
635635

636636
ret.idioms = env::var(IDIOMS_ENV).is_ok();
637-
ret
637+
Ok(ret)
638638
}
639639

640640
fn apply(&self, cmd: &mut Command) {
@@ -643,9 +643,9 @@ impl FixArgs {
643643
}
644644

645645
cmd.args(&self.other).arg("--cap-lints=warn");
646-
if let Some(edition) = &self.enabled_edition {
647-
cmd.arg("--edition").arg(edition);
648-
if self.idioms && edition == "2018" {
646+
if let Some(edition) = self.enabled_edition {
647+
cmd.arg("--edition").arg(edition.to_string());
648+
if self.idioms && edition >= Edition::Edition2018 {
649649
cmd.arg("-Wrust-2018-idioms");
650650
}
651651
}
@@ -667,7 +667,7 @@ impl FixArgs {
667667
Some(s) => s,
668668
None => return Ok(()),
669669
};
670-
let enabled = match &self.enabled_edition {
670+
let enabled = match self.enabled_edition {
671671
Some(s) => s,
672672
None => return Ok(()),
673673
};
@@ -688,23 +688,19 @@ impl FixArgs {
688688
process::exit(1);
689689
}
690690

691-
fn prepare_for_edition_resolve(&self) -> Option<&str> {
692-
match &self.prepare_for_edition {
691+
fn prepare_for_edition_resolve(&self) -> Option<Edition> {
692+
match self.prepare_for_edition {
693693
PrepareFor::Edition(s) => Some(s),
694694
PrepareFor::Next => Some(self.next_edition()),
695695
PrepareFor::None => None,
696696
}
697697
}
698698

699-
fn next_edition(&self) -> &str {
700-
match self.enabled_edition.as_deref() {
701-
// 2015 -> 2018,
702-
None | Some("2015") => "2018",
703-
704-
// This'll probably be wrong in 2020, but that's future Cargo's
705-
// problem. Eventually though we'll just add more editions here as
706-
// necessary.
707-
_ => "2018",
699+
fn next_edition(&self) -> Edition {
700+
match self.enabled_edition {
701+
None | Some(Edition::Edition2015) => Edition::Edition2018,
702+
Some(Edition::Edition2018) => Edition::Edition2018, // TODO: Change to 2021 when rustc is ready for it.
703+
Some(Edition::Edition2021) => Edition::Edition2021,
708704
}
709705
}
710706
}

src/cargo/util/command_prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ pub trait AppExt: Sized {
188188
._arg(opt("lib", "Use a library template"))
189189
._arg(
190190
opt("edition", "Edition to set for the crate generated")
191-
.possible_values(&["2015", "2018"])
191+
.possible_values(&["2015", "2018", "2021"])
192192
.value_name("YEAR"),
193193
)
194194
._arg(

src/doc/man/generated_txt/cargo-init.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ OPTIONS
6969

7070
--edition edition
7171
Specify the Rust edition to use. Default is 2018. Possible values:
72-
2015, 2018
72+
2015, 2018, 2021
7373

7474
--name name
7575
Set the package name. Defaults to the directory name.

src/doc/man/generated_txt/cargo-new.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ OPTIONS
6464

6565
--edition edition
6666
Specify the Rust edition to use. Default is 2018. Possible values:
67-
2015, 2018
67+
2015, 2018, 2021
6868

6969
--name name
7070
Set the package name. Defaults to the directory name.

src/doc/man/includes/options-new.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Create a package with a library target (`src/lib.rs`).
1111

1212
{{#option "`--edition` _edition_" }}
1313
Specify the Rust edition to use. Default is 2018.
14-
Possible values: 2015, 2018
14+
Possible values: 2015, 2018, 2021
1515
{{/option}}
1616

1717
{{#option "`--name` _name_" }}

src/doc/src/commands/cargo-init.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ This is the default behavior.</dd>
6666

6767
<dt class="option-term" id="option-cargo-init---edition"><a class="option-anchor" href="#option-cargo-init---edition"></a><code>--edition</code> <em>edition</em></dt>
6868
<dd class="option-desc">Specify the Rust edition to use. Default is 2018.
69-
Possible values: 2015, 2018</dd>
69+
Possible values: 2015, 2018, 2021</dd>
7070

7171

7272
<dt class="option-term" id="option-cargo-init---name"><a class="option-anchor" href="#option-cargo-init---name"></a><code>--name</code> <em>name</em></dt>

src/doc/src/commands/cargo-new.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ This is the default behavior.</dd>
6161

6262
<dt class="option-term" id="option-cargo-new---edition"><a class="option-anchor" href="#option-cargo-new---edition"></a><code>--edition</code> <em>edition</em></dt>
6363
<dd class="option-desc">Specify the Rust edition to use. Default is 2018.
64-
Possible values: 2015, 2018</dd>
64+
Possible values: 2015, 2018, 2021</dd>
6565

6666

6767
<dt class="option-term" id="option-cargo-new---name"><a class="option-anchor" href="#option-cargo-new---name"></a><code>--name</code> <em>name</em></dt>

src/etc/_cargo

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ _cargo() {
136136
init)
137137
_arguments -s -S $common $registry \
138138
'--lib[use library template]' \
139-
'--edition=[specify edition to set for the crate generated]:edition:(2015 2018)' \
139+
'--edition=[specify edition to set for the crate generated]:edition:(2015 2018 2021)' \
140140
'--vcs=[initialize a new repo with a given VCS]:vcs:(git hg pijul fossil none)' \
141141
'--name=[set the resulting package name]:name' \
142142
'1:path:_directories'

src/etc/man/cargo-init.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Create a package with a library target (\fBsrc/lib.rs\fR).
102102
\fB\-\-edition\fR \fIedition\fR
103103
.RS 4
104104
Specify the Rust edition to use. Default is 2018.
105-
Possible values: 2015, 2018
105+
Possible values: 2015, 2018, 2021
106106
.RE
107107
.sp
108108
\fB\-\-name\fR \fIname\fR

src/etc/man/cargo-new.1

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Create a package with a library target (\fBsrc/lib.rs\fR).
9797
\fB\-\-edition\fR \fIedition\fR
9898
.RS 4
9999
Specify the Rust edition to use. Default is 2018.
100-
Possible values: 2015, 2018
100+
Possible values: 2015, 2018, 2021
101101
.RE
102102
.sp
103103
\fB\-\-name\fR \fIname\fR

tests/testsuite/package.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ Caused by:
10431043
failed to parse the `edition` key
10441044
10451045
Caused by:
1046-
supported edition values are `2015` or `2018`, but `chicken` is unknown
1046+
supported edition values are `2015`, `2018`, or `2021`, but `chicken` is unknown
10471047
"
10481048
.to_string(),
10491049
)
@@ -1075,7 +1075,7 @@ Caused by:
10751075
failed to parse the `edition` key
10761076
10771077
Caused by:
1078-
this version of Cargo is older than the `2038` edition, and only supports `2015` and `2018` editions.
1078+
this version of Cargo is older than the `2038` edition, and only supports `2015`, `2018`, and `2021` editions.
10791079
"
10801080
.to_string(),
10811081
)

0 commit comments

Comments
 (0)