Skip to content

Commit

Permalink
Auto merge of #17994 - Veykril:proc-macro-srv-from-str-panic, r=Veykril
Browse files Browse the repository at this point in the history
fix: Fix proc-macro server crashing when parsing a non-lexable string into a TokenStream

We might wanna consider backporting this to beta if that's simple enough to do
  • Loading branch information
bors committed Aug 29, 2024
2 parents 6f3356d + c47b206 commit a555c74
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs
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(|| {

Check failure on line 145 in crates/proc-macro-srv/src/server_impl/rust_analyzer_span.rs

View workflow job for this annotation

GitHub Actions / Rust (ubuntu-latest)

closure is expected to take 1 argument, but it takes 0 arguments
Self::TokenStream::from_str(
"compile_error!(\"failed to parse str to token stream\")",
self.call_site,
)
.unwrap()
})
}
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string()
Expand Down
8 changes: 7 additions & 1 deletion crates/proc-macro-srv/src/server_impl/token_id.rs
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(|| {

Check failure on line 134 in crates/proc-macro-srv/src/server_impl/token_id.rs

View workflow job for this annotation

GitHub Actions / Rust (ubuntu-latest)

closure is expected to take 1 argument, but it takes 0 arguments
Self::TokenStream::from_str(
"compile_error!(\"failed to parse str to token stream\")",
self.call_site,
)
.unwrap()
})
}
fn to_string(&mut self, stream: &Self::TokenStream) -> String {
stream.to_string()
Expand Down
2 changes: 1 addition & 1 deletion crates/proc-macro-srv/src/server_impl/token_stream.rs
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 a555c74

Please sign in to comment.