mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[ConstraintSystem] Clarify DefaultClosureType constraint and add its own simplify method
This commit is contained in:
@@ -6448,13 +6448,14 @@ 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,
|
||||
addUnsolvedConstraint(
|
||||
Constraint::create(*this, ConstraintKind::Defaultable, first, second,
|
||||
getConstraintLocator(locator)));
|
||||
return SolutionKind::Solved;
|
||||
}
|
||||
@@ -6466,6 +6467,28 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyDefaultableConstraint(
|
||||
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;
|
||||
}
|
||||
|
||||
return SolutionKind::Unsolved;
|
||||
}
|
||||
|
||||
// Otherwise, any type is fine.
|
||||
return SolutionKind::Solved;
|
||||
}
|
||||
|
||||
ConstraintSystem::SolutionKind
|
||||
ConstraintSystem::simplifyOneWayConstraint(
|
||||
ConstraintKind kind,
|
||||
@@ -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(),
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
|
||||
@@ -410,8 +416,8 @@ 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);
|
||||
Type First, Type Second, ConstraintLocator *locator,
|
||||
ArrayRef<TypeVariableType *> extraTypeVars = {});
|
||||
|
||||
/// Create a new constraint.
|
||||
static Constraint *create(ConstraintSystem &cs, ConstraintKind Kind,
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user