Skip to content

Commit be2aeb5

Browse files
committed
[Sema] Rework DiscardAssignmentExpr checking wrt SequenceExpr folding
Now that we're checking DiscardAssignmentExprs in PreCheckExpr, we have to be careful to make sure we don't diagnose anything until after SequenceExprs have been folded, since the relevant AssignExprs won't even have a "right hand side" and "left hand side" until after folding.
1 parent e4ea167 commit be2aeb5

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/Sema/PreCheckExpr.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,9 @@ namespace {
814814
/// Keep track of acceptable DiscardAssignmentExpr's.
815815
llvm::SmallPtrSet<DiscardAssignmentExpr*, 2> CorrectDiscardAssignmentExprs;
816816

817+
/// The current number of nested \c SequenceExprs that we're within.
818+
unsigned SequenceExprDepth = 0;
819+
817820
/// Simplify expressions which are type sugar productions that got parsed
818821
/// as expressions due to the parser not knowing which identifiers are
819822
/// type names.
@@ -1145,6 +1148,9 @@ namespace {
11451148
if (auto *assignment = dyn_cast<AssignExpr>(expr))
11461149
markAcceptableDiscardExprs(assignment->getDest());
11471150

1151+
if (isa<SequenceExpr>(expr))
1152+
SequenceExprDepth++;
1153+
11481154
return finish(true, expr);
11491155
}
11501156

@@ -1160,6 +1166,7 @@ namespace {
11601166
// Fold sequence expressions.
11611167
if (auto *seqExpr = dyn_cast<SequenceExpr>(expr)) {
11621168
auto result = TypeChecker::foldSequence(seqExpr, DC);
1169+
SequenceExprDepth--;
11631170
return result->walk(*this);
11641171
}
11651172

@@ -1280,6 +1287,19 @@ namespace {
12801287
if (auto *simplified = simplifyTypeExpr(expr))
12811288
return simplified;
12821289

1290+
// Diagnose a '_' that isn't on the immediate LHS of an assignment. We
1291+
// skip diagnostics if we've explicitly marked the expression as valid,
1292+
// or if we're inside a SequenceExpr (since the whole tree will be
1293+
// re-checked when we finish folding anyway).
1294+
if (auto *DAE = dyn_cast<DiscardAssignmentExpr>(expr)) {
1295+
if (!CorrectDiscardAssignmentExprs.count(DAE) &&
1296+
SequenceExprDepth == 0) {
1297+
ctx.Diags.diagnose(expr->getLoc(),
1298+
diag::discard_expr_outside_of_assignment);
1299+
return nullptr;
1300+
}
1301+
}
1302+
12831303
if (auto KPE = dyn_cast<KeyPathExpr>(expr)) {
12841304
resolveKeyPathExpr(KPE);
12851305
return KPE;

0 commit comments

Comments
 (0)