[ConstraintSystem] Clarify DefaultClosureType constraint and add its own simplify method

This commit is contained in:
Pavel Yaskevich
2019-12-16 12:15:50 -08:00
parent 58f615dfd2
commit 9257b29465
4 changed files with 58 additions and 17 deletions

View File

@@ -6448,14 +6448,37 @@ ConstraintSystem::simplifyValueWitnessConstraint(
}
ConstraintSystem::SolutionKind ConstraintSystem::simplifyDefaultableConstraint(
ConstraintKind kind, Type first, Type second, TypeMatchOptions flags,
Type first, Type second, TypeMatchOptions flags,
ConstraintLocatorBuilder locator) {
first = getFixedTypeRecursive(first, flags, true);
if (first->isTypeVariableOrMember()) {
if (flags.contains(TMF_GenerateConstraints)) {
addUnsolvedConstraint(Constraint::create(*this, kind, first, second,
getConstraintLocator(locator)));
addUnsolvedConstraint(
Constraint::create(*this, ConstraintKind::Defaultable, first, second,
getConstraintLocator(locator)));
return SolutionKind::Solved;
}
return SolutionKind::Unsolved;
}
// Otherwise, any type is fine.
return SolutionKind::Solved;
}
ConstraintSystem::SolutionKind
ConstraintSystem::simplifyDefaultClosureTypeConstraint(
Type closureType, Type inferredType,
ArrayRef<TypeVariableType *> referencedOuterParameters,
TypeMatchOptions flags, ConstraintLocatorBuilder locator) {
closureType = getFixedTypeRecursive(closureType, flags, /*wantRValue=*/true);
if (closureType->isTypeVariableOrMember()) {
if (flags.contains(TMF_GenerateConstraints)) {
addUnsolvedConstraint(Constraint::create(
*this, ConstraintKind::DefaultClosureType, closureType, inferredType,
getConstraintLocator(locator), referencedOuterParameters));
return SolutionKind::Solved;
}
@@ -8857,9 +8880,7 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
return simplifyOptionalObjectConstraint(first, second, subflags, locator);
case ConstraintKind::Defaultable:
case ConstraintKind::DefaultClosureType:
return simplifyDefaultableConstraint(kind, first, second, subflags,
locator);
return simplifyDefaultableConstraint(first, second, subflags, locator);
case ConstraintKind::FunctionInput:
case ConstraintKind::FunctionResult:
@@ -8876,6 +8897,7 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
case ConstraintKind::Disjunction:
case ConstraintKind::KeyPath:
case ConstraintKind::KeyPathApplication:
case ConstraintKind::DefaultClosureType:
llvm_unreachable("Use the correct addConstraint()");
}
@@ -9270,13 +9292,18 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
constraint.getLocator());
case ConstraintKind::Defaultable:
case ConstraintKind::DefaultClosureType:
return simplifyDefaultableConstraint(constraint.getKind(),
constraint.getFirstType(),
return simplifyDefaultableConstraint(constraint.getFirstType(),
constraint.getSecondType(),
TMF_GenerateConstraints,
constraint.getLocator());
case ConstraintKind::DefaultClosureType:
return simplifyDefaultClosureTypeConstraint(constraint.getFirstType(),
constraint.getSecondType(),
constraint.getTypeVariables(),
TMF_GenerateConstraints,
constraint.getLocator());
case ConstraintKind::FunctionInput:
case ConstraintKind::FunctionResult:
return simplifyFunctionComponentConstraint(constraint.getKind(),

View File

@@ -608,13 +608,16 @@ bool Constraint::isExplicitConversion() const {
Constraint *Constraint::create(ConstraintSystem &cs, ConstraintKind kind,
Type first, Type second,
ConstraintLocator *locator) {
ConstraintLocator *locator,
ArrayRef<TypeVariableType *> extraTypeVars) {
// Collect type variables.
SmallVector<TypeVariableType *, 4> typeVars;
if (first->hasTypeVariable())
first->getTypeVariables(typeVars);
if (second && second->hasTypeVariable())
second->getTypeVariables(typeVars);
typeVars.append(extraTypeVars.begin(), extraTypeVars.end());
uniqueTypeVariables(typeVars);
// Conformance constraints expect an existential on the right-hand side.

View File

@@ -161,8 +161,14 @@ enum class ConstraintKind : char {
/// type). At that point, this constraint will be treated like an `Equal`
/// constraint.
OneWayEqual,
/// If there are no contextual types e.g. `_ = { 42 }` default first type
/// to a second type (inferred closure type).
/// If there is no contextual info e.g. `_ = { 42 }` default first type
/// to a second type (inferred closure type). This is effectively a
/// `Defaultable` constraint which a couple of differences:
///
/// - References inferred closure type and all of the outer parameters
/// referenced by closure body.
/// - Handled specially by binding inference, specifically contributes
/// to the bindings only if there are no contextual types available.
DefaultClosureType,
};
@@ -409,9 +415,9 @@ class Constraint final : public llvm::ilist_node<Constraint>,
public:
/// Create a new constraint.
static Constraint *create(ConstraintSystem &cs, ConstraintKind Kind,
Type First, Type Second,
ConstraintLocator *locator);
static Constraint *create(ConstraintSystem &cs, ConstraintKind Kind,
Type First, Type Second, ConstraintLocator *locator,
ArrayRef<TypeVariableType *> extraTypeVars = {});
/// Create a new constraint.
static Constraint *create(ConstraintSystem &cs, ConstraintKind Kind,

View File

@@ -3481,11 +3481,16 @@ private:
ConstraintLocatorBuilder locator);
/// Attempt to simplify the given defaultable constraint.
SolutionKind simplifyDefaultableConstraint(ConstraintKind kind,
Type first, Type second,
SolutionKind simplifyDefaultableConstraint(Type first, Type second,
TypeMatchOptions flags,
ConstraintLocatorBuilder locator);
/// Attempt to simplify the given defaultable closure type constraint.
SolutionKind simplifyDefaultClosureTypeConstraint(
Type closureType, Type inferredType,
ArrayRef<TypeVariableType *> referencedOuterParameters,
TypeMatchOptions flags, ConstraintLocatorBuilder locator);
/// Attempt to simplify a one-way constraint.
SolutionKind simplifyOneWayConstraint(ConstraintKind kind,
Type first, Type second,