Skip to content

[GSB] Allow redundant inheritance for all Objective-C-defined protocols. #15338

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
23 changes: 14 additions & 9 deletions lib/AST/GenericSignatureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6178,29 +6178,34 @@ Constraint<T> GenericSignatureBuilder::checkConstraintList(

/// Determine whether this is a redundantly inheritable Objective-C protocol.
///
/// If we do have a redundantly inheritable Objective-C protocol, record that
/// the conformance was restated on the protocol whose requirement signature
/// we are computing.
/// A redundantly-inheritable Objective-C protocol is one where we will
/// silently accept a directly-stated redundant conformance to this protocol,
/// and emit this protocol in the list of "inherited" protocols. There are
/// two cases where we allow this:
///
/// At present, there is only one such protocol that we know about:
/// JavaScriptCore's JSExport.
// 1) For a protocol defined in Objective-C, so that we will match Clang's
/// behavior, and
/// 2) For an @objc protocol defined in Swift that directly inherits from
/// JavaScriptCore's JSExport, which depends on this behavior.
static bool isRedundantlyInheritableObjCProtocol(
ProtocolDecl *proto,
const RequirementSource *source) {
if (!proto->isObjC()) return false;

// Only JSExport protocol behaves this way.
if (!proto->getName().is("JSExport")) return false;

// Only do this for the requirement signature computation.
auto parentSource = source->parent;
if (!parentSource ||
parentSource->kind != RequirementSource::RequirementSignatureSelf)
return false;

// Check the two conditions in which we will suppress the diagnostic and
// emit the redundant inheritance.
auto inheritingProto = parentSource->getProtocolDecl();
if (!inheritingProto->hasClangNode() && !proto->getName().is("JSExport"))
return false;

// If the inheriting protocol already has @_restatedObjCConformance with
// this protocol, we're done.
auto inheritingProto = parentSource->getProtocolDecl();
for (auto *attr : inheritingProto->getAttrs()
.getAttributes<RestatedObjCConformanceAttr>()) {
if (attr->Proto == proto) return true;
Expand Down
6 changes: 6 additions & 0 deletions test/IRGen/Inputs/usr/include/Gizmo.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ NSString *NSStringFromRect(struct NSRect r);
- (void)foo;
@end

@protocol NSFungingAndRuncing <NSRuncing, NSFunging>
@end

@protocol NSDoubleInheritedFunging <NSFungingAndRuncing, NSFunging>
@end

typedef NS_ENUM(unsigned short, NSRuncingOptions) {
NSRuncingMince = 123,
NSRuncingQuinceSliced = 4567,
Expand Down
7 changes: 7 additions & 0 deletions test/IRGen/objc_protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ protocol InheritingProtocol : BaseProtocol { }
// CHECK: @_PROTOCOLS__TtC14objc_protocols17ImplementingClass {{.*}} @_PROTOCOL__TtP14objc_protocols12BaseProtocol_
class ImplementingClass : InheritingProtocol { }

// CHECK: @_PROTOCOL_PROTOCOLS_NSDoubleInheritedFunging = private constant{{.*}}i64 2{{.*}} @_PROTOCOL_NSFungingAndRuncing {{.*}}@_PROTOCOL_NSFunging

// -- Force generation of witness for Zim.
// CHECK: define hidden swiftcc { %objc_object*, i8** } @"$S14objc_protocols22mixed_heritage_erasure{{[_0-9a-zA-Z]*}}F"
func mixed_heritage_erasure(_ x: Zim) -> Frungible {
Expand Down Expand Up @@ -209,3 +211,8 @@ func canter<T : Palomino>(_ t: Stirrup<T>) {}
func gallop<T : Vanner>(_ t: Stirrup<T>) {
canter(t)
}

// SR-7130
func triggerDoubleInheritedFunging() -> AnyObject {
return NSDoubleInheritedFunging.self as AnyObject
}