Skip to content

Commit 179a044

Browse files
committed
[Constraint] Add an ability to attach a fix to BindOverload constraint
1 parent d9642d0 commit 179a044

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

lib/Sema/Constraint.cpp

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ Constraint::Constraint(ConstraintKind kind, Type first, Type second,
166166
}
167167

168168
Constraint::Constraint(Type type, OverloadChoice choice, DeclContext *useDC,
169-
ConstraintLocator *locator,
169+
ConstraintFix *fix, ConstraintLocator *locator,
170170
ArrayRef<TypeVariableType *> typeVars)
171-
: Kind(ConstraintKind::BindOverload), HasRestriction(false),
171+
: Kind(ConstraintKind::BindOverload), TheFix(fix), HasRestriction(false),
172172
IsActive(false), IsDisabled(false), RememberChoice(false),
173173
IsFavored(false),
174174
NumTypeVariables(typeVars.size()), Overload{type, choice, useDC},
@@ -639,18 +639,7 @@ Constraint *Constraint::createBindOverload(ConstraintSystem &cs, Type type,
639639
OverloadChoice choice,
640640
DeclContext *useDC,
641641
ConstraintLocator *locator) {
642-
// Collect type variables.
643-
SmallVector<TypeVariableType *, 4> typeVars;
644-
if (type->hasTypeVariable())
645-
type->getTypeVariables(typeVars);
646-
if (auto baseType = choice.getBaseType()) {
647-
baseType->getTypeVariables(typeVars);
648-
}
649-
650-
// Create the constraint.
651-
unsigned size = totalSizeToAlloc<TypeVariableType*>(typeVars.size());
652-
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
653-
return new (mem) Constraint(type, choice, useDC, locator, typeVars);
642+
return createFixedChoice(cs, type, choice, useDC, /*fix=*/nullptr, locator);
654643
}
655644

656645
Constraint *Constraint::createRestricted(ConstraintSystem &cs,
@@ -690,6 +679,25 @@ Constraint *Constraint::createFixed(ConstraintSystem &cs, ConstraintKind kind,
690679
return new (mem) Constraint(kind, fix, first, second, locator, typeVars);
691680
}
692681

682+
Constraint *Constraint::createFixedChoice(ConstraintSystem &cs, Type type,
683+
OverloadChoice choice,
684+
DeclContext *useDC,
685+
ConstraintFix *fix,
686+
ConstraintLocator *locator) {
687+
// Collect type variables.
688+
SmallVector<TypeVariableType *, 4> typeVars;
689+
if (type->hasTypeVariable())
690+
type->getTypeVariables(typeVars);
691+
if (auto baseType = choice.getBaseType()) {
692+
baseType->getTypeVariables(typeVars);
693+
}
694+
695+
// Create the constraint.
696+
unsigned size = totalSizeToAlloc<TypeVariableType *>(typeVars.size());
697+
void *mem = cs.getAllocator().Allocate(size, alignof(Constraint));
698+
return new (mem) Constraint(type, choice, useDC, fix, locator, typeVars);
699+
}
700+
693701
Constraint *Constraint::createDisjunction(ConstraintSystem &cs,
694702
ArrayRef<Constraint *> constraints,
695703
ConstraintLocator *locator,

lib/Sema/Constraint.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,10 @@ class Constraint final : public llvm::ilist_node<Constraint>,
339339
ConstraintLocator *locator,
340340
ArrayRef<TypeVariableType *> typeVars);
341341

342-
/// Construct a new overload-binding constraint.
342+
/// Construct a new overload-binding constraint, which might have a fix.
343343
Constraint(Type type, OverloadChoice choice, DeclContext *useDC,
344-
ConstraintLocator *locator, ArrayRef<TypeVariableType *> typeVars);
344+
ConstraintFix *fix, ConstraintLocator *locator,
345+
ArrayRef<TypeVariableType *> typeVars);
345346

346347
/// Construct a restricted constraint.
347348
Constraint(ConstraintKind kind, ConversionRestrictionKind restriction,
@@ -399,6 +400,12 @@ class Constraint final : public llvm::ilist_node<Constraint>,
399400
ConstraintFix *fix, Type first, Type second,
400401
ConstraintLocator *locator);
401402

403+
/// Create a bind overload choice with a fix.
404+
static Constraint *createFixedChoice(ConstraintSystem &cs, Type type,
405+
OverloadChoice choice,
406+
DeclContext *useDC, ConstraintFix *fix,
407+
ConstraintLocator *locator);
408+
402409
/// Create a new disjunction constraint.
403410
static Constraint *createDisjunction(ConstraintSystem &cs,
404411
ArrayRef<Constraint *> constraints,

0 commit comments

Comments
 (0)