Skip to content
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

Handle negative literals in token_to_literal #19371

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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.

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

Successfully merging this pull request may close these issues.

3 participants