Skip to content

Commit f5158f8

Browse files
authored
Merge pull request swiftlang#79023 from xedin/rdar-143799118
[CSOptimizer] Disjunctions with IUO overload choices are unsupported
2 parents 1aded39 + 471ee21 commit f5158f8

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/Sema/CSOptimizer.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,14 @@ static bool isSupportedDisjunction(Constraint *disjunction) {
128128
if (choice->getKind() != ConstraintKind::BindOverload)
129129
return false;
130130

131-
if (auto *decl = getOverloadChoiceDecl(choice))
131+
if (auto *decl = getOverloadChoiceDecl(choice)) {
132+
// Cannot optimize declarations that return IUO because
133+
// they form a disjunction over a result type once attempted.
134+
if (decl->isImplicitlyUnwrappedOptional())
135+
return false;
136+
132137
return decl->getInterfaceType()->is<FunctionType>();
138+
}
133139

134140
return false;
135141
});
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-typecheck-verify-swift -target %target-swift-5.1-abi-triple
2+
3+
func test1(v: Int!) -> [Any]! { nil }
4+
// This is important because it defeats old solver hack that
5+
// checked the number of matching overloads purely based on
6+
// how many parameters there are.
7+
func test1(v: Int!) async throws -> [Int]! { nil }
8+
func test1(v: Int!, other: String = "") throws -> [Int] { [] }
9+
10+
func test2(v: Int!) -> [Any]! { nil }
11+
func test2(v: Int!, other: String = "") throws -> [Int] { [] }
12+
13+
func performTest(v: Int!) {
14+
guard let _ = test1(v: v) as? [Int] else { // Ok
15+
return
16+
}
17+
18+
guard let _ = test2(v: v) as? [Int] else {
19+
// expected-error@-1 {{call can throw, but it is not marked with 'try' and the error is not handled}}
20+
// expected-warning@-2 {{conditional cast from '[Int]' to '[Int]' always succeeds}}
21+
return
22+
}
23+
}

0 commit comments

Comments
 (0)