I'm in the middle of a project right now so will be brief and can add more later. Given code like such: ==== auto somethingResult = getSomething(); if(somethingResult.empty) goto dont_have_something; auto something = somethingResult.front; dont_have_something: if(something !is null) { // potential problem here if the result was empty - "something" doesn't refer to what I thought it would! } ======= The goto over the variable declaration probably should have been a compile error, I believe, from the spec. This was kinda a pain to find when it started crashing since the variable actually referred to an entirely different object! I'm using last month's dmd too, so possible it's already been fixed. I just want to post something here before I forget about it.
The spec says "It is illegal for a GotoStatement to be used to skip initializations.". ( http://www.d-programming-language.org/statement.html#GotoStatement ) According to the glossary (http://www.d-programming-language.org/glossary.html), "A code construct is illegal if it does not conform to the D language specification. This may be true even if the compiler or runtime fails to detect the error." So the compiler is free to ignore illegal code. A future compiler, potentially not dmd, may indeed flag it as a compile-time error, so any behavior caused by illegal code should be regarded as unpredictable and unreliable.
Is that saying "bugs in the compiler don't override the spec" or "goto over initializations at your own risk"? If it's the latter, this isn't a bug at all. (I can see how the compiler throwing an error on this could get annoying, so I suppose it could go both ways.)
It's the latter. Not only do you run the risk of your variables being garbage, but your code might not compile on a future compiler. (not that this has ever *not* been a problem with D, mind) The compiler is free to issue an error or not, but the code is definitely wrong.
*** This issue has been marked as a duplicate of issue 602 ***
(In reply to comment #1) > The spec says "It is illegal for a GotoStatement to be used to skip > initializations.". ( > http://www.d-programming-language.org/statement.html#GotoStatement ) > > According to the glossary > (http://www.d-programming-language.org/glossary.html), "A code construct is > illegal if it does not conform to the D language specification. This may be > true even if the compiler or runtime fails to detect the error." For all I know, that probably means "This may be true even if some compiler, because of a bug in it, fails to detect the error." > So the compiler is free to ignore illegal code. Which is the bit of illegal code - the goto statement or the declaration it skips? In my experience, the compiler doesn't ignore either. Moreover, why does the spec bother to forbid anything if compiler writers are free to ignore it all? > A future compiler, potentially > not dmd, may indeed flag it as a compile-time error, so any behavior caused by > illegal code should be regarded as unpredictable and unreliable. So you're claiming all accepts-invalid bugs are really enhancement requests?
(In reply to comment #5) > Moreover, why does the spec bother to forbid anything if compiler writers are > free to ignore it all? Specs are meant to be long-term things. When you write the specs for a language as complex as D you are free to write things that your current working compiler is not yet able to do, or you have not yet implemented. There are several things written in D specs (or TDPL) that DMD is not yet doing.