diff --git a/lib/Sema/CSApply.cpp b/lib/Sema/CSApply.cpp index 00951b71d9e..e044b4d00c4 100644 --- a/lib/Sema/CSApply.cpp +++ b/lib/Sema/CSApply.cpp @@ -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; } diff --git a/validation-test/Sema/type_checker_crashers_fixed/rdar78102266.swift b/validation-test/Sema/type_checker_crashers_fixed/rdar78102266.swift new file mode 100644 index 00000000000..704728f2f16 --- /dev/null +++ b/validation-test/Sema/type_checker_crashers_fixed/rdar78102266.swift @@ -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