Skip to content

Commit bf4c513

Browse files
committed
[CSFix] Add a fix to use when type of closure parameter can't be inferred
1 parent 2b8518b commit bf4c513

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/Sema/CSFix.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "ConstraintSystem.h"
2323
#include "OverloadChoice.h"
2424
#include "swift/AST/Expr.h"
25+
#include "swift/AST/ParameterList.h"
2526
#include "swift/AST/Type.h"
2627
#include "swift/AST/Types.h"
2728
#include "swift/Basic/SourceManager.h"
@@ -1229,6 +1230,37 @@ SpecifyBaseTypeForContextualMember *SpecifyBaseTypeForContextualMember::create(
12291230
SpecifyBaseTypeForContextualMember(cs, member, locator);
12301231
}
12311232

1233+
std::string SpecifyClosureParameterType::getName() const {
1234+
std::string name;
1235+
llvm::raw_string_ostream OS(name);
1236+
1237+
auto *closure = castToExpr<ClosureExpr>(getAnchor());
1238+
auto paramLoc =
1239+
getLocator()->castLastElementTo<LocatorPathElt::TupleElement>();
1240+
1241+
auto *PD = closure->getParameters()->get(paramLoc.getIndex());
1242+
1243+
OS << "specify type for parameter ";
1244+
if (PD->isAnonClosureParam()) {
1245+
OS << "$" << paramLoc.getIndex();
1246+
} else {
1247+
OS << "'" << PD->getParameterName() << "'";
1248+
}
1249+
1250+
return OS.str();
1251+
}
1252+
1253+
bool SpecifyClosureParameterType::diagnose(const Solution &solution,
1254+
bool asNote) const {
1255+
return false;
1256+
}
1257+
1258+
SpecifyClosureParameterType *
1259+
SpecifyClosureParameterType::create(ConstraintSystem &cs,
1260+
ConstraintLocator *locator) {
1261+
return new (cs.getAllocator()) SpecifyClosureParameterType(cs, locator);
1262+
}
1263+
12321264
bool SpecifyClosureReturnType::diagnose(const Solution &solution,
12331265
bool asNote) const {
12341266
UnableToInferClosureReturnType failure(solution, getLocator());

lib/Sema/CSFix.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,10 @@ enum class FixKind : uint8_t {
234234
/// inferred and has to be specified explicitly.
235235
SpecifyBaseTypeForContextualMember,
236236

237+
/// Type of the closure parameter used in the body couldn't be inferred
238+
/// and has to be specified explicitly.
239+
SpecifyClosureParameterType,
240+
237241
/// Closure return type has to be explicitly specified because it can't be
238242
/// inferred in current context e.g. because it's a multi-statement closure.
239243
SpecifyClosureReturnType,
@@ -253,7 +257,7 @@ enum class FixKind : uint8_t {
253257

254258
/// A warning fix that allows a coercion to perform a force-cast.
255259
AllowCoercionToForceCast,
256-
260+
257261
/// Allow key path root type mismatch when applying a key path that has a
258262
/// root type not convertible to the type of the base instance.
259263
AllowKeyPathRootTypeMismatch,
@@ -1712,6 +1716,19 @@ class SpecifyBaseTypeForContextualMember final : public ConstraintFix {
17121716
create(ConstraintSystem &cs, DeclNameRef member, ConstraintLocator *locator);
17131717
};
17141718

1719+
class SpecifyClosureParameterType final : public ConstraintFix {
1720+
SpecifyClosureParameterType(ConstraintSystem &cs, ConstraintLocator *locator)
1721+
: ConstraintFix(cs, FixKind::SpecifyClosureParameterType, locator) {}
1722+
1723+
public:
1724+
std::string getName() const;
1725+
1726+
bool diagnose(const Solution &solution, bool asNote = false) const;
1727+
1728+
static SpecifyClosureParameterType *create(ConstraintSystem &cs,
1729+
ConstraintLocator *locator);
1730+
};
1731+
17151732
class SpecifyClosureReturnType final : public ConstraintFix {
17161733
SpecifyClosureReturnType(ConstraintSystem &cs, ConstraintLocator *locator)
17171734
: ConstraintFix(cs, FixKind::SpecifyClosureReturnType, locator) {}

0 commit comments

Comments
 (0)