[Issue 2702] Struct initialisation silently inserts deadly casts

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Aug 7 01:16:34 PDT 2009


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

           What    |Removed                     |Added
           Keywords|                            |accepts-invalid, patch
            Version|2.025                       |1.040
            Summary|Declaring struct, assigning |Struct initialisation
                   |rvalue via opAssign in same |silently inserts deadly
                   |statement fails silently    |casts

--- Comment #2 from Don <clugdbug at yahoo.com.au>  2009-08-07 01:16:33 PDT ---
Actually there is a bug:
Bar bar = 1;
should not be accepted.

Here's a really frightening case:

struct A { char [] a; }
struct B { long x; }

void main() {
   B s; 
   A q = s;
The cause is in VarDeclaration::semantic in declaration.c, line 1083 (DMD
2.031), in the section dealing with initialisation of a struct. The code is:

            if (!ei->exp->implicitConvTo(type))
            {    Type *ti = ei->exp->type->toBasetype();
            // Don't cast away invariant or mutability in initializer
            if (!(ti->ty == Tstruct && t->toDsymbol(sc) == ti->toDsymbol(sc)))
                ei->exp = new CastExp(loc, ei->exp, type);

and in D1, it's just (line 1074):
    if (!ei->exp->implicitConvTo(type))
    ei->exp = new CastExp(loc, ei->exp, type);

If it can't be implicitly converted to a struct, why the heck should an
explicit cast be inserted??? 
PATCH(D1): Delete those 2 lines.
On D2, we need the same logic as for assignment.

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