[Issue 4667] New: Goto skipping variable initializations

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Aug 17 07:49:09 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4667

           Summary: Goto skipping variable initializations
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: diagnostic
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-08-17 07:49:03 PDT ---
This D2 code runs with no errors (DMD 2.048):

import std.c.stdio: printf;
void main(string[] args) {
    if (args.length < 2)
        goto FOO1;
    int x = 100;
  FOO1:
    printf("%d\n", x);

    goto FOO2;
    for (int i = 0; i < 10; i++) {
        FOO2:
        printf("%d\n", i);
        break;
    }
}



But that's not good code, the initialization of i is always skipped, and the
initialization of x is sometimes skipped.

bernardh in IRC #D quotes from TDPL:
"another restriction is that a goto cannot skip the definition point of a value
that's visible at the landing point."

See also bug 3820


Similar code in C99:

#include "stdio.h"
int main(int argc, const char* argv[]) {
    if (argc < 2)
        goto FOO1;
    int x = 100;
  FOO1:
    printf("%d\n", x);

    goto FOO2;
    for (int i = 0; i < 10; i++) {
      FOO2:
        printf("%d\n", i);
        break;
    }

    return 0;
}


Compiled with GCC 4.5.0 shows a warning:

...>gcc -Wall -std=c99 -m32 test.c -o test
test.c: In function 'main':
test.c:13:15: warning: 'i' is used uninitialized in this function

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


More information about the Digitalmars-d-bugs mailing list