Skip to content

[3.0 compat] Don't diagnose @escaping var-arg closures. #5243

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 2 commits into from
Oct 12, 2016
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
3 changes: 3 additions & 0 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,9 @@ class ASTContext {
clang::ObjCInterfaceDecl *classDecl,
bool forInstance);

/// Whether our effective Swift version is in the Swift 3 family
bool isSwiftVersion3() const { return LangOpts.isSwiftVersion3(); }

private:
friend class Decl;
Optional<RawComment> getRawComment(const Decl *D);
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ namespace swift {
return CustomConditionalCompilationFlags;
}

/// Whether our effective Swift version is in the Swift 3 family
bool isSwiftVersion3() const {
return EffectiveLanguageVersion.isVersion3();
}

/// Returns true if the 'os' platform condition argument represents
/// a supported target operating system.
///
Expand Down
3 changes: 3 additions & 0 deletions include/swift/Basic/Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ class Version {
/// are attempting to maintain backward-compatibility support for.
bool isValidEffectiveLanguageVersion() const;

/// Whether this version is in the Swift 3 family
bool isVersion3() const { return !empty() && Components[0] == 3; }

/// Parse a version in the form used by the _compiler_version \#if condition.
static Version parseCompilerVersionString(StringRef VersionString,
SourceLoc Loc,
Expand Down
5 changes: 5 additions & 0 deletions lib/Basic/Version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,13 @@ Version::isValidEffectiveLanguageVersion() const
// Whitelist of backward-compatibility versions that we permit passing as
// -swift-version <vers>
char const *whitelist[] = {
// Swift 3 family
"3",
"3.0",

// Swift 4 family
"4",
"4.0",
};
for (auto const i : whitelist) {
auto v = parseVersionString(i, SourceLoc(), nullptr);
Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2032,8 +2032,14 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
// Handle @escaping
if (hasFunctionAttr && ty->is<FunctionType>()) {
if (attrs.has(TAK_escaping)) {
// For compatibility with 3.0, we don't emit an error if it appears on a
// variadic argument list.
bool skipDiagnostic =
isVariadicFunctionParam && Context.isSwiftVersion3();

// The attribute is meaningless except on parameter types.
if (!isFunctionParam) {
bool shouldDiagnose = !isFunctionParam && !skipDiagnostic;
if (shouldDiagnose) {
auto &SM = TC.Context.SourceMgr;
auto loc = attrs.getLoc(TAK_escaping);
auto attrRange = SourceRange(
Expand Down
10 changes: 10 additions & 0 deletions test/Compatibility/attr_escaping.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-parse-verify-swift

// This is allowed, in order to keep source compat with Swift version 3.0.
func takesVarargsOfFunctionsExplicitEscaping(_ fns: @escaping () -> ()...) {}

func takesVarargsOfFunctions(_ fn: () -> (), _ fns: () -> ()...) {
// expected-note@-1{{parameter 'fn' is implicitly non-escaping}}
takesVarargsOfFunctionsExplicitEscaping(fns[0], fns[1]) // ok
takesVarargsOfFunctionsExplicitEscaping(fn) // expected-error{{passing non-escaping parameter 'fn' to function expecting an @escaping closure}}
}
4 changes: 3 additions & 1 deletion test/attr/attr_escaping.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-parse-verify-swift
// RUN: %target-parse-verify-swift -swift-version 4

@escaping var fn : () -> Int = { 4 } // expected-error {{attribute can only be applied to types, not declarations}}
func paramDeclEscaping(@escaping fn: (Int) -> Void) {} // expected-error {{attribute can only be applied to types, not declarations}}
Expand Down Expand Up @@ -140,6 +140,8 @@ func takesVarargsOfFunctions(fns: () -> ()...) {
}
}

func takesVarargsOfFunctionsExplicitEscaping(fns: @escaping () -> ()...) {} // expected-error{{@escaping attribute may only be used in function parameter position}}

func takesNoEscapeFunction(fn: () -> ()) { // expected-note {{parameter 'fn' is implicitly non-escaping}}
takesVarargsOfFunctions(fns: fn) // expected-error {{passing non-escaping parameter 'fn' to function expecting an @escaping closure}}
}
Expand Down