without -unittest switch, this compiles:
```d
unittest anything goes here, even open parentheses without matching ones: (((((
{
stuff in here can be complete garbage as well???!!!
}
```
The grammar states that Unittest should be:
```
Unittest:
unittest BlockStatement
```
So really, nothing but whitespace should be allowed between the `unittest` keyword and the opening brace, and the block statement should be valid D grammar.
I get that the unittest itself can be skipped, and while I can understand a small benefit from avoiding the parsing of the function itself, I don't believe there's any other place where unparseable text is allowed. Even with templates that aren't instantiated, or inside version(none) blocks.
I believe the code above should not compile, even when -unittest is not specified.
Comment #1 by dkorpel — 2023-08-08T12:24:57Z
unittests are not being parsed without -unittest by design
> I believe the code above should not compile, even when -unittest is not specified.
Why?
Comment #2 by schveiguy — 2023-08-08T18:42:43Z
> Why?
The grammar specifies "BlockStatement", not "GarbageWithSomeBraceMatching"
The result is surprising to say the least. I don't know what is saved by doing an alternate grammar parsing, but I doubt it's worth it. This basically says that the D compiler is not a valid parser of the D grammar unless the -unittest switch is enabled.
A good test would be to parse (but not compile) std.datetime, which is full of unittests, and compare the performance (memory and speed) and see if it's worth it.
Would it be really bad to at least expect an opening brace as the next token? That shouldn't be too expensive. I'm not sure how much parser state can be kept without generating AST to validate the grammar, but it just feels wrong to have invalid syntax pass the parser.
Comment #3 by robert.schadek — 2024-12-13T19:30:12Z