Skip to content

Commit

Permalink
Handle multiple nested None-delimited groups
Browse files Browse the repository at this point in the history
When rust-lang/rust#72388 re-lands, we may accumulate several 'layers'
of `None`-delimited groups. This commit ensures that we 'unwrap' all of
the layers, allowing consumers to avoid needing to handle these cases.
  • Loading branch information
Aaron1011 committed May 31, 2020
1 parent 7a81558 commit 405f1bb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,23 @@ impl<'a> Cursor<'a> {
Cursor::create(self.ptr.offset(1), self.scope)
}

/// If the cursor is looking at a `None`-delimited group, move it to look at
/// While the cursor is looking at a `None`-delimited group, move it to look at
/// the first token inside instead. If the group is empty, this will move
/// the cursor past the `None`-delimited group.
///
/// WARNING: This mutates its argument.
/// WARNING: This mutates its argument, stopping when it finds something
/// other than a `None`-delimited group.
fn ignore_none(&mut self) {
if let Entry::Group(group, buf) = self.entry() {
while let Entry::Group(group, buf) = self.entry() {
if group.delimiter() == Delimiter::None {
// NOTE: We call `Cursor::create` here to make sure that
// situations where we should immediately exit the span after
// entering it are handled correctly.
unsafe {
*self = Cursor::create(&buf.data[0], self.scope);
}
} else {
break;
}
}
}
Expand Down
19 changes: 18 additions & 1 deletion tests/test_lit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use proc_macro2::{Span, TokenStream, TokenTree};
#[macro_use]
mod macros;

use proc_macro2::{Delimiter, Group, Literal, Span, TokenStream, TokenTree};
use quote::ToTokens;
use std::iter::FromIterator;
use std::str::FromStr;
use syn::{Lit, LitFloat, LitInt};

Expand Down Expand Up @@ -229,3 +233,16 @@ fn suffix() {
assert_eq!(get_suffix("1.0f32"), "f32");
assert_eq!(get_suffix("1.0_f32"), "f32");
}

#[test]
fn test_deep_group_empty() {
let tokens = TokenStream::from_iter(vec![TokenTree::Group(Group::new(
Delimiter::None,
TokenStream::from_iter(vec![TokenTree::Group(Group::new(
Delimiter::None,
TokenStream::from_iter(vec![TokenTree::Literal(Literal::string("hi"))]),
))]),
))]);

snapshot!(tokens as Lit, @r#""hi""# );
}

0 comments on commit 405f1bb

Please sign in to comment.