Skip to content

"-swift-version 3" means Swift 3.1, not 3.0. #7883

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

Merged
merged 1 commit into from
Mar 3, 2017
Merged
Show file tree
Hide file tree
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
23 changes: 17 additions & 6 deletions include/swift/Basic/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ namespace version {
/// a: [0 - 999]
/// b: [0 - 999]
class Version {
SmallVector<uint64_t, 5> Components;
SmallVector<unsigned, 5> Components;
public:
/// Create the empty compiler version - this always compares greater
/// or equal to any other CompilerVersion, as in the case of building Swift
/// from latest sources outside of a build/integration/release context.
Version() = default;

/// Create a literal version from a list of components.
Version(std::initializer_list<unsigned> Values) : Components(Values) {}

/// Create a version from a string in source code.
///
/// Must include only groups of digits separated by a dot.
Expand Down Expand Up @@ -94,11 +97,15 @@ class Version {
/// away any 5th component that might be in this version.
operator clang::VersionTuple() const;

/// Return whether this version is a valid Swift language version number
/// to set the compiler to using -swift-version; this is not the same as
/// the set of Swift versions that have ever existed, just those that we
/// are attempting to maintain backward-compatibility support for.
bool isValidEffectiveLanguageVersion() const;
/// Returns the concrete version to use when \e this version is provided as
/// an argument to -swift-version.
///
/// This is not the same as the set of Swift versions that have ever existed,
/// just those that we are attempting to maintain backward-compatibility
/// support for. It's also common for valid versions to produce a different
/// result; for example "-swift-version 3" at one point instructed the
/// compiler to act as if it is version 3.1.
Optional<Version> getEffectiveLanguageVersion() const;

/// Whether this version is in the Swift 3 family
bool isVersion3() const { return !empty() && Components[0] == 3; }
Expand Down Expand Up @@ -141,6 +148,10 @@ bool operator==(const Version &lhs, const Version &rhs);
raw_ostream &operator<<(raw_ostream &os, const Version &version);

/// Retrieves the numeric {major, minor} Swift version.
///
/// Note that this is the underlying version of the language, ignoring any
/// -swift-version flags that may have been used in a particular invocation of
/// the compiler.
std::pair<unsigned, unsigned> getSwiftNumericVersion();

/// Retrieves a string representing the complete Swift version, which includes
Expand Down
52 changes: 32 additions & 20 deletions lib/Basic/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,11 @@ Version Version::getCurrentCompilerVersion() {
}

Version Version::getCurrentLanguageVersion() {
#ifndef SWIFT_VERSION_STRING
#error Swift language version is not set!
#if SWIFT_VERSION_PATCHLEVEL
return {SWIFT_VERSION_MAJOR, SWIFT_VERSION_MINOR, SWIFT_VERSION_PATCHLEVEL};
#else
return {SWIFT_VERSION_MAJOR, SWIFT_VERSION_MINOR};
#endif
auto currentVersion = Version::parseVersionString(
SWIFT_VERSION_STRING, SourceLoc(), nullptr);
assert(currentVersion.hasValue() &&
"Embedded Swift language version couldn't be parsed: '"
SWIFT_VERSION_STRING
"'");
return currentVersion.getValue();
}

raw_ostream &operator<<(raw_ostream &os, const Version &version) {
Expand Down Expand Up @@ -304,18 +299,35 @@ Version::operator clang::VersionTuple() const
}
}

bool Version::isValidEffectiveLanguageVersion() const {
for (auto verStr : getValidEffectiveVersions()) {
auto v = parseVersionString(verStr, SourceLoc(), nullptr);
assert(v.hasValue());
// 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;
Optional<Version> Version::getEffectiveLanguageVersion() const {
switch (size()) {
case 0:
return None;
case 1:
break;
default:
// We do not want to permit users requesting more precise effective language
// versions since accepting such an argument promises more than we're able
// to deliver.
return None;
}

// FIXME: When we switch to Swift 4 by default, the "3" case should return
// a version newer than any released 3.x compiler (probably "3.2"), and the
// "4" case should start returning getCurrentLanguageVersion. We should
// also check for the presence of SWIFT_VERSION_PATCHLEVEL, and if that's
// set apply it to the "3" case, so that Swift 4.0.1 will automatically
// have a compatibility mode of 3.2.1.
switch (Components[0]) {
case 3:
static_assert(SWIFT_VERSION_MAJOR == 3,
"getCurrentLanguageVersion is no longer correct here");
return Version::getCurrentLanguageVersion();
case 4:
return Version{4, 0};
default:
return None;
}
return false;
}

Version Version::asMajorVersion() const {
Expand Down
15 changes: 9 additions & 6 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ static void diagnoseSwiftVersion(Optional<version::Version> &vers, Arg *verArg,

// Check for an unneeded minor version, otherwise just list valid versions
if (vers.hasValue() && !vers.getValue().empty() &&
vers.getValue().asMajorVersion().isValidEffectiveLanguageVersion()) {
vers.getValue().asMajorVersion().getEffectiveLanguageVersion()) {
diags.diagnose(SourceLoc(), diag::note_swift_version_major,
vers.getValue()[0]);
} else {
Expand All @@ -822,12 +822,15 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
if (auto A = Args.getLastArg(OPT_swift_version)) {
auto vers = version::Version::parseVersionString(
A->getValue(), SourceLoc(), &Diags);
if (vers.hasValue() &&
vers.getValue().isValidEffectiveLanguageVersion()) {
Opts.EffectiveLanguageVersion = vers.getValue();
} else {
diagnoseSwiftVersion(vers, A, Args, Diags);
bool isValid = false;
if (vers.hasValue()) {
if (auto effectiveVers = vers.getValue().getEffectiveLanguageVersion()) {
Opts.EffectiveLanguageVersion = effectiveVers.getValue();
isValid = true;
}
}
if (!isValid)
diagnoseSwiftVersion(vers, A, Args, Diags);
}

Opts.AttachCommentsToDecls |= Args.hasArg(OPT_dump_api_path);
Expand Down
18 changes: 18 additions & 0 deletions test/Driver/swift-version-default.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,26 @@ asdf // expected-error {{use of unresolved identifier}}
jkl
#endif

#if swift(>=3.1)
asdf // expected-error {{use of unresolved identifier}}
#else
jkl
#endif

#if swift(>=4)
aoeu
#else
htn // expected-error {{use of unresolved identifier}}
#endif

#if swift(>=4.1)
aoeu
#else
htn // expected-error {{use of unresolved identifier}}
#endif

#if swift(>=5)
aoeu
#else
htn // expected-error {{use of unresolved identifier}}
#endif
43 changes: 41 additions & 2 deletions test/Driver/swift-version.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// RUN: %target-swiftc_driver -swift-version 3 %s
// RUN: not %target-swiftc_driver -swift-version foo %s 2>&1 | %FileCheck --check-prefix BAD %s
// RUN: not %target-swiftc_driver -swift-version 1 %s 2>&1 | %FileCheck --check-prefix BAD %s
// RUN: not %target-swiftc_driver -swift-version 2 %s 2>&1 | %FileCheck --check-prefix BAD %s
Expand All @@ -9,10 +8,50 @@
// RUN: not %target-swiftc_driver -swift-version 3.3 %s 2>&1 | %FileCheck --check-prefix FIXIT_3 %s
// RUN: not %target-swiftc_driver -swift-version 4.3 %s 2>&1 | %FileCheck --check-prefix FIXIT_4 %s

// RUN: not %target-swiftc_driver -swift-version 3 -typecheck %s 2>&1 | %FileCheck --check-prefix ERROR_3 %s
// RUN: not %target-swiftc_driver -swift-version 4 -typecheck %s 2>&1 | %FileCheck --check-prefix ERROR_4 %s

// BAD: invalid value
// BAD: note: valid arguments to '-swift-version' are '3', '4'

// FIXIT_3: use major version, as in '-swift-version 3'
// FIXIT_4: use major version, as in '-swift-version 4'

let x = 1

#if swift(>=3)
asdf
// ERROR_3: [[@LINE-1]]:1: error: {{use of unresolved identifier}}
// ERROR_4: [[@LINE-2]]:1: error: {{use of unresolved identifier}}
#else
jkl
#endif

#if swift(>=3.1)
asdf
// ERROR_3: [[@LINE-1]]:1: error: {{use of unresolved identifier}}
// ERROR_4: [[@LINE-2]]:1: error: {{use of unresolved identifier}}
#else
jkl
#endif

#if swift(>=4)
asdf // ERROR_4: [[@LINE]]:1: error: {{use of unresolved identifier}}
#else
jkl // ERROR_3: [[@LINE]]:1: error: {{use of unresolved identifier}}
#endif

#if swift(>=4.1)
asdf
#else
jkl
// ERROR_3: [[@LINE-1]]:1: error: {{use of unresolved identifier}}
// ERROR_4: [[@LINE-2]]:1: error: {{use of unresolved identifier}}
#endif

#if swift(>=5)
asdf
#else
jkl
// ERROR_3: [[@LINE-1]]:1: error: {{use of unresolved identifier}}
// ERROR_4: [[@LINE-2]]:1: error: {{use of unresolved identifier}}
#endif
18 changes: 11 additions & 7 deletions tools/swift-api-digester/swift-api-digester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3491,15 +3491,19 @@ static int prepareForDump(const char *Main,
options::ModuleCachePath;

if (!options::SwiftVersion.empty()) {
if (auto Version = version::Version::
parseVersionString(options::SwiftVersion, SourceLoc(), nullptr)) {
if (Version.getValue().isValidEffectiveLanguageVersion())
InitInvok.getLangOptions().EffectiveLanguageVersion = Version.getValue();
else {
llvm::errs() << "Unsupported Swift Version.\n";
return 1;
using version::Version;
bool isValid = false;
if (auto Version = Version::parseVersionString(options::SwiftVersion,
SourceLoc(), nullptr)) {
if (auto Effective = Version.getValue().getEffectiveLanguageVersion()) {
InitInvok.getLangOptions().EffectiveLanguageVersion = *Effective;
isValid = true;
}
}
if (!isValid) {
llvm::errs() << "Unsupported Swift Version.\n";
return 1;
}
}

if (!options::ResourceDir.empty()) {
Expand Down
3 changes: 2 additions & 1 deletion tools/swift-ide-test/swift-ide-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2957,7 +2957,8 @@ int main(int argc, char *argv[]) {
if (auto swiftVersion =
version::Version::parseVersionString(options::SwiftVersion,
SourceLoc(), nullptr)) {
InitInvok.getLangOptions().EffectiveLanguageVersion = *swiftVersion;
if (auto actual = swiftVersion.getValue().getEffectiveLanguageVersion())
InitInvok.getLangOptions().EffectiveLanguageVersion = actual.getValue();
}
}
InitInvok.getClangImporterOptions().ModuleCachePath =
Expand Down