Skip to content

Commit

Permalink
Change Span to DelimSpan in token::Paren/Brace/Bracket
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Mar 12, 2023
1 parent 297c8a7 commit 67001b1
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Expand Up @@ -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]]
Expand Down
9 changes: 5 additions & 4 deletions src/buffer.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand All @@ -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) };
Expand All @@ -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) };
Expand Down
5 changes: 3 additions & 2 deletions 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 {
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions 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)]
Expand Down Expand Up @@ -62,15 +63,15 @@ 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,
})
}

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);
Expand Down
22 changes: 8 additions & 14 deletions src/mac.rs
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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);
}
}

Expand Down
17 changes: 16 additions & 1 deletion 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;
Expand Down Expand Up @@ -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
}
}
11 changes: 5 additions & 6 deletions src/token.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -483,22 +484,20 @@ 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(),
}
}

impl std::default::Default for $name {
fn default() -> Self {
$name {
span: Span::call_site(),
}
$name(Span::call_site())
}
}

Expand Down Expand Up @@ -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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/test_size.rs
Expand Up @@ -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)]
Expand Down

0 comments on commit 67001b1

Please sign in to comment.