Skip to content

Macros 2.0: Negative numbers as a single token #48889

Closed
@alexcrichton

Description

@alexcrichton

@ngg ran into some weird bugs and made an awesome reduction which boils down to:

#[proc_macro]
pub fn buggy_macro(_: TokenStream) -> TokenStream {
    TokenNode::Literal(Literal::i32(-1)).into()
}

It turns out that when expanding this rustc will print out:

error: int literal is too large

Originating from here I think that this is actually a misdiagnosed error in the sense that the parse is failing because the string is "-1" which isn't parseable as a u128, but the current code assumes that it can only fail due to overflow. Generally this is correct because the parser never feeds minus signs to this function!

So the broader issue here I believe is that Literal::i32 can't actually exist. Our parser, as-is today, (and tokenizer?) dictates that the source string -1 is lexed as two tokens, the minus and then the 1. This means that Literal::i32 pretending it can only be one token isn't actually right!

Should we remove Literal::i32 and all other signed functions? If so should we also panic on negative floats? If not, should we refactor the AST to support literal tokens with negative integers?

cc @jseyfried, @dtolnay, @nrc

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-decl-macros-2-0Area: Declarative macros 2.0 (#39412)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions