Skip to content

Commit

Permalink
fix: Fix proc-macro server crashing when parsing a non-lexable string…
Browse files Browse the repository at this point in the history
… into a TokenStream
  • Loading branch information
Veykril committed Aug 29, 2024
1 parent 3a14e30 commit 14ec120
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ impl server::TokenStream for RaSpanServer {
stream.is_empty()
}
fn from_str(&mut self, src: &str) -> Self::TokenStream {
Self::TokenStream::from_str(src, self.call_site).expect("cannot parse string")
Self::TokenStream::from_str(src, self.call_site).unwrap_or_else(|e| {
Self::TokenStream::from_str(
&format!("compile_error!(\"failed to parse str to token stream: {e}\")"),
self.call_site,
)
.unwrap()
})
}
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,13 @@ impl server::TokenStream for TokenIdServer {
stream.is_empty()
}
fn from_str(&mut self, src: &str) -> Self::TokenStream {
Self::TokenStream::from_str(src, self.call_site).expect("cannot parse string")
Self::TokenStream::from_str(src, self.call_site).unwrap_or_else(|e| {
Self::TokenStream::from_str(
&format!("compile_error!(\"failed to parse str to token stream: {e}\")"),
self.call_site,
)
.unwrap()
})
}
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub(super) mod token_stream {
call_site,
src,
)
.ok_or("lexing error")?;
.ok_or_else(|| format!("lexing error: {src}"))?;

Ok(TokenStream::with_subtree(subtree))
}
Expand Down

0 comments on commit 14ec120

Please sign in to comment.