@@ -902,13 +902,6 @@ namespace {
902
902
// / insert RebindSelfInConstructorExpr nodes.
903
903
llvm::SmallVector<Expr *, 8 > ExprStack;
904
904
905
- // / A stack, corresponding to \c ExprStack above, that indicates at each
906
- // / level whether the enclosing context is plausibly something that will
907
- // / be folded into a \c TypeExpr after \c simplifyTypeExpr is called.
908
- // / This helps us determine whether '_' should become a placeholder type or
909
- // / not, which lets us produce better diagnostics during type checking.
910
- llvm::SmallVector<bool , 8 > PossibleTypeContextStack;
911
-
912
905
// / The 'self' variable to use when rebinding 'self' in a constructor.
913
906
VarDecl *UnresolvedCtorSelf = nullptr ;
914
907
@@ -942,12 +935,12 @@ namespace {
942
935
// / the type conforms to the expected literal protocol.
943
936
Expr *simplifyTypeConstructionWithLiteralArg (Expr *E);
944
937
945
- // / Whether we are in a context that might turn out to be a \c TypeExpr
946
- // / after \c simplifyTypeExpr is called up the tree. This function allows
947
- // / us to make better guesses about whether invalid uses of '_' were
948
- // / "supposed" to be \c DiscardAssignmentExprs or patterns, which results
949
- // / in better diagnostics after type checking.
950
- bool possiblyInTypeContext ();
938
+ // / Whether the current expression \p E is in a context that might turn out
939
+ // / to be a \c TypeExpr after \c simplifyTypeExpr is called up the tree.
940
+ // / This function allows us to make better guesses about whether invalid
941
+ // / uses of '_' were "supposed" to be \c DiscardAssignmentExprs or patterns,
942
+ // / which results in better diagnostics after type checking.
943
+ bool possiblyInTypeContext (Expr *E );
951
944
952
945
// / Whether we can simplify the given discard assignment expr. Not possible
953
946
// / if it's been marked "valid" or if the current state of the AST disallows
@@ -1170,17 +1163,7 @@ namespace {
1170
1163
if (recursive) {
1171
1164
if (isa<SequenceExpr>(expr))
1172
1165
SequenceExprDepth++;
1173
-
1174
- // The next level is considered a type context if either:
1175
- // - The expression is a valid parent for a TypeExpr, or
1176
- // - The current level is a type context and the expression "looks
1177
- // like" a type (and is not a call arg).
1178
- bool shouldEnterTypeContext = expr->isValidTypeExprParent ();
1179
- bool shouldContinueTypeContext = possiblyInTypeContext () &&
1180
- exprLooksLikeAType (expr) && !CallArgs.count (expr);
1181
1166
ExprStack.push_back (expr);
1182
- PossibleTypeContextStack.push_back (shouldEnterTypeContext ||
1183
- shouldContinueTypeContext);
1184
1167
}
1185
1168
1186
1169
return std::make_pair (recursive, expr);
@@ -1302,7 +1285,6 @@ namespace {
1302
1285
// Remove this expression from the stack.
1303
1286
assert (ExprStack.back () == expr);
1304
1287
ExprStack.pop_back ();
1305
- PossibleTypeContextStack.pop_back ();
1306
1288
1307
1289
// Mark the direct callee as being a callee.
1308
1290
if (auto *call = dyn_cast<ApplyExpr>(expr))
@@ -1591,17 +1573,30 @@ TypeExpr *PreCheckExpression::simplifyUnresolvedSpecializeExpr(
1591
1573
return nullptr ;
1592
1574
}
1593
1575
1594
- bool PreCheckExpression::possiblyInTypeContext () {
1595
- return !PossibleTypeContextStack.empty () && PossibleTypeContextStack.back ();
1576
+ bool PreCheckExpression::possiblyInTypeContext (Expr *E) {
1577
+ // Walk back up the stack of parents looking for a valid type context.
1578
+ for (auto *ParentExpr : llvm::reverse (ExprStack)) {
1579
+ // We're considered to be in a type context if either:
1580
+ // - We have a valid parent for a TypeExpr, or
1581
+ // - The parent "looks like" a type (and is not a call arg), and we can
1582
+ // reach a valid parent for a TypeExpr if we continue walking.
1583
+ if (ParentExpr->isValidParentOfTypeExpr (E))
1584
+ return true ;
1585
+
1586
+ if (!exprLooksLikeAType (ParentExpr) || CallArgs.count (ParentExpr))
1587
+ return false ;
1588
+
1589
+ E = ParentExpr;
1590
+ }
1591
+ return false ;
1596
1592
}
1597
1593
1598
1594
// / Only allow simplification of a DiscardAssignmentExpr if it hasn't already
1599
1595
// / been explicitly marked as correct, and the current AST state allows it.
1600
1596
bool PreCheckExpression::canSimplifyDiscardAssignmentExpr (
1601
1597
DiscardAssignmentExpr *DAE) {
1602
- return !CorrectDiscardAssignmentExprs.count (DAE) &&
1603
- possiblyInTypeContext () &&
1604
- SequenceExprDepth == 0 ;
1598
+ return !CorrectDiscardAssignmentExprs.count (DAE) && SequenceExprDepth == 0 &&
1599
+ possiblyInTypeContext (DAE);
1605
1600
}
1606
1601
1607
1602
// / Simplify expressions which are type sugar productions that got parsed
0 commit comments