@@ -930,6 +930,16 @@ namespace {
930
930
= { nullptr , nullptr };
931
931
unsigned currentEditorPlaceholderVariable = 0 ;
932
932
933
+ // / Returns false and emits the specified diagnostic if the member reference
934
+ // / base is a nil literal. Returns true otherwise.
935
+ bool isValidBaseOfMemberRef (Expr *base, Diag<> diagnostic) {
936
+ if (auto nilLiteral = dyn_cast<NilLiteralExpr>(base)) {
937
+ CS.getASTContext ().Diags .diagnose (nilLiteral->getLoc (), diagnostic);
938
+ return false ;
939
+ }
940
+ return true ;
941
+ }
942
+
933
943
// / Add constraints for a reference to a named member of the given
934
944
// / base type, and return the type of such a reference.
935
945
Type addMemberRefConstraints (Expr *expr, Expr *base, DeclNameRef name,
@@ -1785,21 +1795,18 @@ namespace {
1785
1795
}
1786
1796
1787
1797
Type visitSubscriptExpr (SubscriptExpr *expr) {
1788
- auto &ctx = CS.getASTContext ();
1789
1798
ValueDecl *decl = nullptr ;
1790
1799
if (expr->hasDecl ()) {
1791
1800
decl = expr->getDecl ().getDecl ();
1792
1801
if (decl->isInvalid ())
1793
1802
return Type ();
1794
1803
}
1795
1804
1796
- if (auto nilLiteral = dyn_cast<NilLiteralExpr>(expr->getBase ())) {
1797
- ctx.Diags .diagnose (nilLiteral->getLoc (),
1798
- diag::cannot_subscript_nil_literal);
1805
+ auto *base = expr->getBase ();
1806
+ if (!isValidBaseOfMemberRef (base, diag::cannot_subscript_nil_literal))
1799
1807
return nullptr ;
1800
- }
1801
1808
1802
- return addSubscriptConstraints (expr, CS.getType (expr-> getBase () ),
1809
+ return addSubscriptConstraints (expr, CS.getType (base ),
1803
1810
expr->getIndex (),
1804
1811
decl, expr->getArgumentLabels (),
1805
1812
expr->hasTrailingClosure ());
0 commit comments