Sema: Fold Constraint::createFixedChoice() into Constraint::createBindOverload()

This commit is contained in:
Slava Pestov
2024-10-11 11:21:20 -04:00
parent 2eaec5a97f
commit b44bff26b5
3 changed files with 21 additions and 36 deletions

View File

@@ -558,10 +558,10 @@ public:
ValueDecl *requirement, DeclContext *useDC, ValueDecl *requirement, DeclContext *useDC,
FunctionRefKind functionRefKind, ConstraintLocator *locator); FunctionRefKind functionRefKind, ConstraintLocator *locator);
/// Create an overload-binding constraint. /// Create an overload-binding constraint, possibly with a fix.
static Constraint *createBindOverload(ConstraintSystem &cs, Type type, static Constraint *createBindOverload(ConstraintSystem &cs, Type type,
OverloadChoice choice, OverloadChoice choice,
DeclContext *useDC, DeclContext *useDC, ConstraintFix *fix,
ConstraintLocator *locator); ConstraintLocator *locator);
/// Create a restricted relational constraint. /// Create a restricted relational constraint.
@@ -575,13 +575,6 @@ public:
ConstraintFix *fix, Type first, Type second, ConstraintFix *fix, Type first, Type second,
ConstraintLocator *locator); ConstraintLocator *locator);
/// Create a bind overload choice with a fix.
/// Note: This constraint is going to be disabled by default.
static Constraint *createFixedChoice(ConstraintSystem &cs, Type type,
OverloadChoice choice,
DeclContext *useDC, ConstraintFix *fix,
ConstraintLocator *locator);
/// Create a new disjunction constraint. /// Create a new disjunction constraint.
static Constraint *createDisjunction(ConstraintSystem &cs, static Constraint *createDisjunction(ConstraintSystem &cs,
ArrayRef<Constraint *> constraints, ArrayRef<Constraint *> constraints,

View File

@@ -805,7 +805,8 @@ Constraint *Constraint::createMemberOrOuterDisjunction(
memberConstraint->setFavored(); memberConstraint->setFavored();
for (auto choice : outerAlternatives) { for (auto choice : outerAlternatives) {
constraints.push_back( constraints.push_back(
Constraint::createBindOverload(cs, first, choice, useDC, locator)); Constraint::createBindOverload(cs, first, choice, useDC, /*fix=*/nullptr,
locator));
} }
return Constraint::createDisjunction(cs, constraints, locator, ForgetChoice); return Constraint::createDisjunction(cs, constraints, locator, ForgetChoice);
} }
@@ -856,8 +857,22 @@ Constraint *Constraint::createValueWitness(
Constraint *Constraint::createBindOverload(ConstraintSystem &cs, Type type, Constraint *Constraint::createBindOverload(ConstraintSystem &cs, Type type,
OverloadChoice choice, OverloadChoice choice,
DeclContext *useDC, DeclContext *useDC,
ConstraintFix *fix,
ConstraintLocator *locator) { ConstraintLocator *locator) {
return createFixedChoice(cs, type, choice, useDC, /*fix=*/nullptr, locator); // Collect type variables.
SmallPtrSet<TypeVariableType *, 4> typeVars;
if (type->hasTypeVariable())
type->getTypeVariables(typeVars);
if (auto baseType = choice.getBaseType()) {
baseType->getTypeVariables(typeVars);
}
// Create the constraint.
auto size =
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
typeVars.size(), fix ? 1 : 0, /*hasOverloadChoice=*/1);
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
return new (mem) Constraint(type, choice, useDC, fix, locator, typeVars);
} }
Constraint *Constraint::createRestricted(ConstraintSystem &cs, Constraint *Constraint::createRestricted(ConstraintSystem &cs,
@@ -899,27 +914,6 @@ Constraint *Constraint::createFixed(ConstraintSystem &cs, ConstraintKind kind,
return new (mem) Constraint(kind, fix, first, second, locator, typeVars); return new (mem) Constraint(kind, fix, first, second, locator, typeVars);
} }
Constraint *Constraint::createFixedChoice(ConstraintSystem &cs, Type type,
OverloadChoice choice,
DeclContext *useDC,
ConstraintFix *fix,
ConstraintLocator *locator) {
// Collect type variables.
SmallPtrSet<TypeVariableType *, 4> typeVars;
if (type->hasTypeVariable())
type->getTypeVariables(typeVars);
if (auto baseType = choice.getBaseType()) {
baseType->getTypeVariables(typeVars);
}
// Create the constraint.
auto size =
totalSizeToAlloc<TypeVariableType *, ConstraintFix *, OverloadChoice>(
typeVars.size(), fix ? 1 : 0, /*hasOverloadChoice=*/1);
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
return new (mem) Constraint(type, choice, useDC, fix, locator, typeVars);
}
Constraint *Constraint::createDisjunction(ConstraintSystem &cs, Constraint *Constraint::createDisjunction(ConstraintSystem &cs,
ArrayRef<Constraint *> constraints, ArrayRef<Constraint *> constraints,
ConstraintLocator *locator, ConstraintLocator *locator,

View File

@@ -3831,10 +3831,8 @@ void ConstraintSystem::generateConstraints(
if (requiresFix && !fix) if (requiresFix && !fix)
return; return;
auto *choice = fix ? Constraint::createFixedChoice(*this, type, overload, auto *choice = Constraint::createBindOverload(*this, type, overload,
useDC, fix, locator) useDC, fix, locator);
: Constraint::createBindOverload(*this, type, overload,
useDC, locator);
if (isFavored) if (isFavored)
choice->setFavored(); choice->setFavored();