Skip to content

[CS] Enforce that withPathElement is called on an lvalue #28498

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 2 commits into from
Dec 2, 2019
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
10 changes: 5 additions & 5 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1235,8 +1235,8 @@ namespace {
if (selected->choice.getKind() == OverloadChoiceKind::DeclViaDynamic) {
// Rewrite for implicit unwrapping if the solution requires it.
auto *dynamicLocator = cs.getConstraintLocator(
locator.withPathElement(ConstraintLocator::SubscriptMember)
.withPathElement(ConstraintLocator::DynamicLookupResult));
locator, {ConstraintLocator::SubscriptMember,
ConstraintLocator::DynamicLookupResult});

if (solution.getDisjunctionChoice(dynamicLocator)) {
auto *forceValue = new (cs.getASTContext())
Expand Down Expand Up @@ -6992,9 +6992,9 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, ConcreteDeclRef callee,

// We're constructing a value of nominal type. Look for the constructor or
// enum element to use.
auto ctorLocator = cs.getConstraintLocator(
locator.withPathElement(ConstraintLocator::ApplyFunction)
.withPathElement(ConstraintLocator::ConstructorMember));
auto *ctorLocator =
cs.getConstraintLocator(locator, {ConstraintLocator::ApplyFunction,
ConstraintLocator::ConstructorMember});
auto selected = solution.getOverloadChoiceIfAvailable(ctorLocator);
if (!selected) {
assert(ty->hasError() || ty->hasUnresolvedType());
Expand Down
1 change: 0 additions & 1 deletion lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,6 @@ namespace {

// Introduce conversions from each element to the element type of the
// array.
ConstraintLocatorBuilder builder(locator);
unsigned index = 0;
for (auto element : expr->getElements()) {
CS.addConstraint(ConstraintKind::Conversion,
Expand Down
17 changes: 7 additions & 10 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,10 +860,9 @@ class ArgumentFailureTracker : public MatchCallArgumentListener {

unsigned newArgIdx = Arguments.size();
auto *argLoc = CS.getConstraintLocator(
Locator
.withPathElement(LocatorPathElt::ApplyArgToParam(
newArgIdx, paramIdx, param.getParameterFlags()))
.withPathElement(LocatorPathElt::SynthesizedArgument(newArgIdx)));
Locator, {LocatorPathElt::ApplyArgToParam(newArgIdx, paramIdx,
param.getParameterFlags()),
LocatorPathElt::SynthesizedArgument(newArgIdx)});

auto *argType =
CS.createTypeVariable(argLoc, TVO_CanBindToInOut | TVO_CanBindToLValue |
Expand Down Expand Up @@ -4523,12 +4522,10 @@ ConstraintSystem::simplifyConstructionConstraint(
/*canonicalVararg=*/false);
Type resultType = fnType->getResult();

if (matchTypes(resultType, desugarValueType,
ConstraintKind::Bind,
flags,
ConstraintLocatorBuilder(locator)
.withPathElement(ConstraintLocator::ApplyFunction))
.isFailure())
ConstraintLocatorBuilder builder(locator);
if (matchTypes(resultType, desugarValueType, ConstraintKind::Bind, flags,
builder.withPathElement(ConstraintLocator::ApplyFunction))
.isFailure())
return SolutionKind::Error;

return matchTypes(argType, valueType, ConstraintKind::Conversion,
Expand Down
6 changes: 4 additions & 2 deletions lib/Sema/ConstraintLocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,10 @@ class ConstraintLocatorBuilder {
: previous(locator), element(),
summaryFlags(locator ? locator->getSummaryFlags() : 0) { }

/// Retrieve a new path with the given path element added to it.
ConstraintLocatorBuilder withPathElement(LocatorPathElt newElt) {
/// Retrieve a new path with the given path element added to it. Note that
/// the produced locator stores a reference to this locator, and therefore
/// must not outlive it.
ConstraintLocatorBuilder withPathElement(LocatorPathElt newElt) & {
unsigned newFlags = summaryFlags | newElt.getNewSummaryFlags();
if (!element)
return ConstraintLocatorBuilder(previous, newElt, newFlags);
Expand Down
29 changes: 24 additions & 5 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,25 @@ ConstraintLocator *ConstraintSystem::getConstraintLocator(
return getConstraintLocator(anchor, path, builder.getSummaryFlags());
}

ConstraintLocator *ConstraintSystem::getConstraintLocator(
ConstraintLocator *locator,
ArrayRef<ConstraintLocator::PathElement> newElts) {
auto oldPath = locator->getPath();
SmallVector<ConstraintLocator::PathElement, 4> newPath;
newPath.append(oldPath.begin(), oldPath.end());
newPath.append(newElts.begin(), newElts.end());
return getConstraintLocator(locator->getAnchor(), newPath);
}

ConstraintLocator *ConstraintSystem::getConstraintLocator(
const ConstraintLocatorBuilder &builder,
ArrayRef<ConstraintLocator::PathElement> newElts) {
SmallVector<ConstraintLocator::PathElement, 4> newPath;
auto *anchor = builder.getLocatorParts(newPath);
newPath.append(newElts.begin(), newElts.end());
return getConstraintLocator(anchor, newPath);
}

ConstraintLocator *
ConstraintSystem::getCalleeLocator(ConstraintLocator *locator,
bool lookThroughApply) {
Expand Down Expand Up @@ -1218,11 +1237,11 @@ void ConstraintSystem::openGenericRequirements(
break;
}

addConstraint(
*openedReq,
locator.withPathElement(LocatorPathElt::OpenedGeneric(signature))
.withPathElement(
LocatorPathElt::TypeParameterRequirement(pos, kind)));
auto openedGenericLoc =
locator.withPathElement(LocatorPathElt::OpenedGeneric(signature));
addConstraint(*openedReq,
openedGenericLoc.withPathElement(
LocatorPathElt::TypeParameterRequirement(pos, kind)));
}
}

Expand Down
15 changes: 13 additions & 2 deletions lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2024,10 +2024,21 @@ class ConstraintSystem {
ConstraintLocator *
getConstraintLocator(ConstraintLocator *locator,
ConstraintLocator::PathElement pathElt) {
return getConstraintLocator(ConstraintLocatorBuilder(locator)
.withPathElement(pathElt));
ConstraintLocatorBuilder builder(locator);
return getConstraintLocator(builder.withPathElement(pathElt));
}

/// Extend the given constraint locator with an array of path elements.
ConstraintLocator *
getConstraintLocator(ConstraintLocator *locator,
ArrayRef<ConstraintLocator::PathElement> newElts);

/// Retrieve the locator described by a given builder extended by an array of
/// path elements.
ConstraintLocator *
getConstraintLocator(const ConstraintLocatorBuilder &builder,
ArrayRef<ConstraintLocator::PathElement> newElts);

/// Retrieve the constraint locator described by the given
/// builder.
ConstraintLocator *
Expand Down