Bug 6683 – Skipping declaration with goto resulted in nonsense
Status
RESOLVED
Resolution
DUPLICATE
Severity
minor
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2011-09-16T16:53:00Z
Last change time
2015-06-09T05:14:44Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
destructionator
Comments
Comment #0 by destructionator — 2011-09-16T16:53:35Z
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.
Comment #1 by simen.kjaras — 2011-09-16T17:23:19Z
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.
Comment #2 by destructionator — 2011-09-16T17:38:52Z
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.)
Comment #3 by simen.kjaras — 2011-09-16T17:47:46Z
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.
Comment #4 by k.hanazuki — 2011-10-26T12:06:07Z
*** This issue has been marked as a duplicate of issue 602 ***
Comment #5 by smjg — 2011-10-26T14:11:20Z
(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?
Comment #6 by bearophile_hugs — 2011-10-26T15:10:50Z
(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.