Skip to content

Commit 6d8b798

Browse files
committed
[CS] Have isLastElement take a locator path elt class
This makes it consistent with `getLastElementAs` and `castLastElementTo`.
1 parent 9121a6e commit 6d8b798

File tree

4 files changed

+19
-21
lines changed

4 files changed

+19
-21
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -767,8 +767,8 @@ void GenericArgumentsMismatchFailure::emitNoteForMismatch(int position) {
767767
// to parameter conversions, let's use parameter type as a source of
768768
// generic parameter information.
769769
auto paramSourceTy =
770-
locator->isLastElement(ConstraintLocator::ApplyArgToParam) ? getRequired()
771-
: getActual();
770+
locator->isLastElement<LocatorPathElt::ApplyArgToParam>() ? getRequired()
771+
: getActual();
772772

773773
auto genericTypeDecl = paramSourceTy->getAnyGeneric();
774774
auto param = genericTypeDecl->getGenericParams()->getParams()[position];
@@ -1246,7 +1246,7 @@ bool MissingOptionalUnwrapFailure::diagnoseAsError() {
12461246
// r-value adjustment because base could be an l-value type.
12471247
// We want to fix both cases by only diagnose one of them,
12481248
// otherwise this is just going to result in a duplcate diagnostic.
1249-
if (getLocator()->isLastElement(ConstraintLocator::UnresolvedMember))
1249+
if (getLocator()->isLastElement<LocatorPathElt::UnresolvedMember>())
12501250
return false;
12511251

12521252
if (auto assignExpr = dyn_cast<AssignExpr>(anchor))
@@ -2035,7 +2035,7 @@ bool ContextualFailure::diagnoseConversionToNil() const {
20352035

20362036
Optional<ContextualTypePurpose> CTP;
20372037
// Easy case were failure has been identified as contextual already.
2038-
if (locator->isLastElement(ConstraintLocator::ContextualType)) {
2038+
if (locator->isLastElement<LocatorPathElt::ContextualType>()) {
20392039
CTP = getContextualTypePurpose();
20402040
} else {
20412041
// Here we need to figure out where where `nil` is located.
@@ -3596,7 +3596,7 @@ bool MissingArgumentsFailure::diagnoseAsError() {
35963596
//
35973597
// foo(bar) // `() -> Void` vs. `(Int) -> Void`
35983598
// ```
3599-
if (locator->isLastElement(ConstraintLocator::ApplyArgToParam)) {
3599+
if (locator->isLastElement<LocatorPathElt::ApplyArgToParam>()) {
36003600
auto info = *getFunctionArgApplyInfo(locator);
36013601

36023602
auto *argExpr = info.getArgExpr();
@@ -3612,7 +3612,7 @@ bool MissingArgumentsFailure::diagnoseAsError() {
36123612
// func foo() {}
36133613
// let _: (Int) -> Void = foo
36143614
// ```
3615-
if (locator->isLastElement(ConstraintLocator::ContextualType)) {
3615+
if (locator->isLastElement<LocatorPathElt::ContextualType>()) {
36163616
auto &cs = getConstraintSystem();
36173617
emitDiagnostic(anchor->getLoc(), diag::cannot_convert_initializer_value,
36183618
getType(anchor), resolveType(cs.getContextualType()));
@@ -3885,7 +3885,7 @@ bool MissingArgumentsFailure::diagnoseClosure(ClosureExpr *closure) {
38853885

38863886
bool MissingArgumentsFailure::diagnoseInvalidTupleDestructuring() const {
38873887
auto *locator = getLocator();
3888-
if (!locator->isLastElement(ConstraintLocator::ApplyArgument))
3888+
if (!locator->isLastElement<LocatorPathElt::ApplyArgument>())
38893889
return false;
38903890

38913891
if (SynthesizedArgs.size() < 2)

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,7 @@ ConstraintSystem::TypeMatchResult constraints::matchCallArguments(
995995
auto isSynthesizedArgument = [](const AnyFunctionType::Param &arg) -> bool {
996996
if (auto *typeVar = arg.getPlainType()->getAs<TypeVariableType>()) {
997997
auto *locator = typeVar->getImpl().getLocator();
998-
return locator->isLastElement(ConstraintLocator::SynthesizedArgument);
998+
return locator->isLastElement<LocatorPathElt::SynthesizedArgument>();
999999
}
10001000

10011001
return false;
@@ -3233,7 +3233,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
32333233
// TODO(diagnostics): Only binding here for function types, because
32343234
// doing so for KeyPath types leaves the constraint system in an
32353235
// unexpected state for key path diagnostics should we fail.
3236-
if (locator->isLastElement(ConstraintLocator::KeyPathType) &&
3236+
if (locator->isLastElement<LocatorPathElt::KeyPathType>() &&
32373237
type2->is<AnyFunctionType>())
32383238
return matchTypesBindTypeVar(typeVar1, type2, kind, flags, locator,
32393239
formUnsolvedResult);

lib/Sema/ConstraintLocator.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,16 @@ bool ConstraintLocator::isForKeyPathComponent() const {
175175
});
176176
}
177177

178-
bool ConstraintLocator::isLastElement(
179-
ConstraintLocator::PathElementKind expectedKind) const {
180-
auto path = getPath();
181-
return !path.empty() && path.back().getKind() == expectedKind;
182-
}
183-
184178
bool ConstraintLocator::isForGenericParameter() const {
185-
return isLastElement(ConstraintLocator::GenericParameter);
179+
return isLastElement<LocatorPathElt::GenericParameter>();
186180
}
187181

188182
bool ConstraintLocator::isForSequenceElementType() const {
189-
return isLastElement(ConstraintLocator::SequenceElementType);
183+
return isLastElement<LocatorPathElt::SequenceElementType>();
190184
}
191185

192186
bool ConstraintLocator::isForContextualType() const {
193-
return isLastElement(ConstraintLocator::ContextualType);
187+
return isLastElement<LocatorPathElt::ContextualType>();
194188
}
195189

196190
GenericTypeParamType *ConstraintLocator::getGenericParameter() const {

lib/Sema/ConstraintLocator.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,13 @@ class ConstraintLocator : public llvm::FoldingSetNode {
441441
return path[0].castTo<T>();
442442
}
443443

444-
/// Check whether the last element in the path of this locator
445-
/// is of a given kind.
446-
bool isLastElement(ConstraintLocator::PathElementKind kind) const;
444+
/// Check whether the last element in the path of this locator (if any)
445+
/// is a given \c LocatorPathElt subclass.
446+
template <class T>
447+
bool isLastElement() const {
448+
auto path = getPath();
449+
return !path.empty() && path.back().is<T>();
450+
}
447451

448452
/// Attempts to cast the last path element of the locator to a specific
449453
/// \c LocatorPathElt subclass, returning \c None if either unsuccessful or

0 commit comments

Comments
 (0)