Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upUnify rules about commas in match arms and semicolons in expressions #40989
Conversation
rust-highfive
assigned
nrc
Apr 1, 2017
This comment has been minimized.
This comment has been minimized.
|
r? @nrc (rust_highfive has picked a reviewer for you, use r? to override) |
petrochenkov
reviewed
Apr 1, 2017
| @@ -3093,8 +3093,7 @@ impl<'a> Parser<'a> { | |||
| self.expect(&token::FatArrow)?; | |||
| let expr = self.parse_expr_res(Restrictions::RESTRICTION_STMT_EXPR, None)?; | |||
|
|
|||
| let require_comma = | |||
| !classify::expr_is_simple_block(&expr) | |||
| let require_comma = classify::expr_requires_semi_to_be_stmt(&expr) | |||
This comment has been minimized.
This comment has been minimized.
petrochenkov
Apr 1, 2017
•
Contributor
Could you also add ExprKind::Catch to expr_requires_semi_to_be_stmt, it's missing.
(A copy of fn expr_requires_semi_to_be_stmt in librustc/hir/print.rs is also badly out-of-date.)
This comment has been minimized.
This comment has been minimized.
matklad
Apr 1, 2017
•
Author
Member
A copy of fn expr_requires_semi_to_be_stmt in librustc/hir/print.rs is also badly out-of-date.
Is it? It seems legit, and even already handles catch (catch is just a block in hir, isn't it?)
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
LGTM, but needs a @rust-lang/lang decision. |
solson
reviewed
Apr 1, 2017
| @@ -0,0 +1,35 @@ | |||
| fn main() { | |||
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
matklad
Apr 1, 2017
•
Author
Member
Aha! That's why I am always not sure if it's a coma or a comma: they are spelled differently : ) ! Thanks!
matklad
force-pushed the
matklad:comma-arms
branch
from
dcf5790
to
60d7cc9
Apr 1, 2017
This comment has been minimized.
This comment has been minimized.
|
Can someone with the access tag this T-lang & I-nominated? |
petrochenkov
added
I-nominated
T-lang
labels
Apr 2, 2017
This comment has been minimized.
This comment has been minimized.
|
@rfcbot fcp merge We discussed this at the lang team meeting. We all love it, but the meeting was not well attended, so just making sure everyone signs off on this. |
This comment has been minimized.
This comment has been minimized.
rfcbot
commented
Apr 6, 2017
•
|
Team member @nrc has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
nrc
removed
the
I-nominated
label
Apr 6, 2017
This comment has been minimized.
This comment has been minimized.
|
@rfcbot reviewed I am so excited by this. I accidentally omit the comma after a control flow expression all the time! I had always assumed there was just some good reason things were like this. |
This comment has been minimized.
This comment has been minimized.
For posterity, I've discovered this issue when I had implemented automatic comma insertion/deletion in IntelliJ Rust, and found out that it breaks my code :P |
This comment has been minimized.
This comment has been minimized.
|
Huh. I have to admit, I am a bit surprised by the current behavior of the parser, but not in the way that others are. In particular, I would have expected this to parse today, but it doesn't seem to: fn main() {
let x = 3;
match x {
1 => if true { 1 } else { 2 } - 2,
// ^ parse error here
-2 => 3,
_ => 4,
}
}(For a bit of history, the old rule was that all match arms required So I'm mildly nervous about making this change without specifying it more clearly. The current rules for handling of semicolon are somewhat subtle, after all. The way I would summarize them is:
Therefore this example: fn main() {
if true { 2 } else { 3 } - 2;
}Is roughly equivalent to this: fn main() {
let () = if true { 2 } else { 3 }; - 2;
}The intention of the Are we going to apply a similar rule to |
This comment has been minimized.
This comment has been minimized.
I actually was sure that this change would be impossible because of the backwards incompatibility, and was pleasantly surprised to find out that we already install the "statement-mode" restriction, and only then specifically check for plain blocks. There's a test for this.
The situation here is a bit different, so it looks like we can avoid the rule. In statements, the expression following the let r = match -2 {
0 => { 1 } - 1
... 2 => { 2 }
_ => 3
};But, because we already do not require a comma after a simple block, I think all potential future ambiguities are already there, so lifting the restriction does not harm. That said, digging through git history I've found out that once upon a time rust has exactly the logic from this PR, which was explicitelly changed to "allow only simple blocks": matklad@2772b2e Not sure what was the motivation for that change at that time... |
This comment has been minimized.
This comment has been minimized.
Huh. I have no memory of that. =) Good question. |
shepmaster
added
the
S-waiting-on-team
label
Apr 14, 2017
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@nikomatsakis @pnkfelix Looks like this is waiting for your approval. |
This comment has been minimized.
This comment has been minimized.
|
I have to admit, I still find this change very perplexing. It's not something I would ever expect to work: but I think I'm just an old fuddy duddy or something, too wedded to the historical evolution here. Certainly I can see the appeal of reusing the category of "block-like things". (The other reason for my discomfort is a general reluctance to go mucking about without an official reference grammar that we actually trust, but that's another story.) So, I'd be happy to r+, however, I think that would be a violation of policy. In particular, I think we should not make this change without a tracking issue and a feature-gate (we're already skipping past an RFC here, it seems). If those are added, I'd be happy to r+. |
alexcrichton
added
S-waiting-on-author
and removed
S-waiting-on-team
labels
May 25, 2017
This comment has been minimized.
This comment has been minimized.
|
ping @matklad -- any objection to adding a tracking issue and feature gate support for this? |
This comment has been minimized.
This comment has been minimized.
|
Sure, I'll try to figure out the feature-gating machinery on this weekend and update the PR! If someone can give an example of a feature-gated syntax change, that would be super helpful :) |
This comment has been minimized.
This comment has been minimized.
|
Hmm, so it's mildly tricky to do I guess. In particular, we don't know what features are enabled until we've parsed (at least) the crate attributes! I think then the best way to do it would be that we have to reflect (in the AST) whether a I am now debating if this much energy is worth it. What will we learn from the unstable period anyway? (But still it does feel like the proper procedure.) |
This comment has been minimized.
This comment has been minimized.
|
I'm going to tag as S-waiting-on-team since I feel like the lang team needs to make a decision here (specifically, seems like @pnkfelix and @nikomatsakis need to sign off). Personally, I wouldn't be too opposed to landing without a feature gate, but also kind of am worried about doing that in general. |
Mark-Simulacrum
added
S-waiting-on-team
and removed
S-waiting-on-author
labels
Jun 4, 2017
This comment has been minimized.
This comment has been minimized.
|
Sorry, looks like overestimated the amount of my free time :) I am still wanting to bring this to completion though :) I agree that this, being a change to the language, needs some sort of process to make sure it does not have any unintended consequences. I don't think that feature-gating really helps here: probably nobody is going type We can punt on this until we have a formal Rust grammar, though it does not seem likely to happen soon. Another alternative is to ask more people to look at this change (by posting a mini-RFC to urlo/irlo) and to hope that all problems would be uncovered that way. But there already was a thread on irlo about this PR, so probably everyone interested already has took a glance. |
This comment has been minimized.
This comment has been minimized.
|
Needs rebase. |
This comment has been minimized.
This comment has been minimized.
|
Will rebase shortly! |
matklad
added some commits
Apr 1, 2017
matklad
force-pushed the
matklad:comma-arms
branch
from
60d7cc9
to
9a7cb93
Jul 18, 2017
This comment has been minimized.
This comment has been minimized.
|
Ok, rebased. Local rebase happened without merge conflicts. Does this mean that the PR conflicts with something in bors's queue, which has not yet hit master? |
This comment has been minimized.
This comment has been minimized.
|
Nah, just bors being rebase-happy with submodules. @bors r=petrochenkov |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
|
@bors retry |
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Jul 18, 2017
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Thanks for the hint, @petrochenkov ! |
This comment has been minimized.
This comment has been minimized.
|
There are unintended submodule updates in 75c1dfd |
matklad
force-pushed the
matklad:comma-arms
branch
from
75c1dfd
to
6309989
Jul 18, 2017
matklad
force-pushed the
matklad:comma-arms
branch
from
6309989
to
e3d052f
Jul 18, 2017
This comment has been minimized.
This comment has been minimized.
|
@petrochenkov fixed! |
This comment has been minimized.
This comment has been minimized.
|
@bors r+ |
This comment has been minimized.
This comment has been minimized.
|
|
This comment has been minimized.
This comment has been minimized.
bors
added a commit
that referenced
this pull request
Jul 19, 2017
This comment has been minimized.
This comment has been minimized.
|
|
matklad commentedApr 1, 2017
•
edited
Original discussion: https://internals.rust-lang.org/t/syntax-of-block-like-expressions-in-match-arms/5025/7.
Currently, rust uses different rules to determine if
,is needed after an expression in a match arm and if;is needed in an expression statement:This seems weird: why would you want to require
,after andif?This PR unifies the rules. It is backwards compatible because it allows strictly more programs.