Skip to content

Commit

Permalink
Auto merge of rust-lang#17994 - Veykril:proc-macro-srv-from-str-panic…
Browse files Browse the repository at this point in the history
…, r=Veykril

fix: Fix TokenStream::to_string implementation dropping quotation marks

Fixes rust-lang/rust-analyzer#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 3a14e30 + 03d6745 commit 266bb1f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 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 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
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
2 changes: 1 addition & 1 deletion src/tools/rust-analyzer/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 266bb1f

Please sign in to comment.