D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6337 - [CTFE] ICE when touching member variable of struct during CTFE
Summary: [CTFE] ICE when touching member variable of struct during CTFE
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: wrong-code
: 6323 6390 (view as issue list)
Depends on:
Blocks:
 
Reported: 2011-07-16 20:37 UTC by Johann MacDonagh
Modified: 2011-09-06 00:56 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Johann MacDonagh 2011-07-16 20:37:26 UTC
This one is weird. I narrowed it down to this test case, although I admit this is still pretty long.

struct Test
{
    string s;
    
    this(string s)
    {
        this.s = s;
    }
    
    string ctfe()
    {
        dchar c = s[0];

        if (test1(c))
        {
            for (c = next(c); test2(c); c = next(c))
            {
            }
        }
        
        return "ctfe";
    }
    
    dchar next(dchar curPeek)
    {
        if (curPeek != dchar.init)
        {
            s = s[1..$];
            
            return s.length > 0 ? s[0] : dchar.init;
        }
        
        return dchar.init;
    }
}

bool test1(dchar c)
{
    return (c >= 'a' && c <= 'z' ||
            c >= 'A' && c <= 'Z' ||
            c == '_');
}

bool test2(dchar c)
{
    return (test1(c) ||
            c >= '0' && c <= '9' );
}

void main(string[] args)
{
    // If this is auto then this executes just fine at runtime
    enum x = Test("abc").ctfe();
}

This will ICE with an out of memory error. I believe it's going into an infinite loop.

If you replace the body of test2 with "return false", then CTFE runs just fine.

This only happens on 2.054. On 2.053 (when you make appropriate modifications to the test case) it crashes DMD.
Comment 1 Don 2011-07-22 03:16:07 UTC
Reduced test case:

struct Bug6337
{
    int k;
    void six() {
        k = 6;
    }
    int ctfe()
    {
        six();
        return k;
    }
}
static assert( Bug6337().ctfe() == 6);
Comment 3 Don 2011-07-28 00:04:06 UTC
*** Issue 6390 has been marked as a duplicate of this issue. ***
Comment 4 Don 2011-09-06 00:56:59 UTC
*** Issue 6323 has been marked as a duplicate of this issue. ***