Skip to content

Commit 8b5771f

Browse files
committed
Auto merge of #9792 - weihanglo:issue-9782, r=ehuss
Emit warning for migrating to unstable edition in stable channel Resolves #9782 Also alter the original error message slightly.
2 parents 491deb6 + e5d6586 commit 8b5771f

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

src/cargo/ops/fix.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,10 @@ fn rustfix_crate(
431431
args: &FixArgs,
432432
config: &Config,
433433
) -> Result<FixedCrate, Error> {
434-
args.check_edition_and_send_status(config)?;
434+
if !args.can_run_rustfix(config)? {
435+
// This fix should not be run. Skipping...
436+
return Ok(FixedCrate::default());
437+
}
435438

436439
// First up, we want to make sure that each crate is only checked by one
437440
// process at a time. If two invocations concurrently check a crate then
@@ -834,15 +837,16 @@ impl FixArgs {
834837
}
835838

836839
/// Validates the edition, and sends a message indicating what is being
837-
/// done.
838-
fn check_edition_and_send_status(&self, config: &Config) -> CargoResult<()> {
840+
/// done. Returns a flag indicating whether this fix should be run.
841+
fn can_run_rustfix(&self, config: &Config) -> CargoResult<bool> {
839842
let to_edition = match self.prepare_for_edition {
840843
Some(s) => s,
841844
None => {
842845
return Message::Fixing {
843846
file: self.file.display().to_string(),
844847
}
845-
.post();
848+
.post()
849+
.and(Ok(true));
846850
}
847851
};
848852
// Unfortunately determining which cargo targets are being built
@@ -856,18 +860,31 @@ impl FixArgs {
856860
// multiple jobs run in parallel (the error appears multiple
857861
// times). Hopefully this doesn't happen often in practice.
858862
if !to_edition.is_stable() && !config.nightly_features_allowed {
859-
bail!(
860-
"cannot migrate {} to edition {to_edition}\n\
861-
Edition {to_edition} is unstable and not allowed in this release, \
862-
consider trying the nightly release channel.",
863-
self.file.display(),
863+
let message = format!(
864+
"`{file}` is on the latest edition, but trying to \
865+
migrate to edition {to_edition}.\n\
866+
Edition {to_edition} is unstable and not allowed in \
867+
this release, consider trying the nightly release channel.",
868+
file = self.file.display(),
864869
to_edition = to_edition
865870
);
871+
return Message::EditionAlreadyEnabled {
872+
message,
873+
edition: to_edition.previous().unwrap(),
874+
}
875+
.post()
876+
.and(Ok(false)); // Do not run rustfix for this the edition.
866877
}
867878
let from_edition = self.enabled_edition.unwrap_or(Edition::Edition2015);
868879
if from_edition == to_edition {
880+
let message = format!(
881+
"`{}` is already on the latest edition ({}), \
882+
unable to migrate further",
883+
self.file.display(),
884+
to_edition
885+
);
869886
Message::EditionAlreadyEnabled {
870-
file: self.file.display().to_string(),
887+
message,
871888
edition: to_edition,
872889
}
873890
.post()
@@ -879,5 +896,6 @@ impl FixArgs {
879896
}
880897
.post()
881898
}
899+
.and(Ok(true))
882900
}
883901
}

src/cargo/util/diagnostic_server.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub enum Message {
5555
message: String,
5656
},
5757
EditionAlreadyEnabled {
58-
file: String,
58+
message: String,
5959
edition: Edition,
6060
},
6161
}
@@ -186,18 +186,13 @@ impl<'a> DiagnosticPrinter<'a> {
186186
)?;
187187
Ok(())
188188
}
189-
Message::EditionAlreadyEnabled { file, edition } => {
189+
Message::EditionAlreadyEnabled { message, edition } => {
190190
if !self.dedupe.insert(msg.clone()) {
191191
return Ok(());
192192
}
193-
let warning = format!(
194-
"`{}` is already on the latest edition ({}), \
195-
unable to migrate further",
196-
file, edition
197-
);
198193
// Don't give a really verbose warning if it has already been issued.
199194
if self.dedupe.insert(Message::EditionAlreadyEnabled {
200-
file: "".to_string(), // Dummy, so that this only long-warns once.
195+
message: "".to_string(), // Dummy, so that this only long-warns once.
201196
edition: *edition,
202197
}) {
203198
self.config.shell().warn(&format!("\
@@ -214,10 +209,10 @@ process requires following these steps:
214209
More details may be found at
215210
https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html
216211
",
217-
warning, this_edition=edition, prev_edition=edition.previous().unwrap()
212+
message, this_edition=edition, prev_edition=edition.previous().unwrap()
218213
))
219214
} else {
220-
self.config.shell().warn(warning)
215+
self.config.shell().warn(message)
221216
}
222217
}
223218
}

tests/testsuite/fix.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ fn prepare_for_unstable() {
809809
}
810810
};
811811
let latest_stable = Edition::LATEST_STABLE;
812+
let prev = latest_stable.previous().unwrap();
812813
let p = project()
813814
.file(
814815
"Cargo.toml",
@@ -828,13 +829,24 @@ fn prepare_for_unstable() {
828829
// -j1 to make the error more deterministic (otherwise there can be
829830
// multiple errors since they run in parallel).
830831
p.cargo("fix --edition --allow-no-vcs -j1")
831-
.with_status(101)
832-
.with_stderr(&format!("\
832+
.with_stderr(&format_args!("\
833833
[CHECKING] foo [..]
834-
[ERROR] cannot migrate src/lib.rs to edition {next}
834+
[WARNING] `src/lib.rs` is on the latest edition, but trying to migrate to edition {next}.
835835
Edition {next} is unstable and not allowed in this release, consider trying the nightly release channel.
836-
error: could not compile `foo`
837-
", next=next))
836+
837+
If you are trying to migrate from the previous edition ({prev}), the
838+
process requires following these steps:
839+
840+
1. Start with `edition = \"{prev}\"` in `Cargo.toml`
841+
2. Run `cargo fix --edition`
842+
3. Modify `Cargo.toml` to set `edition = \"{latest_stable}\"`
843+
4. Run `cargo build` or `cargo test` to verify the fixes worked
844+
845+
More details may be found at
846+
https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html
847+
848+
[FINISHED] [..]
849+
", next=next, latest_stable=latest_stable, prev=prev))
838850
.run();
839851

840852
if !is_nightly() {

0 commit comments

Comments
 (0)