Skip to content

Commit

Permalink
Fix proc_macro::quote! for raw ident
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Jan 9, 2025
1 parent 5c94be6 commit 3a2f3b9
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 3 deletions.
10 changes: 8 additions & 2 deletions library/proc_macro/src/quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,14 @@ pub fn quote(stream: TokenStream) -> TokenStream {
)), &mut ts);)
}
TokenTree::Ident(tt) => {
minimal_quote!(crate::ToTokens::to_tokens(&crate::TokenTree::Ident(crate::Ident::new(
(@ TokenTree::from(Literal::string(&tt.to_string()))),
let literal = tt.to_string();
let (literal, ctor) = if let Some(stripped) = literal.strip_prefix("r#") {
(stripped, minimal_quote!(crate::Ident::new_raw))
} else {
(literal.as_str(), minimal_quote!(crate::Ident::new))
};
minimal_quote!(crate::ToTokens::to_tokens(&crate::TokenTree::Ident((@ ctor)(
(@ TokenTree::from(Literal::string(literal))),
(@ quote_span(proc_macro_crate.clone(), tt.span())),
)), &mut ts);)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/proc-macro/quote/auxiliary/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn run_tests(_: TokenStream) -> TokenStream {
test_inner_block_comment();
test_outer_attr();
test_inner_attr();
// test_quote_raw_id(); // FIXME: Fix it in a subsequent commit
test_quote_raw_id();

TokenStream::new()
}
Expand Down
1 change: 1 addition & 0 deletions tests/ui/proc-macro/quote/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ extern crate proc_macro;
fn main() {
proc_macro::quote! {
let hello = "world";
let r#raw_ident = r#"raw"literal"#;
}
}
19 changes: 19 additions & 0 deletions tests/ui/proc-macro/quote/debug.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ fn main() {
}), &mut ts);
crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';',
crate::Spacing::Alone)), &mut ts);
crate::ToTokens::to_tokens(&crate::TokenTree::Ident(crate::Ident::new("let",
crate::Span::recover_proc_macro_span(3))), &mut ts);
crate::ToTokens::to_tokens(&crate::TokenTree::Ident(crate::Ident::new_raw("raw_ident",
crate::Span::recover_proc_macro_span(4))), &mut ts);
crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new('=',
crate::Spacing::Alone)), &mut ts);
crate::ToTokens::to_tokens(&crate::TokenTree::Literal({
let mut iter =
"r#\"raw\"literal\"#".parse::<crate::TokenStream>().unwrap().into_iter();
if let (Some(crate::TokenTree::Literal(mut lit)), None) =
(iter.next(), iter.next()) {
lit.set_span(crate::Span::recover_proc_macro_span(5));
lit
} else {
::core::panicking::panic("internal error: entered unreachable code")
}
}), &mut ts);
crate::ToTokens::to_tokens(&crate::TokenTree::Punct(crate::Punct::new(';',
crate::Spacing::Alone)), &mut ts);
ts
}
}
Expand Down

0 comments on commit 3a2f3b9

Please sign in to comment.