Skip to content

Commit

Permalink
Merge pull request #486 from dtolnay/compilerlex
Browse files Browse the repository at this point in the history
Ensure that compiler tokenstream parsing only produces a compiler lexerror
  • Loading branch information
dtolnay authored Nov 21, 2024
2 parents 1ce5f04 + a9146d6 commit 27c5494
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1225,14 +1225,16 @@ fn escape_utf8(string: &str, repr: &mut String) {
#[cfg(feature = "proc-macro")]
pub(crate) trait FromStr2: FromStr<Err = proc_macro::LexError> {
#[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<Self, imp::LexError> {
// 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)) {
Expand All @@ -1250,15 +1252,15 @@ pub(crate) trait FromStr2: FromStr<Err = proc_macro::LexError> {
#[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()
}
}

0 comments on commit 27c5494

Please sign in to comment.