[Type checker] Factor out ConstraintSystem::addJoinConstraint().

Use it for ternary expressions; it'll gain other clients soon.
This commit is contained in:
Doug Gregor
2019-10-11 10:07:21 -07:00
parent f692420e31
commit eb61ae7971
3 changed files with 48 additions and 12 deletions

View File

@@ -8075,6 +8075,35 @@ void ConstraintSystem::addConstraint(ConstraintKind kind, Type first,
}
}
Type ConstraintSystem::addJoinConstraint(
ConstraintLocator *locator,
ArrayRef<std::pair<Type, ConstraintLocator *>> inputs) {
switch (inputs.size()) {
case 0:
return Type();
case 1:
return inputs.front().first;
default:
// Produce the join below.
break;
}
// Create a type variable to capture the result of the join.
Type resultTy = createTypeVariable(locator,
(TVO_PrefersSubtypeBinding |
TVO_CanBindToNoEscape));
// Introduce conversions from each input type to the type variable.
for (const auto &input : inputs) {
addConstraint(
ConstraintKind::Conversion, input.first, resultTy, input.second);
}
return resultTy;
}
void ConstraintSystem::addExplicitConversionConstraint(
Type fromType, Type toType,
bool allowFixes,