Skip to content

Commit 0dc0186

Browse files
committed
Emit warning for migrating to unstable edition in stable channel
1 parent 3d3ddd2 commit 0dc0186

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/cargo/ops/fix.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,10 @@ fn rustfix_crate(
414414
args: &FixArgs,
415415
config: &Config,
416416
) -> Result<FixedCrate, Error> {
417-
args.check_edition_and_send_status(config)?;
417+
if !args.can_run_rustfix(config)? {
418+
// This fix should not be run. Skipping...
419+
return Ok(FixedCrate::default());
420+
}
418421

419422
// First up, we want to make sure that each crate is only checked by one
420423
// process at a time. If two invocations concurrently check a crate then
@@ -804,15 +807,16 @@ impl FixArgs {
804807
}
805808

806809
/// Validates the edition, and sends a message indicating what is being
807-
/// done.
808-
fn check_edition_and_send_status(&self, config: &Config) -> CargoResult<()> {
810+
/// done. Returns a flag indicating whether this fix should be run.
811+
fn can_run_rustfix(&self, config: &Config) -> CargoResult<bool> {
809812
let to_edition = match self.prepare_for_edition {
810813
Some(s) => s,
811814
None => {
812815
return Message::Fixing {
813816
file: self.file.display().to_string(),
814817
}
815-
.post();
818+
.post()
819+
.and(Ok(true));
816820
}
817821
};
818822
// Unfortunately determining which cargo targets are being built
@@ -826,18 +830,31 @@ impl FixArgs {
826830
// multiple jobs run in parallel (the error appears multiple
827831
// times). Hopefully this doesn't happen often in practice.
828832
if !to_edition.is_stable() && !config.nightly_features_allowed {
829-
bail!(
830-
"cannot migrate {} to edition {to_edition}\n\
831-
Edition {to_edition} is unstable and not allowed in this release, \
832-
consider trying the nightly release channel.",
833-
self.file.display(),
833+
let message = format!(
834+
"`{file}` is on the latest edition, but trying to \
835+
migrate to edition {to_edition}.\n\
836+
Edition {to_edition} is unstable and not allowed in \
837+
this release, consider trying the nightly release channel.",
838+
file = self.file.display(),
834839
to_edition = to_edition
835840
);
841+
return Message::EditionAlreadyEnabled {
842+
message,
843+
edition: to_edition.previous().unwrap(),
844+
}
845+
.post()
846+
.and(Ok(false)); // Do not run rustfix for this the edition.
836847
}
837848
let from_edition = self.enabled_edition.unwrap_or(Edition::Edition2015);
838849
if from_edition == to_edition {
850+
let message = format!(
851+
"`{}` is already on the latest edition ({}), \
852+
unable to migrate further",
853+
self.file.display(),
854+
to_edition
855+
);
839856
Message::EditionAlreadyEnabled {
840-
file: self.file.display().to_string(),
857+
message,
841858
edition: to_edition,
842859
}
843860
.post()
@@ -849,5 +866,6 @@ impl FixArgs {
849866
}
850867
.post()
851868
}
869+
.and(Ok(true))
852870
}
853871
}

src/cargo/util/diagnostic_server.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub enum Message {
5454
message: String,
5555
},
5656
EditionAlreadyEnabled {
57-
file: String,
57+
message: String,
5858
edition: Edition,
5959
},
6060
}
@@ -177,18 +177,13 @@ impl<'a> DiagnosticPrinter<'a> {
177177
)?;
178178
Ok(())
179179
}
180-
Message::EditionAlreadyEnabled { file, edition } => {
180+
Message::EditionAlreadyEnabled { message, edition } => {
181181
if !self.dedupe.insert(msg.clone()) {
182182
return Ok(());
183183
}
184-
let warning = format!(
185-
"`{}` is already on the latest edition ({}), \
186-
unable to migrate further",
187-
file, edition
188-
);
189184
// Don't give a really verbose warning if it has already been issued.
190185
if self.dedupe.insert(Message::EditionAlreadyEnabled {
191-
file: "".to_string(), // Dummy, so that this only long-warns once.
186+
message: "".to_string(), // Dummy, so that this only long-warns once.
192187
edition: *edition,
193188
}) {
194189
self.config.shell().warn(&format!("\
@@ -205,10 +200,10 @@ process requires following these steps:
205200
More details may be found at
206201
https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html
207202
",
208-
warning, this_edition=edition, prev_edition=edition.previous().unwrap()
203+
message, this_edition=edition, prev_edition=edition.previous().unwrap()
209204
))
210205
} else {
211-
self.config.shell().warn(warning)
206+
self.config.shell().warn(message)
212207
}
213208
}
214209
}

0 commit comments

Comments
 (0)