Skip to content

Commit 8c6098a

Browse files
committed
[CSFix] Add a fix to detect when type of couldn't be inferred
1 parent e6da0c2 commit 8c6098a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/Sema/CSFix.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,3 +1603,14 @@ IgnoreInvalidFunctionBuilderBody *IgnoreInvalidFunctionBuilderBody::create(
16031603
return new (cs.getAllocator())
16041604
IgnoreInvalidFunctionBuilderBody(cs, phase, locator);
16051605
}
1606+
1607+
bool SpecifyContextualTypeForNil::diagnose(const Solution &solution,
1608+
bool asNote) const {
1609+
return false;
1610+
}
1611+
1612+
SpecifyContextualTypeForNil *
1613+
SpecifyContextualTypeForNil::create(ConstraintSystem &cs,
1614+
ConstraintLocator *locator) {
1615+
return new (cs.getAllocator()) SpecifyContextualTypeForNil(cs, locator);
1616+
}

lib/Sema/CSFix.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,9 @@ enum class FixKind : uint8_t {
279279

280280
/// Ignore function builder body which fails `pre-check` call.
281281
IgnoreInvalidFunctionBuilderBody,
282+
283+
/// Resolve type of `nil` by providing a contextual type.
284+
SpecifyContextualTypeForNil,
282285
};
283286

284287
class ConstraintFix {
@@ -2026,6 +2029,26 @@ class IgnoreInvalidFunctionBuilderBody final : public ConstraintFix {
20262029
create(ConstraintSystem &cs, ErrorInPhase phase, ConstraintLocator *locator);
20272030
};
20282031

2032+
class SpecifyContextualTypeForNil final : public ConstraintFix {
2033+
SpecifyContextualTypeForNil(ConstraintSystem &cs,
2034+
ConstraintLocator *locator)
2035+
: ConstraintFix(cs, FixKind::SpecifyContextualTypeForNil, locator) {}
2036+
2037+
public:
2038+
std::string getName() const override {
2039+
return "specify contextual type to resolve `nil` literal";
2040+
}
2041+
2042+
bool diagnose(const Solution &solution, bool asNote = false) const override;
2043+
2044+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
2045+
return diagnose(*commonFixes.front().first);
2046+
}
2047+
2048+
static SpecifyContextualTypeForNil *create(ConstraintSystem & cs,
2049+
ConstraintLocator * locator);
2050+
};
2051+
20292052
} // end namespace constraints
20302053
} // end namespace swift
20312054

0 commit comments

Comments
 (0)