about summary refs log tree commit diff
path: root/src/libproc_macro/lib.rs
diff options
context:
space:
mode:
authorSergio Benitez <sb@sergio.bz>2017-08-28 02:56:43 -0700
committerSergio Benitez <sb@sergio.bz>2017-08-28 02:58:22 -0700
commit8be132e9d76232feb2376de9edcbb34fe3ac99ac (patch)
treee1cdbc4e0e9e64151a896c49af2d52fae00d48a8 /src/libproc_macro/lib.rs
parenta0c3bd2d23f6d08ecf9b4191ee4fff5866a120d1 (diff)
downloadrust-8be132e9d76232feb2376de9edcbb34fe3ac99ac.tar.gz
rust-8be132e9d76232feb2376de9edcbb34fe3ac99ac.zip
Initial diagnostic API for proc-macros.
This commit introduces the ability to create and emit `Diagnostic`
structures from proc-macros, allowing for proc-macro authors to emit
warning, error, note, and help messages just like the compiler does.
Diffstat (limited to 'src/libproc_macro/lib.rs')
-rw-r--r--src/libproc_macro/lib.rs22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs
index 3f425c24a91..4e7783da671 100644
--- a/src/libproc_macro/lib.rs
+++ b/src/libproc_macro/lib.rs
@@ -42,6 +42,12 @@
 #[macro_use]
 extern crate syntax;
 extern crate syntax_pos;
+extern crate rustc_errors;
+
+mod diagnostic;
+
+#[unstable(feature = "proc_macro", issue = "38356")]
+pub use diagnostic::{Diagnostic, Level};
 
 use std::{ascii, fmt, iter};
 use std::str::FromStr;
@@ -191,12 +197,28 @@ pub fn quote_span(span: Span) -> TokenStream {
     TokenStream(quote::Quote::quote(&span.0))
 }
 
+macro_rules! diagnostic_method {
+    ($name:ident, $level:expr) => (
+        /// Create a new `Diagnostic` with the given `message` at the span
+        /// `self`.
+        #[unstable(feature = "proc_macro", issue = "38356")]
+        pub fn $name<T: Into<String>>(self, message: T) -> Diagnostic {
+            Diagnostic::spanned(self, $level, message)
+        }
+    )
+}
+
 impl Span {
     /// The span of the invocation of the current procedural macro.
     #[unstable(feature = "proc_macro", issue = "38356")]
     pub fn call_site() -> Span {
         ::__internal::with_sess(|(_, mark)| Span(mark.expn_info().unwrap().call_site))
     }
+
+    diagnostic_method!(error, Level::Error);
+    diagnostic_method!(warning, Level::Warning);
+    diagnostic_method!(note, Level::Note);
+    diagnostic_method!(help, Level::Help);
 }
 
 /// A single token or a delimited sequence of token trees (e.g. `[1, (), ..]`).