www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3374] New: Associative array type not inferred

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

           Summary: Associative array type not inferred
           Product: D
           Version: unspecified
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrei metalanguage.com



08:43:25 PDT ---
The error when trying to compile this snippet:

auto famousNamedConstants = 
    [ "pi" : 3.14, "e" : 2.71, "moving sofa" : 2.22 ];

is:

Error: cannot infer type from this array initializer
dmd: init.c:420: virtual Expression* ArrayInitializer::toExpression():
Assertion `0' failed.

Changing the sample to the following works:

double[string] famousNamedConstants = 
    [ "pi" : 3.14, "e" : 2.71, "moving sofa" : 2.22 ];

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 08 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3374


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
           Platform|Other                       |All
            Version|unspecified                 |2.033
         OS/Version|Linux                       |All



The ICE only occurs if it's inside a function. BTW if you change 'auto' into
enum, and
'assert' into 'static assert', it also works with the patch in place.
void bug3374()
{
   auto famousNamedConstants = 
    [ "pi" : 3.14, "e" : 2.71, "moving sofa" : 2.22 ];

    assert(famousNamedConstants["e"]==2.71);
}

There are two parts to this patch. The first part, in
ArrayInitializer::toExpression() is just for the ICE.
The ICE occurs because it's not checking for an ERROR type. (was NULL in D1).
This changes it from
ice-on-valid-code into rejects-valid. To completely fix the bug requires the
second part.

The second part, in ArrayInitializer::inferType(Scope *sc), is for the AA type
inference.
But, I had to do a semantic on the expression. Not sure if that's OK. This part
of the patch is for D1 as well.

PATCH against DMD 2.033. init.c, ArrayInitializer::toExpression(), line 407.
Index: init.c
===================================================================
--- init.c    (revision 201)
+++ init.c    (working copy)
   -404,6 +404,7   
     Type *t = NULL;
     if (type)
     {
+        if (type == Type::terror) goto Lno;
     t = type->toBasetype();
     switch (t->ty)
     {
   -542,8 +543,18   
     return type;

 Lno:
-    error(loc, "cannot infer type from this array initializer");
-    return Type::terror;
+    Initializer *iz = (Initializer *)value.data[0];
+    Expression *indexinit = (Expression *)index.data[0];
+    if (iz && indexinit)
+    {   Type *t = iz->inferType(sc);
+    indexinit = indexinit->semantic(sc);
+    Type *indext = indexinit->type;
+    t = new TypeAArray(t, indext);
+    t = t->semantic(loc, sc);
+    type = t;        
+    }
+    return type;
+    
 }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 09 2009
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3374


Walter Bright <bugzilla digitalmars.com> changed:

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



13:49:32 PDT ---
Fixed dmd 1.049 and 2.034

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