Skip to content

Commit bcde8b5

Browse files
[CSFix] Diagnosing ambiguious involving FunctionArgument mismatches and overloaded ref expression argument
1 parent 38306a9 commit bcde8b5

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ ERROR(no_candidates_match_result_type,none,
245245
"no '%0' candidates produce the expected contextual result type %1",
246246
(StringRef, Type))
247247

248+
ERROR(no_candidates_match_argument_type,none,
249+
"no '%0' candidates produce the expected parameter type %1",
250+
(StringRef, Type))
251+
248252
ERROR(cannot_infer_closure_type,none,
249253
"unable to infer closure type in the current context", ())
250254
ERROR(cannot_infer_closure_result_type,none,

lib/Sema/CSFix.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,32 @@ bool AllowFunctionTypeMismatch::coalesceAndDiagnose(
366366
return failure.diagnose(asNote);
367367
}
368368

369+
bool AllowFunctionTypeMismatch::diagnoseForAmbiguity(CommonFixesArray commonFixes) const {
370+
const Solution *solution;
371+
const ConstraintFix *fix;
372+
std::tie(solution, fix) = commonFixes.front();
373+
auto applyInfo = solution->getFunctionArgApplyInfo(fix->getLocator());
374+
if (!applyInfo)
375+
return false;
376+
377+
if (auto *ODRE = dyn_cast<OverloadedDeclRefExpr>(applyInfo->getArgExpr())) {
378+
auto &DE = getConstraintSystem().getASTContext().Diags;
379+
380+
DE.diagnose(ODRE->getLoc(), diag::no_candidates_match_argument_type,
381+
ODRE->getDecls()[0]->getName().getBaseName().userFacingName(),
382+
applyInfo->getParamType());
383+
384+
for (auto decl : ODRE->getDecls()) {
385+
if (decl->getLoc().isValid())
386+
DE.diagnose(decl->getLoc(), diag::found_candidate_type,
387+
decl->getInterfaceType());
388+
}
389+
return true;
390+
}
391+
392+
return false;
393+
}
394+
369395
bool AllowFunctionTypeMismatch::diagnose(const Solution &solution,
370396
bool asNote) const {
371397
return coalesceAndDiagnose(solution, {}, asNote);

lib/Sema/CSFix.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,9 @@ class AllowFunctionTypeMismatch final : public ContextualMismatch {
10621062
bool coalesceAndDiagnose(const Solution &solution,
10631063
ArrayRef<ConstraintFix *> secondaryFixes,
10641064
bool asNote = false) const override;
1065-
1065+
1066+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override;
1067+
10661068
bool diagnose(const Solution &solution, bool asNote = false) const override;
10671069
};
10681070

test/Sema/diag_ambiguous_overloads.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,10 @@ func SR12689_2(_ u: Int) {}
151151

152152
SR12689_2(SR12689_1(1 as Double)) // expected-error {{no exact matches in call to global function 'SR12689_1'}}
153153
SR12689_1(1 as Double) as Int // expected-error {{no exact matches in call to global function 'SR12689_1'}}
154+
155+
// Ambiguos OverloadRefExpr
156+
func SR12689_O(_ p: Int) {} // expected-note {{found candidate with type '(Int) -> ()'}}
157+
func SR12689_O(_ p: Double) {} // expected-note {{found candidate with type '(Double) -> ()'}}
158+
159+
func SR12689_3(_ param: (String)-> Void) {}
160+
SR12689_3(SR12689_O) // expected-error {{no 'SR12689_O' candidates produce the expected parameter type '(String) -> Void'}}

0 commit comments

Comments
 (0)