From 75a9b045fb8faf2d6d5c7705b9491f3967856dd2 Mon Sep 17 00:00:00 2001 From: Nathan Sashihara <21227491+n8sh@users.noreply.github.com> Date: Wed, 5 Sep 2018 14:16:09 -0400 Subject: [PATCH] Fix Issue 19226 - std.typecons.Nullable(T, T nullValue) doesn't fully handle non-self-equal nullValue --- std/typecons.d | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/std/typecons.d b/std/typecons.d index 291a0d41715..16a788d65d5 100644 --- a/std/typecons.d +++ b/std/typecons.d @@ -3445,7 +3445,7 @@ Returns: } //Need to use 'is' if T is a float type //because NaN != NaN - else static if (isFloatingPoint!T) + else static if (__traits(compiles, { static assert(!(nullValue == nullValue)); })) { return _value is nullValue; } @@ -3466,6 +3466,14 @@ Returns: assert(!ni.isNull); } +@system unittest +{ + assert(typeof(this).init.isNull, typeof(this).stringof ~ + ".isNull does not work correctly because " ~ typeof(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 @@ -3640,6 +3648,17 @@ if (is (typeof(nullValue) == T)) assert(a.isNull); } +@safe unittest +{ + // issue 19226 - fully handle non-self-equal nullValue + static struct S + { + float f; + } + alias N = Nullable!(S, S.init); + assert(N(S.init).isNull); +} + @safe unittest { static int f(scope const Nullable!(int, int.min) x) {