From 639bedbea8d90959d94feaf15d1358977e204305 Mon Sep 17 00:00:00 2001 From: Andrei Alexandrescu Date: Sun, 13 Sep 2020 17:25:20 -0400 Subject: [PATCH] Fix min --- std/algorithm/comparison.d | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/std/algorithm/comparison.d b/std/algorithm/comparison.d index 8b1e998b4..fb62d3b43 100644 --- a/std/algorithm/comparison.d +++ b/std/algorithm/comparison.d @@ -1668,7 +1668,11 @@ if (T.length >= 2 && !is(CommonType!T == void)) // Compute the returned type. static if (is(typeof(mostNegative!T0 < mostNegative!T1))) // Both are numeric (or character or Boolean), so we choose the one with the lowest minimum. - alias Result = Select!(mostNegative!T1 < mostNegative!T0, T1, T0); + // If they have the same minimum, choose the one with the smallest size. + // If both mostNegative and sizeof are equal, go for stability: pick the type of the first one. + alias Result = Select!(mostNegative!T1 < mostNegative!T0 || + mostNegative!T1 == mostNegative!T0 && T1.sizeof < T0.sizeof, + T1, T0); else // At least one is non-numeric, so just go with the common type. alias Result = CommonType!(T0, T1); @@ -1691,6 +1695,14 @@ if (T.length >= 2 && !is(CommonType!T == void)) auto e = min(a, b, c); static assert(is(typeof(e) == double)); assert(e == 2); + ulong f = 0xffff_ffff_ffff; + const uint g = min(f, 0xffff_0000); + assert(g == 0xffff_0000); + dchar h = 100; + uint i = 101; + static assert(is(typeof(min(h, i)) == dchar)); + static assert(is(typeof(min(i, h)) == uint)); + assert(min(h, i) == 100); } /**