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

min must be stable #7605

Merged
merged 1 commit into from
Aug 28, 2020
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
17 changes: 15 additions & 2 deletions std/algorithm/comparison.d
Original file line number Diff line number Diff line change
Expand Up @@ -1713,8 +1713,8 @@ if (T.length >= 2)

//Do the "min" proper with a and b
import std.functional : lessThan;
immutable chooseA = lessThan!(T0, T1)(a, b);
return cast(typeof(return)) (chooseA ? a : b);
immutable chooseB = lessThan!(T1, T0)(b, a);
return cast(typeof(return)) (chooseB ? b : a);
}

///
Expand Down Expand Up @@ -1757,6 +1757,19 @@ store the lowest values.
assert(min(Date.max, Date.min) == Date.min);
}

// min must be stable: when in doubt, return the first argument.
@safe unittest
{
assert(min(1.0, double.nan) == 1.0);
assert(min(double.nan, 1.0) is double.nan);
static struct A {
int x;
string y;
int opCmp(const A a) const { return int(x > a.x) - int(x < a.x); }
}
assert(min(A(1, "first"), A(1, "second")) == A(1, "first"));
}

// mismatch
/**
Sequentially compares elements in `r1` and `r2` in lockstep, and
Expand Down