Skip to content

Commit c0d0908

Browse files
committed
[ConstraintSystem] NFC: Generalize DefaultClosureType constraint
This constraint is useful in more places than closures because it gives a way to provide a "fallback type" without it having effect on inference.
1 parent 612a2e7 commit c0d0908

File tree

7 files changed

+47
-49
lines changed

7 files changed

+47
-49
lines changed

include/swift/Sema/Constraint.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,12 @@ enum class ConstraintKind : char {
186186
/// constraint.
187187
OneWayBindParam,
188188
/// If there is no contextual info e.g. `_ = { 42 }` default first type
189-
/// to a second type (inferred closure type). This is effectively a
190-
/// `Defaultable` constraint which a couple of differences:
189+
/// to a second type. This is effectively a `Defaultable` constraint
190+
/// which one significant difference:
191191
///
192-
/// - References inferred closure type and all of the outer parameters
193-
/// referenced by closure body.
194192
/// - Handled specially by binding inference, specifically contributes
195193
/// to the bindings only if there are no contextual types available.
196-
DefaultClosureType,
194+
FallbackType,
197195
/// The first type represents a result of an unresolved member chain,
198196
/// and the second type is its base type. This constraint acts almost
199197
/// like `Equal` but also enforces following semantics:
@@ -701,7 +699,7 @@ class Constraint final : public llvm::ilist_node<Constraint>,
701699
case ConstraintKind::OptionalObject:
702700
case ConstraintKind::OneWayEqual:
703701
case ConstraintKind::OneWayBindParam:
704-
case ConstraintKind::DefaultClosureType:
702+
case ConstraintKind::FallbackType:
705703
case ConstraintKind::UnresolvedMemberChainBase:
706704
case ConstraintKind::PackElementOf:
707705
case ConstraintKind::SameShape:

include/swift/Sema/ConstraintSystem.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4852,11 +4852,12 @@ class ConstraintSystem {
48524852
TypeMatchOptions flags,
48534853
ConstraintLocatorBuilder locator);
48544854

4855-
/// Attempt to simplify the given defaultable closure type constraint.
4856-
SolutionKind simplifyDefaultClosureTypeConstraint(
4857-
Type closureType, Type inferredType,
4858-
ArrayRef<TypeVariableType *> referencedOuterParameters,
4859-
TypeMatchOptions flags, ConstraintLocatorBuilder locator);
4855+
/// Attempt to simplify the given fallback type constraint.
4856+
SolutionKind
4857+
simplifyFallbackTypeConstraint(Type defaultableType, Type fallbackType,
4858+
ArrayRef<TypeVariableType *> referencedVars,
4859+
TypeMatchOptions flags,
4860+
ConstraintLocatorBuilder locator);
48604861

48614862
/// Attempt to simplify a property wrapper constraint.
48624863
SolutionKind simplifyPropertyWrapperConstraint(Type wrapperType, Type wrappedValueType,

lib/Sema/CSBindings.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ void BindingSet::inferTransitiveBindings(
452452

453453
// Infer transitive defaults.
454454
for (const auto &def : bindings.Defaults) {
455-
if (def.getSecond()->getKind() == ConstraintKind::DefaultClosureType)
455+
if (def.getSecond()->getKind() == ConstraintKind::FallbackType)
456456
continue;
457457

458458
addDefault(def.second);
@@ -1510,7 +1510,7 @@ void PotentialBindings::infer(Constraint *constraint) {
15101510
}
15111511

15121512
case ConstraintKind::Defaultable:
1513-
case ConstraintKind::DefaultClosureType:
1513+
case ConstraintKind::FallbackType:
15141514
// Do these in a separate pass.
15151515
if (CS.getFixedTypeRecursive(constraint->getFirstType(), true)
15161516
->getAs<TypeVariableType>() == TypeVar) {
@@ -1634,7 +1634,7 @@ void PotentialBindings::retract(Constraint *constraint) {
16341634
break;
16351635

16361636
case ConstraintKind::Defaultable:
1637-
case ConstraintKind::DefaultClosureType: {
1637+
case ConstraintKind::FallbackType: {
16381638
Defaults.erase(constraint);
16391639
break;
16401640
}
@@ -2075,11 +2075,10 @@ bool TypeVarBindingProducer::computeNext() {
20752075
if (NumTries == 0) {
20762076
// Add defaultable constraints (if any).
20772077
for (auto *constraint : DelayedDefaults) {
2078-
if (constraint->getKind() == ConstraintKind::DefaultClosureType) {
2079-
// If there are no other possible bindings for this closure
2080-
// let's default it to the type inferred from its parameters/body,
2081-
// otherwise we should only attempt contextual types as a
2082-
// top-level closure type.
2078+
if (constraint->getKind() == ConstraintKind::FallbackType) {
2079+
// If there are no other possible bindings for this variable
2080+
// let's default it to the fallback type, otherwise we should
2081+
// only attempt contextual types.
20832082
if (!ExploredTypes.empty())
20842083
continue;
20852084
}

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2921,9 +2921,9 @@ namespace {
29212921
SmallVector<TypeVariableType *, 4> referencedVars{
29222922
collectVarRefs.varRefs.begin(), collectVarRefs.varRefs.end()};
29232923

2924-
CS.addUnsolvedConstraint(Constraint::create(
2925-
CS, ConstraintKind::DefaultClosureType, closureType, inferredType,
2926-
locator, referencedVars));
2924+
CS.addUnsolvedConstraint(
2925+
Constraint::create(CS, ConstraintKind::FallbackType, closureType,
2926+
inferredType, locator, referencedVars));
29272927

29282928
CS.setClosureType(closure, inferredType);
29292929
return closureType;

lib/Sema/CSSimplify.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2284,7 +2284,7 @@ ConstraintSystem::matchTupleTypes(TupleType *tuple1, TupleType *tuple2,
22842284
case ConstraintKind::BridgingConversion:
22852285
case ConstraintKind::OneWayEqual:
22862286
case ConstraintKind::OneWayBindParam:
2287-
case ConstraintKind::DefaultClosureType:
2287+
case ConstraintKind::FallbackType:
22882288
case ConstraintKind::UnresolvedMemberChainBase:
22892289
case ConstraintKind::PropertyWrapper:
22902290
case ConstraintKind::SyntacticElement:
@@ -2643,7 +2643,7 @@ static bool matchFunctionRepresentations(FunctionType::ExtInfo einfo1,
26432643
case ConstraintKind::ValueWitness:
26442644
case ConstraintKind::OneWayEqual:
26452645
case ConstraintKind::OneWayBindParam:
2646-
case ConstraintKind::DefaultClosureType:
2646+
case ConstraintKind::FallbackType:
26472647
case ConstraintKind::UnresolvedMemberChainBase:
26482648
case ConstraintKind::PropertyWrapper:
26492649
case ConstraintKind::SyntacticElement:
@@ -3161,7 +3161,7 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
31613161
case ConstraintKind::BridgingConversion:
31623162
case ConstraintKind::OneWayEqual:
31633163
case ConstraintKind::OneWayBindParam:
3164-
case ConstraintKind::DefaultClosureType:
3164+
case ConstraintKind::FallbackType:
31653165
case ConstraintKind::UnresolvedMemberChainBase:
31663166
case ConstraintKind::PropertyWrapper:
31673167
case ConstraintKind::SyntacticElement:
@@ -6811,7 +6811,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
68116811
case ConstraintKind::ValueWitness:
68126812
case ConstraintKind::OneWayEqual:
68136813
case ConstraintKind::OneWayBindParam:
6814-
case ConstraintKind::DefaultClosureType:
6814+
case ConstraintKind::FallbackType:
68156815
case ConstraintKind::UnresolvedMemberChainBase:
68166816
case ConstraintKind::PropertyWrapper:
68176817
case ConstraintKind::SyntacticElement:
@@ -10989,18 +10989,18 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyDefaultableConstraint(
1098910989
return SolutionKind::Solved;
1099010990
}
1099110991

10992-
ConstraintSystem::SolutionKind
10993-
ConstraintSystem::simplifyDefaultClosureTypeConstraint(
10994-
Type closureType, Type inferredType,
10995-
ArrayRef<TypeVariableType *> referencedOuterParameters,
10996-
TypeMatchOptions flags, ConstraintLocatorBuilder locator) {
10997-
closureType = getFixedTypeRecursive(closureType, flags, /*wantRValue=*/true);
10992+
ConstraintSystem::SolutionKind ConstraintSystem::simplifyFallbackTypeConstraint(
10993+
Type defaultableType, Type fallbackType,
10994+
ArrayRef<TypeVariableType *> referencedVars, TypeMatchOptions flags,
10995+
ConstraintLocatorBuilder locator) {
10996+
defaultableType =
10997+
getFixedTypeRecursive(defaultableType, flags, /*wantRValue=*/true);
1099810998

10999-
if (closureType->isTypeVariableOrMember()) {
10999+
if (defaultableType->isTypeVariableOrMember()) {
1100011000
if (flags.contains(TMF_GenerateConstraints)) {
1100111001
addUnsolvedConstraint(Constraint::create(
11002-
*this, ConstraintKind::DefaultClosureType, closureType, inferredType,
11003-
getConstraintLocator(locator), referencedOuterParameters));
11002+
*this, ConstraintKind::FallbackType, defaultableType, fallbackType,
11003+
getConstraintLocator(locator), referencedVars));
1100411004
return SolutionKind::Solved;
1100511005
}
1100611006

@@ -15064,7 +15064,7 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
1506415064
case ConstraintKind::Conjunction:
1506515065
case ConstraintKind::KeyPath:
1506615066
case ConstraintKind::KeyPathApplication:
15067-
case ConstraintKind::DefaultClosureType:
15067+
case ConstraintKind::FallbackType:
1506815068
case ConstraintKind::SyntacticElement:
1506915069
llvm_unreachable("Use the correct addConstraint()");
1507015070
}
@@ -15596,12 +15596,12 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
1559615596
/*flags*/ None,
1559715597
constraint.getLocator());
1559815598

15599-
case ConstraintKind::DefaultClosureType:
15600-
return simplifyDefaultClosureTypeConstraint(constraint.getFirstType(),
15601-
constraint.getSecondType(),
15602-
constraint.getTypeVariables(),
15603-
/*flags*/ None,
15604-
constraint.getLocator());
15599+
case ConstraintKind::FallbackType:
15600+
return simplifyFallbackTypeConstraint(constraint.getFirstType(),
15601+
constraint.getSecondType(),
15602+
constraint.getTypeVariables(),
15603+
/*flags*/ None,
15604+
constraint.getLocator());
1560515605

1560615606
case ConstraintKind::PropertyWrapper:
1560715607
return simplifyPropertyWrapperConstraint(constraint.getFirstType(),

lib/Sema/Constraint.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second,
9999
llvm_unreachable("Wrong constructor for member constraint");
100100

101101
case ConstraintKind::Defaultable:
102-
case ConstraintKind::DefaultClosureType:
102+
case ConstraintKind::FallbackType:
103103
assert(!First.isNull());
104104
assert(!Second.isNull());
105105
break;
@@ -164,7 +164,7 @@ Constraint::Constraint(ConstraintKind Kind, Type First, Type Second, Type Third,
164164
case ConstraintKind::Conjunction:
165165
case ConstraintKind::OneWayEqual:
166166
case ConstraintKind::OneWayBindParam:
167-
case ConstraintKind::DefaultClosureType:
167+
case ConstraintKind::FallbackType:
168168
case ConstraintKind::UnresolvedMemberChainBase:
169169
case ConstraintKind::PropertyWrapper:
170170
case ConstraintKind::SyntacticElement:
@@ -314,7 +314,7 @@ Constraint *Constraint::clone(ConstraintSystem &cs) const {
314314
case ConstraintKind::Defaultable:
315315
case ConstraintKind::OneWayEqual:
316316
case ConstraintKind::OneWayBindParam:
317-
case ConstraintKind::DefaultClosureType:
317+
case ConstraintKind::FallbackType:
318318
case ConstraintKind::UnresolvedMemberChainBase:
319319
case ConstraintKind::PropertyWrapper:
320320
case ConstraintKind::BindTupleOfFunctionParams:
@@ -469,8 +469,8 @@ void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm,
469469
case ConstraintKind::OpenedExistentialOf: Out << " opened archetype of "; break;
470470
case ConstraintKind::OneWayEqual: Out << " one-way bind to "; break;
471471
case ConstraintKind::OneWayBindParam: Out << " one-way bind param to "; break;
472-
case ConstraintKind::DefaultClosureType:
473-
Out << " closure can default to ";
472+
case ConstraintKind::FallbackType:
473+
Out << " can fallback to ";
474474
break;
475475
case ConstraintKind::UnresolvedMemberChainBase:
476476
Out << " unresolved member chain base ";
@@ -740,7 +740,7 @@ gatherReferencedTypeVars(Constraint *constraint,
740740
case ConstraintKind::SelfObjectOfProtocol:
741741
case ConstraintKind::OneWayEqual:
742742
case ConstraintKind::OneWayBindParam:
743-
case ConstraintKind::DefaultClosureType:
743+
case ConstraintKind::FallbackType:
744744
case ConstraintKind::UnresolvedMemberChainBase:
745745
case ConstraintKind::PropertyWrapper:
746746
case ConstraintKind::BindTupleOfFunctionParams:

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7394,7 +7394,7 @@ bool TypeVarBindingProducer::requiresOptionalAdjustment(
73947394
PotentialBinding
73957395
TypeVarBindingProducer::getDefaultBinding(Constraint *constraint) const {
73967396
assert(constraint->getKind() == ConstraintKind::Defaultable ||
7397-
constraint->getKind() == ConstraintKind::DefaultClosureType);
7397+
constraint->getKind() == ConstraintKind::FallbackType);
73987398

73997399
auto type = constraint->getSecondType();
74007400
Binding binding{type, BindingKind::Exact, constraint};

0 commit comments

Comments
 (0)