Skip to content

Commit 6a7c378

Browse files
committed
Sema: Move getting key-path argument logic to a closure
1 parent 2f94a21 commit 6a7c378

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -986,16 +986,20 @@ bool AssignmentFailure::diagnoseAsError() {
986986
// Attempt diagnostics based on the overload choice.
987987
if (choice.hasValue()) {
988988

989+
auto getKeyPathArgument = [](SubscriptExpr *expr) {
990+
auto *TE = dyn_cast<TupleExpr>(expr->getIndex());
991+
assert(TE->getNumElements() == 1);
992+
return TE->getElement(0);
993+
};
994+
989995
if (!choice->isDecl()) {
990996
if (choice->getKind() == OverloadChoiceKind::KeyPathApplication &&
991997
!isa<ApplyExpr>(immInfo.first)) {
992998
std::string message = "";
993999
if (auto *SE = dyn_cast<SubscriptExpr>(immInfo.first)) {
994-
if (auto *tupleExpr = dyn_cast<TupleExpr>(SE->getIndex())) {
995-
if (auto *DRE = dyn_cast<DeclRefExpr>(tupleExpr->getElement(0))) {
996-
auto identifier = DRE->getDecl()->getBaseName().getIdentifier();
997-
message = "'" + identifier.str().str() + "' ";
998-
}
1000+
if (auto *DRE = dyn_cast<DeclRefExpr>(getKeyPathArgument(SE))) {
1001+
auto identifier = DRE->getDecl()->getBaseName().getIdentifier();
1002+
message = "'" + identifier.str().str() + "' ";
9991003
}
10001004
}
10011005
emitDiagnostic(Loc, DeclDiagnostic, message + "is read-only")

0 commit comments

Comments
 (0)