Skip to content

Commit 85a08ea

Browse files
committed
Move catch_unwind into from_str_checked
1 parent 3670a21 commit 85a08ea

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/fallback.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use core::str;
2121
#[cfg(feature = "proc-macro")]
2222
use core::str::FromStr;
2323
use std::ffi::CStr;
24+
#[cfg(wrap_proc_macro)]
25+
use std::panic;
2426
#[cfg(procmacro2_semver_exempt)]
2527
use std::path::PathBuf;
2628

@@ -1224,7 +1226,12 @@ fn escape_utf8(string: &str, repr: &mut String) {
12241226
pub(crate) trait FromStr2: FromStr<Err = proc_macro::LexError> {
12251227
#[cfg(wrap_proc_macro)]
12261228
fn from_str_checked(src: &str) -> Result<Self, imp::LexError> {
1227-
Self::from_str(src).map_err(imp::LexError::Compiler)
1229+
// Catch panic to work around https://github.com/rust-lang/rust/issues/58736.
1230+
match panic::catch_unwind(|| Self::from_str(src)) {
1231+
Ok(Ok(ok)) => Ok(ok),
1232+
Ok(Err(lex)) => Err(imp::LexError::Compiler(lex)),
1233+
Err(_panic) => Err(imp::LexError::CompilerPanic),
1234+
}
12281235
}
12291236

12301237
fn from_str_unchecked(src: &str) -> Self {

src/wrapper.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use core::fmt::{self, Debug, Display};
88
use core::ops::Range;
99
use core::ops::RangeBounds;
1010
use std::ffi::CStr;
11-
use std::panic;
1211
#[cfg(super_unstable)]
1312
use std::path::PathBuf;
1413

@@ -88,12 +87,9 @@ impl TokenStream {
8887

8988
pub(crate) fn from_str_checked(src: &str) -> Result<Self, LexError> {
9089
if inside_proc_macro() {
91-
// Catch panic to work around https://github.com/rust-lang/rust/issues/58736.
92-
match panic::catch_unwind(|| proc_macro::TokenStream::from_str_checked(src)) {
93-
Ok(Ok(tokens)) => Ok(TokenStream::Compiler(DeferredTokenStream::new(tokens))),
94-
Ok(Err(lex)) => Err(lex),
95-
Err(_panic) => Err(LexError::CompilerPanic),
96-
}
90+
Ok(TokenStream::Compiler(DeferredTokenStream::new(
91+
proc_macro::TokenStream::from_str_checked(src)?,
92+
)))
9793
} else {
9894
Ok(TokenStream::Fallback(
9995
fallback::TokenStream::from_str_checked(src)?,

0 commit comments

Comments
 (0)