Skip to content

Commit 6cc8cce

Browse files
committed
[ConstraintLocator] Generalize pattern decl locator to support "named" and "any"
1 parent dd40e14 commit 6cc8cce

File tree

4 files changed

+51
-2
lines changed

4 files changed

+51
-2
lines changed

include/swift/Sema/ConstraintLocator.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,39 @@ class LocatorPathElt::PatternBindingElement final
10761076
}
10771077
};
10781078

1079+
class LocatorPathElt::PatternDecl : public StoredIntegerElement<1> {
1080+
public:
1081+
PatternDecl(ConstraintLocator::PathElementKind kind)
1082+
: StoredIntegerElement(kind, /*placeholder=*/0) {
1083+
assert(classof(this) && "classof needs updating");
1084+
}
1085+
1086+
static bool classof(const LocatorPathElt *elt) {
1087+
return elt->getKind() == ConstraintLocator::NamedPatternDecl ||
1088+
elt->getKind() == ConstraintLocator::AnyPatternDecl;
1089+
}
1090+
};
1091+
1092+
class LocatorPathElt::NamedPatternDecl final
1093+
: public LocatorPathElt::PatternDecl {
1094+
public:
1095+
NamedPatternDecl() : PatternDecl(ConstraintLocator::NamedPatternDecl) {}
1096+
1097+
static bool classof(const LocatorPathElt *elt) {
1098+
return elt->getKind() == ConstraintLocator::NamedPatternDecl;
1099+
}
1100+
};
1101+
1102+
class LocatorPathElt::AnyPatternDecl final
1103+
: public LocatorPathElt::PatternDecl {
1104+
public:
1105+
AnyPatternDecl() : PatternDecl(ConstraintLocator::AnyPatternDecl) {}
1106+
1107+
static bool classof(const LocatorPathElt *elt) {
1108+
return elt->getKind() == ConstraintLocator::AnyPatternDecl;
1109+
}
1110+
};
1111+
10791112
namespace details {
10801113
template <typename CustomPathElement>
10811114
class PathElement {

include/swift/Sema/ConstraintLocatorPathElts.def

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,12 @@ CUSTOM_LOCATOR_PATH_ELT(SyntacticElement)
239239
/// The element of the pattern binding declaration.
240240
CUSTOM_LOCATOR_PATH_ELT(PatternBindingElement)
241241

242-
/// The variable declared by a named pattern.
243-
SIMPLE_LOCATOR_PATH_ELT(NamedPatternDecl)
242+
/// A declaration introduced by a pattern: name (i.e. `x`) or `_`
243+
ABSTRACT_LOCATOR_PATH_ELT(PatternDecl)
244+
/// The variable declared by a named pattern.
245+
CUSTOM_LOCATOR_PATH_ELT(NamedPatternDecl)
246+
/// The anonymous variable declared by a `_` pattern.
247+
CUSTOM_LOCATOR_PATH_ELT(AnyPatternDecl)
244248

245249
#undef LOCATOR_PATH_ELT
246250
#undef CUSTOM_LOCATOR_PATH_ELT

lib/Sema/ConstraintLocator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ unsigned LocatorPathElt::getNewSummaryFlags() const {
9898
case ConstraintLocator::PackElement:
9999
case ConstraintLocator::PatternBindingElement:
100100
case ConstraintLocator::NamedPatternDecl:
101+
case ConstraintLocator::AnyPatternDecl:
101102
return 0;
102103

103104
case ConstraintLocator::FunctionArgument:
@@ -447,6 +448,11 @@ void LocatorPathElt::dump(raw_ostream &out) const {
447448
out << "named pattern decl";
448449
break;
449450
}
451+
452+
case ConstraintLocator::AnyPatternDecl: {
453+
out << "'_' pattern decl";
454+
break;
455+
}
450456
}
451457
}
452458

lib/Sema/ConstraintSystem.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5275,6 +5275,12 @@ void constraints::simplifyLocator(ASTNode &anchor,
52755275
break;
52765276
}
52775277

5278+
case ConstraintLocator::AnyPatternDecl: {
5279+
anchor = anchor.get<Pattern *>();
5280+
path = path.slice(1);
5281+
break;
5282+
}
5283+
52785284
case ConstraintLocator::ImplicitConversion:
52795285
break;
52805286

0 commit comments

Comments
 (0)