D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7449 - Algebraic's operator[] is incorrect
Summary: Algebraic's operator[] is incorrect
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-02-05 20:26 UTC by Andrei Alexandrescu
Modified: 2020-03-21 03:56 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2012-02-05 20:26:58 UTC
import std.variant;
import std.stdio;

void main() {
    int[string] o;
    o["foo"] = 42;

    Variant a = o;
    Algebraic!(int[string], string) b = o;

    writeln(a["foo"]);
    writeln(b["foo"]);
}

The last line fails at runtime with an exception.
Comment 1 Andrei Alexandrescu 2012-02-05 20:27:44 UTC
Hans Fugal identified this blames to revision 3a1f9109, this snippet:
@@ -313,20 +364,23 @@ private:
 
         case OpID.index:
             auto me = cast(A*) pStore;
-            static if (isArray!(A))
+            // Added allowed!(...) prompted by a bug report by Chris
+            // Nicholson-Sauls.
+            static if (isArray!(A) && allowed!(typeof(A.init[0])))
             {
                 // array type; input and output are the same VariantN 
                 auto result = cast(VariantN*) parm;
                 size_t index = result.convertsTo!(int)
                     ? result.get!(int) : result.get!(size_t);
                 *result = (*me)[index];
-               break;
+                break;
             }
-            else static if (isAssociativeArray!(A))
+            else static if (isAssociativeArray!(A)
+                    && allowed!(typeof(A.init.values[0])))
             {
                 auto result = cast(VariantN*) parm;
                 *result = (*me)[result.get!(typeof(A.keys[0]))];
-               break;
+                break;
             }
             else
             {
Comment 2 Dicebot 2014-08-23 01:51:52 UTC
*** Issue 13354 has been marked as a duplicate of this issue. ***
Comment 3 basile-z 2015-12-07 01:56:42 UTC
a dup of this one is resolved fixed.