Skip to content

Commit 7e4eb9c

Browse files
committed
[Diagnostics] Diagnose inability to infer type of a closure parameter
1 parent bf4c513 commit 7e4eb9c

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,9 @@ ERROR(no_candidates_match_result_type,none,
250250
"no '%0' candidates produce the expected contextual result type %1",
251251
(StringRef, Type))
252252

253+
ERROR(cannot_infer_closure_parameter_type,none,
254+
"unable to infer type of a closure parameter %0 in the current context",
255+
(StringRef))
253256
ERROR(cannot_infer_closure_type,none,
254257
"unable to infer closure type in the current context", ())
255258
ERROR(cannot_infer_closure_result_type,none,

lib/Sema/CSDiagnostics.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6125,6 +6125,28 @@ bool MissingContextualBaseInMemberRefFailure::diagnoseAsError() {
61256125
return true;
61266126
}
61276127

6128+
bool UnableToInferClosureParameterType::diagnoseAsError() {
6129+
auto *closure = castToExpr<ClosureExpr>(getRawAnchor());
6130+
auto paramIdx = getLocator()
6131+
->castLastElementTo<LocatorPathElt::TupleElement>()
6132+
.getIndex();
6133+
6134+
auto *PD = closure->getParameters()->get(paramIdx);
6135+
6136+
llvm::SmallString<16> id;
6137+
llvm::raw_svector_ostream OS(id);
6138+
6139+
if (PD->isAnonClosureParam()) {
6140+
OS << "$" << paramIdx;
6141+
} else {
6142+
OS << "'" << PD->getParameterName() << "'";
6143+
}
6144+
6145+
auto loc = PD->isAnonClosureParam() ? getLoc() : PD->getLoc();
6146+
emitDiagnosticAt(loc, diag::cannot_infer_closure_parameter_type, OS.str());
6147+
return true;
6148+
}
6149+
61286150
bool UnableToInferClosureReturnType::diagnoseAsError() {
61296151
auto *closure = castToExpr<ClosureExpr>(getRawAnchor());
61306152

lib/Sema/CSDiagnostics.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1971,6 +1971,15 @@ class MissingContextualBaseInMemberRefFailure final : public FailureDiagnostic {
19711971
bool diagnoseAsError();
19721972
};
19731973

1974+
class UnableToInferClosureParameterType final : public FailureDiagnostic {
1975+
public:
1976+
UnableToInferClosureParameterType(const Solution &solution,
1977+
ConstraintLocator *locator)
1978+
: FailureDiagnostic(solution, locator) {}
1979+
1980+
bool diagnoseAsError();
1981+
};
1982+
19741983
class UnableToInferClosureReturnType final : public FailureDiagnostic {
19751984
public:
19761985
UnableToInferClosureReturnType(const Solution &solution,

0 commit comments

Comments
 (0)