1 #[macro_use]
2 extern crate syn;
3 
4 use syn::parse::{discouraged::Speculative, Parse, ParseStream, Parser, Result};
5 
6 #[test]
7 #[should_panic(expected = "Fork was not derived from the advancing parse stream")]
smuggled_speculative_cursor_between_sources()8 fn smuggled_speculative_cursor_between_sources() {
9     struct BreakRules;
10     impl Parse for BreakRules {
11         fn parse(input1: ParseStream) -> Result<Self> {
12             let nested = |input2: ParseStream| {
13                 input1.advance_to(&input2);
14                 Ok(Self)
15             };
16             nested.parse_str("")
17         }
18     }
19 
20     syn::parse_str::<BreakRules>("").unwrap();
21 }
22 
23 #[test]
24 #[should_panic(expected = "Fork was not derived from the advancing parse stream")]
smuggled_speculative_cursor_between_brackets()25 fn smuggled_speculative_cursor_between_brackets() {
26     struct BreakRules;
27     impl Parse for BreakRules {
28         fn parse(input: ParseStream) -> Result<Self> {
29             let a;
30             let b;
31             parenthesized!(a in input);
32             parenthesized!(b in input);
33             a.advance_to(&b);
34             Ok(Self)
35         }
36     }
37 
38     syn::parse_str::<BreakRules>("()()").unwrap();
39 }
40 
41 #[test]
42 #[should_panic(expected = "Fork was not derived from the advancing parse stream")]
smuggled_speculative_cursor_into_brackets()43 fn smuggled_speculative_cursor_into_brackets() {
44     struct BreakRules;
45     impl Parse for BreakRules {
46         fn parse(input: ParseStream) -> Result<Self> {
47             let a;
48             parenthesized!(a in input);
49             input.advance_to(&a);
50             Ok(Self)
51         }
52     }
53 
54     syn::parse_str::<BreakRules>("()").unwrap();
55 }
56