Skip to content

Commit 50cb31c

Browse files
committed
[Constraint solver] Make 'defaultable' constraint creation lazy.
1 parent 1b8d439 commit 50cb31c

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,7 +2567,7 @@ ConstraintSystem::simplifyOptionalObjectConstraint(
25672567
TypeMatchOptions flags,
25682568
ConstraintLocatorBuilder locator) {
25692569
// Resolve the optional type.
2570-
Type optLValueTy = getFixedTypeRecursive(first, /*wantRValue=*/false)
2570+
Type optLValueTy = getFixedTypeRecursive(first, /*wantRValue=*/false);
25712571
Type optTy = optLValueTy->getRValueType();
25722572
if (optTy.getPointer() != optLValueTy.getPointer())
25732573
optTy = getFixedTypeRecursive(optTy, /*wantRValue=*/false);
@@ -3288,11 +3288,23 @@ ConstraintSystem::simplifyMemberConstraint(ConstraintKind kind,
32883288
}
32893289

32903290
ConstraintSystem::SolutionKind
3291-
ConstraintSystem::simplifyDefaultableConstraint(const Constraint &constraint) {
3292-
auto baseTy = getFixedTypeRecursive(constraint.getFirstType(), true);
3291+
ConstraintSystem::simplifyDefaultableConstraint(
3292+
Type first, Type second,
3293+
TypeMatchOptions flags,
3294+
ConstraintLocatorBuilder locator) {
3295+
first = getFixedTypeRecursive(first, true);
3296+
3297+
if (first->isTypeVariableOrMember()) {
3298+
if (flags.contains(TMF_GenerateConstraints)) {
3299+
addUnsolvedConstraint(
3300+
Constraint::create(*this, ConstraintKind::Defaultable, first, second,
3301+
DeclName(), FunctionRefKind::Compound,
3302+
getConstraintLocator(locator)));
3303+
return SolutionKind::Solved;
3304+
}
32933305

3294-
if (baseTy->isTypeVariableOrMember())
32953306
return SolutionKind::Unsolved;
3307+
}
32963308

32973309
// Otherwise, any type is fine.
32983310
return SolutionKind::Solved;
@@ -4173,9 +4185,10 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
41734185
case ConstraintKind::OptionalObject:
41744186
return simplifyOptionalObjectConstraint(first, second, subflags, locator);
41754187

4176-
case ConstraintKind::Bind: // FIXME: This should go through matchTypes() above
4188+
case ConstraintKind::Defaultable:
4189+
return simplifyDefaultableConstraint(first, second, subflags, locator);
41774190

4178-
default: {
4191+
case ConstraintKind::Bind: { // FIXME: This should go through matchTypes() above
41794192
// FALLBACK CASE: do the slow thing.
41804193
auto c = Constraint::create(*this, kind, first, second, DeclName(),
41814194
FunctionRefKind::Compound,
@@ -4189,6 +4202,7 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
41894202
case ConstraintKind::UnresolvedValueMember:
41904203
case ConstraintKind::TypeMember:
41914204
case ConstraintKind::BindOverload:
4205+
case ConstraintKind::Disjunction:
41924206
llvm_unreachable("Use the correct addConstraint()");
41934207
}
41944208
}
@@ -4316,7 +4330,10 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
43164330
constraint.getLocator());
43174331

43184332
case ConstraintKind::Defaultable:
4319-
return simplifyDefaultableConstraint(constraint);
4333+
return simplifyDefaultableConstraint(constraint.getFirstType(),
4334+
constraint.getSecondType(),
4335+
TMF_GenerateConstraints,
4336+
constraint.getLocator());
43204337

43214338
case ConstraintKind::Disjunction:
43224339
// Disjunction constraints are never solved here.

lib/Sema/ConstraintSystem.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2055,7 +2055,9 @@ class ConstraintSystem {
20552055
ConstraintLocatorBuilder locator);
20562056

20572057
/// \brief Attempt to simplify the given defaultable constraint.
2058-
SolutionKind simplifyDefaultableConstraint(const Constraint &c);
2058+
SolutionKind simplifyDefaultableConstraint(Type first, Type second,
2059+
TypeMatchOptions flags,
2060+
ConstraintLocatorBuilder locator);
20592061

20602062
/// \brief Simplify a conversion constraint by applying the given
20612063
/// reduction rule, which is known to apply at the outermost level.

0 commit comments

Comments
 (0)