Skip to content

[CSSolver] NFC: Replace some uses of last() with endsWith() #64322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5861,7 +5861,7 @@ ArgumentList *ExprRewriter::coerceCallArguments(
ArgumentList *args, AnyFunctionType *funcType, ConcreteDeclRef callee,
ApplyExpr *apply, ConstraintLocatorBuilder locator,
ArrayRef<AppliedPropertyWrapper> appliedPropertyWrappers) {
assert(locator.last() && locator.last()->is<LocatorPathElt::ApplyArgument>());
assert(locator.endsWith<LocatorPathElt::ApplyArgument>());

auto &ctx = getConstraintSystem().getASTContext();
auto params = funcType->getParams();
Expand Down Expand Up @@ -7282,8 +7282,7 @@ Expr *ExprRewriter::coerceToType(Expr *expr, Type toType,

/// Whether the given effect is polymorphic at this location.
auto isEffectPolymorphic = [&](EffectKind kind) -> bool {
auto last = locator.last();
if (!(last && last->is<LocatorPathElt::ApplyArgToParam>()))
if (!locator.endsWith<LocatorPathElt::ApplyArgToParam>())
return false;

if (auto *call = getAsExpr<ApplyExpr>(locator.getAnchor())) {
Expand Down
117 changes: 49 additions & 68 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3007,12 +3007,9 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
return getTypeMatchFailure(locator);
}

bool forClosureInArgumentPosition = false;
if (auto last = locator.last()) {
forClosureInArgumentPosition =
last->is<LocatorPathElt::ApplyArgToParam>() &&
isa<ClosureExpr>(locator.trySimplifyToExpr());
}
bool forClosureInArgumentPosition =
locator.endsWith<LocatorPathElt::ApplyArgToParam>() &&
isa<ClosureExpr>(locator.trySimplifyToExpr());

// Since it's possible to infer `async` from the body of a
// closure, score for sync -> async mismatch is increased
Expand Down Expand Up @@ -4255,12 +4252,10 @@ ConstraintSystem::matchTypesBindTypeVar(
// to a particular type e.g. `l-value` or `inout`.
auto fixReferenceMismatch = [&](TypeVariableType *typeVar,
Type type) -> bool {
if (auto last = locator.last()) {
if (last->is<LocatorPathElt::ContextualType>()) {
auto *fix = IgnoreContextualType::create(*this, typeVar, type,
getConstraintLocator(locator));
return !recordFix(fix);
}
if (locator.endsWith<LocatorPathElt::ContextualType>()) {
auto *fix = IgnoreContextualType::create(*this, typeVar, type,
getConstraintLocator(locator));
return !recordFix(fix);
}

return false;
Expand Down Expand Up @@ -4561,8 +4556,7 @@ repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
// `let _: Bool = try? foo()` and `foo()` produces `Int`
// we should diagnose it as type mismatch instead of missing unwrap.
bool possibleContextualMismatch = [&]() {
auto last = locator.last();
if (!(last && last->is<LocatorPathElt::ContextualType>()))
if (!locator.endsWith<LocatorPathElt::ContextualType>())
return false;

// If the contextual type is optional as well, it's definitely a
Expand Down Expand Up @@ -4695,8 +4689,7 @@ repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
// variable e.g. `T?`, there can be no optional mismatch
// because `T` could be bound to an optional of any depth.
if (isa<OptionalEvaluationExpr>(anchor) && toUnwraps > 0) {
auto last = locator.last();
if (last && last->is<LocatorPathElt::ContextualType>() &&
if (locator.endsWith<LocatorPathElt::ContextualType>() &&
toObjectType->is<TypeVariableType>())
return false;
}
Expand Down Expand Up @@ -6664,13 +6657,11 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
// producing diagnostics for both missing conformance and
// invalid element type.
if (shouldAttemptFixes()) {
if (auto last = locator.last()) {
if (last->is<LocatorPathElt::SequenceElementType>() &&
desugar1->is<DependentMemberType>() &&
!desugar1->hasTypeVariable()) {
recordPotentialHole(typeVar2);
return getTypeMatchSuccess();
}
if (locator.endsWith<LocatorPathElt::SequenceElementType>() &&
desugar1->is<DependentMemberType>() &&
!desugar1->hasTypeVariable()) {
recordPotentialHole(typeVar2);
return getTypeMatchSuccess();
}
}

Expand Down Expand Up @@ -6911,35 +6902,33 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
ConstraintLocatorBuilder location{locator};
// Look through all value-to-optional promotions to allow
// conversions like Double -> CGFloat?? and vice versa.
if (auto last = location.last()) {
// T -> Optional<T>
if (last->is<LocatorPathElt::OptionalPayload>()) {
SmallVector<LocatorPathElt, 4> path;
auto anchor = location.getLocatorParts(path);

// An attempt at Double/CGFloat conversion through
// optional chaining. This is not supported at the
// moment because solution application doesn't know
// how to map Double to/from CGFloat through optionals.
if (isExpr<OptionalEvaluationExpr>(anchor)) {
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);
// T -> Optional<T>
if (location.endsWith<LocatorPathElt::OptionalPayload>()) {
SmallVector<LocatorPathElt, 4> path;
auto anchor = location.getLocatorParts(path);

// An attempt at Double/CGFloat conversion through
// optional chaining. This is not supported at the
// moment because solution application doesn't know
// how to map Double to/from CGFloat through optionals.
if (isExpr<OptionalEvaluationExpr>(anchor)) {
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);

conversionsOrFixes.push_back(ContextualMismatch::create(
*this, nominal1, nominal2, getConstraintLocator(locator)));
break;
}
conversionsOrFixes.push_back(ContextualMismatch::create(
*this, nominal1, nominal2, getConstraintLocator(locator)));
break;
}

// Drop all of the applied `value-to-optional` promotions.
path.erase(llvm::remove_if(
path,
[](const LocatorPathElt &elt) {
return elt.is<LocatorPathElt::OptionalPayload>();
}),
path.end());
// Drop all of the applied `value-to-optional` promotions.
path.erase(llvm::remove_if(
path,
[](const LocatorPathElt &elt) {
return elt.is<LocatorPathElt::OptionalPayload>();
}),
path.end());

location = getConstraintLocator(anchor, path);
}
location = getConstraintLocator(anchor, path);
}

// Support implicit Double<->CGFloat conversions only for
Expand Down Expand Up @@ -8122,12 +8111,10 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
// as well, and if so, avoid producing a fix here, because
// contextual mismatch mentions the source/destination
// types of the assignment.
if (auto last = locator.last()) {
if (last->is<LocatorPathElt::FunctionResult>() &&
hasFixFor(getConstraintLocator(anchor,
LocatorPathElt::FunctionArgument())))
return SolutionKind::Solved;
}
if (locator.endsWith<LocatorPathElt::FunctionResult>() &&
hasFixFor(
getConstraintLocator(anchor, LocatorPathElt::FunctionArgument())))
return SolutionKind::Solved;

auto srcType = getType(assignment->getSrc());
auto dstType = getType(assignment->getDest());
Expand Down Expand Up @@ -8450,9 +8437,7 @@ static ConstraintFix *maybeWarnAboutExtraneousCast(
SmallVector<Type, 4> toOptionals,
ConstraintSystem::TypeMatchOptions flags,
ConstraintLocatorBuilder locator) {

auto last = locator.last();
if (last && last->is<LocatorPathElt::GenericArgument>())
if (locator.endsWith<LocatorPathElt::GenericArgument>())
return nullptr;

// Both types have to be fixed.
Expand Down Expand Up @@ -11906,11 +11891,9 @@ ConstraintSystem::simplifyKeyPathApplicationConstraint(

// When locator points to a KeyPathDynamicMemberLookup, reject the
// key path application.
auto last = locator.last();
if (last && last->isKeyPathDynamicMember()) {
if (locator.endsWith<LocatorPathElt::KeyPathDynamicMember>())
return SolutionKind::Error;
}


if (keyPathTy->isAnyKeyPath()) {
// Read-only keypath, whose projected value is upcast to `Any?`.
// The root type can be anything.
Expand Down Expand Up @@ -14157,12 +14140,10 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
// an l-value, it has to have an increased impact because it's either
// a function - which is completely incorrect, or it's a get-only
// subscript, which requires changes to declaration to become mutable.
if (auto last = locator.last()) {
impact += (last->is<LocatorPathElt::FunctionResult>() ||
last->is<LocatorPathElt::SubscriptMember>())
? 1
: 0;
}
impact += (locator.endsWith<LocatorPathElt::FunctionResult>() ||
locator.endsWith<LocatorPathElt::SubscriptMember>())
? 1
: 0;

return recordFix(fix, impact) ? SolutionKind::Error : SolutionKind::Solved;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/CSSyntacticElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1411,8 +1411,7 @@ bool ConstraintSystem::isInResultBuilderContext(ClosureExpr *closure) const {
}

bool isConditionOfStmt(ConstraintLocatorBuilder locator) {
auto last = locator.last();
if (!(last && last->is<LocatorPathElt::Condition>()))
if (!locator.endsWith<LocatorPathElt::Condition>())
return false;

SmallVector<LocatorPathElt, 4> path;
Expand Down
14 changes: 4 additions & 10 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1458,10 +1458,8 @@ unwrapPropertyWrapperParameterTypes(ConstraintSystem &cs, AbstractFunctionDecl *
//
// Note: If the transform is ever enabled for patterns - new branch
// would have to be added to `nameLoc` selection.
if (auto last = locator.last()) {
if (last->is<LocatorPathElt::PatternMatch>())
return functionType;
}
if (locator.endsWith<LocatorPathElt::PatternMatch>())
return functionType;

auto *paramList = funcDecl->getParameters();
auto paramTypes = functionType->getParams();
Expand Down Expand Up @@ -1510,12 +1508,8 @@ unwrapPropertyWrapperParameterTypes(ConstraintSystem &cs, AbstractFunctionDecl *

/// Determine whether the given locator is for a witness or requirement.
static bool isRequirementOrWitness(const ConstraintLocatorBuilder &locator) {
if (auto last = locator.last()) {
return last->getKind() == ConstraintLocator::ProtocolRequirement ||
last->getKind() == ConstraintLocator::Witness;
}

return false;
return locator.endsWith<LocatorPathElt::ProtocolRequirement>() ||
locator.endsWith<LocatorPathElt::Witness>();
}

AnyFunctionType *ConstraintSystem::adjustFunctionTypeForConcurrency(
Expand Down