about summary refs log tree commit diff
path: root/compiler/rustc_errors/src
diff options
context:
space:
mode:
authorXiretza <xiretza@xiretza.xyz>2022-10-05 21:40:56 +0200
committerXiretza <xiretza@xiretza.xyz>2023-02-01 21:49:45 +0100
commitfc0ba2c8b6c144a8bda60b8267fcbbdc8ed48084 (patch)
tree9f0e1fa35cbf28c5edc4adacf260da1495616d6b /compiler/rustc_errors/src
parent0e36e7cebe4db494478982f80db2f928f59802d6 (diff)
downloadrust-fc0ba2c8b6c144a8bda60b8267fcbbdc8ed48084.tar.gz
rust-fc0ba2c8b6c144a8bda60b8267fcbbdc8ed48084.zip
Use AddToDiagnostic for "use latest edition" help
Diffstat (limited to 'compiler/rustc_errors/src')
-rw-r--r--compiler/rustc_errors/src/diagnostic.rs48
-rw-r--r--compiler/rustc_errors/src/diagnostic_builder.rs1
-rw-r--r--compiler/rustc_errors/src/lib.rs2
3 files changed, 37 insertions, 14 deletions
diff --git a/compiler/rustc_errors/src/diagnostic.rs b/compiler/rustc_errors/src/diagnostic.rs
index df949e46fbd..632f819e584 100644
--- a/compiler/rustc_errors/src/diagnostic.rs
+++ b/compiler/rustc_errors/src/diagnostic.rs
@@ -555,18 +555,6 @@ impl Diagnostic {
         self
     }
 
-    /// Help the user upgrade to the latest edition.
-    /// This is factored out to make sure it does the right thing with `Cargo.toml`.
-    pub fn help_use_latest_edition(&mut self) -> &mut Self {
-        if std::env::var_os("CARGO").is_some() {
-            self.help(&format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION));
-        } else {
-            self.help(&format!("pass `--edition {}` to `rustc`", LATEST_STABLE_EDITION));
-        }
-        self.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
-        self
-    }
-
     /// Disallow attaching suggestions this diagnostic.
     /// Any suggestions attached e.g. with the `span_suggestion_*` methods
     /// (before and after the call to `disable_suggestions`) will be ignored.
@@ -1083,3 +1071,39 @@ impl PartialEq for Diagnostic {
         self.keys() == other.keys()
     }
 }
+
+pub enum HelpUseLatestEdition {
+    Cargo,
+    Standalone,
+}
+
+impl HelpUseLatestEdition {
+    pub fn new() -> Self {
+        if std::env::var_os("CARGO").is_some() { Self::Cargo } else { Self::Standalone }
+    }
+}
+
+impl AddToDiagnostic for HelpUseLatestEdition {
+    fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, f: F)
+    where
+        F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
+    {
+        let msg = f(
+            diag,
+            match self {
+                Self::Cargo => {
+                    format!("set `edition = \"{}\"` in `Cargo.toml`", LATEST_STABLE_EDITION)
+                }
+                Self::Standalone => {
+                    format!("pass `--edition {}` to `rustc`", LATEST_STABLE_EDITION)
+                }
+            }
+            .into(),
+        );
+        diag.help(msg);
+
+        let msg =
+            f(diag, "for more on editions, read https://doc.rust-lang.org/edition-guide".into());
+        diag.note(msg);
+    }
+}
diff --git a/compiler/rustc_errors/src/diagnostic_builder.rs b/compiler/rustc_errors/src/diagnostic_builder.rs
index c9d662ad43f..3064d2bedbe 100644
--- a/compiler/rustc_errors/src/diagnostic_builder.rs
+++ b/compiler/rustc_errors/src/diagnostic_builder.rs
@@ -669,7 +669,6 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
         sp: impl Into<MultiSpan>,
         msg: impl Into<SubdiagnosticMessage>,
     ) -> &mut Self);
-    forward!(pub fn help_use_latest_edition(&mut self,) -> &mut Self);
     forward!(pub fn set_is_lint(&mut self,) -> &mut Self);
 
     forward!(pub fn disable_suggestions(&mut self,) -> &mut Self);
diff --git a/compiler/rustc_errors/src/lib.rs b/compiler/rustc_errors/src/lib.rs
index ec04e865d53..05cfebbfe8a 100644
--- a/compiler/rustc_errors/src/lib.rs
+++ b/compiler/rustc_errors/src/lib.rs
@@ -378,7 +378,7 @@ pub struct DelayedBugPanic;
 
 pub use diagnostic::{
     AddToDiagnostic, DecorateLint, Diagnostic, DiagnosticArg, DiagnosticArgValue, DiagnosticId,
-    DiagnosticStyledString, IntoDiagnosticArg, SubDiagnostic,
+    DiagnosticStyledString, HelpUseLatestEdition, IntoDiagnosticArg, SubDiagnostic,
 };
 pub use diagnostic_builder::{DiagnosticBuilder, EmissionGuarantee, Noted};
 pub use diagnostic_impls::{DiagnosticArgFromDisplay, DiagnosticSymbolList};