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 TokenStream::to_string implementation dropping quotation marks

Fixes #17986
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 + c103fee commit 82ea248
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
17 changes: 14 additions & 3 deletions 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(|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 Expand Up @@ -501,12 +507,17 @@ mod tests {
close: span,
kind: tt::DelimiterKind::Brace,
},
token_trees: Box::new([]),
token_trees: Box::new([tt::TokenTree::Leaf(tt::Leaf::Literal(tt::Literal {
kind: tt::LitKind::Str,
symbol: Symbol::intern("string"),
suffix: None,
span,
}))]),
}),
],
};

assert_eq!(s.to_string(), "struct T {}");
assert_eq!(s.to_string(), "struct T {\"string\"}");
}

#[test]
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(|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
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
2 changes: 1 addition & 1 deletion crates/tt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,7 @@ pub fn pretty<S>(tkns: &[TokenTree<S>]) -> String {
TokenTree::Leaf(Leaf::Ident(ident)) => {
format!("{}{}", ident.is_raw.as_str(), ident.sym)
}
TokenTree::Leaf(Leaf::Literal(literal)) => literal.symbol.as_str().to_owned(),
TokenTree::Leaf(Leaf::Literal(literal)) => format!("{literal}"),
TokenTree::Leaf(Leaf::Punct(punct)) => format!("{}", punct.char),
TokenTree::Subtree(subtree) => {
let content = pretty(&subtree.token_trees);
Expand Down

0 comments on commit 82ea248

Please sign in to comment.