[CSApply] Load l-value before wrapping it in try expression

Just like `try?` other types of try - `try` and `try!` have to load
the value before using it.

Resolve: rdar://78102266
This commit is contained in:
Pavel Yaskevich
2021-05-21 16:44:04 -07:00
parent 99c837b413
commit 8ab8b2e3e9
2 changed files with 28 additions and 1 deletions

View File

@@ -3412,7 +3412,21 @@ namespace {
}
Expr *visitAnyTryExpr(AnyTryExpr *expr) {
cs.setType(expr, cs.getType(expr->getSubExpr()));
auto *subExpr = expr->getSubExpr();
auto type = simplifyType(cs.getType(subExpr));
// Let's load the value associated with this try.
if (type->hasLValueType()) {
subExpr = coerceToType(subExpr, type->getRValueType(),
cs.getConstraintLocator(subExpr));
if (!subExpr)
return nullptr;
}
cs.setType(expr, cs.getType(subExpr));
expr->setSubExpr(subExpr);
return expr;
}

View File

@@ -0,0 +1,13 @@
// RUN: %target-swift-frontend %s -typecheck
struct Info {
}
class Test {
var info: Info = Info()
init() throws {}
}
_ = try Test().info // Ok
_ = try! Test().info // Ok