Skip to content

Commit 7e05667

Browse files
committed
[CSGen] Abstract checking for a nil literal in visitSubscriptExpr into a
general isValidBaseOfMemberRef function.
1 parent e26794a commit 7e05667

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

lib/Sema/CSGen.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,16 @@ namespace {
930930
= { nullptr, nullptr };
931931
unsigned currentEditorPlaceholderVariable = 0;
932932

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+
933943
/// Add constraints for a reference to a named member of the given
934944
/// base type, and return the type of such a reference.
935945
Type addMemberRefConstraints(Expr *expr, Expr *base, DeclNameRef name,
@@ -1785,21 +1795,18 @@ namespace {
17851795
}
17861796

17871797
Type visitSubscriptExpr(SubscriptExpr *expr) {
1788-
auto &ctx = CS.getASTContext();
17891798
ValueDecl *decl = nullptr;
17901799
if (expr->hasDecl()) {
17911800
decl = expr->getDecl().getDecl();
17921801
if (decl->isInvalid())
17931802
return Type();
17941803
}
17951804

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))
17991807
return nullptr;
1800-
}
18011808

1802-
return addSubscriptConstraints(expr, CS.getType(expr->getBase()),
1809+
return addSubscriptConstraints(expr, CS.getType(base),
18031810
expr->getIndex(),
18041811
decl, expr->getArgumentLabels(),
18051812
expr->hasTrailingClosure());

0 commit comments

Comments
 (0)