Skip to content

[ClangImporter] Don't transform NSObject<...> when there's no NSObject protocol #27409

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
Sep 30, 2019
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
45 changes: 25 additions & 20 deletions lib/ClangImporter/ImportType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -921,27 +921,32 @@ namespace {
return {};
}
if (nsObjectTy && importedType->isEqual(nsObjectTy)) {
SmallVector<clang::ObjCProtocolDecl *, 4> protocols{
type->qual_begin(), type->qual_end()
};
auto *nsObjectProto =
Impl.getNSObjectProtocolType()->getAnyNominal();
if (!nsObjectProto) {
// Input is malformed
return {};
// Skip if there is no NSObject protocol.
auto nsObjectProtoType =
Impl.getNSObjectProtocolType();
if (nsObjectProtoType) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens in the else case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be treated like the other non NSObject<...> types, except if it hits another special case it is imported as a protocol composition of the class and the protocols.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right, so far we've just tested that "we have protocols" and "the class is NSObject". Okay.

auto *nsObjectProto = nsObjectProtoType->getAnyNominal();
if (!nsObjectProto) {
// Input is malformed
return {};
}

SmallVector<clang::ObjCProtocolDecl *, 4> protocols{
type->qual_begin(), type->qual_end()
};
auto *clangProto =
cast<clang::ObjCProtocolDecl>(nsObjectProto->getClangDecl());
protocols.push_back(
const_cast<clang::ObjCProtocolDecl *>(clangProto));

clang::ASTContext &clangCtx = Impl.getClangASTContext();
clang::QualType protosOnlyType =
clangCtx.getObjCObjectType(clangCtx.ObjCBuiltinIdTy,
/*type args*/{},
protocols,
/*kindof*/false);
return Visit(clangCtx.getObjCObjectPointerType(protosOnlyType));
}
auto *clangProto =
cast<clang::ObjCProtocolDecl>(nsObjectProto->getClangDecl());
protocols.push_back(
const_cast<clang::ObjCProtocolDecl *>(clangProto));

clang::ASTContext &clangCtx = Impl.getClangASTContext();
clang::QualType protosOnlyType =
clangCtx.getObjCObjectType(clangCtx.ObjCBuiltinIdTy,
/*type args*/{},
protocols,
/*kindof*/false);
return Visit(clangCtx.getObjCObjectPointerType(protosOnlyType));
}
}

Expand Down
15 changes: 15 additions & 0 deletions test/ClangImporter/Inputs/no-nsobject-protocol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Define our own NSObject interface, but not the protocol.
@interface NSObject
- nsobjectFunc;
@end

@protocol FooProtocol
- fooFunc;
@end

typedef NSObject<FooProtocol> Foo;

@interface Bar : Foo
- (instancetype)init;
- (NSObject<FooProtocol> *)barFunc;
@end
9 changes: 9 additions & 0 deletions test/ClangImporter/no-nsobject-protocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// Don't crash when there's no NSObject protocol.
/// rdar://problem/34597302

// RUN: %target-swift-frontend -typecheck %s -import-objc-header %S/Inputs/no-nsobject-protocol.h -enable-objc-interop

var a = Bar()!
var b = a.barFunc()!;
b.nsobjectFunc()
b.fooFunc()