Skip to content

Commit 8451536

Browse files
committed
[Constraint solver] Lazily allocate DynamicTypeOf constraints.
1 parent ed71fd0 commit 8451536

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3275,45 +3275,60 @@ ConstraintSystem::simplifyDefaultableConstraint(const Constraint &constraint) {
32753275

32763276

32773277
ConstraintSystem::SolutionKind
3278-
ConstraintSystem::simplifyDynamicTypeOfConstraint(const Constraint &constraint) {
3278+
ConstraintSystem::simplifyDynamicTypeOfConstraint(
3279+
Type type1, Type type2,
3280+
TypeMatchOptions flags,
3281+
ConstraintLocatorBuilder locator) {
3282+
TypeMatchOptions subflags = getDefaultDecompositionOptions(flags);
3283+
3284+
// Local function to form an unsolved result.
3285+
auto formUnsolved = [&] {
3286+
if (flags.contains(TMF_GenerateConstraints)) {
3287+
addUnsolvedConstraint(
3288+
Constraint::create(*this, ConstraintKind::DynamicTypeOf, type1, type2,
3289+
DeclName(), FunctionRefKind::Compound,
3290+
getConstraintLocator(locator)));
3291+
return SolutionKind::Solved;
3292+
}
3293+
3294+
return SolutionKind::Unsolved;
3295+
};
3296+
32793297
// Solve forward.
3280-
Type type2 = getFixedTypeRecursive(constraint.getSecondType(),
3281-
/*wantRValue=*/ true);
3298+
type2 = getFixedTypeRecursive(type2, /*wantRValue=*/ true);
32823299
if (!type2->isTypeVariableOrMember()) {
32833300
Type dynamicType2;
32843301
if (type2->isAnyExistentialType()) {
32853302
dynamicType2 = ExistentialMetatypeType::get(type2);
32863303
} else {
32873304
dynamicType2 = MetatypeType::get(type2);
32883305
}
3289-
return matchTypes(constraint.getFirstType(), dynamicType2,
3290-
TypeMatchKind::BindType, TMF_GenerateConstraints,
3291-
constraint.getLocator());
3306+
return matchTypes(type1, dynamicType2, TypeMatchKind::BindType, subflags,
3307+
locator);
32923308
}
32933309

32943310
// Okay, can't solve forward. See what we can do backwards.
3295-
Type type1 = getFixedTypeRecursive(constraint.getFirstType(), true);
3296-
3311+
type1 = getFixedTypeRecursive(type1, true);
32973312
if (type1->isTypeVariableOrMember())
3298-
return SolutionKind::Unsolved;
3313+
return formUnsolved();
32993314

33003315
// If we have an existential metatype, that's good enough to solve
33013316
// the constraint.
33023317
if (auto metatype1 = type1->getAs<ExistentialMetatypeType>())
33033318
return matchTypes(metatype1->getInstanceType(), type2,
33043319
TypeMatchKind::BindType,
3305-
TMF_GenerateConstraints, constraint.getLocator());
3320+
subflags, locator);
33063321

33073322
// If we have a normal metatype, we can't solve backwards unless we
33083323
// know what kind of object it is.
33093324
if (auto metatype1 = type1->getAs<MetatypeType>()) {
33103325
Type instanceType1 = getFixedTypeRecursive(metatype1->getInstanceType(),
33113326
true);
3312-
if (!instanceType1->isTypeVariableOrMember()) {
3313-
return matchTypes(instanceType1, type2,
3314-
TypeMatchKind::BindType,
3315-
TMF_GenerateConstraints, constraint.getLocator());
3316-
}
3327+
if (instanceType1->isTypeVariableOrMember())
3328+
return formUnsolved();
3329+
3330+
return matchTypes(instanceType1, type2, TypeMatchKind::BindType, subflags,
3331+
locator);
33173332
}
33183333

33193334
// It's definitely not either kind of metatype, so we can
@@ -4120,6 +4135,9 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
41204135
case ConstraintKind::ApplicableFunction:
41214136
return simplifyApplicableFnConstraint(first, second, subflags, locator);
41224137

4138+
case ConstraintKind::DynamicTypeOf:
4139+
return simplifyDynamicTypeOfConstraint(first, second, subflags, locator);
4140+
41234141
case ConstraintKind::Bind: // FIXME: This should go through matchTypes() above
41244142

41254143
default: {
@@ -4201,7 +4219,10 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
42014219
None, constraint.getLocator());
42024220

42034221
case ConstraintKind::DynamicTypeOf:
4204-
return simplifyDynamicTypeOfConstraint(constraint);
4222+
return simplifyDynamicTypeOfConstraint(constraint.getFirstType(),
4223+
constraint.getSecondType(),
4224+
None,
4225+
constraint.getLocator());
42054226

42064227
case ConstraintKind::BindOverload:
42074228
resolveOverload(constraint.getLocator(), constraint.getFirstType(),

lib/Sema/ConstraintSystem.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,10 @@ class ConstraintSystem {
20462046
ConstraintLocatorBuilder locator);
20472047

20482048
/// \brief Attempt to simplify the given DynamicTypeOf constraint.
2049-
SolutionKind simplifyDynamicTypeOfConstraint(const Constraint &constraint);
2049+
SolutionKind simplifyDynamicTypeOfConstraint(
2050+
Type type1, Type type2,
2051+
TypeMatchOptions flags,
2052+
ConstraintLocatorBuilder locator);
20502053

20512054
/// \brief Attempt to simplify the given defaultable constraint.
20522055
SolutionKind simplifyDefaultableConstraint(const Constraint &c);

0 commit comments

Comments
 (0)