| 1 |
#![allow(clippy::non_ascii_literal)] |
| 2 |
|
| 3 |
use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, TokenStream, TokenTree}; |
| 4 |
use std::panic; |
| 5 |
use syn::parse::discouraged::Speculative as _; |
| 6 |
use syn::parse::{Parse, ParseStream, Parser, Result}; |
| 7 |
use syn::{parenthesized, Token}; |
| 8 |
|
| 9 |
#[test] |
| 10 |
#[should_panic(expected = "fork was not derived from the advancing parse stream")] |
| 11 |
fn smuggled_speculative_cursor_between_sources() { |
| 12 |
struct BreakRules; |
| 13 |
impl Parse for BreakRules { |
| 14 |
fn parse(input1: ParseStream) -> Result<Self> { |
| 15 |
let nested = |input2: ParseStream| { |
| 16 |
input1.advance_to(input2); |
| 17 |
Ok(Self) |
| 18 |
}; |
| 19 |
nested.parse_str("") |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
syn::parse_str::<BreakRules>("").unwrap(); |
| 24 |
} |
| 25 |
|
| 26 |
#[test] |
| 27 |
#[should_panic(expected = "fork was not derived from the advancing parse stream")] |
| 28 |
fn smuggled_speculative_cursor_between_brackets() { |
| 29 |
struct BreakRules; |
| 30 |
impl Parse for BreakRules { |
| 31 |
fn parse(input: ParseStream) -> Result<Self> { |
| 32 |
let a; |
| 33 |
let b; |
| 34 |
parenthesized!(a in input); |
| 35 |
parenthesized!(b in input); |
| 36 |
a.advance_to(&b); |
| 37 |
Ok(Self) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
syn::parse_str::<BreakRules>("()()").unwrap(); |
| 42 |
} |
| 43 |
|
| 44 |
#[test] |
| 45 |
#[should_panic(expected = "fork was not derived from the advancing parse stream")] |
| 46 |
fn smuggled_speculative_cursor_into_brackets() { |
| 47 |
struct BreakRules; |
| 48 |
impl Parse for BreakRules { |
| 49 |
fn parse(input: ParseStream) -> Result<Self> { |
| 50 |
let a; |
| 51 |
parenthesized!(a in input); |
| 52 |
input.advance_to(&a); |
| 53 |
Ok(Self) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
syn::parse_str::<BreakRules>("()").unwrap(); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn trailing_empty_none_group() { |
| 62 |
fn parse(input: ParseStream) -> Result<()> { |
| 63 |
input.parse::<Token![+]>()?; |
| 64 |
|
| 65 |
let content; |
| 66 |
parenthesized!(content in input); |
| 67 |
content.parse::<Token![+]>()?; |
| 68 |
|
| 69 |
Ok(()) |
| 70 |
} |
| 71 |
|
| 72 |
// `+ ( + «∅ ∅» ) «∅ «∅ ∅» ∅»` |
| 73 |
let tokens = TokenStream::from_iter([ |
| 74 |
TokenTree::Punct(Punct::new('+', Spacing::Alone)), |
| 75 |
TokenTree::Group(Group::new( |
| 76 |
Delimiter::Parenthesis, |
| 77 |
TokenStream::from_iter([ |
| 78 |
TokenTree::Punct(Punct::new('+', Spacing::Alone)), |
| 79 |
TokenTree::Group(Group::new(Delimiter::None, TokenStream::new())), |
| 80 |
]), |
| 81 |
)), |
| 82 |
TokenTree::Group(Group::new(Delimiter::None, TokenStream::new())), |
| 83 |
TokenTree::Group(Group::new( |
| 84 |
Delimiter::None, |
| 85 |
TokenStream::from_iter([TokenTree::Group(Group::new( |
| 86 |
Delimiter::None, |
| 87 |
TokenStream::new(), |
| 88 |
))]), |
| 89 |
)), |
| 90 |
]); |
| 91 |
|
| 92 |
parse.parse2(tokens).unwrap(); |
| 93 |
} |
| 94 |
|
| 95 |
#[test] |
| 96 |
#[cfg_attr(miri, ignore)] // https://github.com/rust-lang/miri/issues/4793 |
| 97 |
fn test_unwind_safe() { |
| 98 |
fn parse(input: ParseStream) -> Result<Ident> { |
| 99 |
let thread_result = panic::catch_unwind(|| input.parse()); |
| 100 |
thread_result.unwrap() |
| 101 |
} |
| 102 |
|
| 103 |
parse.parse_str("throw").unwrap(); |
| 104 |
} |