www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3984] New: CTFE array assignment for struct members fails

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

           Summary: CTFE array assignment for struct members fails
           Product: D
           Version: 2.041
          Platform: Other
        OS/Version: All
            Status: NEW
          Keywords: ice-on-valid-code, wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: ludwig informatik.uni-luebeck.de



23:44:20 PDT ---
In the following code snipped, for global variables or enums, this.s is not
correctly written in the constructor. The direct way to assign a value without
the 'dst' temporary does not work due to
http://d.puremagic.com/issues/show_bug.cgi?id=3801.

---
struct S {
    float[1] s;

    this(float x){
        float[] dst = this.s;
        dst[0] = x;
    }
}

S S_zero = S(0); // initialization fails
enum S S_zero2 = S(0); // initialization fails

void main()
{
    //enum S z = S(0); // ICE
    //static S z = S(0); // ICE
    //S z = S(0); // works, initialized with 0
    //S z = S_zero; // contains NaN
    S z = S_zero2; // contains NaN

    assert(z.s[0] == 0);
}
---

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |clugdbug yahoo.com.au



Most of the issues described here are duplicates of bug 1330, or bug 3801.
The segfault is the only new bug here. Here's a reduced test case:

struct Segfault3984 {
    int a;
    this(int x){
      a = x;
    }
}

void bug3984(){
    static assert(Segfault3984(3).a == 3);
}


Root cause:
Struct constructors can result in a situation where a CTFE variable is created
outside of a CTFE function. They need a context to put the variable into.
Effectively, the comma expression acts a very simple function.

PATCH:

Interpret.c line 2641

Expression *CommaExp::interpret(InterState *istate)
{
#if LOG
    printf("CommaExp::interpret() %s\n", toChars());
#endif
    // If the comma returns a temporary variable, it needs to be an lvalue
    // (this is particularly important for struct constructors)
    if (e1->op == TOKdeclaration && e2->op == TOKvar 
       && ((DeclarationExp *)e1)->declaration == ((VarExp*)e2)->var)
    {
+    // If there's no context for the variable to be created in,
+    // we need to create one now.
+    InterState istateComma;
+    if (!istate)
+        istate = &istateComma;

    VarExp* ve = (VarExp *)e2;
    VarDeclaration *v = ve->var->isVarDeclaration();
    if (!v->init && !v->value)
        v->value = v->type->defaultInitLiteral();

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


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com



12:40:45 PDT ---
changeset 423

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


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED



Fixed DMD1.058 and 2.043.

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