[Issue 19572] New: std.array.Appender.put invokes struct constructor via cast

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Jan 10 14:27:48 UTC 2019


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

          Issue ID: 19572
           Summary: std.array.Appender.put invokes struct constructor via
                    cast
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: default_357-line at yahoo.de

Repro:

struct Struct
{
    private int key_;

    public int fun() const { return 23; }

    alias fun this;
}

void main()
{
    import std.array : Appender;

    Appender!(Struct[]) appender;

    appender.put(const(Struct)(42));

    auto result = appender.data[0];

    assert(result.key_ != 23);
}

What happens is that Appender checks that it can convert const(Struct)
implicitly to Struct, but it does so via cast(Struct), which then invokes
Struct(const(Struct).fun) implicitly.

Appender should cast() its parameter, then rely on implicit conversion to do
the rest.

--


More information about the Digitalmars-d-bugs mailing list