Thread overview
[Issue 17461] Bad codegen: compiler emit's call to destructor for uninitialised temporary
Jun 02, 2017
Manu
Jun 02, 2017
Vladimir Panteleev
Jun 02, 2017
ag0aep6g@gmail.com
Jun 02, 2017
ag0aep6g@gmail.com
Jun 03, 2017
ZombineDev
Mar 23, 2018
Manu
Nov 24, 2019
Basile-z
Mar 21, 2020
Basile-z
June 02, 2017
https://issues.dlang.org/show_bug.cgi?id=17461

--- Comment #1 from Manu <turkeyman@gmail.com> ---
For clarity; the high-level flow of the disassembly is:

void t()
{
  B __tmpfordtor1804 = u1();

  B __tmpfordtor1805 = void; // uninitialised

  if (__tmpfordtor1804)
  {
    __tmpfordtor1805 = u1(); // initialised here

    A(__tmpfordtor1805);
  }
  else
  {
    A(u2);
  }

  // __tmpfordtor1805 destructs, may not have been initialised!
  __tmpfordtor1805.~this();

  // __tmpfordtor1804 destructs, no problem
  __tmpfordtor1804.~this();
}

--
June 02, 2017
https://issues.dlang.org/show_bug.cgi?id=17461

Vladimir Panteleev <thecybershadow@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code

--
June 02, 2017
https://issues.dlang.org/show_bug.cgi?id=17461

uplink.coder@googlemail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |uplink.coder@googlemail.com

--- Comment #2 from uplink.coder@googlemail.com ---
Manu this code is amazing.
with -O -inline it crashes in the dwarf-emission.

--
June 02, 2017
https://issues.dlang.org/show_bug.cgi?id=17461

--- Comment #3 from uplink.coder@googlemail.com ---
Please check if the bug is still present in 2.075 ~master. I cannot reproduce it.

--
June 02, 2017
https://issues.dlang.org/show_bug.cgi?id=17461

ag0aep6g@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ag0aep6g@gmail.com

--- Comment #4 from ag0aep6g@gmail.com ---
(In reply to uplink.coder from comment #2)
> with -O -inline it crashes in the dwarf-emission.

For that crash the code can be reduced to this:

----
void t()
{
  auto a = A(false ? B().p : null);
}

struct A
{
  void* p;
}

struct B
{
  void* p;
  ~this() {}
}
----

`dmd -c -O test.d` crashes with "Internal error: ddmd/backend/dwarfeh.c 205".

--
June 02, 2017
https://issues.dlang.org/show_bug.cgi?id=17461

--- Comment #5 from ag0aep6g@gmail.com ---
(In reply to uplink.coder from comment #3)
> Please check if the bug is still present in 2.075 ~master. I cannot reproduce it.

Here's a reduction of the original code, tweaked to make an assert fail:

----
void t()
{
  auto a = A(B().p ? B() : B());
}

struct A
{
  int p;
}

struct B
{
  int p = 42;
  alias p this;

  ~this()
  {
    import std.conv: text;
    assert(p == 42, text(p)); /* fails; prints "1234567890" */
  }
}

void main()
{
  stomp();
  t();
}

void stomp() { int[5] stomper = 1234567890; }
----

Tested with dmd v2.075.0-devel-7326893 on Linux.

--
June 03, 2017
https://issues.dlang.org/show_bug.cgi?id=17461

ZombineDev <petar.p.kirov@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |petar.p.kirov@gmail.com

--
March 23, 2018
https://issues.dlang.org/show_bug.cgi?id=17461

Manu <turkeyman@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |industry

--
November 24, 2019
https://issues.dlang.org/show_bug.cgi?id=17461

Basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |b2.temp@gmx.com
         Resolution|---                         |WORKSFORME

--- Comment #6 from Basile-z <b2.temp@gmx.com> ---
pass since 2.084. I'll open a new issue for the ICE with -O

--
March 21, 2020
https://issues.dlang.org/show_bug.cgi?id=17461

Basile-z <b2.temp@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|b2.temp@gmx.com             |

--