Skip to content

Commit bc3562d

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 6cc8cce commit bc3562d

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

@@ -1989,16 +1989,16 @@ TypeVariableBinding::fixForHole(ConstraintSystem &cs) const {
19891989
return std::make_pair(fix, /*impact=*/(unsigned)10);
19901990
}
19911991

1992-
if (auto pattern = getAsPattern<NamedPattern>(dstLocator->getAnchor())) {
1992+
if (auto pattern = getAsPattern(dstLocator->getAnchor())) {
19931993
if (dstLocator->getPath().size() == 1 &&
1994-
dstLocator->isLastElement<LocatorPathElt::NamedPatternDecl>()) {
1994+
dstLocator->isLastElement<LocatorPathElt::PatternDecl>()) {
19951995
// Not being able to infer the type of a variable in a pattern binding
19961996
// decl is more dramatic than anything that could happen inside the
19971997
// expression because we want to preferrably point the diagnostic to a
19981998
// part of the expression that caused us to be unable to infer the
19991999
// variable's type.
20002000
ConstraintFix *fix =
2001-
IgnoreInvalidNamedPattern::create(cs, pattern, dstLocator);
2001+
IgnoreUnresolvedPatternVar::create(cs, pattern, dstLocator);
20022002
return std::make_pair(fix, /*impact=*/(unsigned)100);
20032003
}
20042004
}

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
@@ -12961,7 +12961,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1296112961
case FixKind::IgnoreInvalidASTNode: {
1296212962
return recordFix(fix, 10) ? SolutionKind::Error : SolutionKind::Solved;
1296312963
}
12964-
case FixKind::IgnoreInvalidNamedPattern: {
12964+
case FixKind::IgnoreUnresolvedPatternVar: {
1296512965
return recordFix(fix, 100) ? SolutionKind::Error : SolutionKind::Solved;
1296612966
}
1296712967

0 commit comments

Comments
 (0)