Skip to content

Commit fc020bb

Browse files
committed
[CS] Add ConstraintSystem::addFixConstraint
This lets us add a constraint to the system with an associated fix. Unlike `addUnsolvedConstraint`, this will attempt to immediately simplify the constraint, producing an unsolved constraint if necessary.
1 parent 9d21fbb commit fc020bb

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8157,6 +8157,30 @@ Type ConstraintSystem::addJoinConstraint(
81578157
return resultTy;
81588158
}
81598159

8160+
void ConstraintSystem::addFixConstraint(ConstraintFix *fix, ConstraintKind kind,
8161+
Type first, Type second,
8162+
ConstraintLocatorBuilder locator,
8163+
bool isFavored) {
8164+
TypeMatchOptions subflags = TMF_GenerateConstraints;
8165+
switch (simplifyFixConstraint(fix, first, second, kind, subflags, locator)) {
8166+
case SolutionKind::Error:
8167+
// Add a failing constraint, if needed.
8168+
if (shouldAddNewFailingConstraint()) {
8169+
auto c = Constraint::createFixed(*this, kind, fix, first, second,
8170+
getConstraintLocator(locator));
8171+
if (isFavored) c->setFavored();
8172+
addNewFailingConstraint(c);
8173+
}
8174+
return;
8175+
8176+
case SolutionKind::Unsolved:
8177+
llvm_unreachable("should have generated constraints");
8178+
8179+
case SolutionKind::Solved:
8180+
return;
8181+
}
8182+
}
8183+
81608184
void ConstraintSystem::addExplicitConversionConstraint(
81618185
Type fromType, Type toType,
81628186
bool allowFixes,

lib/Sema/ConstraintSystem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,6 +2151,12 @@ class ConstraintSystem {
21512151
Type addJoinConstraint(ConstraintLocator *locator,
21522152
ArrayRef<std::pair<Type, ConstraintLocator *>> inputs);
21532153

2154+
/// Add a constraint to the constraint system with an associated fix.
2155+
void addFixConstraint(ConstraintFix *fix, ConstraintKind kind,
2156+
Type first, Type second,
2157+
ConstraintLocatorBuilder locator,
2158+
bool isFavored = false);
2159+
21542160
/// Add a key path application constraint to the constraint system.
21552161
void addKeyPathApplicationConstraint(Type keypath, Type root, Type value,
21562162
ConstraintLocatorBuilder locator,

0 commit comments

Comments
 (0)