Skip to content

Commit 12efcc9

Browse files
committed
[Version] Don't allow effective sub-versions, only major versions
Also offer a note when the major version is valid on its own.
1 parent db816f8 commit 12efcc9

File tree

8 files changed

+32
-11
lines changed

8 files changed

+32
-11
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ ERROR(error_no_source_location_scope_map,none,
9797
"-dump-scope-maps argument must be 'expanded' or a list of source locations",
9898
())
9999

100+
NOTE(note_swift_version_major, none,
101+
"use major version, as in '-swift-version %0'", (unsigned))
102+
100103
ERROR(error_mode_cannot_emit_dependencies,none,
101104
"this mode does not support emitting dependency files", ())
102105
ERROR(error_mode_cannot_emit_header,none,

include/swift/Basic/Version.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ class Version {
103103
/// Whether this version is in the Swift 3 family
104104
bool isVersion3() const { return !empty() && Components[0] == 3; }
105105

106+
/// Return this Version struct with minor and sub-minor components stripped
107+
Version asMajorVersion() const;
108+
106109
/// Parse a version in the form used by the _compiler_version \#if condition.
107110
static Version parseCompilerVersionString(StringRef VersionString,
108111
SourceLoc Loc,

lib/Basic/Version.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,8 @@ Version::isValidEffectiveLanguageVersion() const
316316
// Whitelist of backward-compatibility versions that we permit passing as
317317
// -swift-version <vers>
318318
char const *whitelist[] = {
319-
// Swift 3 family
320319
"3",
321-
"3.0",
322-
323-
// Swift 4 family
324320
"4",
325-
"4.0",
326321
};
327322
for (auto const i : whitelist) {
328323
auto v = parseVersionString(i, SourceLoc(), nullptr);
@@ -333,6 +328,14 @@ Version::isValidEffectiveLanguageVersion() const
333328
return false;
334329
}
335330

331+
Version Version::asMajorVersion() const {
332+
if (empty())
333+
return {};
334+
Version res;
335+
res.Components.push_back(Components[0]);
336+
return res;
337+
}
338+
336339
bool operator>=(const class Version &lhs,
337340
const class Version &rhs) {
338341

lib/Frontend/CompilerInvocation.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,8 +773,16 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
773773
vers.getValue().isValidEffectiveLanguageVersion()) {
774774
Opts.EffectiveLanguageVersion = vers.getValue();
775775
} else {
776+
// General invalid version error
776777
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_value,
777778
A->getAsString(Args), A->getValue());
779+
780+
// Check for an unneeded minor version
781+
if (vers.hasValue() && !vers.getValue().empty() &&
782+
vers.getValue().asMajorVersion().isValidEffectiveLanguageVersion()) {
783+
Diags.diagnose(SourceLoc(), diag::note_swift_version_major,
784+
vers.getValue()[0]);
785+
}
778786
}
779787
}
780788

test/APINotes/versioned.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: rm -rf %t && mkdir -p %t
22

3-
// RUN: %target-swift-ide-test -F %S/Inputs/custom-frameworks -print-module -source-filename %s -module-to-print=APINotesFrameworkTest -function-definitions=false -print-regular-comments -swift-version 4.0 | %FileCheck -check-prefix=CHECK-SWIFT-4 %s
3+
// RUN: %target-swift-ide-test -F %S/Inputs/custom-frameworks -print-module -source-filename %s -module-to-print=APINotesFrameworkTest -function-definitions=false -print-regular-comments -swift-version 4 | %FileCheck -check-prefix=CHECK-SWIFT-4 %s
44

5-
// RUN: %target-swift-ide-test -F %S/Inputs/custom-frameworks -print-module -source-filename %s -module-to-print=APINotesFrameworkTest -function-definitions=false -print-regular-comments -swift-version 3.0 | %FileCheck -check-prefix=CHECK-SWIFT-3 %s
5+
// RUN: %target-swift-ide-test -F %S/Inputs/custom-frameworks -print-module -source-filename %s -module-to-print=APINotesFrameworkTest -function-definitions=false -print-regular-comments -swift-version 3 | %FileCheck -check-prefix=CHECK-SWIFT-3 %s
66

77
// CHECK-SWIFT-4: func jumpTo(x: Double, y: Double, z: Double)
88
// CHECK-SWIFT-3: func jumpTo(x: Double, y: Double, z: Double)

test/Driver/swift-version.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
// RUN: %target-swiftc_driver -swift-version 3 %s
2-
// RUN: %target-swiftc_driver -swift-version 3.0 %s
32
// RUN: not %target-swiftc_driver -swift-version 1 %s 2>&1 | %FileCheck --check-prefix BAD %s
43
// RUN: not %target-swiftc_driver -swift-version 2 %s 2>&1 | %FileCheck --check-prefix BAD %s
54
// RUN: not %target-swiftc_driver -swift-version 2.3 %s 2>&1 | %FileCheck --check-prefix BAD %s
65
// RUN: not %target-swiftc_driver -swift-version 7 %s 2>&1 | %FileCheck --check-prefix BAD %s
76
// RUN: not %target-swiftc_driver -swift-version 7.2 %s 2>&1 | %FileCheck --check-prefix BAD %s
7+
// RUN: not %target-swiftc_driver -swift-version 3.0 %s 2>&1 | %FileCheck --check-prefix FIXIT_3 %s
8+
// RUN: not %target-swiftc_driver -swift-version 3.3 %s 2>&1 | %FileCheck --check-prefix FIXIT_3 %s
9+
// RUN: not %target-swiftc_driver -swift-version 4.3 %s 2>&1 | %FileCheck --check-prefix FIXIT_4 %s
810

911
// BAD: invalid value
12+
// FIXIT_3: use major version, as in '-swift-version 3'
13+
// FIXIT_4: use major version, as in '-swift-version 4
1014

1115
let x = 1

test/api-digester/source-stability.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// REQUIRES: OS=macosx
22
// RUN: rm -rf %S/tmp && mkdir %S/tmp && mkdir %S/tmp/module-cache && mkdir %S/tmp/dummy.sdk
3-
// RUN: %api-digester -dump-sdk -module Swift -o %S/tmp/current-stdlib.json -module-cache-path %S/tmp/module-cache -sdk %S/tmp/dummy.sdk -swift-version 3.0
3+
// RUN: %api-digester -dump-sdk -module Swift -o %S/tmp/current-stdlib.json -module-cache-path %S/tmp/module-cache -sdk %S/tmp/dummy.sdk -swift-version 3
44
// RUN: %api-digester -diagnose-sdk -input-paths %S/stdlib-stable.json -input-paths %S/tmp/current-stdlib.json >> %S/tmp/changes.txt
55
// RUN: %clang -E -P -x c %S/source-stability.swift.expected -o - | sed '/^\s*$/d' > %S/tmp/source-stability.swift.expected
66
// RUN: %clang -E -P -x c %S/tmp/changes.txt -o - | sed '/^\s*$/d' > %S/tmp/changes.txt.tmp

test/attr/attr_availability_swift_deserialize.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: rm -rf %t && mkdir -p %t
22
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/OldAndNew.swiftmodule -module-name OldAndNew %S/Inputs/OldAndNew.swift
3-
// RUN: not %target-swift-frontend -parse -I %t -swift-version 3.0 %s 2>&1 | %FileCheck -check-prefix THREE %s
4-
// RUN: not %target-swift-frontend -parse -I %t -swift-version 4.0 %s 2>&1 | %FileCheck -check-prefix FOUR %s
3+
// RUN: not %target-swift-frontend -parse -I %t -swift-version 3 %s 2>&1 | %FileCheck -check-prefix THREE %s
4+
// RUN: not %target-swift-frontend -parse -I %t -swift-version 4 %s 2>&1 | %FileCheck -check-prefix FOUR %s
55

66
import OldAndNew
77

0 commit comments

Comments
 (0)