diff options
| author | bors <bors@rust-lang.org> | 2018-09-09 13:27:44 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-09-09 13:27:44 +0000 |
| commit | 40fc8ba5f95125f307ef17f11ec02b3f5fdad562 (patch) | |
| tree | 4cf149c2ccf4ced464d567ef8e7f2075c51d8815 /src/libproc_macro | |
| parent | df6ba0c4acceb5f63090bb20bd23f29c4f439376 (diff) | |
| parent | 57d6ada91d7c2d9af1115ae515b000fda93b3c4e (diff) | |
| download | rust-40fc8ba5f95125f307ef17f11ec02b3f5fdad562.tar.gz rust-40fc8ba5f95125f307ef17f11ec02b3f5fdad562.zip | |
Auto merge of #53902 - dtolnay:group, r=petrochenkov
proc_macro::Group::span_open and span_close
Before this addition, every delimited group like `(`...`)` `[`...`]` `{`...`}` has only a single Span that covers the full source location from opening delimiter to closing delimiter. This makes it impossible for a procedural macro to trigger an error pointing to just the opening or closing delimiter. The Rust compiler does not seem to have the same limitation:
```rust
mod m {
type T =
}
```
```console
error: expected type, found `}`
--> src/main.rs:3:1
|
3 | }
| ^
```
On that same input, a procedural macro would be forced to trigger the error on the last token inside the block, on the entire block, or on the next token after the block, none of which is really what you want for an error like above.
This commit adds `group.span_open()` and `group.span_close()` which access the Span associated with just the opening delimiter and just the closing delimiter of the group. Relevant to Syn as we implement real error messages for when parsing fails in a procedural macro: https://github.com/dtolnay/syn/issues/476.
```diff
impl Group {
fn span(&self) -> Span;
+ fn span_open(&self) -> Span;
+ fn span_close(&self) -> Span;
}
```
Fixes #48187
r? @alexcrichton
Diffstat (limited to 'src/libproc_macro')
| -rw-r--r-- | src/libproc_macro/lib.rs | 37 | ||||
| -rw-r--r-- | src/libproc_macro/rustc.rs | 4 |
2 files changed, 34 insertions, 7 deletions
diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index 8c667d2f871..d4737052875 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -63,7 +63,7 @@ use std::str::FromStr; use syntax::errors::DiagnosticBuilder; use syntax::parse::{self, token}; use syntax::symbol::Symbol; -use syntax::tokenstream; +use syntax::tokenstream::{self, DelimSpan}; use syntax_pos::{Pos, FileName}; /// The main type provided by this crate, representing an abstract stream of @@ -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), } } @@ -671,9 +671,36 @@ impl Group { /// Returns the span for the delimiters of this token stream, spanning the /// entire `Group`. + /// + /// ```text + /// pub fn span(&self) -> Span { + /// ^^^^^^^ + /// ``` #[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. + /// + /// ```text + /// pub fn span_open(&self) -> Span { + /// ^ + /// ``` + #[unstable(feature = "proc_macro_span", issue = "38356")] + pub fn span_open(&self) -> Span { + Span(self.span.open) + } + + /// Returns the span pointing to the closing delimiter of this group. + /// + /// ```text + /// pub fn span_close(&self) -> Span { + /// ^ + /// ``` + #[unstable(feature = "proc_macro_span", issue = "38356")] + pub fn span_close(&self) -> Span { + Span(self.span.close) } /// Configures the span for this `Group`'s delimiters, but not its internal @@ -684,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(), |
