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

Support negative numbers in Literal::from_str #87262

Merged
merged 1 commit into from Aug 3, 2021

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jul 18, 2021

proc_macro::Literal has allowed negative numbers in a single literal token ever since Rust 1.29, using https://doc.rust-lang.org/stable/proc_macro/struct.Literal.html#method.isize_unsuffixed and similar constructors.

let lit = proc_macro::Literal::isize_unsuffixed(-10);

However, the suite of constructors on Literal is not sufficient for all use cases, for example arbitrary precision floats, or custom suffixes in FFI macros.

let lit = proc_macro::Literal::f64_unsuffixed(0.101001000100001000001000000100000001); // :(
let lit = proc_macro::Literal::i???_suffixed(10ulong); // :(

For those, macros construct the literal using from_str instead, which preserves arbitrary precision, custom suffixes, base, and digit grouping.

let lit = "0.101001000100001000001000000100000001".parse::<Literal>().unwrap();
let lit = "10ulong".parse::<Literal>().unwrap();
let lit = "0b1000_0100_0010_0001".parse::<Literal>().unwrap();

However, until this PR it was not possible to construct a literal token that is both negative and preserving of arbitrary precision etc.

This PR fixes Literal::from_str to recognize negative integer and float literals.

@rust-highfive
Copy link
Collaborator

r? @LeSeulArtichaut

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 18, 2021
@LeSeulArtichaut
Copy link
Contributor

r? @Aaron1011 Do you want to take this?

test_parse_literal();
}

fn test_display_literal() {
assert_eq!(Literal::isize_unsuffixed(-10).to_string(), "- 10");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a pre-existing bug - this literal will not round-trip, since "- 10" will result in a parse error. Could you open an issue for this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fixed in #87267.

@Aaron1011
Copy link
Member

Sorry for the delay in getting to this.

@bors r+

@bors
Copy link
Contributor

bors commented Aug 3, 2021

📌 Commit 55ff45a has been approved by Aaron1011

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 3, 2021
@bors
Copy link
Contributor

bors commented Aug 3, 2021

⌛ Testing commit 55ff45a with merge e91405b...

@bors
Copy link
Contributor

bors commented Aug 3, 2021

☀️ Test successful - checks-actions
Approved by: Aaron1011
Pushing e91405b to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors label Aug 3, 2021
@bors bors merged commit e91405b into rust-lang:master Aug 3, 2021
@rustbot rustbot added this to the 1.56.0 milestone Aug 3, 2021
@dtolnay dtolnay deleted the negative branch August 3, 2021 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants