Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around issue 22619 - Avoid Nullable copy ctor unless required #8358

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 34 additions & 8 deletions std/typecons.d
Original file line number Diff line number Diff line change
Expand Up @@ -2792,13 +2792,24 @@ struct Nullable(T)
}
}

this (ref return scope inout Nullable!T rhs) inout
static if (__traits(hasPostblit, T))
{
_isNull = rhs._isNull;
if (!_isNull)
_value.payload = rhs._value.payload;
else
_value = DontCallDestructorT.init;
this(this)
{
if (!_isNull)
_value.payload.__xpostblit();
}
}
else static if (__traits(hasCopyConstructor, T))
{
this(ref return scope inout Nullable!T rhs) inout
{
_isNull = rhs._isNull;
if (!_isNull)
_value.payload = rhs._value.payload;
else
_value = DontCallDestructorT.init;
}
}

/**
Expand Down Expand Up @@ -9546,13 +9557,28 @@ unittest
{
int b;
@disable this(this);
this (ref return scope inout S rhs) inout
this(ref return scope inout S rhs) inout
{
this.b = rhs.b + 1;
}
}

Nullable!S s1 = S(1);
assert(s1.get().b == 2);
Nullable!S s2 = s1;
assert(s2.get().b == 3);
}

@safe unittest
{
static struct S
{
int b;
this(this) { ++b; }
}

Nullable!S s1 = S(1);
assert(s1.get().b == 2);
Nullable!S s2 = s1;
assert(s2.get().b > s1.get().b);
assert(s2.get().b == 3);
}