about summary refs log tree commit diff
path: root/src/libproc_macro
diff options
context:
space:
mode:
authorOlivier Goffart <ogoffart@woboq.com>2018-11-08 10:07:02 +0100
committerOlivier Goffart <ogoffart@woboq.com>2018-12-01 09:02:11 +0100
commite88b0d9a94118168fd02a917e7c99ad47cf3418e (patch)
tree34c83a7f8eb815514591ca6e4bb44e56bcfc397d /src/libproc_macro
parentd48ab693d1ce99f30c0cf9abdf45c209824fe825 (diff)
downloadrust-e88b0d9a94118168fd02a917e7c99ad47cf3418e.tar.gz
rust-e88b0d9a94118168fd02a917e7c99ad47cf3418e.zip
Introduce proc_macro::Span::source_text
Diffstat (limited to 'src/libproc_macro')
-rw-r--r--src/libproc_macro/bridge/mod.rs1
-rw-r--r--src/libproc_macro/lib.rs12
2 files changed, 13 insertions, 0 deletions
diff --git a/src/libproc_macro/bridge/mod.rs b/src/libproc_macro/bridge/mod.rs
index f03c63fc04c..cf9229909d9 100644
--- a/src/libproc_macro/bridge/mod.rs
+++ b/src/libproc_macro/bridge/mod.rs
@@ -165,6 +165,7 @@ macro_rules! with_api {
                 fn end($self: $S::Span) -> LineColumn;
                 fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
                 fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
+                fn source_text($self: $S::Span) -> Option<String>;
             },
         }
     };
diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs
index 32c81302931..b39e139de54 100644
--- a/src/libproc_macro/lib.rs
+++ b/src/libproc_macro/lib.rs
@@ -341,6 +341,18 @@ impl Span {
         self.0 == other.0
     }
 
+    /// Returns the source text behind a span. This preserves the original source
+    /// code, including spaces and comments. It only returns a result if the span
+    /// corresponds to real source code.
+    ///
+    /// Note: The observable result of a macro should only rely on the tokens and
+    /// not on this source text. The result of this function is a best effort to
+    /// be used for diagnostics only.
+    #[unstable(feature = "proc_macro_span", issue = "54725")]
+    pub fn source_text(&self) -> Option<String> {
+        self.0.source_text()
+    }
+
     diagnostic_method!(error, Level::Error);
     diagnostic_method!(warning, Level::Warning);
     diagnostic_method!(note, Level::Note);