From a9146d6cb558851b1ae180ef28b93cd6987cd4ab Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Wed, 20 Nov 2024 21:36:11 -0800 Subject: [PATCH] Ensure that compiler tokenstream parsing only produces a compiler lexerror --- src/fallback.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/fallback.rs b/src/fallback.rs index 53bb66a..92b342f 100644 --- a/src/fallback.rs +++ b/src/fallback.rs @@ -1225,14 +1225,16 @@ fn escape_utf8(string: &str, repr: &mut String) { #[cfg(feature = "proc-macro")] pub(crate) trait FromStr2: FromStr { #[cfg(wrap_proc_macro)] - fn validate(src: &str) -> Result<(), LexError>; + fn valid(src: &str) -> bool; #[cfg(wrap_proc_macro)] fn from_str_checked(src: &str) -> Result { // Validate using fallback parser, because rustc is incapable of // returning a recoverable Err for certain invalid token streams, and // will instead permanently poison the compilation. - Self::validate(src)?; + if !Self::valid(src) { + return Err(imp::LexError::CompilerPanic); + } // Catch panic to work around https://github.com/rust-lang/rust/issues/58736. match panic::catch_unwind(|| Self::from_str(src)) { @@ -1250,15 +1252,15 @@ pub(crate) trait FromStr2: FromStr { #[cfg(feature = "proc-macro")] impl FromStr2 for proc_macro::TokenStream { #[cfg(wrap_proc_macro)] - fn validate(src: &str) -> Result<(), LexError> { - TokenStream::from_str_checked(src).map(drop) + fn valid(src: &str) -> bool { + TokenStream::from_str_checked(src).is_ok() } } #[cfg(feature = "proc-macro")] impl FromStr2 for proc_macro::Literal { #[cfg(wrap_proc_macro)] - fn validate(src: &str) -> Result<(), LexError> { - Literal::from_str_checked(src).map(drop) + fn valid(src: &str) -> bool { + Literal::from_str_checked(src).is_ok() } }