rustc: Tweak Literal APIs of proc_macro
#48894
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit tweaks the public api of the
proc_macro::Literal
structure tosolve #48889, an issue about negative integers and their representation in
proc_macro
tokens. Currently the compiler assumes that all integer tokens arepositive integers rather than embedding a negative sign. The negative sign is
a separate token from the integer itself.
Currently there's a function like:
but unfortunately this doesn't work for negative integers. When called with
negative integers weird errors end up getting thrown later during the parsing
process.
This function tweaks the definitions to instead use:
This construction makes it more clear that negative arguments are not supported.
This additionally allows literals like
-128i8
where128
, the positiveinteger part of this literal, isn't representable in
i8
. By taking an unsignednumber in each constructor we can allow constructing the minimum literal for
each signed type as well.
The final tweak of this commit is to panic in
Literal::{f32, f64}
if the inputnumber is negative. This is similar to how infinite/nan literals are handled
today (they panic) and constructing a literal should be possible by negating a
float and then separately passing in a literal.
Closes #48889