diff options
| author | David Tolnay <dtolnay@gmail.com> | 2018-09-08 18:07:02 -0700 |
|---|---|---|
| committer | David Tolnay <dtolnay@gmail.com> | 2018-09-08 19:01:48 -0700 |
| commit | a1dd39e724c2a37f08f534e90ced134d38a49417 (patch) | |
| tree | 63a5dcdd4f9249bac10b208ffd78e53441696d8b /src/libproc_macro | |
| parent | c5a561c0abe432006063408dc3371891585319a6 (diff) | |
Track distinct spans for open and close delimiter
Diffstat (limited to 'src/libproc_macro')
| -rw-r--r-- | src/libproc_macro/lib.rs | 34 | ||||
| -rw-r--r-- | src/libproc_macro/rustc.rs | 4 |
2 files changed, 12 insertions, 26 deletions
diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 8ed7b8f52ba..d4737052875 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -63,8 +63,8 @@ use std::str::FromStr; use syntax::errors::DiagnosticBuilder; use syntax::parse::{self, token}; use syntax::symbol::Symbol; -use syntax::tokenstream; -use syntax_pos::{BytePos, Pos, FileName}; +use syntax::tokenstream::{self, DelimSpan}; +use syntax_pos::{Pos, FileName}; /// The main type provided by this crate, representing an abstract stream of /// tokens, or, more specifically, a sequence of token trees. @@ -609,7 +609,7 @@ impl fmt::Display for TokenTree { pub struct Group { delimiter: Delimiter, stream: TokenStream, - span: Span, + span: DelimSpan, } #[stable(feature = "proc_macro_lib2", since = "1.29.0")] @@ -650,7 +650,7 @@ impl Group { Group { delimiter: delimiter, stream: stream, - span: Span::call_site(), + span: DelimSpan::from_single(Span::call_site().0), } } @@ -678,11 +678,10 @@ impl Group { /// ``` #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn span(&self) -> Span { - self.span + Span(self.span.entire()) } - /// Returns the span pointing to the opening delimiter of this group, or the - /// span of the entire group if this is a None-delimited group. + /// Returns the span pointing to the opening delimiter of this group. /// /// ```text /// pub fn span_open(&self) -> Span { @@ -690,17 +689,10 @@ impl Group { /// ``` #[unstable(feature = "proc_macro_span", issue = "38356")] pub fn span_open(&self) -> Span { - if self.delimiter == Delimiter::None { - self.span - } else { - let lo = self.span.0.lo(); - let new_hi = BytePos::from_usize(lo.to_usize() + 1); - Span(self.span.0.with_hi(new_hi)) - } + Span(self.span.open) } - /// Returns the span pointing to the closing delimiter of this group, or the - /// span of the entire group if this is a None-delimited group. + /// Returns the span pointing to the closing delimiter of this group. /// /// ```text /// pub fn span_close(&self) -> Span { @@ -708,13 +700,7 @@ impl Group { /// ``` #[unstable(feature = "proc_macro_span", issue = "38356")] pub fn span_close(&self) -> Span { - let hi = self.span.0.hi(); - if self.delimiter == Delimiter::None || hi.to_usize() == 0 { - self.span - } else { - let new_lo = BytePos::from_usize(hi.to_usize() - 1); - Span(self.span.0.with_lo(new_lo)) - } + Span(self.span.close) } /// Configures the span for this `Group`'s delimiters, but not its internal @@ -725,7 +711,7 @@ impl Group { /// tokens at the level of the `Group`. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn set_span(&mut self, span: Span) { - self.span = span; + self.span = DelimSpan::from_single(span.0); } } diff --git a/src/libproc_macro/rustc.rs b/src/libproc_macro/rustc.rs index 21229d3299d..3ce02d1afb1 100644 --- a/src/libproc_macro/rustc.rs +++ b/src/libproc_macro/rustc.rs @@ -64,7 +64,7 @@ impl TokenTree { tokenstream::TokenTree::Delimited(span, delimed) => { let delimiter = Delimiter::from_internal(delimed.delim); let mut g = Group::new(delimiter, ::TokenStream(delimed.tts.into())); - g.set_span(Span(span)); + g.span = span; return g.into(); } }; @@ -192,7 +192,7 @@ impl TokenTree { self::TokenTree::Punct(tt) => (tt.as_char(), tt.spacing(), tt.span()), self::TokenTree::Group(tt) => { return TokenTree::Delimited( - tt.span.0, + tt.span, Delimited { delim: tt.delimiter.to_internal(), tts: tt.stream.0.into(), |
