Skip to content

[Type checker] Suggest @objc for ill-formed extensions of ObjC generic classes. #14903

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 1, 2018
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,8 @@ ERROR(objc_generic_extension_using_type_parameter,none,
"generic parameters at runtime", ())
NOTE(objc_generic_extension_using_type_parameter_here,none,
"generic parameter used here", ())
NOTE(objc_generic_extension_using_type_parameter_try_objc,none,
"add '@objc' to allow uses of 'self' within the function body", ())

// Protocols
ERROR(type_does_not_conform,none,
Expand Down
14 changes: 13 additions & 1 deletion lib/Sema/TypeCheckCaptures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/ForeignErrorConvention.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/TypeWalker.h"
#include "swift/Basic/Defer.h"
Expand Down Expand Up @@ -743,9 +744,20 @@ void TypeChecker::computeCaptures(AnyFunctionRef AFR) {
// their context.
if (AFD && GenericParamCaptureLoc.isValid()) {
if (auto Clas = AFD->getParent()->getAsClassOrClassExtensionContext()) {
if (Clas->isGenericContext() && Clas->hasClangNode()) {
if (Clas->usesObjCGenericsModel()) {
diagnose(AFD->getLoc(),
diag::objc_generic_extension_using_type_parameter);

// If it's possible, suggest adding @objc.
Optional<ForeignErrorConvention> errorConvention;
if (!AFD->isObjC() &&
isRepresentableInObjC(AFD, ObjCReason::MemberOfObjCMembersClass,
errorConvention)) {
diagnose(AFD->getLoc(),
diag::objc_generic_extension_using_type_parameter_try_objc)
.fixItInsert(AFD->getAttributeInsertionLoc(false), "@objc ");
}

diagnose(GenericParamCaptureLoc,
diag::objc_generic_extension_using_type_parameter_here);
}
Expand Down
7 changes: 6 additions & 1 deletion test/ClangImporter/objc_bridging_generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ extension GenericClass {
_ = T.self // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
@objc func usesGenericParamE(_ x: Int) {
@objc func usesGenericParamE(_ x: Int) {
_ = x as? T // expected-note{{used here}}
}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
Expand Down Expand Up @@ -249,18 +249,22 @@ extension AnimalContainer {
}


// expected-note@+2{{add '@objc' to allow uses of 'self' within the function body}}{{3-3=@objc }}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamA(_ x: T) {
_ = T(noise: x) // expected-note{{used here}}
}
// expected-note@+2{{add '@objc' to allow uses of 'self' within the function body}}{{3-3=@objc }}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamB() {
_ = T.create() // expected-note{{used here}}
}
// expected-note@+2{{add '@objc' to allow uses of 'self' within the function body}}{{3-3=@objc }}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamC() {
_ = T.apexPredator // expected-note{{used here}}
}
// expected-note@+2{{add '@objc' to allow uses of 'self' within the function body}}{{3-3=@objc }}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamD(_ x: T) {
T.apexPredator = x // expected-note{{used here}}
Expand All @@ -269,6 +273,7 @@ extension AnimalContainer {
// rdar://problem/27796375 -- allocating init entry points for ObjC
// initializers are generated as true Swift generics, so reify type
// parameters.
// expected-note@+2{{add '@objc' to allow uses of 'self' within the function body}}{{3-3=@objc }}
// expected-error@+1{{extension of a generic Objective-C class cannot access the class's generic parameters}}
func usesGenericParamE(_ x: T) {
_ = GenericClass(thing: x) // expected-note{{used here}}
Expand Down