Bug 22077 – `std.sumtype` support for copy constructors is incomplete

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-06-23T03:50:11Z
Last change time
2021-08-12T03:23:18Z
Keywords
pull
Assigned to
No Owner
Creator
Mathias LANG

Comments

Comment #0 by pro.mathias.lang — 2021-06-23T03:50:11Z
The following code does not compile: ``` import std.sumtype; struct Struct { int foobar; this (int value) @system { this.foobar = value; } this (const ref Struct value) inout @system { this.foobar = value.foobar; } } void main () @safe { Struct n; SumType!(Struct, int) x = n; } ``` Error message as of v2.097.0: ``` oops.d(15): Error: none of the overloads of `this` are callable using argument types `(Struct)`, candidates are: std/sumtype.d(364): `std.sumtype.SumType!(Struct, int).SumType.this(Struct value)` std/sumtype.d(364): `std.sumtype.SumType!(Struct, int).SumType.this(int value)` std/sumtype.d(395): `std.sumtype.SumType!(Struct, int).SumType.this(const(Struct) value)` std/sumtype.d(413): `std.sumtype.SumType!(Struct, int).SumType.this(immutable(Struct) value)` std/sumtype.d(395): `std.sumtype.SumType!(Struct, int).SumType.this(const(int) value)` oops.d(15): ... (2 more, -v to show) ... ``` Making the copy ctor `@safe` works, but it means that an extra copy is incurred. Additionally, if the copy constructor is defined as: ``` this (const ref Struct value) @safe { this.foobar = value.foobar; } ``` (So, not `inout`), it will fail with the following error: ``` std/sumtype.d-mixin-417(417): Error: none of the overloads of `__ctor` are callable using a `immutable` object, candidates are: oops.d(8): `oops.Struct.this(int value)` oops.d(9): `oops.Struct.this(ref const(Struct) value)` Error: constructor `std.sumtype.SumType!(Struct, int).SumType.this` label `__returnLabel` is undefined oops.d(15): Error: template instance `std.sumtype.SumType!(Struct, int)` error instantiating ```
Comment #1 by snarwin+bugzilla — 2021-08-11T21:44:31Z
The first example looks like a bad error message. The actual problem is that you cannot call a @system copy constructor from @safe code--and indeed, changing main to @system allows the example to compile successfully. The second example is a mistake in SumType, and should not be difficult to fix.
Comment #2 by snarwin+bugzilla — 2021-08-11T22:26:35Z
Bad error message submitted as issue 22202.
Comment #3 by dlang-bot — 2021-08-11T22:57:28Z
@pbackus created dlang/phobos pull request #8192 "Fix issue 22077 - `std.sumtype` support for copy constructors is inco…" fixing this issue: - Fix issue 22077 - `std.sumtype` support for copy constructors is incomplete Previously, SumType would define the wrong set of constructors for types whose copyability varies depending on their mutability--a situation that was impossible with postblits, but is now possible with copy constructors. https://github.com/dlang/phobos/pull/8192
Comment #4 by dlang-bot — 2021-08-12T03:23:18Z
dlang/phobos pull request #8192 "Fix issue 22077 - `std.sumtype` support for copy constructors is inco…" was merged into master: - cce2d6e926a5147ff9651a2c3fbb10dec59288e4 by Paul Backus: Fix issue 22077 - `std.sumtype` support for copy constructors is incomplete Previously, SumType would define the wrong set of constructors for types whose copyability varies depending on their mutability--a situation that was impossible with postblits, but is now possible with copy constructors. https://github.com/dlang/phobos/pull/8192