]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_errors/diagnostic_builder.rs
Rollup merge of #68222 - alexcrichton:update-wasi-libc, r=kennytm
[rust.git] / src / librustc_errors / diagnostic_builder.rs
index a95c29f8c2729fd4fde3efe97a92d82114cea7c2..73f66d550374050c4be7cc0eef6a3eceacfa1549 100644 (file)
@@ -1,11 +1,11 @@
+use crate::{Applicability, Handler, Level, StashKey};
 use crate::{Diagnostic, DiagnosticId, DiagnosticStyledString};
-use crate::{Applicability, Level, Handler, StashKey};
 
+use log::debug;
+use rustc_span::{MultiSpan, Span};
 use std::fmt::{self, Debug};
 use std::ops::{Deref, DerefMut};
 use std::thread::panicking;
-use syntax_pos::{MultiSpan, Span};
-use log::debug;
 
 /// Used for emitting structured error messages and other diagnostic information.
 ///
@@ -106,11 +106,7 @@ pub fn emit(&mut self) {
     ///
     /// See `emit` and `delay_as_bug` for details.
     pub fn emit_unless(&mut self, delay: bool) {
-        if delay {
-            self.delay_as_bug()
-        } else {
-            self.emit()
-        }
+        if delay { self.delay_as_bug() } else { self.emit() }
     }
 
     /// Stashes diagnostic for possible later improvement in a different,
@@ -127,8 +123,8 @@ pub fn stash(self, span: Span, key: StashKey) {
     /// Converts the builder to a `Diagnostic` for later emission,
     /// unless handler has disabled such buffering.
     pub fn into_diagnostic(mut self) -> Option<(Diagnostic, &'a Handler)> {
-        if self.0.handler.flags.dont_buffer_diagnostics ||
-            self.0.handler.flags.treat_err_as_bug.is_some()
+        if self.0.handler.flags.dont_buffer_diagnostics
+            || self.0.handler.flags.treat_err_as_bug.is_some()
         {
             self.emit();
             return None;
@@ -241,13 +237,9 @@ pub fn multipart_suggestion(
         applicability: Applicability,
     ) -> &mut Self {
         if !self.0.allow_suggestions {
-            return self
+            return self;
         }
-        self.0.diagnostic.multipart_suggestion(
-            msg,
-            suggestion,
-            applicability,
-        );
+        self.0.diagnostic.multipart_suggestion(msg, suggestion, applicability);
         self
     }
 
@@ -258,13 +250,9 @@ pub fn tool_only_multipart_suggestion(
         applicability: Applicability,
     ) -> &mut Self {
         if !self.0.allow_suggestions {
-            return self
+            return self;
         }
-        self.0.diagnostic.tool_only_multipart_suggestion(
-            msg,
-            suggestion,
-            applicability,
-        );
+        self.0.diagnostic.tool_only_multipart_suggestion(msg, suggestion, applicability);
         self
     }
 
@@ -276,14 +264,9 @@ pub fn span_suggestion(
         applicability: Applicability,
     ) -> &mut Self {
         if !self.0.allow_suggestions {
-            return self
+            return self;
         }
-        self.0.diagnostic.span_suggestion(
-            sp,
-            msg,
-            suggestion,
-            applicability,
-        );
+        self.0.diagnostic.span_suggestion(sp, msg, suggestion, applicability);
         self
     }
 
@@ -295,14 +278,9 @@ pub fn span_suggestions(
         applicability: Applicability,
     ) -> &mut Self {
         if !self.0.allow_suggestions {
-            return self
+            return self;
         }
-        self.0.diagnostic.span_suggestions(
-            sp,
-            msg,
-            suggestions,
-            applicability,
-        );
+        self.0.diagnostic.span_suggestions(sp, msg, suggestions, applicability);
         self
     }
 
@@ -314,14 +292,9 @@ pub fn span_suggestion_short(
         applicability: Applicability,
     ) -> &mut Self {
         if !self.0.allow_suggestions {
-            return self
+            return self;
         }
-        self.0.diagnostic.span_suggestion_short(
-            sp,
-            msg,
-            suggestion,
-            applicability,
-        );
+        self.0.diagnostic.span_suggestion_short(sp, msg, suggestion, applicability);
         self
     }
 
@@ -333,14 +306,9 @@ pub fn span_suggestion_hidden(
         applicability: Applicability,
     ) -> &mut Self {
         if !self.0.allow_suggestions {
-            return self
+            return self;
         }
-        self.0.diagnostic.span_suggestion_hidden(
-            sp,
-            msg,
-            suggestion,
-            applicability,
-        );
+        self.0.diagnostic.span_suggestion_hidden(sp, msg, suggestion, applicability);
         self
     }
 
@@ -352,14 +320,9 @@ pub fn tool_only_span_suggestion(
         applicability: Applicability,
     ) -> &mut Self {
         if !self.0.allow_suggestions {
-            return self
+            return self;
         }
-        self.0.diagnostic.tool_only_span_suggestion(
-            sp,
-            msg,
-            suggestion,
-            applicability,
-        );
+        self.0.diagnostic.tool_only_span_suggestion(sp, msg, suggestion, applicability);
         self
     }
 
@@ -379,19 +342,19 @@ pub fn allow_suggestions(&mut self, allow: bool) -> &mut Self {
 
     /// Convenience function for internal use, clients should use one of the
     /// struct_* methods on Handler.
-    crate fn new_with_code(handler: &'a Handler,
-                         level: Level,
-                         code: Option<DiagnosticId>,
-                         message: &str)
-                         -> DiagnosticBuilder<'a> {
+    crate fn new_with_code(
+        handler: &'a Handler,
+        level: Level,
+        code: Option<DiagnosticId>,
+        message: &str,
+    ) -> DiagnosticBuilder<'a> {
         let diagnostic = Diagnostic::new_with_code(level, code, message);
         DiagnosticBuilder::new_diagnostic(handler, diagnostic)
     }
 
     /// Creates a new `DiagnosticBuilder` with an already constructed
     /// diagnostic.
-    crate fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic)
-                         -> DiagnosticBuilder<'a> {
+    crate fn new_diagnostic(handler: &'a Handler, diagnostic: Diagnostic) -> DiagnosticBuilder<'a> {
         DiagnosticBuilder(Box::new(DiagnosticBuilderInner {
             handler,
             diagnostic,
@@ -422,3 +385,22 @@ fn drop(&mut self) {
         }
     }
 }
+
+#[macro_export]
+macro_rules! struct_span_err {
+    ($session:expr, $span:expr, $code:ident, $($message:tt)*) => ({
+        $session.struct_span_err_with_code(
+            $span,
+            &format!($($message)*),
+            $crate::error_code!($code),
+        )
+    })
+}
+
+#[macro_export]
+macro_rules! error_code {
+    ($code:ident) => {{
+        let _ = $code;
+        $crate::DiagnosticId::Error(stringify!($code).to_owned())
+    }};
+}