Hi! I’ve noticed that rustc treats block-like expressions differently for determining if the ; or , is needed:
fn stmt() {
# no need for semicolons
{ () }
if true { () } else { () }
loop {}
while true {}
}
fn match_arm(n: i32) {
match n {
1 => { () } # can omit comma here
2 => if true { () } else { () }, # but all other cases do need semicolons.
3 => loop { },
4 => while true {},
_ => ()
}
}
This seems just weird to me… Is there a reason you really need , after an if in match? Perhaps this is a bug that needs to be fixed?
I think it makes sense to make these consistent. I’ve definitely found the need for a comma after an inner match annoying (you missed this one but its the same).
I came here to figure out what conflicts this would introduce from a parsing perspective and, armed with an LALR grammar I recently wrote, I tried loosening the need for a comma after block-like expressions (if, loop, for, while, match, macros with braces, and unsafe blocks) to see what conflicts that would introduce. To my surprise, it didn’t introduce any. Of course, it could be my grammar that is broken.
Naturally, we still do need commas after struct literals even if they end in a block…
if true { 1 } else { 2 } &3isn’t a block-like expression, so it isn’t even in the running. &3 cannot be anything but a pattern.
The problem you are describing is an ambiguity that one might see happening in expression statements. To deal with that Rust expects the LHS of a binary expression to not be block-like for it to be turned into a statement.
In any case, this poses no problems in match blocks.