Skip to content

Commit 63c7992

Browse files
authored
Merge pull request #21350 from slavapestov/optional-try-sillyness-5.0
Sema: Fix applying solution for OptionalTryExpr [5.0]
2 parents 7f25c32 + e3773f8 commit 63c7992

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

lib/Sema/CSApply.cpp

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,23 +2825,14 @@ namespace {
28252825
return simplifyExprType(expr);
28262826
}
28272827

2828-
Type subExprType = cs.getType(expr->getSubExpr());
2829-
Type targetType = simplifyType(subExprType);
2830-
2831-
// If the subexpression is not optional, wrap it in
2832-
// an InjectIntoOptionalExpr. Then use the type of the
2833-
// subexpression as the type of the 'try?' expr
2834-
bool subExprIsOptional = (bool) subExprType->getOptionalObjectType();
2835-
2836-
if (!subExprIsOptional) {
2837-
targetType = OptionalType::get(targetType);
2838-
auto subExpr = coerceToType(expr->getSubExpr(), targetType,
2839-
cs.getConstraintLocator(expr));
2840-
if (!subExpr) return nullptr;
2841-
expr->setSubExpr(subExpr);
2842-
}
2843-
2844-
cs.setType(expr, targetType);
2828+
Type exprType = simplifyType(cs.getType(expr));
2829+
2830+
auto subExpr = coerceToType(expr->getSubExpr(), exprType,
2831+
cs.getConstraintLocator(expr));
2832+
if (!subExpr) return nullptr;
2833+
expr->setSubExpr(subExpr);
2834+
2835+
cs.setType(expr, exprType);
28452836
return expr;
28462837
}
28472838

test/Parse/try.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,14 @@ let _: Int? = try? producer.produceDoubleOptionalInt() // expected-error {{value
258258
let _: Int?? = try? producer.produceDoubleOptionalInt() // expected-error {{value of optional type 'Int???' not unwrapped; did you mean to use 'try!' or chain with '?'?}}
259259
let _: Int??? = try? producer.produceDoubleOptionalInt() // good
260260
let _: String = try? producer.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int???' to specified type 'String'}}
261+
262+
// rdar://problem/46742002
263+
protocol Dummy : class {}
264+
265+
class F<T> {
266+
func wait() throws -> T { fatalError() }
267+
}
268+
269+
func bar(_ a: F<Dummy>, _ b: F<Dummy>) {
270+
_ = (try? a.wait()) === (try? b.wait())
271+
}

test/Parse/try_swift5.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,14 @@ let _: Int? = try? producer.produceDoubleOptionalInt() // expected-error {{canno
261261
let _: Int?? = try? producer.produceDoubleOptionalInt() // good
262262
let _: Int??? = try? producer.produceDoubleOptionalInt() // good
263263
let _: String = try? producer.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}
264+
265+
// rdar://problem/46742002
266+
protocol Dummy : class {}
267+
268+
class F<T> {
269+
func wait() throws -> T { fatalError() }
270+
}
271+
272+
func bar(_ a: F<Dummy>, _ b: F<Dummy>) {
273+
_ = (try? a.wait()) === (try? b.wait())
274+
}

0 commit comments

Comments
 (0)