Skip to content

Commit 5cf343f

Browse files
committed
Logically compare swift versions as though X == X.0 == X.0.0, etc.
1 parent 1af5c85 commit 5cf343f

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

lib/Basic/Version.cpp

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,11 @@ bool Version::isValidEffectiveLanguageVersion() const {
314314
for (auto verStr : getValidEffectiveVersions()) {
315315
auto v = parseVersionString(verStr, SourceLoc(), nullptr);
316316
assert(v.hasValue());
317-
if (v == *this)
317+
// In this case, use logical-equality _and_ precision-equality. We do not
318+
// want to permit users requesting effective language versions more precise
319+
// than our whitelist (eg. we permit 3 but not 3.0 or 3.0.0), since
320+
// accepting such an argument promises more than we're able to deliver.
321+
if (v == *this && v.getValue().size() == size())
318322
return true;
319323
}
320324
return false;
@@ -336,23 +340,27 @@ bool operator>=(const class Version &lhs,
336340
if (lhs.empty())
337341
return true;
338342

339-
auto n = std::min(lhs.size(), rhs.size());
343+
auto n = std::max(lhs.size(), rhs.size());
340344

341345
for (size_t i = 0; i < n; ++i) {
342-
if (lhs[i] < rhs[i])
346+
auto lv = i < lhs.size() ? lhs[i] : 0;
347+
auto rv = i < rhs.size() ? rhs[i] : 0;
348+
if (lv < rv)
343349
return false;
344-
else if (lhs[i] > rhs[i])
350+
else if (lv > rv)
345351
return true;
346352
}
347-
return lhs.size() >= rhs.size();
353+
// Equality
354+
return true;
348355
}
349356

350357
bool operator==(const class Version &lhs,
351358
const class Version &rhs) {
352-
if (lhs.size() != rhs.size())
353-
return false;
354-
for (size_t i = 0; i < lhs.size(); ++i) {
355-
if (lhs[i] != rhs[i])
359+
auto n = std::max(lhs.size(), rhs.size());
360+
for (size_t i = 0; i < n; ++i) {
361+
auto lv = i < lhs.size() ? lhs[i] : 0;
362+
auto rv = i < rhs.size() ? rhs[i] : 0;
363+
if (lv != rv)
356364
return false;
357365
}
358366
return true;

test/Parse/ConditionalCompilation/language_version_explicit.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,24 @@
77
asdf asdf asdf asdf
88
#endif
99

10+
#if swift(>=4.0)
11+
let x = 1
12+
#else
13+
// This shouldn't emit any diagnostics.
14+
asdf asdf asdf asdf
15+
#endif
16+
17+
#if swift(>=4.0.0)
18+
let y = 1
19+
#else
20+
// This shouldn't emit any diagnostics.
21+
asdf asdf asdf asdf
22+
#endif
23+
24+
#if swift(>=4.0.1)
25+
// This shouldn't emit any diagnostics.
26+
asdf asdf asdf asdf
27+
#else
28+
let z = 1
29+
#endif
30+

0 commit comments

Comments
 (0)