Skip to content

Commit da66636

Browse files
committed
Perform up-front validation using the fallback parser
1 parent 7d4023e commit da66636

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/fallback.rs

+20-2
Original file line numberDiff line numberDiff line change
@@ -1224,8 +1224,16 @@ fn escape_utf8(string: &str, repr: &mut String) {
12241224

12251225
#[cfg(feature = "proc-macro")]
12261226
pub(crate) trait FromStr2: FromStr<Err = proc_macro::LexError> {
1227+
#[cfg(wrap_proc_macro)]
1228+
fn validate(src: &str) -> Result<(), LexError>;
1229+
12271230
#[cfg(wrap_proc_macro)]
12281231
fn from_str_checked(src: &str) -> Result<Self, imp::LexError> {
1232+
// Validate using fallback parser, because rustc is incapable of
1233+
// returning a recoverable Err for certain invalid token streams, and
1234+
// will instead permanently poison the compilation.
1235+
Self::validate(src)?;
1236+
12291237
// Catch panic to work around https://github.com/rust-lang/rust/issues/58736.
12301238
match panic::catch_unwind(|| Self::from_str(src)) {
12311239
Ok(Ok(ok)) => Ok(ok),
@@ -1240,7 +1248,17 @@ pub(crate) trait FromStr2: FromStr<Err = proc_macro::LexError> {
12401248
}
12411249

12421250
#[cfg(feature = "proc-macro")]
1243-
impl FromStr2 for proc_macro::TokenStream {}
1251+
impl FromStr2 for proc_macro::TokenStream {
1252+
#[cfg(wrap_proc_macro)]
1253+
fn validate(src: &str) -> Result<(), LexError> {
1254+
TokenStream::from_str_checked(src).map(drop)
1255+
}
1256+
}
12441257

12451258
#[cfg(feature = "proc-macro")]
1246-
impl FromStr2 for proc_macro::Literal {}
1259+
impl FromStr2 for proc_macro::Literal {
1260+
#[cfg(wrap_proc_macro)]
1261+
fn validate(src: &str) -> Result<(), LexError> {
1262+
Literal::from_str_checked(src).map(drop)
1263+
}
1264+
}

0 commit comments

Comments
 (0)