Skip to content

Commit fe35d9c

Browse files
authored
Merge pull request #4125 from jckarter/subscript-nonnull-crash
Clang Importer: Set the ElementType of synthesized SubscriptDecls to the contextual generic type.
2 parents b23af7c + 84cd5cc commit fe35d9c

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
lines changed

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ namespace {
726726
void visitSubscriptDecl(SubscriptDecl *SD) {
727727
printCommon(SD, "subscript_decl");
728728
OS << " storage_kind=" << getStorageKindName(SD->getStorageKind());
729+
OS << " element=" << SD->getElementType()->getCanonicalType();
729730
printAccessors(SD);
730731
OS << ')';
731732
}

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4832,6 +4832,9 @@ namespace {
48324832
auto elementTy
48334833
= getter->getInterfaceType()->castTo<AnyFunctionType>()->getResult()
48344834
->castTo<AnyFunctionType>()->getResult();
4835+
auto elementContextTy
4836+
= getter->getType()->castTo<AnyFunctionType>()->getResult()
4837+
->castTo<AnyFunctionType>()->getResult();
48354838

48364839
// Local function to mark the setter unavailable.
48374840
auto makeSetterUnavailable = [&] {
@@ -4934,7 +4937,7 @@ namespace {
49344937
getOverridableAccessibility(dc),
49354938
name, decl->getLoc(), bodyParams,
49364939
decl->getLoc(),
4937-
TypeLoc::withoutLoc(elementTy), dc);
4940+
TypeLoc::withoutLoc(elementContextTy),dc);
49384941

49394942
/// Record the subscript as an alternative declaration.
49404943
Impl.AlternateDecls[associateWithSetter ? setter : getter] = subscript;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
@import Foundation;
2+
3+
@interface NonnullMembers<T> : NSObject
4+
5+
- (T _Nonnull) method;
6+
7+
@property (readonly) T _Nonnull property;
8+
9+
- (T _Nonnull)objectForKeyedSubscript:(id _Nonnull)key;
10+
11+
@end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %target-swift-frontend -emit-silgen -verify -import-objc-header %S/Inputs/objc_bridged_generic_nonnull.h %s
2+
// REQUIRES: objc_interop
3+
4+
public func test<T: AnyObject>(_ x: NonnullMembers<T>) -> T? {
5+
var z: T?
6+
z = x.method()
7+
z = x.property
8+
z = x.property
9+
z = x[x]
10+
_ = z
11+
}

0 commit comments

Comments
 (0)