Logically compare swift versions as though X == X.0 == X.0.0, etc.

This commit is contained in:
Graydon Hoare
2016-10-24 19:11:09 -07:00
parent 1af5c856fa
commit 5cf343f8f6
2 changed files with 38 additions and 9 deletions

View File

@@ -314,7 +314,11 @@ bool Version::isValidEffectiveLanguageVersion() const {
for (auto verStr : getValidEffectiveVersions()) {
auto v = parseVersionString(verStr, SourceLoc(), nullptr);
assert(v.hasValue());
if (v == *this)
// In this case, use logical-equality _and_ precision-equality. We do not
// want to permit users requesting effective language versions more precise
// than our whitelist (eg. we permit 3 but not 3.0 or 3.0.0), since
// accepting such an argument promises more than we're able to deliver.
if (v == *this && v.getValue().size() == size())
return true;
}
return false;
@@ -336,23 +340,27 @@ bool operator>=(const class Version &lhs,
if (lhs.empty())
return true;
auto n = std::min(lhs.size(), rhs.size());
auto n = std::max(lhs.size(), rhs.size());
for (size_t i = 0; i < n; ++i) {
if (lhs[i] < rhs[i])
auto lv = i < lhs.size() ? lhs[i] : 0;
auto rv = i < rhs.size() ? rhs[i] : 0;
if (lv < rv)
return false;
else if (lhs[i] > rhs[i])
else if (lv > rv)
return true;
}
return lhs.size() >= rhs.size();
// Equality
return true;
}
bool operator==(const class Version &lhs,
const class Version &rhs) {
if (lhs.size() != rhs.size())
return false;
for (size_t i = 0; i < lhs.size(); ++i) {
if (lhs[i] != rhs[i])
auto n = std::max(lhs.size(), rhs.size());
for (size_t i = 0; i < n; ++i) {
auto lv = i < lhs.size() ? lhs[i] : 0;
auto rv = i < rhs.size() ? rhs[i] : 0;
if (lv != rv)
return false;
}
return true;