Skip to content

Handle negative literals in token_to_literal #19371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

0xJonas
Copy link
Contributor

@0xJonas 0xJonas commented Mar 15, 2025

token_to_literal does not consider the leading - for negative int/float literals, resulting in an error when expanding certain proc-macros:

use quote::quote;
use proc_macro2::Literal;
use proc_macro::TokenStream;

#[proc_macro]
pub fn foo(_: TokenStream) -> TokenStream {
    let t = Literal::i32_unsuffixed(-1);
    quote!(vec![#t]).into()
}
foo!(); // Error: expected Expr

The problem affects proc-macros, where the expansion contains a declarative macro, which contains a negative Literal.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 15, 2025
@Veykril
Copy link
Member

Veykril commented Mar 17, 2025

This might cause issues elsewhere for us, pretty sure our code so far assumes the minus token to be separate at all times. Would need to check more thoroughly

@0xJonas
Copy link
Contributor Author

0xJonas commented Mar 18, 2025

Would it be better if negative literals were split into minus+literal before parsing? The AST is technically missing a minus node.

@Veykril
Copy link
Member

Veykril commented Mar 19, 2025

The AST is correct I believe. number literals do not contain a prefixed minus in rust's token model https://doc.rust-lang.org/reference/tokens.html#number-literals with the exception of proc-macros.

@Veykril
Copy link
Member

Veykril commented May 5, 2025

I am having a look at t his right now, the tests from #19745 do pass so that's good. But I think this change might be a breaking change for the proc-macro server. Investigating that right now

@Veykril
Copy link
Member

Veykril commented May 5, 2025

Hmm, looking more into this, have you verified that this fixes your issues you have been having? I don't think this touches the part responsible for what is happening in your code snippet?

@Veykril Veykril removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants