Skip to content

[5.9] Two @objcImpl protocol fixes #65685

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
May 5, 2023
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
13 changes: 11 additions & 2 deletions lib/IRGen/GenClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1228,7 +1228,7 @@ namespace {
const ClassLayout &fieldLayout)
: IGM(IGM), TheEntity(theUnion), TheExtension(nullptr),
FieldLayout(&fieldLayout) {
visitConformances(getClass()->getImplementationContext());
visitConformances(getClass());

if (getClass()->isRootDefaultActor()) {
Ivars.push_back(Field::DefaultActorStorage);
Expand All @@ -1249,7 +1249,7 @@ namespace {
FieldLayout(nullptr) {
buildCategoryName(CategoryName);

visitConformances(theExtension->getImplementationContext());
visitConformances(theExtension);

for (Decl *member : TheExtension->getImplementationContext()->getMembers())
visit(member);
Expand Down Expand Up @@ -1295,6 +1295,15 @@ namespace {
/// Gather protocol records for all of the explicitly-specified Objective-C
/// protocol conformances.
void visitConformances(const IterableDeclContext *idc) {
auto dc = idc->getAsGenericContext();
if (dc->getImplementedObjCContext() != dc) {
// We want to use the conformance list imported from the ObjC header.
auto importedIDC = cast<IterableDeclContext>(
dc->getImplementedObjCContext()->getAsDecl());
visitConformances(importedIDC);
return;
}

llvm::SmallSetVector<ProtocolDecl *, 2> protocols;
for (auto conformance : idc->getLocalConformances(
ConformanceLookupKind::OnlyExplicit)) {
Expand Down
19 changes: 19 additions & 0 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "swift/AST/SourceFile.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/StringExtras.h"

#include "clang/AST/DeclObjC.h"

using namespace swift;

#pragma mark Determine whether an entity is representable in Objective-C.
Expand Down Expand Up @@ -3326,9 +3329,25 @@ class ObjCImplementationChecker {
return Identifier();
}

/// Is this member an `@optional` ObjC protocol requirement?
static bool isOptionalObjCProtocolRequirement(ValueDecl *vd) {
if (auto clangDecl = vd->getClangDecl()) {
if (auto method = dyn_cast<clang::ObjCMethodDecl>(clangDecl))
return method->isOptional();
if (auto property = dyn_cast<clang::ObjCPropertyDecl>(clangDecl))
return property->isOptional();
}

return false;
}

public:
void diagnoseUnmatchedRequirements() {
for (auto req : unmatchedRequirements) {
// Ignore `@optional` protocol requirements.
if (isOptionalObjCProtocolRequirement(req))
continue;

auto ext = cast<IterableDeclContext>(req->getDeclContext()->getAsDecl())
->getImplementationContext();

Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/Inputs/objc_implementation.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#import <Foundation/Foundation.h>

@interface ImplClass: NSObject
@interface ImplClass: NSObject <NSCopying>

- (nonnull instancetype)init;

Expand Down
Loading