Skip to content

[4.1] [Sema] Disallow conditional conformances on objective-c generics. #14665

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
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,10 @@ ERROR(objc_runtime_visible_cannot_conform_to_objc_protocol,none,
"class %0 cannot conform to @objc protocol %1 because "
"the class is only visible via the Objective-C runtime",
(Type, Type))
ERROR(objc_generics_cannot_conditionally_conform,none,
"type %0 cannot conditionally conform to protocol %1 because "
"the type uses the Objective-C generics model",
(Type, Type))
ERROR(protocol_has_missing_requirements,none,
"type %0 cannot conform to protocol %1 because it has requirements that "
"cannot be satisfied", (Type, Type))
Expand Down
21 changes: 21 additions & 0 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,27 @@ checkIndividualConformance(NormalProtocolConformance *conformance,
}
}

// Not every protocol/type is compatible with conditional conformances.
if (!conformance->getConditionalRequirements().empty()) {
auto nestedType = canT;
// Obj-C generics cannot be looked up at runtime, so we don't support
// conditional conformances involving them. Check the full stack of nested
// types for any obj-c ones.
while (nestedType) {
if (auto clas = nestedType->getClassOrBoundGenericClass()) {
if (clas->usesObjCGenericsModel()) {
TC.diagnose(ComplainLoc,
diag::objc_generics_cannot_conditionally_conform, T,
Proto->getDeclaredType());
conformance->setInvalid();
return conformance;
}
}

nestedType = nestedType.getNominalParent();
}
}

// If the protocol contains missing requirements, it can't be conformed to
// at all.
if (Proto->hasMissingRequirements()) {
Expand Down
7 changes: 7 additions & 0 deletions test/Generics/Inputs/conditional_conformances_objc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import Foundation;

@interface ObjC <ObjectType>: NSObject
@end

@interface ObjC2: NSObject
@end
34 changes: 34 additions & 0 deletions test/Generics/conditional_conformances_objc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// RUN: %target-typecheck-verify-swift -import-objc-header %S/Inputs/conditional_conformances_objc.h

// REQUIRES: objc_interop

protocol Foo {}
extension ObjC: Foo where ObjectType == ObjC2 {}
// expected-error@-1{{type 'ObjC<ObjectType>' cannot conditionally conform to protocol 'Foo' because the type uses the Objective-C generics model}}

protocol Bar {}
extension ObjC: Bar where ObjectType: Bar {}
// expected-error@-1{{type 'ObjC<ObjectType>' cannot conditionally conform to protocol 'Bar' because the type uses the Objective-C generics model}}

extension ObjC {
struct Struct {
enum Enum {}
}
class Class<T> {}
}

extension ObjC.Struct: Foo where ObjectType == ObjC2 {}
// expected-error@-1{{type 'ObjC<ObjectType>.Struct' cannot conditionally conform to protocol 'Foo' because the type uses the Objective-C generics model}}
extension ObjC.Struct: Bar where ObjectType: Bar {}
// expected-error@-1{{type 'ObjC<ObjectType>.Struct' cannot conditionally conform to protocol 'Bar' because the type uses the Objective-C generics model}}

extension ObjC.Struct.Enum: Foo where ObjectType == ObjC2 {}
// expected-error@-1{{type 'ObjC<ObjectType>.Struct.Enum' cannot conditionally conform to protocol 'Foo' because the type uses the Objective-C generics model}}
extension ObjC.Struct.Enum: Bar where ObjectType: Bar {}
// expected-error@-1{{type 'ObjC<ObjectType>.Struct.Enum' cannot conditionally conform to protocol 'Bar' because the type uses the Objective-C generics model}}

extension ObjC.Class: Foo where T == ObjC2 {}
// expected-error@-1{{type 'ObjC<ObjectType>.Class<T>' cannot conditionally conform to protocol 'Foo' because the type uses the Objective-C generics model}}
extension ObjC.Class: Bar where T: Bar {}
// expected-error@-1{{type 'ObjC<ObjectType>.Class<T>' cannot conditionally conform to protocol 'Bar' because the type uses the Objective-C generics model}}