Bug 22619 – Missing inout substitution for __copytmp temporaries caused by copy ctors

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-12-21T17:12:42Z
Last change time
2022-01-21T23:27:50Z
Keywords
industry
Assigned to
No Owner
Creator
kinke

Comments

Comment #0 by kinke — 2021-12-21T17:12:42Z
This newly fails to compile with DMD v2.098.1, but works with 2.098.0: ``` import std.typecons : Nullable, nullable; struct Module { Nullable!string name; } Nullable!(const(Module)) findModule(const ref Module mod) { return nullable(mod); } ``` Error: ``` std/typecons.d(3100): Error: variable `std.typecons.nullable!(const(Module)).nullable.__copytmp15` `inout` variables can only be declared inside `inout` functions bug.d(8): Error: template instance `std.typecons.nullable!(const(Module))` error instantiating ``` The copy ctor was added in https://github.com/dlang/phobos/pull/8318.
Comment #1 by robertschadek — 2022-01-06T13:57:41Z
In 2.098.1 I get the following as well. ```dlang Nullable!(T) mapHelper(T)(T[] data, long id) { auto f = data.find!(it => it.personId == id); if(f.empty) { return Nullable!(T).init; } else { return nullable(f.front); // failing line } } ``` file.d(6): Error: variable `rest.excel.mapHelper!(SomeStruct).mapHelper.__copytmp12381` `inout` variables can only be declared inside `inout` functions SomeStruct is just that, some struct. It compiles and works if I rewrite mapHelper ti ```dlang Nullable!(T) mapHelper(T)(T[] data, long id) { auto f = data.find!(it => it.personId == id); if(f.empty) { return Nullable!(T).init; } else { T ret = f.front; Nullable!(T) tmp; tmp = ret; return tmp; } } ``` This required a 160 line patch today for work.
Comment #2 by elronnd — 2022-01-08T09:35:57Z
Golfed: union Wrapper(T) { T x; this(inout T value) { this.x = value; } this(ref scope inout Wrapper!T rhs) inout { this.x = rhs.x; } } void f(const ref Wrapper!string x) { auto r = Wrapper!(typeof(x))(x); }
Comment #3 by elronnd — 2022-01-10T11:07:17Z
Further reduced: struct W1 { int x; this(ref inout W1 rhs) inout { this.x = rhs.x; } } inout(W1) f(inout W1 x) { return x; } void g(W1 x) { auto r = f(x); } Something is going wrong around expressionsem.d:2443 (the store to tret is clearly dead).
Comment #4 by dlang-bot — 2022-01-10T15:50:44Z
@kinke created dlang/phobos pull request #8358 "Work around issue 22619 - Avoid Nullable copy ctor unless required" mentioning this issue: - Work around issue 22619 - Avoid Nullable copy ctor unless required Copy ctors are still buggy, so unconditionally adding one for Nullable is everything but a non-breaking change (and was added in the 2.098.1 point release). https://github.com/dlang/phobos/pull/8358
Comment #5 by dlang-bot — 2022-01-11T08:58:00Z
dlang/phobos pull request #8358 "Work around issue 22619 - Avoid Nullable copy ctor unless required" was merged into stable: - 9db3a9afdf07263aefdc0e3f04dc40ef3969e3cc by Martin Kinkelin: Work around issue 22619 - Avoid Nullable copy ctor unless required Copy ctors are still buggy, so unconditionally adding one for Nullable is everything but a non-breaking change (and was added in the 2.098.1 point release). https://github.com/dlang/phobos/pull/8358
Comment #6 by kinke — 2022-01-11T09:29:59Z
[Phobos' Nullable is now usable again, so this issue is now about the underlying compiler bug.]
Comment #7 by kinke — 2022-01-12T13:54:29Z
*** Issue 22667 has been marked as a duplicate of this issue. ***
Comment #8 by dlang-bot — 2022-01-21T23:27:50Z
dlang/dmd pull request #13543 "Fix Issue 22619 - Missing inout substitution for __copytmp temporaries caused by copy ctors" was merged into master: - 225103c0e70355c796c607f232d83bcc127e57f5 by RazvanN7: Fix Issue 22619 - Missing inout substitution for __copytmp temporaries caused by copy ctors https://github.com/dlang/dmd/pull/13543