[Issue 16652] New: returned rvalue gets destroyed before expressions ends

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Oct 31 17:57:44 PDT 2016


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

          Issue ID: 16652
           Summary: returned rvalue gets destroyed before expressions ends
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: code at dawg.eu

cat > bug.d << CODE
import std.stdio;

struct Vector
{
    ubyte[] opSlice()
    {
        writeln("opslice");
        return buf[];
    }

    ~this()
    {
        writeln("dtor");
    }

    ubyte[4] buf;
}

void bar(ubyte[])
{
    writeln("bar");
}

void main()
{
    bar(Vector()[]);
}
CODE
dmd -inline -run bug
---
opslice
dtor <- !!! destroyed
bar <- stale reference
---

The order of evaluation changes when -inline is passed to the compiler. With
that the destructor runs before the function call finishes, thus possibly
passing a stale reference.

Also see https://github.com/rejectedsoftware/vibe.d/pull/1578 and
https://github.com/etcimon/botan/issues/23.

--


More information about the Digitalmars-d-bugs mailing list