Bug 5799 – Address-of operator fails on nested conditional operator expression

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-03-30T13:43:00Z
Last change time
2011-08-13T14:58:04Z
Keywords
patch, rejects-valid
Assigned to
nobody
Creator
timon.gehr

Comments

Comment #0 by timon.gehr — 2011-03-30T13:43:22Z
The following (perfectly valid) D code is rejected by dmd: int main(){ int a; int *u=&(a ? a : (a ? a : a)); return 0; } Error Message: minimal.d(3): Error: incompatible types for ((&a) ? (&*(a ? &a : &a))): 'int*' and 'int**' minimal.d(3): Error: cannot implicitly convert expression (a ? __error : (__error)) of type int* to int* This is nonsense, clearly, the expression (a ? a : (a ? a : a)) evaluates to a valid int lvalue, therefore it can have the Address-of operator applied to it. (For comparison: the following code compiles: int main(){ int a; (a ? a : (a ? a : a))=a; return 0; })
Comment #1 by timon.gehr — 2011-04-18T16:00:30Z
I had a look at the DMD source code and I identified the problem: expression.c (1326): Expression *Expression::addressOf(Scope *sc){ Expression *e; //printf("Expression::addressOf()\n"); e = toLvalue(sc, NULL); e = new AddrExp(loc, e); e->type = type->pointerTo(); return e; } Note how the instruction e->type = type->pointerTo(); is dependent on the fact that method toLvalue does not change the type of the expression. However, the current Implementation of CondExp::toLvalue changes the object while creating an Lvalue. Disaster strikes because CondExp::toLvalue calls addressOf on it's two subexpressions. If one or both of them are CondExp, e->type may be incorrect. The reported bug is an instance of this one. This can be easily resolved by operating on a copy of the CondExp object in CondExp::toLvalue instead of on the original object. Suggested fix: Replace the current implementation of CondExp::toLvalue in expression.c (11140) - Expression *CondExp::toLvalue(Scope *sc, Expression *ex) - { - PtrExp *e; - - // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) - e = new PtrExp(loc, this, type); - - e1 = e1->addressOf(sc); - //e1 = e1->toLvalue(sc, NULL); - - e2 = e2->addressOf(sc); - //e2 = e2->toLvalue(sc, NULL); - - typeCombine(sc); - - type = e2->type; - return e; - } With this one: + Expression *CondExp::toLvalue(Scope *sc, Expression *ex) + { + CondExp *e = (CondExp*)copy(); + + // convert (econd ? e1 : e2) to *(econd ? &e1 : &e2) + e->e1 = e->e1->addressOf(sc); + e->e2 = e->e2->addressOf(sc); + + e->typeCombine(sc); + + e->type = e->e2->type; + return new PtrExp(loc, e, type); + }
Comment #2 by k.hara.pg — 2011-07-07T23:38:21Z
Comment #3 by bugzilla — 2011-08-13T14:58:04Z