Skip to content

Commit 9db3a9a

Browse files
committed
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).
1 parent a5c467d commit 9db3a9a

File tree

1 file changed

+34
-8
lines changed

1 file changed

+34
-8
lines changed

std/typecons.d

+34-8
Original file line numberDiff line numberDiff line change
@@ -2792,13 +2792,24 @@ struct Nullable(T)
27922792
}
27932793
}
27942794

2795-
this (ref return scope inout Nullable!T rhs) inout
2795+
static if (__traits(hasPostblit, T))
27962796
{
2797-
_isNull = rhs._isNull;
2798-
if (!_isNull)
2799-
_value.payload = rhs._value.payload;
2800-
else
2801-
_value = DontCallDestructorT.init;
2797+
this(this)
2798+
{
2799+
if (!_isNull)
2800+
_value.payload.__xpostblit();
2801+
}
2802+
}
2803+
else static if (__traits(hasCopyConstructor, T))
2804+
{
2805+
this(ref return scope inout Nullable!T rhs) inout
2806+
{
2807+
_isNull = rhs._isNull;
2808+
if (!_isNull)
2809+
_value.payload = rhs._value.payload;
2810+
else
2811+
_value = DontCallDestructorT.init;
2812+
}
28022813
}
28032814

28042815
/**
@@ -9546,13 +9557,28 @@ unittest
95469557
{
95479558
int b;
95489559
@disable this(this);
9549-
this (ref return scope inout S rhs) inout
9560+
this(ref return scope inout S rhs) inout
95509561
{
95519562
this.b = rhs.b + 1;
95529563
}
95539564
}
95549565

95559566
Nullable!S s1 = S(1);
9567+
assert(s1.get().b == 2);
9568+
Nullable!S s2 = s1;
9569+
assert(s2.get().b == 3);
9570+
}
9571+
9572+
@safe unittest
9573+
{
9574+
static struct S
9575+
{
9576+
int b;
9577+
this(this) { ++b; }
9578+
}
9579+
9580+
Nullable!S s1 = S(1);
9581+
assert(s1.get().b == 2);
95569582
Nullable!S s2 = s1;
9557-
assert(s2.get().b > s1.get().b);
9583+
assert(s2.get().b == 3);
95589584
}

0 commit comments

Comments
 (0)