Skip to content

A hex int literal passed as a macro parameter forgets its identity and gets stringify!-ed as a decimal int literal #33047

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
nodakai opened this issue Apr 17, 2016 · 3 comments
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug.

Comments

@nodakai
Copy link
Contributor

nodakai commented Apr 17, 2016

Minor papercut:

macro_rules! m {
    ($e: expr) => (stringify!($e))
}

fn main() {
    println!("{:?}", stringify!(1 +1));
    println!("{:?}",         m!(1 +1));
    println!("{:?}", stringify!(0xff));
    println!("{:?}",         m!(0xff));
    println!("{:?}", stringify!(1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001));
    println!("{:?}",         m!(1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001));
}
"1 + 1"
"1 + 1"
"0xff"
"255"
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"

0xff is converted into 255 while 1.000....1 isn't (the delta from 1.0 is clearly smaller than IEEE854 double's machine epsilon)

May or may not be related to #28415

@TimNN
Copy link
Contributor

TimNN commented Apr 17, 2016

The reason for this is the way they are represented at the time macro expansion happens: Integers are stored as a u64 value, their representation in the source is lost. Float literals are stored as strings at this point.

@nodakai
Copy link
Contributor Author

nodakai commented Apr 17, 2016

@TimNN Thanks for the info, that's exactly what I expected from the above observations.

And I'd claim such an implementation detail should not affect an observable behavior of a programming language.

Ideally stringify!(1 +1) should yield "1 +1" rather than "1 + 1" but this is acceptable for me at the moment.

@steveklabnik steveklabnik added the A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) label Jul 25, 2016
@Mark-Simulacrum Mark-Simulacrum added the C-bug Category: This is a bug. label Jul 25, 2017
@Mark-Simulacrum
Copy link
Member

Output today is:

"1 + 1"
"1 + 1"
"0xff"
"0xff"
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
"1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"

which is pretty much what we'd expect I think so going to go ahead and close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

4 participants