The Rust Reference, under Expression statements, states:
An expression that consists of only a block expression or control flow expression, if used in a context where a statement is permitted, can omit the trailing semicolon. This can cause an ambiguity between it being parsed as a standalone statement and as a part of another expression; in this case, it is parsed as a statement. The type of ExpressionWithBlock expressions when used as statements must be the unit type.
I appreciate that, however one chooses to resolve grammatical ambiguities like this, there will always be some edge cases that cause paper-cuts. In this case, the above leads to situations like that raised yesterday in #98093:
fn test() -> bool {
unsafe { std::str::from_utf8_unchecked(&[65]) } == "A"
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// parsed as ExpressionWithBlock statement, expecting unit type
//
// ^^^^^^ syntax error
}
It occurs to me that Rust could potentially modify the above rule thusly:
An expression that consists of only a block expression or control flow expression, if used in a context where a statement is permitted, can omit the trailing semicolon. This can cause an ambiguity between it being parsed as a standalone statement and as a part of another expression; in this case, it is parsed as a statement — unless it is immediately followed by an infix operator, in which case it is parsed as that operator's left-hand operand. The type of ExpressionWithBlock expressions when used as statements must be the unit type.
Of course, this would then raise different ambiguities in the case of tokens which could be either prefix operators or infix operators: namely &
(borrow/and), *
(dereference/multiply) and -
(negate/subtract): it would be necessary to resolve those ambiguities in favour of them being prefix operators so that currently valid code is not parsed any differently to the status quo.
I think such a change should therefore only result in some programs that are (surprisingly to some) currently rejected then being accepted.
I've searched around for previous discussions but unfortunately was only able to unearth:
- a very early rust-dev thread that's only really relevant for historical interest; and
- an RLIO topic from last year that discussed the purpose of having semicolons at all.
Is this something that has been discussed before but I've not been able to find it? If not, is such a grammatical change viable? I would be happy to work on an RFC if it could potentially be well received (albeit I'd also be grateful if someone is willing to help steer me through the process, having not proposed one before).