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





--- Comment #5 from Don <clugd...@yahoo.com.au>  2009-07-24 00:01:29 PDT ---
(In reply to comment #4)
> (In reply to comment #3)
> > The patch I posted was incomplete, so I'm withdrawing it. Done properly, it
> > should support slicing assignment, and arrays initialized to void.
> 
> This is a nasty little bugger. Any idea when the patch will be ready?

The patch is now complete for array literals, and also fixes bug#1948 and
bug#3205. It also gives nicer error messages when it can't compile an
assignment in CTFE. There are an unbelievable number of special cases.

The only thing that's missing is that it doesn't deal with assignment from
string literals. Which means that:
char [5] s = "abc"; // fails
char [6] t = ['a', 'b', 'c']; // ok


Here's my test case. All the tests below are working on my patched DMD:

struct S {
    int x;
    char y;
}

// Functions which should fail CTFE

int badfoo(){
   S[2] c;
   c[4].x=6;  // array bounds error
   return 7;
}

int badglobal = 1;

int badfoo2(){
   S[] c;
   c[7].x=6;  // uninitialized error
   return 7;
}

int badfoo3(){
   S[2] c;
   c[badglobal].x=6;  // global index error
   return 7;
}

int badfoo4(){
   static S[2] c;
   c[0].x=6;  // Cannot access static
   return 7;
}

int badfoo5(){
   S[] c = void;
   c[0].x=6;  // c is uninitialized, and not a static array.
   return 1;
}

int badfoo6()
{
    S[] b = [S(7), S(15), S(56), S(12)];
    b[-2..4] = S(17); // exceeding (negative) array bounds
    return 1;
}

int badfoo7()
{
    S[] b = [S(7), S(15), S(56), S(12), S(67)];
    b[1..4] = [S(17), S(4)]; // slice mismatch in dynamic array
    return 1;
}

int badfoo8()
{
    S[] b; 
    b[1..3] = [S(17), S(4)]; // slice assign to uninitialized dynamic array
    return 1;
}


template Compileable(int z) { bool OK;}
static assert(!is(typeof(Compileable!(badfoo()).OK)));
static assert(!is(typeof(Compileable!(badfoo2()).OK)));
static assert(!is(typeof(Compileable!(badfoo3()).OK)));
static assert(!is(typeof(Compileable!(badfoo4()).OK)));
static assert(!is(typeof(Compileable!(badfoo5()).OK)));
static assert(!is(typeof(Compileable!(badfoo6()).OK)));
static assert(!is(typeof(Compileable!(badfoo7()).OK)));
static assert(!is(typeof(Compileable!(badfoo8()).OK)));

// Functions which should pass CTFE

int goodfoo1()
{
   int[8] w;    // use static array in CTFE
   w[]=7;       // full slice assign
   w[$-1]=538;  // use of $ in index assignment
   assert(w[6]==7);
   return w[7];
}
static assert(goodfoo1()==538);

int goodfoo2()
{
   S[4] w = S(101);  // Block-initialize array of structs
   w[$-2].x = 917; // use $ in index member assignment
   w[$-2].y = 58; // this must not clobber the prev assignment
   return w[2].x; // check we got the correct one
}
static assert(goodfoo2()==917);

int goodfoo3()
{
   S[4] w = void; // uninitialized array of structs
   w[$-2].x = 217; // initialize one member
   return w[2].x;
}
static assert(goodfoo3()==217);

int goodfoo4()
{
   S[4] b = [S(7), S(15), S(56), S(12)]; // assign from array literal
   assert(b[3]==S(12));
   return b[2].x-55;
}
static assert(goodfoo4()==1);

int goodfoo5()
{
    S[4] b = [S(7), S(15), S(56), S(12)];
    b[0..2] = [S(2),S(6)]; // slice assignment from array literal
    assert(b[3]==S(12));
    assert(b[1]==S(6));
    return b[0].x;
}
static assert(goodfoo5()==2);
static assert(goodfoo5()==2); // check for memory corruption

int goodfoo6()
{
    S[6] b = void; 
    b[2..5] = [S(2),S(6), S(17)]; // slice assign to uninitialized var
    assert(b[4]==S(17));
    return b[3].x;
}
static assert(goodfoo6()==6);

int goodfoo7()
{
    S[8] b = void; 
    b[2..5] = S(217); // slice assign to uninitialized var
    assert(b[4]==S(217));
    return b[3].x;
}
static assert(goodfoo7()==217);

int goodfoo8()
{
    S[] b = [S(7), S(15), S(56), S(12), S(67)];
    b[2..4] = S(17); // dynamic array block slice assign
    assert(b[3]==S(17));
    assert(b[4]==S(67));
    return b[0].x;
}
static assert(goodfoo8()==7);

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

Reply via email to