Skip to content

Commit 755321f

Browse files
authored
Merge pull request #21348 from slavapestov/optional-try-sillyness
Sema: Fix applying solution for OptionalTryExpr
2 parents 0a9464f + 48e89f4 commit 755321f

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
@@ -2802,23 +2802,14 @@ namespace {
28022802
return simplifyExprType(expr);
28032803
}
28042804

2805-
Type subExprType = cs.getType(expr->getSubExpr());
2806-
Type targetType = simplifyType(subExprType);
2807-
2808-
// If the subexpression is not optional, wrap it in
2809-
// an InjectIntoOptionalExpr. Then use the type of the
2810-
// subexpression as the type of the 'try?' expr
2811-
bool subExprIsOptional = (bool) subExprType->getOptionalObjectType();
2812-
2813-
if (!subExprIsOptional) {
2814-
targetType = OptionalType::get(targetType);
2815-
auto subExpr = coerceToType(expr->getSubExpr(), targetType,
2816-
cs.getConstraintLocator(expr));
2817-
if (!subExpr) return nullptr;
2818-
expr->setSubExpr(subExpr);
2819-
}
2820-
2821-
cs.setType(expr, targetType);
2805+
Type exprType = simplifyType(cs.getType(expr));
2806+
2807+
auto subExpr = coerceToType(expr->getSubExpr(), exprType,
2808+
cs.getConstraintLocator(expr));
2809+
if (!subExpr) return nullptr;
2810+
expr->setSubExpr(subExpr);
2811+
2812+
cs.setType(expr, exprType);
28222813
return expr;
28232814
}
28242815

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)