Skip to content

Commit ac89df5

Browse files
committed
[CSFix] Generalize a fix for unresolved pattern decl
The fix should support both named (i.e. `test(a)` and "any" patterns i.e. `test(_)`.
1 parent dee6a31 commit ac89df5

File tree

4 files changed

+21
-22
lines changed

4 files changed

+21
-22
lines changed

include/swift/Sema/CSFix.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ enum class FixKind : uint8_t {
298298
/// Ignore `ErrorExpr` or `ErrorType` during pre-check.
299299
IgnoreInvalidASTNode,
300300

301-
/// Ignore a named pattern whose type we couldn't infer. This issue should
302-
/// already have been diagnosed elsewhere.
303-
IgnoreInvalidNamedPattern,
301+
/// Ignore a named or `_` pattern whose type we couldn't infer.
302+
/// This issue should already have been diagnosed elsewhere.
303+
IgnoreUnresolvedPatternVar,
304304

305305
/// Resolve type of `nil` by providing a contextual type.
306306
SpecifyContextualTypeForNil,
@@ -2746,10 +2746,10 @@ class IgnoreInvalidASTNode final : public ConstraintFix {
27462746
}
27472747
};
27482748

2749-
class IgnoreInvalidNamedPattern final : public ConstraintFix {
2750-
IgnoreInvalidNamedPattern(ConstraintSystem &cs, NamedPattern *pattern,
2751-
ConstraintLocator *locator)
2752-
: ConstraintFix(cs, FixKind::IgnoreInvalidNamedPattern, locator) {}
2749+
class IgnoreUnresolvedPatternVar final : public ConstraintFix {
2750+
IgnoreUnresolvedPatternVar(ConstraintSystem &cs, Pattern *pattern,
2751+
ConstraintLocator *locator)
2752+
: ConstraintFix(cs, FixKind::IgnoreUnresolvedPatternVar, locator) {}
27532753

27542754
public:
27552755
std::string getName() const override {
@@ -2762,12 +2762,11 @@ class IgnoreInvalidNamedPattern final : public ConstraintFix {
27622762
return diagnose(*commonFixes.front().first);
27632763
}
27642764

2765-
static IgnoreInvalidNamedPattern *create(ConstraintSystem &cs,
2766-
NamedPattern *pattern,
2767-
ConstraintLocator *locator);
2765+
static IgnoreUnresolvedPatternVar *
2766+
create(ConstraintSystem &cs, Pattern *pattern, ConstraintLocator *locator);
27682767

27692768
static bool classof(ConstraintFix *fix) {
2770-
return fix->getKind() == FixKind::IgnoreInvalidNamedPattern;
2769+
return fix->getKind() == FixKind::IgnoreUnresolvedPatternVar;
27712770
}
27722771
};
27732772

lib/Sema/CSBindings.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ bool BindingSet::isDelayed() const {
115115
// allows us to produce more specific errors because the type variable in
116116
// the expression that introduced the placeholder might be diagnosable using
117117
// fixForHole.
118-
if (locator->isLastElement<LocatorPathElt::NamedPatternDecl>()) {
118+
if (locator->isLastElement<LocatorPathElt::PatternDecl>()) {
119119
return true;
120120
}
121121

@@ -2061,16 +2061,16 @@ TypeVariableBinding::fixForHole(ConstraintSystem &cs) const {
20612061
return std::make_pair(fix, /*impact=*/(unsigned)10);
20622062
}
20632063

2064-
if (auto pattern = getAsPattern<NamedPattern>(dstLocator->getAnchor())) {
2064+
if (auto pattern = getAsPattern(dstLocator->getAnchor())) {
20652065
if (dstLocator->getPath().size() == 1 &&
2066-
dstLocator->isLastElement<LocatorPathElt::NamedPatternDecl>()) {
2066+
dstLocator->isLastElement<LocatorPathElt::PatternDecl>()) {
20672067
// Not being able to infer the type of a variable in a pattern binding
20682068
// decl is more dramatic than anything that could happen inside the
20692069
// expression because we want to preferrably point the diagnostic to a
20702070
// part of the expression that caused us to be unable to infer the
20712071
// variable's type.
20722072
ConstraintFix *fix =
2073-
IgnoreInvalidNamedPattern::create(cs, pattern, dstLocator);
2073+
IgnoreUnresolvedPatternVar::create(cs, pattern, dstLocator);
20742074
return std::make_pair(fix, /*impact=*/(unsigned)100);
20752075
}
20762076
}

lib/Sema/CSFix.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2004,18 +2004,18 @@ IgnoreResultBuilderWithReturnStmts::create(ConstraintSystem &cs, Type builderTy,
20042004
IgnoreResultBuilderWithReturnStmts(cs, builderTy, locator);
20052005
}
20062006

2007-
bool IgnoreInvalidNamedPattern::diagnose(const Solution &solution,
2008-
bool asNote) const {
2007+
bool IgnoreUnresolvedPatternVar::diagnose(const Solution &solution,
2008+
bool asNote) const {
20092009
// Not being able to infer the type of a pattern should already have been
20102010
// diagnosed on the pattern's initializer or as a structural issue of the AST.
20112011
return true;
20122012
}
20132013

2014-
IgnoreInvalidNamedPattern *
2015-
IgnoreInvalidNamedPattern::create(ConstraintSystem &cs, NamedPattern *pattern,
2016-
ConstraintLocator *locator) {
2014+
IgnoreUnresolvedPatternVar *
2015+
IgnoreUnresolvedPatternVar::create(ConstraintSystem &cs, Pattern *pattern,
2016+
ConstraintLocator *locator) {
20172017
return new (cs.getAllocator())
2018-
IgnoreInvalidNamedPattern(cs, pattern, locator);
2018+
IgnoreUnresolvedPatternVar(cs, pattern, locator);
20192019
}
20202020

20212021
bool SpecifyBaseTypeForOptionalUnresolvedMember::diagnose(

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12938,7 +12938,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1293812938
case FixKind::IgnoreInvalidASTNode: {
1293912939
return recordFix(fix, 10) ? SolutionKind::Error : SolutionKind::Solved;
1294012940
}
12941-
case FixKind::IgnoreInvalidNamedPattern: {
12941+
case FixKind::IgnoreUnresolvedPatternVar: {
1294212942
return recordFix(fix, 100) ? SolutionKind::Error : SolutionKind::Solved;
1294312943
}
1294412944

0 commit comments

Comments
 (0)