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

Fix Issue 19226 - std.typecons.Nullable(T, T nullValue) doesn't fully handle non-self-equal nullValue #6693

Merged
merged 1 commit into from
May 9, 2019
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
29 changes: 28 additions & 1 deletion std/typecons.d
Original file line number Diff line number Diff line change
Expand Up @@ -3542,7 +3542,7 @@ Returns:
}
//Need to use 'is' if T is a float type
//because NaN != NaN
else static if (isFloatingPoint!T)
else static if (__traits(isFloating, T) || __traits(compiles, { static assert(!(nullValue == nullValue)); }))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: Why is there the seemingly-extraneous __traits(isFloating, T)?
A: So this patch doesn't change the behavior of Nullable!(double, -0.0).

{
return _value is nullValue;
}
Expand All @@ -3563,6 +3563,14 @@ Returns:
assert(!ni.isNull);
}

@system unittest
{
assert(typeof(this).init.isNull, typeof(this).stringof ~
".isNull does not work correctly because " ~ T.stringof ~
" has an == operator that is non-reflexive and could not be" ~
" determined before runtime to be non-reflexive!");
}

// https://issues.dlang.org/show_bug.cgi?id=11135
// disable test until https://issues.dlang.org/show_bug.cgi?id=15316 gets fixed
version (none) @system unittest
Expand Down Expand Up @@ -3737,6 +3745,25 @@ if (is (typeof(nullValue) == T))
assert(a.isNull);
}

@nogc nothrow pure @safe unittest
{
// issue 19226 - fully handle non-self-equal nullValue
static struct Fraction
{
int denominator;
bool isNaN() const
{
return denominator == 0;
}
bool opEquals(const Fraction rhs) const
{
return !isNaN && denominator == rhs.denominator;
}
}
alias N = Nullable!(Fraction, Fraction.init);
assert(N.init.isNull);
}

@safe unittest
{
static int f(scope const Nullable!(int, int.min) x) {
Expand Down