Skip to content

Commit 1b8d439

Browse files
committed
[Constraint solver] Make optional object constraint construction lazy.
1 parent 12b6894 commit 1b8d439

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2562,13 +2562,25 @@ ConstraintSystem::simplifyCheckedCastConstraint(
25622562
}
25632563

25642564
ConstraintSystem::SolutionKind
2565-
ConstraintSystem::simplifyOptionalObjectConstraint(const Constraint &constraint)
2566-
{
2565+
ConstraintSystem::simplifyOptionalObjectConstraint(
2566+
Type first, Type second,
2567+
TypeMatchOptions flags,
2568+
ConstraintLocatorBuilder locator) {
25672569
// Resolve the optional type.
2568-
Type optLValueTy = simplifyType(constraint.getFirstType());
2570+
Type optLValueTy = getFixedTypeRecursive(first, /*wantRValue=*/false)
25692571
Type optTy = optLValueTy->getRValueType();
2570-
2572+
if (optTy.getPointer() != optLValueTy.getPointer())
2573+
optTy = getFixedTypeRecursive(optTy, /*wantRValue=*/false);
2574+
25712575
if (optTy->isTypeVariableOrMember()) {
2576+
if (flags.contains(TMF_GenerateConstraints)) {
2577+
addUnsolvedConstraint(
2578+
Constraint::create(*this, ConstraintKind::OptionalObject, optLValueTy,
2579+
second, DeclName(), FunctionRefKind::Compound,
2580+
getConstraintLocator(locator)));
2581+
return SolutionKind::Solved;
2582+
}
2583+
25722584
return SolutionKind::Unsolved;
25732585
}
25742586

@@ -2582,9 +2594,7 @@ ConstraintSystem::simplifyOptionalObjectConstraint(const Constraint &constraint)
25822594
objectTy = LValueType::get(objectTy);
25832595

25842596
// Equate it to the other type in the constraint.
2585-
addConstraint(ConstraintKind::Bind, objectTy, constraint.getSecondType(),
2586-
constraint.getLocator());
2587-
2597+
addConstraint(ConstraintKind::Bind, objectTy, second, locator);
25882598
return SolutionKind::Solved;
25892599
}
25902600

@@ -4160,6 +4170,9 @@ ConstraintSystem::addConstraintImpl(ConstraintKind kind, Type first,
41604170
case ConstraintKind::CheckedCast:
41614171
return simplifyCheckedCastConstraint(first, second, subflags, locator);
41624172

4173+
case ConstraintKind::OptionalObject:
4174+
return simplifyOptionalObjectConstraint(first, second, subflags, locator);
4175+
41634176
case ConstraintKind::Bind: // FIXME: This should go through matchTypes() above
41644177

41654178
default: {
@@ -4286,7 +4299,10 @@ ConstraintSystem::simplifyConstraint(const Constraint &constraint) {
42864299
}
42874300

42884301
case ConstraintKind::OptionalObject:
4289-
return simplifyOptionalObjectConstraint(constraint);
4302+
return simplifyOptionalObjectConstraint(constraint.getFirstType(),
4303+
constraint.getSecondType(),
4304+
TMF_GenerateConstraints,
4305+
constraint.getLocator());
42904306

42914307
case ConstraintKind::ValueMember:
42924308
case ConstraintKind::UnresolvedValueMember:

lib/Sema/ConstraintSystem.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2036,7 +2036,10 @@ class ConstraintSystem {
20362036

20372037

20382038
/// \brief Attempt to simplify the optional object constraint.
2039-
SolutionKind simplifyOptionalObjectConstraint(const Constraint &constraint);
2039+
SolutionKind simplifyOptionalObjectConstraint(
2040+
Type first, Type second,
2041+
TypeMatchOptions flags,
2042+
ConstraintLocatorBuilder locator);
20402043

20412044
/// \brief Attempt to simplify the ApplicableFunction constraint.
20422045
SolutionKind simplifyApplicableFnConstraint(

0 commit comments

Comments
 (0)