Skip to content

Commit 80a988e

Browse files
committed
[CSGen] forcing an optional should prefer subtyping only if it's applied to a member
`TVO_PrefersSubtypeBinding` in this case is intended to support references to Objective-C imported members that can be force unwrapped before being used i.e. accessing an overloaded method on `AnyObject` should always prefer an overload that returns a subtype: ``` @objc class A: NSObject { @objc func test() -> A { } @objc func test() -> B { } } func test(obj: AnyObject) { obj.test!() } ``` where `B` is a subtype of `A` should result in selection of `() -> B`.
1 parent a892e57 commit 80a988e

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

lib/Sema/CSGen.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -2797,10 +2797,12 @@ namespace {
27972797
// Force-unwrap an optional of type T? to produce a T.
27982798
auto locator = CS.getConstraintLocator(expr);
27992799

2800-
auto objectTy = CS.createTypeVariable(locator,
2801-
TVO_PrefersSubtypeBinding |
2802-
TVO_CanBindToLValue |
2803-
TVO_CanBindToNoEscape);
2800+
auto options = TVO_CanBindToLValue | TVO_CanBindToNoEscape;
2801+
2802+
if (isExpr<UnresolvedDotExpr>(expr->getSubExpr()))
2803+
options |= TVO_PrefersSubtypeBinding;
2804+
2805+
auto objectTy = CS.createTypeVariable(locator, options);
28042806

28052807
auto *valueExpr = expr->getSubExpr();
28062808
// It's invalid to force unwrap `nil` literal e.g. `_ = nil!` or

0 commit comments

Comments
 (0)