D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2564 - CTFE: the index in a tuple foreach is uninitialized (bogus error)
Summary: CTFE: the index in a tuple foreach is uninitialized (bogus error)
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: patch, rejects-valid
Depends on:
Blocks:
 
Reported: 2009-01-07 14:05 UTC by Don
Modified: 2015-06-09 01:21 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Don 2009-01-07 14:05:32 UTC
int foo(E...)(string[2] s)
{
    foreach(int i,T; E) {
        auto z = s[i].dup;
    }
    return 0;
}

static x = foo!(int, int)(["x", "y"]);
---
bug.d(4): Error: variable i is used before initialization
bug.d(9): Error: cannot evaluate foo(&["x","y"]) at compile ti
me
bug.d(9): Error: cannot evaluate foo(&["x","y"]) at compile ti
me
---
A workaround:
    foreach(int j,T; E) {
       int i=j;
       ...
    }
Does not happen in D1.039
Comment 1 Don 2009-08-25 00:15:16 UTC
Actually it's nothing to do with foreach. Reduced test case shows it is use of 'enum' manifest constants. They're not getting constant-folded correctly.

int bug2564()
{
    enum int Q=0;
    string [2] s = ["a", "b"];    
    assert(s[Q].dup=="a");
    return 0;
}

static int bug2564b = bug2564();

PATCH:
In interpret.c, getVarExp.
-	if ((v->isConst() || v->isInvariant()) && v->init 
+	if ((v->isConst() || v->isInvariant() || v->storage_class & STCmanifest) && v->init
Comment 2 Walter Bright 2009-09-03 13:34:33 UTC
Fixed dmd 2.032