Repository: dtolnay/syn
Pull request #1393 (2023-03-12 06:32:00)

Change Span to DelimSpan in token::Paren/Brace/Bracket
See https://github.com/dtolnay/proc-macro2/pull/366.


Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 32bc7885..a58645d0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -37,8 +37,8 @@ proc-macro = ["proc-macro2/proc-macro", "quote/proc-macro"]
 test = ["syn-test-suite/all-features"]
 
 [dependencies]
-proc-macro2 = { version = "1.0.46", default-features = false }
-quote = { version = "1", optional = true, default-features = false }
+proc-macro2 = { version = "1.0.52", default-features = false }
+quote = { version = "1.0.25", optional = true, default-features = false }
 unicode-ident = "1"
 
 [dev-dependencies]
diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml
index ed4b8897..3a2dee48 100644
--- a/fuzz/Cargo.toml
+++ b/fuzz/Cargo.toml
@@ -10,7 +10,7 @@ cargo-fuzz = true
 
 [dependencies]
 libfuzzer-sys = "0.4"
-proc-macro2 = "1"
+proc-macro2 = "1.0.52"
 syn = { path = "..", default-features = false, features = ["full", "parsing"] }
 
 [[bin]]
diff --git a/src/buffer.rs b/src/buffer.rs
index 7f593b34..e16f2ade 100644
--- a/src/buffer.rs
+++ b/src/buffer.rs
@@ -11,6 +11,7 @@
 ))]
 use crate::proc_macro as pm;
 use crate::Lifetime;
+use proc_macro2::extra::DelimSpan;
 use proc_macro2::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
 use std::cmp::Ordering;
 use std::marker::PhantomData;
