Skip to content

[SR-9839] Fixes ambiguity in convention function argument inference #30022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2763,14 +2763,22 @@ static bool diagnoseConflictingGenericArguments(ConstraintSystem &cs,
if (!diff.overloads.empty())
return false;

if (!llvm::all_of(solutions, [](const Solution &solution) -> bool {
bool noFixes = llvm::all_of(solutions, [](const Solution &solution) -> bool {
const auto score = solution.getFixedScore();
return score.Data[SK_Fix] == 0 && solution.Fixes.empty();
});

bool allMismatches =
llvm::all_of(solutions, [](const Solution &solution) -> bool {
return llvm::all_of(
solution.Fixes, [](const ConstraintFix *fix) -> bool {
return fix->getKind() == FixKind::AllowArgumentTypeMismatch ||
fix->getKind() == FixKind::AllowFunctionTypeMismatch ||
fix->getKind() == FixKind::AllowTupleTypeMismatch;
});
}))
});

if (!noFixes && !allMismatches)
return false;

auto &DE = cs.getASTContext().Diags;
Expand Down Expand Up @@ -2923,6 +2931,11 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
if (solutions.empty())
return false;

SolutionDiff solutionDiff(solutions);

if (diagnoseConflictingGenericArguments(*this, solutionDiff, solutions))
return true;

if (auto bestScore = solverState->BestScore) {
solutions.erase(llvm::remove_if(solutions,
[&](const Solution &solution) {
Expand All @@ -2938,11 +2951,6 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
return false;
}

SolutionDiff solutionDiff(solutions);

if (diagnoseConflictingGenericArguments(*this, solutionDiff, solutions))
return true;

if (diagnoseAmbiguityWithEphemeralPointers(*this, solutions))
return true;

Expand Down
21 changes: 21 additions & 0 deletions test/expr/closure/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,24 @@ let closure = { // expected-error {{unable to infer complex closure return type;
var helper = true
return helper
}

// SR-9839
func SR9839(_ x: @escaping @convention(block) () -> Void) {}

func id<T>(_ x: T) -> T {
return x
}

var qux: () -> Void = {}

SR9839(qux)
SR9839(id(qux)) // expected-error {{conflicting arguments to generic parameter 'T' ('() -> Void' vs. '@convention(block) () -> Void')}}

func forceUnwrap<T>(_ x: T?) -> T {
return x!
}

var qux1: (() -> Void)? = {}

SR9839(qux1!)
SR9839(forceUnwrap(qux1))