Skip to content

Commit 6d2899c

Browse files
authored
Merge pull request dtolnay#434 from dtolnay/mismatch
Fix compiler/fallback mismatch when catching rustc lex error panic
2 parents 4ae50db + 662426c commit 6d2899c

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/fallback.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl LexError {
4545
self.span
4646
}
4747

48-
fn call_site() -> Self {
48+
pub(crate) fn call_site() -> Self {
4949
LexError {
5050
span: Span::call_site(),
5151
}

src/wrapper.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,10 @@ pub(crate) struct DeferredTokenStream {
2828
pub(crate) enum LexError {
2929
Compiler(proc_macro::LexError),
3030
Fallback(fallback::LexError),
31-
}
3231

33-
impl LexError {
34-
fn call_site() -> Self {
35-
LexError::Fallback(fallback::LexError {
36-
span: fallback::Span::call_site(),
37-
})
38-
}
32+
// Rustc was supposed to return a LexError, but it panicked instead.
33+
// https://github.com/rust-lang/rust/issues/58736
34+
CompilerPanic,
3935
}
4036

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

132128
impl Display for TokenStream {
@@ -264,7 +260,7 @@ impl Debug for TokenStream {
264260
impl LexError {
265261
pub(crate) fn span(&self) -> Span {
266262
match self {
267-
LexError::Compiler(_) => Span::call_site(),
263+
LexError::Compiler(_) | LexError::CompilerPanic => Span::call_site(),
268264
LexError::Fallback(e) => Span::Fallback(e.span()),
269265
}
270266
}
@@ -287,6 +283,10 @@ impl Debug for LexError {
287283
match self {
288284
LexError::Compiler(e) => Debug::fmt(e, f),
289285
LexError::Fallback(e) => Debug::fmt(e, f),
286+
LexError::CompilerPanic => {
287+
let fallback = fallback::LexError::call_site();
288+
Debug::fmt(&fallback, f)
289+
}
290290
}
291291
}
292292
}
@@ -296,6 +296,10 @@ impl Display for LexError {
296296
match self {
297297
LexError::Compiler(e) => Display::fmt(e, f),
298298
LexError::Fallback(e) => Display::fmt(e, f),
299+
LexError::CompilerPanic => {
300+
let fallback = fallback::LexError::call_site();
301+
Display::fmt(&fallback, f)
302+
}
299303
}
300304
}
301305
}

0 commit comments

Comments
 (0)