Skip to content

Commit 91e4c0e

Browse files
authored
Merge pull request #27409 from xymus/fix-no-nsobject
[ClangImporter] Don't transform NSObject<...> when there's no NSObject protocol
2 parents 40e4010 + cf526b4 commit 91e4c0e

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

lib/ClangImporter/ImportType.cpp

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -932,27 +932,32 @@ namespace {
932932
return {};
933933
}
934934
if (nsObjectTy && importedType->isEqual(nsObjectTy)) {
935-
SmallVector<clang::ObjCProtocolDecl *, 4> protocols{
936-
type->qual_begin(), type->qual_end()
937-
};
938-
auto *nsObjectProto =
939-
Impl.getNSObjectProtocolType()->getAnyNominal();
940-
if (!nsObjectProto) {
941-
// Input is malformed
942-
return {};
935+
// Skip if there is no NSObject protocol.
936+
auto nsObjectProtoType =
937+
Impl.getNSObjectProtocolType();
938+
if (nsObjectProtoType) {
939+
auto *nsObjectProto = nsObjectProtoType->getAnyNominal();
940+
if (!nsObjectProto) {
941+
// Input is malformed
942+
return {};
943+
}
944+
945+
SmallVector<clang::ObjCProtocolDecl *, 4> protocols{
946+
type->qual_begin(), type->qual_end()
947+
};
948+
auto *clangProto =
949+
cast<clang::ObjCProtocolDecl>(nsObjectProto->getClangDecl());
950+
protocols.push_back(
951+
const_cast<clang::ObjCProtocolDecl *>(clangProto));
952+
953+
clang::ASTContext &clangCtx = Impl.getClangASTContext();
954+
clang::QualType protosOnlyType =
955+
clangCtx.getObjCObjectType(clangCtx.ObjCBuiltinIdTy,
956+
/*type args*/{},
957+
protocols,
958+
/*kindof*/false);
959+
return Visit(clangCtx.getObjCObjectPointerType(protosOnlyType));
943960
}
944-
auto *clangProto =
945-
cast<clang::ObjCProtocolDecl>(nsObjectProto->getClangDecl());
946-
protocols.push_back(
947-
const_cast<clang::ObjCProtocolDecl *>(clangProto));
948-
949-
clang::ASTContext &clangCtx = Impl.getClangASTContext();
950-
clang::QualType protosOnlyType =
951-
clangCtx.getObjCObjectType(clangCtx.ObjCBuiltinIdTy,
952-
/*type args*/{},
953-
protocols,
954-
/*kindof*/false);
955-
return Visit(clangCtx.getObjCObjectPointerType(protosOnlyType));
956961
}
957962
}
958963

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Define our own NSObject interface, but not the protocol.
2+
@interface NSObject
3+
- nsobjectFunc;
4+
@end
5+
6+
@protocol FooProtocol
7+
- fooFunc;
8+
@end
9+
10+
typedef NSObject<FooProtocol> Foo;
11+
12+
@interface Bar : Foo
13+
- (instancetype)init;
14+
- (NSObject<FooProtocol> *)barFunc;
15+
@end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/// Don't crash when there's no NSObject protocol.
2+
/// rdar://problem/34597302
3+
4+
// RUN: %target-swift-frontend -typecheck %s -import-objc-header %S/Inputs/no-nsobject-protocol.h -enable-objc-interop
5+
6+
var a = Bar()!
7+
var b = a.barFunc()!;
8+
b.nsobjectFunc()
9+
b.fooFunc()

0 commit comments

Comments
 (0)