@@ -188,7 +189,7 @@ impl<'a> Cursor<'a> {
 
     /// If the cursor is pointing at a `Group` with the given delimiter, returns
     /// a cursor into that group and one pointing to the next `TokenTree`.
-    pub fn group(mut self, delim: Delimiter) -> Option<(Cursor<'a>, Span, Cursor<'a>)> {
+    pub fn group(mut self, delim: Delimiter) -> Option<(Cursor<'a>, DelimSpan, Cursor<'a>)> {
         // If we're not trying to enter a none-delimited group, we want to
         // ignore them. We have to make sure to _not_ ignore them when we want
         // to enter them, of course. For obvious reasons.
@@ -198,7 +199,7 @@ impl<'a> Cursor<'a> {
 
         if let Entry::Group(group, end_offset) = self.entry() {
             if group.delimiter() == delim {
-                let span = group.span();
+                let span = group.delim_span();
                 let end_of_group = unsafe { self.ptr.add(*end_offset) };
                 let inside_of_group = unsafe { Cursor::create(self.ptr.add(1), end_of_group) };
                 let after_group = unsafe { Cursor::create(end_of_group, self.scope) };
@@ -209,10 +210,10 @@ impl<'a> Cursor<'a> {
         None
     }
 
-    pub(crate) fn any_group(self) -> Option<(Cursor<'a>, Delimiter, Span, Cursor<'a>)> {
+    pub(crate) fn any_group(self) -> Option<(Cursor<'a>, Delimiter, DelimSpan, Cursor<'a>)> {
         if let Entry::Group(group, end_offset) = self.entry() {
             let delimiter = group.delimiter();
-            let span = group.span();
+            let span = group.delim_span();
             let end_of_group = unsafe { self.ptr.add(*end_offset) };
             let inside_of_group = unsafe { Cursor::create(self.ptr.add(1), end_of_group) };
             let after_group = unsafe { Cursor::create(end_of_group, self.scope) };
diff --git a/src/discouraged.rs b/src/discouraged.rs
index 7bf515de..fb98d633 100644
--- a/src/discouraged.rs
+++ b/src/discouraged.rs
@@ -1,6 +1,7 @@
 //! Extensions to the parsing API with niche applicability.
 
 use super::*;
+use proc_macro2::extra::DelimSpan;
 
 /// Extensions to the `ParseStream` API to support speculative parsing.
 pub trait Speculative {
@@ -198,11 +199,11 @@ impl<'a> Speculative for ParseBuffer<'a> {
 pub trait AnyDelimiter {
     /// Returns the delimiter, the span of the delimiter token, and the nested
     /// contents for further parsing.
-    fn parse_any_delimiter(&self) -> Result<(Delimiter, Span, ParseBuffer)>;
+    fn parse_any_delimiter(&self) -> Result<(Delimiter, DelimSpan, ParseBuffer)>;
 }
 
 impl<'a> AnyDelimiter for ParseBuffer<'a> {
-    fn parse_any_delimiter(&self) -> Result<(Delimiter, Span, ParseBuffer)> {
+    fn parse_any_delimiter(&self) -> Result<(Delimiter, DelimSpan, ParseBuffer)> {
         self.step(|cursor| {
             if let Some((content, delimiter, span, rest)) = cursor.any_group() {
                 let scope = crate::buffer::close_span_of_group(*cursor);
diff --git a/src/group.rs b/src/group.rs
index 328ab0b9..cccbc467 100644
--- a/src/group.rs
+++ b/src/group.rs
@@ -1,7 +1,8 @@
 use crate::error::Result;
 use crate::parse::ParseBuffer;
 use crate::token;
-use proc_macro2::{Delimiter, Span};
+use proc_macro2::extra::DelimSpan;
+use proc_macro2::Delimiter;
 
 // Not public API.
 #[doc(hidden)]
@@ -62,7 +63,7 @@ pub fn parse_brackets<'a>(input: &ParseBuffer<'a>) -> Result<Brackets<'a>> {
 #[cfg(any(feature = "full", feature = "derive"))]
 pub(crate) fn parse_group<'a>(input: &ParseBuffer<'a>) -> Result<Group<'a>> {
     parse_delimited(input, Delimiter::None).map(|(span, content)| Group {
-        token: token::Group(span),
+        token: token::Group(span.join()),
         content,
     })
 }
@@ -70,7 +71,7 @@ pub(crate) fn parse_group<'a>(input: &ParseBuffer<'a>) -> Result<Group<'a>> {
 fn parse_delimited<'a>(
     input: &ParseBuffer<'a>,
     delimiter: Delimiter,
-) -> Result<(Span, ParseBuffer<'a>)> {
+) -> Result<(DelimSpan, ParseBuffer<'a>)> {
     input.step(|cursor| {
         if let Some((content, span, rest)) = cursor.group(delimiter) {
             let scope = crate::buffer::close_span_of_group(*cursor);
diff --git a/src/mac.rs b/src/mac.rs
index 36c92b99..47ad6651 100644
--- a/src/mac.rs
+++ b/src/mac.rs
@@ -4,7 +4,7 @@ use crate::token::{Brace, Bracket, Paren};
 use proc_macro2::Delimiter;
 use proc_macro2::TokenStream;
 #[cfg(feature = "parsing")]
-use proc_macro2::{Group, Span, TokenTree};
+use proc_macro2::{Span, TokenTree};
 
 #[cfg(feature = "parsing")]
 use crate::parse::{Parse, ParseStream, Parser, Result};
@@ -32,18 +32,12 @@ ast_enum! {
 
 #[cfg(feature = "parsing")]
 pub(crate) fn delimiter_span_close(macro_delimiter: &MacroDelimiter) -> Span {
-    let delimiter = match macro_delimiter {
-        MacroDelimiter::Paren(_) => Delimiter::Parenthesis,
-        MacroDelimiter::Brace(_) => Delimiter::Brace,
-        MacroDelimiter::Bracket(_) => Delimiter::Bracket,
+    let delim_span = match macro_delimiter {
+        MacroDelimiter::Paren(token) => &token.span,
+        MacroDelimiter::Brace(token) => &token.span,
+        MacroDelimiter::Bracket(token) => &token.span,
     };
-    let mut group = Group::new(delimiter, TokenStream::new());
-    group.set_span(match macro_delimiter {
-        MacroDelimiter::Paren(token) => token.span,
-        MacroDelimiter::Brace(token) => token.span,
-        MacroDelimiter::Bracket(token) => token.span,
-    });
-    group.span_close()
+    delim_span.close()
 }
 
 impl Macro {
@@ -149,7 +143,7 @@ impl Macro {
 pub(crate) fn parse_delimiter(input: ParseStream) -> Result<(MacroDelimiter, TokenStream)> {
     input.step(|cursor| {
         if let Some((TokenTree::Group(g), rest)) = cursor.token_tree() {
-            let span = g.span();
+            let span = g.delim_span();
             let delimiter = match g.delimiter() {
                 Delimiter::Parenthesis => MacroDelimiter::Paren(Paren(span)),
                 Delimiter::Brace => MacroDelimiter::Brace(Brace(span)),
@@ -201,7 +195,7 @@ mod printing {
                 MacroDelimiter::Brace(brace) => (Delimiter::Brace, brace.span),
                 MacroDelimiter::Bracket(bracket) => (Delimiter::Bracket, bracket.span),
             };
-            token::printing::delim(delim, span, tokens, inner);
+            token::printing::delim(delim, span.join(), tokens, inner);
         }
     }
 
diff --git a/src/span.rs b/src/span.rs
index adbb6d80..50a26b83 100644
--- a/src/span.rs
+++ b/src/span.rs
@@ -1,4 +1,5 @@
-use proc_macro2::Span;
+use proc_macro2::extra::DelimSpan;
+use proc_macro2::{Delimiter, Group, Span, TokenStream};
 
 pub trait IntoSpans<S> {
     fn into_spans(self) -> S;
@@ -45,3 +46,17 @@ impl IntoSpans<[Span; 3]> for [Span; 3] {
         self
     }
 }
+
+impl IntoSpans<DelimSpan> for Span {
+    fn into_spans(self) -> DelimSpan {
+        let mut group = Group::new(Delimiter::None, TokenStream::new());
+        group.set_span(self);
+        group.delim_span()
+    }
+}
+
+impl IntoSpans<DelimSpan> for DelimSpan {
+    fn into_spans(self) -> DelimSpan {
+        self
+    }
+}
diff --git a/src/token.rs b/src/token.rs
index efda3e45..c140571a 100644
--- a/src/token.rs
+++ b/src/token.rs
@@ -102,6 +102,7 @@ use crate::lookahead;
 #[cfg(feature = "parsing")]
 use crate::parse::{Parse, ParseStream};
 use crate::span::IntoSpans;
+use proc_macro2::extra::DelimSpan;
 use proc_macro2::Span;
 #[cfg(feature = "printing")]
 use proc_macro2::TokenStream;
@@ -483,12 +484,12 @@ macro_rules! define_delimiters {
         $(
             #[$doc]
             pub struct $name {
-                pub span: Span,
+                pub span: DelimSpan,
             }
 
             #[doc(hidden)]
             #[allow(non_snake_case)]
-            pub fn $name<S: IntoSpans<Span>>(span: S) -> $name {
+            pub fn $name<S: IntoSpans<DelimSpan>>(span: S) -> $name {
                 $name {
                     span: span.into_spans(),
                 }
@@ -496,9 +497,7 @@ macro_rules! define_delimiters {
 
             impl std::default::Default for $name {
                 fn default() -> Self {
-                    $name {
-                        span: Span::call_site(),
-                    }
+                    $name(Span::call_site())
                 }
             }
 
@@ -548,7 +547,7 @@ macro_rules! define_delimiters {
                 {
                     let mut inner = TokenStream::new();
                     f(&mut inner);
-                    printing::delim(Delimiter::$delim, self.span, tokens, inner);
+                    printing::delim(Delimiter::$delim, self.span.join(), tokens, inner);
                 }
             }
 


Test patch:
diff --git a/tests/test_size.rs b/tests/test_size.rs
index 107a6956..864c0116 100644
--- a/tests/test_size.rs
+++ b/tests/test_size.rs
@@ -14,19 +14,19 @@ fn test_expr_size() {
 #[rustversion::attr(before(2022-09-09), ignore)]
 #[test]
 fn test_item_size() {
-    assert_eq!(mem::size_of::<Item>(), 344);
+    assert_eq!(mem::size_of::<Item>(), 360);
 }
 
 #[rustversion::attr(before(2022-11-24), ignore)]
 #[test]
 fn test_type_size() {
-    assert_eq!(mem::size_of::<Type>(), 224);
+    assert_eq!(mem::size_of::<Type>(), 240);
 }
 
 #[rustversion::attr(before(2021-10-11), ignore)]
 #[test]
 fn test_pat_size() {
-    assert_eq!(mem::size_of::<Pat>(), 184);
+    assert_eq!(mem::size_of::<Pat>(), 192);
 }
 
 #[rustversion::attr(before(2022-09-09), ignore)]