Skip to content

Commit a9146d6

Browse files
committed
Ensure that compiler tokenstream parsing only produces a compiler lexerror
1 parent 1ce5f04 commit a9146d6

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

src/fallback.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1225,14 +1225,16 @@ fn escape_utf8(string: &str, repr: &mut String) {
12251225
#[cfg(feature = "proc-macro")]
12261226
pub(crate) trait FromStr2: FromStr<Err = proc_macro::LexError> {
12271227
#[cfg(wrap_proc_macro)]
1228-
fn validate(src: &str) -> Result<(), LexError>;
1228+
fn valid(src: &str) -> bool;
12291229

12301230
#[cfg(wrap_proc_macro)]
12311231
fn from_str_checked(src: &str) -> Result<Self, imp::LexError> {
12321232
// Validate using fallback parser, because rustc is incapable of
12331233
// returning a recoverable Err for certain invalid token streams, and
12341234
// will instead permanently poison the compilation.
1235-
Self::validate(src)?;
1235+
if !Self::valid(src) {
1236+
return Err(imp::LexError::CompilerPanic);
1237+
}
12361238

12371239
// Catch panic to work around https://github.com/rust-lang/rust/issues/58736.
12381240
match panic::catch_unwind(|| Self::from_str(src)) {
@@ -1250,15 +1252,15 @@ pub(crate) trait FromStr2: FromStr<Err = proc_macro::LexError> {
12501252
#[cfg(feature = "proc-macro")]
12511253
impl FromStr2 for proc_macro::TokenStream {
12521254
#[cfg(wrap_proc_macro)]
1253-
fn validate(src: &str) -> Result<(), LexError> {
1254-
TokenStream::from_str_checked(src).map(drop)
1255+
fn valid(src: &str) -> bool {
1256+
TokenStream::from_str_checked(src).is_ok()
12551257
}
12561258
}
12571259

12581260
#[cfg(feature = "proc-macro")]
12591261
impl FromStr2 for proc_macro::Literal {
12601262
#[cfg(wrap_proc_macro)]
1261-
fn validate(src: &str) -> Result<(), LexError> {
1262-
Literal::from_str_checked(src).map(drop)
1263+
fn valid(src: &str) -> bool {
1264+
Literal::from_str_checked(src).is_ok()
12631265
}
12641266
}

0 commit comments

Comments
 (0)