[Issue 2931] Initialization struct with array from another struct

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Jul 28 05:35:00 PDT 2010


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


Don <clugdbug at yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #1 from Don <clugdbug at yahoo.com.au> 2010-07-28 05:34:56 PDT ---
Here's a reduced test case. The fact that the static assert passes shows that
it's not a front-end bug. The bug only happens when an array is
block-initialized.
--------
struct Bug2931 {
        int val[3][4];
}

struct Outer2931 {
        Bug2931 p = Bug2931(67);  // Applies to struct static initializers too
        int zoom = 2;
        int move = 3;
        int scale = 4;
}

int bug2931()
{
  Outer2931 v;
  assert(v.move==3);
  return v.zoom;
}

static assert(bug2931()==2);

void main() {
    assert(bug2931()==2);
}

CAUSE:
todt.c, StructLiteralExp::toDt() contains code which seems to be completely
erroneous. It duplicates the array initializer multiple times. The test suite
still passes when it is removed.

PATCH: todt.c, Line 690, in svn 589.

            if (v->offset < offset)
                error("duplicate union initialization for %s", v->toChars());
            else
            {   unsigned sz = dt_size(d);
                unsigned vsz = v->type->size();
                unsigned voffset = v->offset;
                assert(sz <= vsz);
-                unsigned dim = 1;
-                for (Type *vt = v->type->toBasetype();
-                     vt->ty == Tsarray;
-                     vt = vt->nextOf()->toBasetype())
-                {   TypeSArray *tsa = (TypeSArray *)vt;
-                    dim *= tsa->dim->toInteger();
-                }
-                for (size_t i = 0; i < dim; i++)
-                {
                    if (offset < voffset)
                        pdt = dtnzeros(pdt, voffset - offset);
                    if (!d)
                    {
                        if (v->init)
                            d = v->init->toDt();
                        else
                            v->type->toDt(&d);
                    }
                    pdt = dtcat(pdt, d);
                    d = NULL;
                    offset = voffset + sz;
                    voffset += vsz / dim;
                    if (sz == vsz)
                        break;
-                }

-- 
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