Skip to content

Commit

Permalink
Merge pull request #434 from dtolnay/mismatch
Browse files Browse the repository at this point in the history
Fix compiler/fallback mismatch when catching rustc lex error panic
  • Loading branch information
dtolnay authored Jan 3, 2024
2 parents 4ae50db + 662426c commit 6d2899c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl LexError {
self.span
}

fn call_site() -> Self {
pub(crate) fn call_site() -> Self {
LexError {
span: Span::call_site(),
}
Expand Down
22 changes: 13 additions & 9 deletions src/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@ pub(crate) struct DeferredTokenStream {
pub(crate) enum LexError {
Compiler(proc_macro::LexError),
Fallback(fallback::LexError),
}

impl LexError {
fn call_site() -> Self {
LexError::Fallback(fallback::LexError {
span: fallback::Span::call_site(),
})
}
// Rustc was supposed to return a LexError, but it panicked instead.
// https://github.com/rust-lang/rust/issues/58736
CompilerPanic,
}

#[cold]
Expand Down Expand Up @@ -126,7 +122,7 @@ impl FromStr for TokenStream {
// Work around https://github.com/rust-lang/rust/issues/58736.
fn proc_macro_parse(src: &str) -> Result<proc_macro::TokenStream, LexError> {
let result = panic::catch_unwind(|| src.parse().map_err(LexError::Compiler));
result.unwrap_or_else(|_| Err(LexError::call_site()))
result.unwrap_or_else(|_| Err(LexError::CompilerPanic))
}

impl Display for TokenStream {
Expand Down Expand Up @@ -264,7 +260,7 @@ impl Debug for TokenStream {
impl LexError {
pub(crate) fn span(&self) -> Span {
match self {
LexError::Compiler(_) => Span::call_site(),
LexError::Compiler(_) | LexError::CompilerPanic => Span::call_site(),
LexError::Fallback(e) => Span::Fallback(e.span()),
}
}
Expand All @@ -287,6 +283,10 @@ impl Debug for LexError {
match self {
LexError::Compiler(e) => Debug::fmt(e, f),
LexError::Fallback(e) => Debug::fmt(e, f),
LexError::CompilerPanic => {
let fallback = fallback::LexError::call_site();
Debug::fmt(&fallback, f)
}
}
}
}
Expand All @@ -296,6 +296,10 @@ impl Display for LexError {
match self {
LexError::Compiler(e) => Display::fmt(e, f),
LexError::Fallback(e) => Display::fmt(e, f),
LexError::CompilerPanic => {
let fallback = fallback::LexError::call_site();
Display::fmt(&fallback, f)
}
}
}
}
Expand Down

0 comments on commit 6d2899c

Please sign in to comment.