www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 602] New: Compiler allows a goto statement to skip an initalization

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=602

           Summary: Compiler allows a goto statement to skip an
                    initalization
           Product: D
           Version: 0.175
          Platform: PC
               URL: http://www.digitalmars.com/d/statement.html#GotoStatemen
                    t
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: smjg iname.com


The spec for GotoStatement states:

"It is illegal for a GotoStatement to be used to skip initializations."

However, this code compiles:

----------
import std.stdio;

void main() {
    goto qwert;

    int yuiop = 42;

qwert:
    writefln(yuiop);
}
----------
4294112
----------

The spec doesn't comment on the use of goto to skip a declaration with no
explicit initialization, but it has the same effect of bypassing the principle
that all variables in D are initialized (unless this behaviour is overridden
with a void).  In other words, this does the same:

----------
import std.stdio;

void main() {
    goto qwert;

    int yuiop;

qwert:
    writefln(yuiop);
}
----------

In both instances, a goto has been used to prevent a variable from being
initialized.  Essentially, the compiler treats the code as being equivalent to:

----------
import std.stdio;

void main() {
    int yuiop = void;

    writefln(yuiop);
}
----------


-- 
Nov 26 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=602





------- Comment #1 from deewiant gmail.com  2007-03-30 13:06 -------
Switch statements can also contain initializations prior to any case or
default. This is either a symptom of this bug, or an enhancement request. I'll
leave it here for now:

void main() {
        switch (1) {
                int x;
                case 1: assert (x == x.init);
        }
}


-- 
Mar 30 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=602


Matti Niemenmaa <matti.niemenmaa+dbugzilla iki.fi> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |anteusz freemail.hu


--- Comment #2 from Matti Niemenmaa <matti.niemenmaa+dbugzilla iki.fi>
2009-11-25 01:38:02 PST ---
*** Issue 3549 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 25 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=602


Lars T. Kyllingstad <bugzilla kyllingen.net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #3 from Lars T. Kyllingstad <bugzilla kyllingen.net> 2010-08-17
09:51:02 PDT ---
*** Issue 4667 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 17 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=602


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
            Version|0.175                       |D1 & D2
         AssignedTo|nobody puremagic.com        |bugzilla digitalmars.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 26 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=602


Kasumi Hanazuki <k.hanazuki gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |destructionator gmail.com


--- Comment #4 from Kasumi Hanazuki <k.hanazuki gmail.com> 2011-10-26 12:06:07
PDT ---
*** Issue 6683 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 26 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=602


Martin Nowak <code dawg.eu> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jlquinn optonline.net


--- Comment #5 from Martin Nowak <code dawg.eu> 2013-10-31 10:15:09 PDT ---
*** Issue 4101 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 31 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=602



--- Comment #6 from Martin Nowak <code dawg.eu> 2013-10-31 10:31:38 PDT ---
Any ideas how to implement this? It seems like an algorithm to distinguish this
is non-trivial.

I tested the following with clang and it gives me a correct warning (gcc 4.7.2
doesn't).
goto.c:3:9:warning: variable 'test' is used uninitialized whenever 'if'
condition is true [-Wsometimes-uninitialized]

int bug(int val)
{
    if (val)
        goto Lno;
    int test = 0;
 Lno: {}
    return test;
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 31 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=602



--- Comment #7 from Stewart Gordon <smjg iname.com> 2013-11-03 17:17:25 PST ---
It seems to me quite simple: If there is any variable that is in scope at the
point of the label but not at the point of the goto statement, then we have a
problem.  Is there any case that this doesn't cover?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 03 2013