After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 585292 - static const's dynamically generated cannot be used as case labels
static const's dynamically generated cannot be used as case labels
Status: RESOLVED DUPLICATE of bug 575628
Product: vala
Classification: Core
Component: Code Generator
0.7.x
Other All
: Normal normal
: ---
Assigned To: Jürg Billeter
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2009-06-09 22:08 UTC by Jim Nelson
Modified: 2009-06-26 06:27 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Jim Nelson 2009-06-09 22:08:08 UTC
Please describe the problem:
If a static const variable is generated at initialization time via code, it can't be used as a case label as expected.  It appears this is because the .c code is a macro, so the case label expands to a line of code.

Steps to reproduce:

static const int ONE = "1".to_int();
static const int TWO = "2".to_int();

void main() {
    int foo = 1;
    switch (foo) {
        case ONE:
            stdout.printf("one\n");
        break;
        
        case TWO:
            stdout.printf("two\n");
        break;
        
        default:
            stdout.printf("unknown\n");
        break;
    }
}

Note that because of the macro technique, this *does* work in place of the switch block:

    if (foo == ONE) {
        stdout.printf("one\n");
    } else if (foo == TWO) {
        stdout.printf("two\n");
    } else {
        stdout.printf("unknown\n");
    }


Actual results:
The above code produces the compiler error "case label does not reduce to an integer constant".

Expected results:


Does this happen every time?


Other information:
Comment 1 Jürg Billeter 2009-06-26 06:27:37 UTC
The constant initializer in your example is invalid, only constant expressions are supported.
Comment 2 Jürg Billeter 2009-06-26 06:27:49 UTC
Thanks for the bug report. This particular bug has already been reported into our bug tracking system, but please feel free to report any further bugs you find.


*** This bug has been marked as a duplicate of 575628 ***