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 a bit better #65

Closed

Conversation

alexcrichton
Copy link
Collaborator

Looks like upstream in rust-lang/rust negative integers are represented as two
tokens instead of one token (and it looks like proc_macro may erroneously (?)
accept negative integers as literals, see rust-lang/rust#48889). As a result
tweak the ToTokens impls for signed integers to maybe put a - token out in
front. Similar treatment is applied to f32/f64 as well.

Special treatment is required, however, for the iNN::min_value() constants.
The actual integral portion isn't actually representable as a positive integer
literal (as it'd overflow back to negative) so to handle this case everything is
just represented as a u64 literal cast to the right type.

Copy link

@ngg ngg left a comment

Choose a reason for hiding this comment

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

LGTM, fixes both my repro case and the original bug in prost as well. Fortunately the unparseable token bug disappeared as well with this.

src/to_tokens.rs Outdated
}

macro_rules! signed_primitive {
($(($t:ident => $signed:ident))*) => ($(
Copy link

Choose a reason for hiding this comment

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

The $signed part is actually the unsigned version, and it's not used, could be removed.

Looks like upstream in rust-lang/rust negative integers are represented as two
tokens instead of one token (and it looks like proc_macro may erroneously (?)
accept negative integers as literals, see rust-lang/rust#48889). As a result
tweak the `ToTokens` impls for signed integers to maybe put a `-` token out in
front. Similar treatment is applied to f32/f64 as well.

Special treatment is required, however, for the `iNN::min_value()` constants.
The actual integral portion isn't actually representable as a positive integer
literal (as it'd overflow back to negative) so to handle this case everything is
just represented as a u64 literal cast to the right type.
@alexcrichton alexcrichton force-pushed the fix-negative-integers branch from d0c4c52 to 0e73800 Compare March 9, 2018 21:50
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

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

I would prefer not to work around this so that we are motivated to fix rust-lang/rust#48889 sooner rather than later.

@dtolnay dtolnay closed this Mar 28, 2018
Repository owner locked and limited conversation to collaborators Dec 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants