Skip to content

[NFC][Sema] Changing API to facilitate recording pontential holes recursively #37209

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
May 3, 2021
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
3 changes: 2 additions & 1 deletion include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3225,7 +3225,8 @@ class ConstraintSystem {
/// subsequent solution would be worse than the best known solution.
bool recordFix(ConstraintFix *fix, unsigned impact = 1);

void recordPotentialHole(Type type);
void recordPotentialHole(TypeVariableType *typeVar);
void recordAnyTypeVarAsPotentialHole(Type type);

void recordMatchCallArgumentResult(ConstraintLocator *locator,
MatchCallArgumentResult result);
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4085,8 +4085,7 @@ ConstraintSystem::applyPropertyWrapperToParameter(
if (!shouldAttemptFixes())
return getTypeMatchFailure(locator);

if (paramType->hasTypeVariable())
recordPotentialHole(paramType);
recordAnyTypeVarAsPotentialHole(paramType);

auto *loc = getConstraintLocator(locator);
auto *fix = RemoveProjectedValueArgument::create(*this, wrapperType, param, loc);
Expand Down
49 changes: 19 additions & 30 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3771,13 +3771,6 @@ bool ConstraintSystem::repairFailures(
});
};

auto markAnyTypeVarsAsPotentialHoles = [&](Type type) {
type.visit([&](Type subType) {
if (auto *typeVar = subType->getAs<TypeVariableType>())
recordPotentialHole(typeVar);
});
};

if (repairArrayLiteralUsedAsDictionary(*this, lhs, rhs, matchKind,
conversionsOrFixes,
getConstraintLocator(locator)))
Expand Down Expand Up @@ -4721,10 +4714,7 @@ bool ConstraintSystem::repairFailures(
// of the conversion (or pattern match) to have holes. This
// helps when conversion if between a type and a tuple e.g.
// `Int` vs. `(_, _)`.
rhs.visit([&](Type type) {
if (auto *typeVar = type->getAs<TypeVariableType>())
recordPotentialHole(typeVar);
});
recordAnyTypeVarAsPotentialHole(rhs);

conversionsOrFixes.push_back(CollectionElementContextualMismatch::create(
*this, lhs, rhs, getConstraintLocator(locator)));
Expand Down Expand Up @@ -4803,8 +4793,8 @@ bool ConstraintSystem::repairFailures(
}

case ConstraintLocator::TernaryBranch: {
markAnyTypeVarsAsPotentialHoles(lhs);
markAnyTypeVarsAsPotentialHoles(rhs);
recordAnyTypeVarAsPotentialHole(lhs);
recordAnyTypeVarAsPotentialHole(rhs);

// If `if` expression has a contextual type, let's consider it a source of
// truth and produce a contextual mismatch instead of per-branch failure,
Expand Down Expand Up @@ -4844,8 +4834,8 @@ bool ConstraintSystem::repairFailures(
// element pattern, call it a contextual mismatch.
auto pattern = elt.castTo<LocatorPathElt::PatternMatch>().getPattern();
if (lhs->is<FunctionType>() && isa<EnumElementPattern>(pattern)) {
markAnyTypeVarsAsPotentialHoles(lhs);
markAnyTypeVarsAsPotentialHoles(rhs);
recordAnyTypeVarAsPotentialHole(lhs);
recordAnyTypeVarAsPotentialHole(rhs);

conversionsOrFixes.push_back(ContextualMismatch::create(
*this, lhs, rhs, getConstraintLocator(locator)));
Expand Down Expand Up @@ -8344,10 +8334,7 @@ ConstraintSystem::simplifyValueWitnessConstraint(
if (!shouldAttemptFixes())
return SolutionKind::Error;

memberType.visit([&](Type type) {
if (auto *typeVar = type->getAs<TypeVariableType>())
recordPotentialHole(typeVar);
});
recordAnyTypeVarAsPotentialHole(memberType);

return SolutionKind::Solved;
}
Expand Down Expand Up @@ -8492,11 +8479,7 @@ ConstraintSystem::simplifyOneWayConstraint(

// Propagate holes through one-way constraints.
if (secondSimplified->isPlaceholder()) {
first.visit([&](Type subType) {
if (auto *typeVar = subType->getAs<TypeVariableType>())
recordPotentialHole(typeVar);
});

recordAnyTypeVarAsPotentialHole(first);
return SolutionKind::Solved;
}

Expand Down Expand Up @@ -9944,7 +9927,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(
if (auto *typeVar = type2->getAs<TypeVariableType>()) {
auto *locator = typeVar->getImpl().getLocator();
if (typeVar->isPlaceholder() || hasFixFor(locator))
recordPotentialHole(func1);
recordAnyTypeVarAsPotentialHole(func1);
}
}

Expand Down Expand Up @@ -10176,7 +10159,7 @@ ConstraintSystem::simplifyApplicableFnConstraint(

// If there are any type variables associated with arguments/result
// they have to be marked as "holes".
recordPotentialHole(func1);
recordAnyTypeVarAsPotentialHole(func1);

if (desugar2->isPlaceholder())
return SolutionKind::Solved;
Expand Down Expand Up @@ -10450,7 +10433,7 @@ ConstraintSystem::simplifyDynamicCallableApplicableFnConstraint(
return SolutionKind::Error;

recordPotentialHole(tv);
recordPotentialHole(func1);
recordAnyTypeVarAsPotentialHole(func1);

return SolutionKind::Solved;
}
Expand Down Expand Up @@ -11149,8 +11132,14 @@ bool ConstraintSystem::recordFix(ConstraintFix *fix, unsigned impact) {
return false;
}

void ConstraintSystem::recordPotentialHole(Type type) {
assert(type->hasTypeVariable());
void ConstraintSystem::recordPotentialHole(TypeVariableType *typeVar) {
typeVar->getImpl().enableCanBindToHole(getSavedBindings());
}

void ConstraintSystem::recordAnyTypeVarAsPotentialHole(Type type) {
if (!type->hasTypeVariable())
return;

type.visit([&](Type type) {
if (auto *typeVar = type->getAs<TypeVariableType>())
typeVar->getImpl().enableCanBindToHole(getSavedBindings());
Expand Down Expand Up @@ -11261,7 +11250,7 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
newTupleTypes.push_back(smallerElt);
} else {
if (largerElt.getType()->isTypeVariableOrMember())
recordPotentialHole(largerElt.getType());
recordAnyTypeVarAsPotentialHole(largerElt.getType());
}
}
auto matchingType =
Expand Down