Skip to content

Commit d5944ff

Browse files
committed
[ConstraintLocator] NFC: Introduce a way to determine whether locator points directly to a specific expression
1 parent 9db7144 commit d5944ff

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

lib/Sema/ConstraintLocator.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,18 +234,15 @@ bool ConstraintLocator::isForContextualType() const {
234234
}
235235

236236
bool ConstraintLocator::isForAssignment() const {
237-
auto *anchor = getAnchor();
238-
return anchor && isa<AssignExpr>(anchor) && getPath().empty();
237+
return directlyAt<AssignExpr>();
239238
}
240239

241240
bool ConstraintLocator::isForCoercion() const {
242-
auto *anchor = getAnchor();
243-
return anchor && isa<CoerceExpr>(anchor) && getPath().empty();
241+
return directlyAt<CoerceExpr>();
244242
}
245243

246244
bool ConstraintLocator::isForOptionalTry() const {
247-
auto *anchor = getAnchor();
248-
return anchor && isa<OptionalTryExpr>(anchor) && getPath().empty();
245+
return directlyAt<OptionalTryExpr>();
249246
}
250247

251248
GenericTypeParamType *ConstraintLocator::getGenericParameter() const {

lib/Sema/ConstraintLocator.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ class ConstraintLocator : public llvm::FoldingSetNode {
374374
/// Determine whether this locator points to the `try?` expression.
375375
bool isForOptionalTry() const;
376376

377+
/// Determine whether this locator points directly to a given expression.
378+
template <class E> bool directlyAt() const {
379+
auto *anchor = getAnchor();
380+
return anchor && isa<E>(anchor) && getPath().empty();
381+
}
382+
377383
/// Attempts to cast the first path element of the locator to a specific
378384
/// \c LocatorPathElt subclass, returning \c None if either unsuccessful or
379385
/// the locator has no path elements.

0 commit comments

Comments
 (0)