Skip to content

Commit b32386b

Browse files
committed
[CS] If locator points to a function call, then compare the fn and semanticFn, otherwise fall back to paren check
This is because otherwise we would have false positives, like 'Foo(Bar())' where Foo's init accepts a non-optional Bar and Bar's init returns an IUO
1 parent 532498b commit b32386b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2199,9 +2199,14 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
21992199
if (!type || !type->is<AnyFunctionType>())
22002200
return false;
22012201

2202-
auto paren = getParentExpr(locator->getAnchor());
2203-
auto result = paren ? isa<ParenExpr>(paren) : false;
2204-
return result;
2202+
if (auto call = dyn_cast<CallExpr>(locator->getAnchor()))
2203+
return call->getSemanticFn() != call->getFn();
2204+
2205+
if (auto paren = getParentExpr(locator->getAnchor())) {
2206+
return isa<ParenExpr>(paren);
2207+
}
2208+
2209+
return false;
22052210
};
22062211

22072212
// In some cases we already created the appropriate bind constraints.

test/Constraints/iuo.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,14 @@ let sr_10492_int1: Int = (sr_10492_s.foo)() // expected-error {{value of optiona
235235
// expected-note@-1 {{coalesce}}{{44-44= ?? <#default value#>}}
236236
// expected-note@-2 {{force-unwrap}}{{44-44=!}}
237237
let sr_10492_int2: Int? = (sr_10492_s.foo)() // Okay
238+
239+
240+
class SR_10492_C1 {
241+
init!() {}
242+
}
243+
244+
class SR_10492_C2 {
245+
init(_ foo: SR_10492_C1) {}
246+
}
247+
248+
let bar = SR_10492_C2(SR_10492_C1()) // Okay

0 commit comments

Comments
 (0)