-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
move some invalid exponent detection into rustc_session #131656
base: master
Are you sure you want to change the base?
move some invalid exponent detection into rustc_session #131656
Conversation
rustbot has assigned @petrochenkov. Use |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
8b41315
to
e8c244e
Compare
This comment has been minimized.
This comment has been minimized.
a642bff
to
82a8103
Compare
This comment has been minimized.
This comment has been minimized.
I've added some ui tests and I think this is now ready for review. It doesn't stop @petrochenkov changing the whole thing later if they so choose. |
This comment has been minimized.
This comment has been minimized.
Apologies for the delays. |
I'd still prefer to see #111628 (comment) implemented, but I think this looks like a compatible subset, so we can go forward. |
There's also one subtle change in behavior here.
then For correct behavior we'll need to use |
633b1b0
to
d13c474
Compare
This comment has been minimized.
This comment has been minimized.
d13c474
to
2c33df5
Compare
This comment has been minimized.
This comment has been minimized.
c3947a0
to
cfb8823
Compare
This comment has been minimized.
This comment has been minimized.
cfb8823
to
79d5952
Compare
Hey @petrochenkov,
Other than the points above, it should be ready for review again. |
I agree that the issue can be addressed separately, because it only appears in cases that produced errors previously and still produce errors after this PR. |
@mattheww Technically we could support However, when |
This comment has been minimized.
This comment has been minimized.
What should happen for something like In that case there's a prefix of the input which is an acceptable floating-point literal, but the longest tokenisable sequence will be the integer literal with suffix |
Co-authored-by: Vadim Petrochenkov <vadim.petrochenkov@gmail.com>
Co-authored-by: Vadim Petrochenkov <vadim.petrochenkov@gmail.com>
e2caf03
to
828cf96
Compare
- factor out code that determines whether the string beggining with `e_` is an exponent or the start of the suffix - reduce use of mutable state
Ok ready for review. I'll try to tackle the issue with xid_continue-but-not-digit after if you like. Is there an issue for it? |
Looks like fixes for #131656 (comment) and #131656 (comment) were lost during a rebase. |
(Float { base, empty_exponent }, suffix_start) | ||
} | ||
('e' | 'E', '_') => { | ||
if let Some(suffix_start) = self.eat_underscore_exponent() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: if let
with short arms usually looks better as a match
.
// might have stuff after the ., and if it does, it needs to start | ||
// with a number | ||
self.bump(); | ||
if !self.first().is_ascii_digit() { | ||
return (Float { base, empty_exponent: false }, None); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks redundant, the logic below does the same thing.
} | ||
_ => Some(self.pos_within_token()), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_ => Some(self.pos_within_token()), | |
_ => None, |
@richard-uk1 |
There's no issue, could you make one? We could discuss the questions like
on that issue too, this PR doesn't change the behavior here (it still produces an error), so it can be postponed. @rustbot author |
This PR moves part of the exponent checks from
rustc_lexer
/rustc_parser
intorustc_session
.This change does not affect which programs are accepted by the complier, or the diagnostics that are reported, with one main exception. That exception is that floats or ints with suffixes beginning with
e
are rejected after the token stream is passed to proc macros, rather than being rejected by the parser as was the case. This gives proc macro authors more consistent access to numeric literals: currently a proc macro could interpret1m
or30s
but not7eggs
or3em
. After this change all are handled the same. The lexer will still reject input if it containse
followed by a number,+
/-
, or_
if they are not followed by a valid integer literal (number +_
), but this doesn't affect macro authors who just want to access alpha suffixes.This PR is a continuation of #79912. In that PR, it was suggested that a new enum was used to indicate type of exponent (whether accepted or rejected). I originally took that approach with this PR, but it didn't seem necessary and made the changes more complex. I can try to go down that road instead if that's the consensus. It is also solving exactly the same problem as #111628.
TODO before ready for review (assuming approach is OK)
1em
)e
(if suffix begins with 'e' suggest an exponential)Currently if the character following theThis now handles arbitrarye
is_
, then the lexer tries to parse an exponent and fails if there are no digits after. The issue is that a valid integer can have any number of_
s before the digit, meaning deciding whether a the suffix is a number or not requires unbounded lookahead. There are a few options here_
s, removing the special case.Although I haven't marked this PR as 'ready for review' since there are outstanding stuff that need doing, I do want to get feedback on the approach.Ready for review now.Also do you want me to write an MCP
r: @petrochenkov, since they reviewed #79912.