Skip to content

Commit deb58e0

Browse files
authored
Merge pull request #30022 from LucianoPAlmeida/SR-9839-convention-function-conversions-fail
[SR-9839] Fixes ambiguity in convention function argument inference
2 parents 7068536 + 426a2f8 commit deb58e0

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2770,14 +2770,22 @@ static bool diagnoseConflictingGenericArguments(ConstraintSystem &cs,
27702770
if (!diff.overloads.empty())
27712771
return false;
27722772

2773-
if (!llvm::all_of(solutions, [](const Solution &solution) -> bool {
2773+
bool noFixes = llvm::all_of(solutions, [](const Solution &solution) -> bool {
2774+
const auto score = solution.getFixedScore();
2775+
return score.Data[SK_Fix] == 0 && solution.Fixes.empty();
2776+
});
2777+
2778+
bool allMismatches =
2779+
llvm::all_of(solutions, [](const Solution &solution) -> bool {
27742780
return llvm::all_of(
27752781
solution.Fixes, [](const ConstraintFix *fix) -> bool {
27762782
return fix->getKind() == FixKind::AllowArgumentTypeMismatch ||
27772783
fix->getKind() == FixKind::AllowFunctionTypeMismatch ||
27782784
fix->getKind() == FixKind::AllowTupleTypeMismatch;
27792785
});
2780-
}))
2786+
});
2787+
2788+
if (!noFixes && !allMismatches)
27812789
return false;
27822790

27832791
auto &DE = cs.getASTContext().Diags;
@@ -2930,6 +2938,11 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
29302938
if (solutions.empty())
29312939
return false;
29322940

2941+
SolutionDiff solutionDiff(solutions);
2942+
2943+
if (diagnoseConflictingGenericArguments(*this, solutionDiff, solutions))
2944+
return true;
2945+
29332946
if (auto bestScore = solverState->BestScore) {
29342947
solutions.erase(llvm::remove_if(solutions,
29352948
[&](const Solution &solution) {
@@ -2945,11 +2958,6 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
29452958
return false;
29462959
}
29472960

2948-
SolutionDiff solutionDiff(solutions);
2949-
2950-
if (diagnoseConflictingGenericArguments(*this, solutionDiff, solutions))
2951-
return true;
2952-
29532961
if (diagnoseAmbiguityWithEphemeralPointers(*this, solutions))
29542962
return true;
29552963

test/expr/closure/closures.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,24 @@ let closure = { // expected-error {{unable to infer complex closure return type;
483483
var helper = true
484484
return helper
485485
}
486+
487+
// SR-9839
488+
func SR9839(_ x: @escaping @convention(block) () -> Void) {}
489+
490+
func id<T>(_ x: T) -> T {
491+
return x
492+
}
493+
494+
var qux: () -> Void = {}
495+
496+
SR9839(qux)
497+
SR9839(id(qux)) // expected-error {{conflicting arguments to generic parameter 'T' ('() -> Void' vs. '@convention(block) () -> Void')}}
498+
499+
func forceUnwrap<T>(_ x: T?) -> T {
500+
return x!
501+
}
502+
503+
var qux1: (() -> Void)? = {}
504+
505+
SR9839(qux1!)
506+
SR9839(forceUnwrap(qux1))

0 commit comments

Comments
 (0)