fix: Fix proc-macro server crashing when parsing a non-lexable string into a TokenStream

This commit is contained in:
Lukas Wirth 2024-08-29 08:08:00 +02:00
parent 3a14e301dd
commit 14ec1202e5
3 changed files with 15 additions and 3 deletions

View File

@ -142,7 +142,13 @@ fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
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()

View File

@ -131,7 +131,13 @@ fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
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()

View File

@ -131,7 +131,7 @@ pub(crate) fn from_str(src: &str, call_site: S) -> Result<TokenStream<S>, String
call_site,
src,
)
.ok_or("lexing error")?;
.ok_or_else(|| format!("lexing error: {src}"))?;
Ok(TokenStream::with_subtree(subtree))
}