From e88b0d9a94118168fd02a917e7c99ad47cf3418e Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Thu, 8 Nov 2018 10:07:02 +0100 Subject: Introduce proc_macro::Span::source_text --- src/libproc_macro/bridge/mod.rs | 1 + src/libproc_macro/lib.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+) (limited to 'src/libproc_macro') 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; }, } }; 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 { + self.0.source_text() + } + diagnostic_method!(error, Level::Error); diagnostic_method!(warning, Level::Warning); diagnostic_method!(note, Level::Note); -- cgit 1.4.1-3-g733a5