Skip to content

Commit

Permalink
Merge pull request #1646 from dtolnay/unwindsafe
Browse files Browse the repository at this point in the history
Make ParseBuffer unwindsafe
  • Loading branch information
dtolnay committed May 11, 2024
2 parents 8bcd277 + 4a1e57f commit 55205b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/parse.rs
Expand Up @@ -197,6 +197,7 @@ use std::hash::{Hash, Hasher};
use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::panic::{RefUnwindSafe, UnwindSafe};
use std::rc::Rc;
use std::str::FromStr;

Expand Down Expand Up @@ -283,6 +284,9 @@ impl<'a> Debug for ParseBuffer<'a> {
}
}

impl<'a> UnwindSafe for ParseBuffer<'a> {}
impl<'a> RefUnwindSafe for ParseBuffer<'a> {}

/// Cursor state associated with speculative parsing.
///
/// This type is the input of the closure provided to [`ParseStream::step`].
Expand Down
13 changes: 12 additions & 1 deletion tests/test_parse_buffer.rs
@@ -1,6 +1,7 @@
#![allow(clippy::non_ascii_literal)]

use proc_macro2::{Delimiter, Group, Punct, Spacing, TokenStream, TokenTree};
use proc_macro2::{Delimiter, Group, Ident, Punct, Spacing, TokenStream, TokenTree};
use std::panic;
use syn::parse::discouraged::Speculative as _;
use syn::parse::{Parse, ParseStream, Parser, Result};
use syn::{parenthesized, Token};
Expand Down Expand Up @@ -90,3 +91,13 @@ fn trailing_empty_none_group() {

parse.parse2(tokens).unwrap();
}

#[test]
fn test_unwind_safe() {
fn parse(input: ParseStream) -> Result<Ident> {
let thread_result = panic::catch_unwind(|| input.parse());
thread_result.unwrap()
}

parse.parse_str("throw").unwrap();
}

0 comments on commit 55205b6

Please sign in to comment.