Skip to content

Commit 7c1c46d

Browse files
committed
[CSOptimizer] Make sure that all parameters without arguments are defaulted
1 parent e404ed7 commit 7c1c46d

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/Sema/CSOptimizer.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,15 +208,29 @@ static void determineBestChoicesInContext(
208208
if (!isViableOverload(decl))
209209
return;
210210

211-
if (overloadType->getNumParams() != argFuncType->getNumParams())
212-
return;
211+
ParameterListInfo paramListInfo(
212+
overloadType->getParams(), decl,
213+
hasAppliedSelf(cs, choice->getOverloadChoice()));
213214

214215
double score = 0.0;
215216
for (unsigned i = 0, n = overloadType->getNumParams(); i != n; ++i) {
217+
const auto &param = overloadType->getParams()[i];
218+
219+
if (i >= candidateArgumentTypes.size()) {
220+
// If parameter has a default - continue matching,
221+
// all of the subsequence parameters should have defaults
222+
// as well, if they don't the overload choice in not viable.
223+
if (paramListInfo.hasDefaultArgument(i))
224+
continue;
225+
226+
// Early return because this overload choice is not viable
227+
// without default value for the current parameter.
228+
return;
229+
}
230+
216231
if (candidateArgumentTypes[i].empty())
217232
continue;
218233

219-
const auto &param = overloadType->getParams()[i];
220234
const auto paramFlags = param.getParameterFlags();
221235

222236
// If parameter is variadic we cannot compare because we don't know

test/Constraints/ranking.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,3 +450,11 @@ struct HasIntInit {
450450
func compare_solutions_with_bindings(x: UInt8, y: UInt8) -> HasIntInit {
451451
return .init(Int(x / numericCast(y)))
452452
}
453+
454+
// Test to make sure that previous favoring behavior is maintained and @autoclosure makes a difference.
455+
func test_no_ambiguity_with_autoclosure(x: Int) {
456+
func test(_ condition: Bool, file: StaticString = #file, line: UInt = #line) {}
457+
func test(_ condition: @autoclosure () -> Bool, file: StaticString = #file, line: UInt = #line) {}
458+
459+
test(x >= 0) // Ok
460+
}

0 commit comments

Comments
 (0)