www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18078] New: [CTFE] wrong initialization of array member with

https://issues.dlang.org/show_bug.cgi?id=18078

          Issue ID: 18078
           Summary: [CTFE] wrong initialization of array member with inout
                    opIndex
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: simon.buerger rwth-aachen.de

In the following example, CTFE does not correctly initialize the data field.
Note that the bug disappears if either
- 'inout' is removed or
- 'Complex!double' is replaced by 'double' or
- 'data' is changed from 'Complex!double[1]' to 'Complex!double'


import std.complex;
import std.stdio;

struct Foo
{
        Complex!double[1] data;

        ref inout(Complex!double) opIndex(size_t i) inout
        {
                return data[i];
        }

        this(Complex!double a)
        {
                this[0] = a;
        }
}

void main()
{
        enum x = Foo(Complex!double(0));
        const y = Foo(Complex!double(0));
        writefln("%s", x.data[0]); // prints 'nan+nani' (incorrect)
        writefln("%s", y.data[0]); // prints '0.0i' (correct)
}

--
Dec 14 2017