Skip to content

Commit f014a9a

Browse files
committed
Sema: Don't coerce subexpression of 'try' to rvalue
The change in 8ab8b2e, was too broad. We want to coerce the subexpression of `try!` to an rvalue, but not the subexpression of a `try`. If the subexpression of a `try` becomes an rvalue even though the type of the parent expression is an lvalue, we can end up with infinite recursion in coerceToType(), as demonstrated by the test case. Fixes swiftlang#85034.
1 parent 97f7efd commit f014a9a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/Sema/CSApply.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3752,7 +3752,11 @@ namespace {
37523752
return expr;
37533753
}
37543754

3755-
Expr *visitAnyTryExpr(AnyTryExpr *expr) {
3755+
Expr *visitTryExpr(TryExpr *expr) {
3756+
return simplifyExprType(expr);
3757+
}
3758+
3759+
Expr *visitForceTryExpr(ForceTryExpr *expr) {
37563760
auto *subExpr = expr->getSubExpr();
37573761
auto type = simplifyType(cs.getType(subExpr));
37583762

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
class C {
4+
var x = 0
5+
}
6+
7+
do {
8+
let x = C()
9+
let _ = (0, try x.x) // expected-warning {{no calls to throwing functions occur within 'try' expression}}
10+
}

0 commit comments

Comments
 (0)