-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Accept tuple.0.0 as tuple indexing #70420
Conversation
@@ -783,47 +780,6 @@ impl<'a> Parser<'a> { | |||
self.struct_span_err(self.token.span, &format!("unexpected token: `{}`", actual)).emit(); | |||
} | |||
|
|||
fn recover_field_access_by_float_lit( |
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.
Happy to see this method go; it was ungreat to refactor in the past :) 🎉
From the POV of someone who is quite familiar with Assuming @petrochenkov & @matklad are happy with this I would be happy to propose FCP merging this to the language team. Though let's also nominate it for discussion today on our language team meeting. |
Am I correct that Am I correct that this change will break this program? macro_rules! m { (.$l:literal) => (); }
m!(.0.0);
fn main() {} It should be added as a test as well. Another case I can think of is Generally, I see two different ways how we can achieve this behavior:
I personally would lean towards the second approach, as it will produce less "false positives". It means that the parser should be exposed to the text of the token, and not only to its lexical class, but it is already somewhat happens for contextual keywords (although this case is different, as the set of context keywords is finite, while the set of \d+.\d+ float literals is not). Makes me wonder why we didn't choose |
Added tests. These are now the way to write IdentDotFloat if that's what the programmer means, but that should be quite rare. It's better than requiring a workaround for IdentDotIntegerDotInteger.
Added test.
Added test. |
I think whitespace sensitivity is bad. Also this may break the pretty printing of expressions as tokenstreams ( |
Some reasons I've leaned toward the first approach:
In any case, if the lang team concludes that |
That's fine, but this PR is no more whitespace sensitive than Furthermore, you'll love that this PR makes things less whitespace sensitive, such as in the case of
They would come out as |
This reminds me of our treatment of punctuation in proc macros. I remember some people (@eddyb at least?) regretted that we didn't break up floats in the same way as multi-character operators while proc macros were unstable, and didn't move the treatment of floats entirely to the parser. In (Perhaps later we'll be able to migrate floats to the "simple token" model internally and expose them as a single token only to proc macros for compatibility. Or maybe not, depending on convenience mostly.) |
With the scheme from my comment above |
We briefly discussed this on the language team meeting, but @petrochenkov's comment wasn't there yet, so I think we'll need some more discussion on the next one. :) |
We discussed this again in this week's language team meeting taking @petrochenkov's notes into account this time. Some conclusions from the meeting:
|
@dtolnay any updates? |
I haven't gotten a chance to try the alternative implementation suggested in #70420 (comment). Maybe @Centril or someone else already familiar with the parser would like to give it a shot? |
I can try implementing it this weekend. |
The alternative implemenation - #71322. |
Based on #71322 (comment), closing in favor of #71322. Thanks @petrochenkov for the nicer approach and implementation! |
Accept tuple.0.0 as tuple indexing (take 2) If we expect something identifier-like when parsing a field name after `.`, but encounter a float token, we break that float token into parts, similarly to how we break `&&` into `&` `&`, or `<<` into `<` `<`, etc. An alternative to rust-lang#70420.
Accept tuple.0.0 as tuple indexing (take 2) If we expect something identifier-like when parsing a field name after `.`, but encounter a float token, we break that float token into parts, similarly to how we break `&&` into `&` `&`, or `<<` into `<` `<`, etc. An alternative to rust-lang#70420.
Accept tuple.0.0 as tuple indexing (take 2) If we expect something identifier-like when parsing a field name after `.`, but encounter a float token, we break that float token into parts, similarly to how we break `&&` into `&` `&`, or `<<` into `<` `<`, etc. An alternative to rust-lang#70420.
Accept tuple.0.0 as tuple indexing (take 2) If we expect something identifier-like when parsing a field name after `.`, but encounter a float token, we break that float token into parts, similarly to how we break `&&` into `&` `&`, or `<<` into `<` `<`, etc. An alternative to rust-lang#70420.
Closes #72 Fairly simple change: - Tuple types - Tuple expressions - Tuple indexing expressions. Error out on invalid indices such as `0xa` or anything that is not a `usize`. Note that `t.0.0` does not currently work, but `t.0 .0`. Once we implement #66, we can then write `(t.0).0`. In the future, we can support `t.0.0` which can be done in two ways: - Special case the lexer to _not_ parse the `0.0` as a real in the case of a tuple access (this means the lexer now has to concern itself with the context). - Break the real `0.0` apart in the pasrer, which kinda what Rust does (see rust-lang/rust#71322 after an attempt for the other method in rust-lang/rust#70420)
Previously rustc would reject
tuple.0.0
, requiring the programmer to write eithertuple.0 .0
or(tuple.0).0
to work around the limitation because the0.0
is emitted as one float token by the lexer.This PR adjusts the lexer to produce IdentDotIntegerDotInteger for the above, instead of IdentDotFloat, thereby making
tuple.0.0
work without a parser change.r? @Centril
FYI @nikomatsakis since your ancient comments came up. ❤️
FYI @petrochenkov and @matklad — I would be interested whether you are on board with a lexer change like this.