Skip to content

Commit 0b16751

Browse files
committed
Sema: Fix recent regression with invalid subscripts
A SubscriptExpr can represent both the result of parsing a subscript and the result of type checking / overload resolution. In the former case, we don't have an associated decl, triggering an assert when FindCapturedVars tries visits an invalid SubscriptExpr inside of an Objective-C extension. Fixes <rdar://problem/26283886>.
1 parent cafbff5 commit 0b16751

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lib/Sema/TypeCheckExpr.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1053,14 +1053,14 @@ namespace {
10531053
// Referring to a class-constrained generic or metatype
10541054
// doesn't require its type metadata.
10551055
if (auto declRef = dyn_cast<DeclRefExpr>(E))
1056-
return !declRef->getDecl()->isObjC()
1057-
&& !E->getType()->hasRetainablePointerRepresentation()
1058-
&& !E->getType()->is<AnyMetatypeType>();
1056+
return (!declRef->getDecl()->isObjC()
1057+
&& !E->getType()->hasRetainablePointerRepresentation()
1058+
&& !E->getType()->is<AnyMetatypeType>());
10591059

10601060
// Loading classes or metatypes doesn't require their metadata.
10611061
if (isa<LoadExpr>(E))
1062-
return !E->getType()->hasRetainablePointerRepresentation()
1063-
&& !E->getType()->is<AnyMetatypeType>();
1062+
return (!E->getType()->hasRetainablePointerRepresentation()
1063+
&& !E->getType()->is<AnyMetatypeType>());
10641064

10651065
// Accessing @objc members doesn't require type metadata.
10661066
if (auto memberRef = dyn_cast<MemberRefExpr>(E))
@@ -1078,13 +1078,14 @@ namespace {
10781078
}
10791079

10801080
if (auto subscriptExpr = dyn_cast<SubscriptExpr>(E)) {
1081-
return !subscriptExpr->getDecl().getDecl()->isObjC();
1081+
return (subscriptExpr->hasDecl() &&
1082+
!subscriptExpr->getDecl().getDecl()->isObjC());
10821083
}
10831084

10841085
// Getting the dynamic type of a class doesn't require type metadata.
10851086
if (isa<DynamicTypeExpr>(E))
1086-
return !E->getType()->castTo<AnyMetatypeType>()->getInstanceType()
1087-
->hasRetainablePointerRepresentation();
1087+
return (!E->getType()->castTo<AnyMetatypeType>()->getInstanceType()
1088+
->hasRetainablePointerRepresentation());
10881089

10891090
// Building a fixed-size tuple doesn't require type metadata.
10901091
// Approximate this for the purposes of being able to invoke @objc methods
@@ -1122,8 +1123,8 @@ namespace {
11221123

11231124
// Opening an @objc existential or metatype is a no-op.
11241125
if (auto open = dyn_cast<OpenExistentialExpr>(E))
1125-
return !open->getSubExpr()->getType()->isObjCExistentialType()
1126-
&& !open->getSubExpr()->getType()->is<AnyMetatypeType>();
1126+
return (!open->getSubExpr()->getType()->isObjCExistentialType()
1127+
&& !open->getSubExpr()->getType()->is<AnyMetatypeType>());
11271128

11281129
// Erasure to an ObjC existential or between metatypes doesn't require
11291130
// type metadata.

test/ClangModules/objc_bridging_generics.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ extension AnimalContainer {
234234
_ = #selector(AnimalContainer.doesntUseGenericParam3)
235235
_ = #selector(AnimalContainer.doesntUseGenericParam4)
236236
}
237+
238+
// rdar://problem/26283886
239+
func funcWithWrongArgType(x: NSObject) {}
240+
241+
func crashWithInvalidSubscript(x: NSArray) {
242+
_ = funcWithWrongArgType(x: x[12])
243+
// expected-error@-1{{'AnyObject' is not convertible to 'NSObject'; did you mean to use 'as!' to force downcast?}}
244+
}
237245
}
238246

239247
extension PettableContainer {

0 commit comments

Comments
 (0)