Optional curly brackets

As one of the maintainers of the parser and resident invalid code recovery experts, my face contorted while reading this proposal and thinking about the fallout from it.

There's nothing stopping us from making the parser recover and provide structured suggestions when code like this is written. There's nothing stopping rust-analyzer from auto-applying the surrounding braces around the code when the confidence is high. But I would put my foot down (not that it could realistically stop any popular proposal) on complicating the grammar because it makes more code valid, which makes inferring intent when people write invalid code much harder. The optional braces for if statements in other languages have been the source of high profile bugs (heartbleed goto-fail comes to mind), and others in this thread have already explained some of the rationales behind not having this feature, but I wanted to also raise the error messaging and recovery point. It's the same reason that we can tell you "you forgot a ; here" with high confidence, but still make you write it: a language with automatic semicolon insertion comes with a whole host of caveats and edge-cases, whereas suggestions can be "fast-and-loose" on what they tell you ("you might have forgotten a ; here, but maybe you meant something else, change your code to make it easier for me, feeble computer, to understand").

31 Likes