www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5799] New: Address-of operator fails on nested conditional operator expression

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

           Summary: Address-of operator fails on nested conditional
                    operator expression
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: timon.gehr gmx.ch



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;
})

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


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
           Priority|P2                          |P3



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);
+ }

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


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2


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


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |k.hara.pg gmail.com



https://github.com/D-Programming-Language/dmd/pull/215

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 07 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5799


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED



14:58:04 PDT ---
https://github.com/D-Programming-Language/dmd/commit/191125f5ba2d621cf7e21e124b1609c89681e425

https://github.com/D-Programming-Language/dmd/pull/215

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