From 3f2f7e30ff7aa2045010ac845d96f652f118ff3e Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Wed, 17 Feb 2021 21:36:28 -0800 Subject: [PATCH] Add a migrating message for `cargo fix --edition`. This helps indicate which edition you are moving from and to. --- crates/cargo-test-support/src/lib.rs | 1 + src/cargo/ops/fix.rs | 44 +++++++++++++--------------- src/cargo/util/diagnostic_server.rs | 18 ++++++++++++ tests/testsuite/fix.rs | 5 ++++ 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/crates/cargo-test-support/src/lib.rs b/crates/cargo-test-support/src/lib.rs index b23561ad7..7b130f443 100644 --- a/crates/cargo-test-support/src/lib.rs +++ b/crates/cargo-test-support/src/lib.rs @@ -1535,6 +1535,7 @@ fn substitute_macros(input: &str) -> String { ("[LOGOUT]", " Logout"), ("[YANK]", " Yank"), ("[OWNER]", " Owner"), + ("[MIGRATING]", " Migrating"), ]; let mut result = input.to_owned(); for &(pat, subst) in ¯os { diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index 78e29ab81..0b42f045b 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -301,7 +301,7 @@ fn rustfix_crate( filename: &Path, args: &FixArgs, ) -> Result { - args.verify_not_preparing_for_enabled_edition()?; + args.check_edition_and_send_status()?; // First up, we want to make sure that each crate is only checked by one // process at a time. If two invocations concurrently check a crate then @@ -685,15 +685,10 @@ impl FixArgs { } } - /// Verifies that we're not both preparing for an enabled edition and enabling - /// the edition. - /// - /// This indicates that `cargo fix --prepare-for` is being executed out of - /// order with enabling the edition itself, meaning that we wouldn't - /// actually be able to fix anything! If it looks like this is happening - /// then yield an error to the user, indicating that this is happening. - fn verify_not_preparing_for_enabled_edition(&self) -> CargoResult<()> { - let edition = match self.prepare_for_edition { + /// Validates the edition, and sends a message indicating what is being + /// done. + fn check_edition_and_send_status(&self) -> CargoResult<()> { + let to_edition = match self.prepare_for_edition { Some(s) => s, None => { return Message::Fixing { @@ -702,20 +697,21 @@ impl FixArgs { .post(); } }; - let enabled = match self.enabled_edition { - Some(s) => s, - None => return Ok(()), - }; - if edition != enabled { - return Ok(()); + let from_edition = self.enabled_edition.unwrap_or(Edition::Edition2015); + if from_edition == to_edition { + Message::EditionAlreadyEnabled { + file: self.file.display().to_string(), + edition: to_edition, + } + .post()?; + process::exit(1); + } else { + Message::Migrating { + file: self.file.display().to_string(), + from_edition, + to_edition, + } + .post() } - - Message::EditionAlreadyEnabled { - file: self.file.display().to_string(), - edition, - } - .post()?; - - process::exit(1); } } diff --git a/src/cargo/util/diagnostic_server.rs b/src/cargo/util/diagnostic_server.rs index dba68c08a..a28b5bbf2 100644 --- a/src/cargo/util/diagnostic_server.rs +++ b/src/cargo/util/diagnostic_server.rs @@ -31,6 +31,11 @@ const PLEASE_REPORT_THIS_BUG: &str = #[derive(Deserialize, Serialize, Hash, Eq, PartialEq, Clone)] pub enum Message { + Migrating { + file: String, + from_edition: Edition, + to_edition: Edition, + }, Fixing { file: String, }, @@ -92,6 +97,19 @@ impl<'a> DiagnosticPrinter<'a> { pub fn print(&mut self, msg: &Message) -> CargoResult<()> { match msg { + Message::Migrating { + file, + from_edition, + to_edition, + } => { + if !self.dedupe.insert(msg.clone()) { + return Ok(()); + } + self.config.shell().status( + "Migrating", + &format!("{} from {} edition to {}", file, from_edition, to_edition), + ) + } Message::Fixing { file } => self .config .shell() diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index c87f957e1..e598f051c 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -286,6 +286,7 @@ fn prepare_for_2018() { let stderr = "\ [CHECKING] foo v0.0.1 ([..]) +[MIGRATING] src/lib.rs from 2015 edition to 2018 [FIXED] src/lib.rs (2 fixes) [FINISHED] [..] "; @@ -324,6 +325,7 @@ fn local_paths() { .with_stderr( "\ [CHECKING] foo v0.0.1 ([..]) +[MIGRATING] src/lib.rs from 2015 edition to 2018 [FIXED] src/lib.rs (1 fix) [FINISHED] [..] ", @@ -409,6 +411,7 @@ fn specify_rustflags() { .with_stderr( "\ [CHECKING] foo v0.0.1 ([..]) +[MIGRATING] src/lib.rs from 2015 edition to 2018 [FIXED] src/lib.rs (1 fix) [FINISHED] [..] ", @@ -842,6 +845,7 @@ fn fix_overlapping() { .with_stderr( "\ [CHECKING] foo [..] +[MIGRATING] src/lib.rs from 2015 edition to 2018 [FIXED] src/lib.rs (2 fixes) [FINISHED] dev [..] ", @@ -1164,6 +1168,7 @@ fn only_warn_for_relevant_crates() { "\ [CHECKING] a v0.1.0 ([..]) [CHECKING] foo v0.1.0 ([..]) +[MIGRATING] src/lib.rs from 2015 edition to 2018 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..] ", )