Skip to content

Commit 88f347a

Browse files
committed
Sema: Collect defaults in PotentialBindings::infer()
1 parent 74fa1e7 commit 88f347a

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ struct PotentialBindings {
248248
/// The set of protocol conformance requirements imposed on this type variable.
249249
llvm::SmallVector<Constraint *, 4> Protocols;
250250

251+
/// The set of fallback constraints imposed on this type variable.
252+
llvm::SmallVector<Constraint *, 2> Defaults;
253+
251254
ASTNode AssociatedCodeCompletionToken = ASTNode();
252255

253256
/// Add a potential binding to the list of bindings,

include/swift/Sema/CSTrail.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ GRAPH_NODE_CHANGE(InferredBindings)
7878
GRAPH_NODE_CHANGE(RetractedBindings)
7979
GRAPH_NODE_CHANGE(RetractedDelayedBy)
8080
GRAPH_NODE_CHANGE(RetractedProtocol)
81+
GRAPH_NODE_CHANGE(RetractedDefault)
8182

8283
BINDING_RELATION_CHANGE(RetractedAdjacentVar)
8384
BINDING_RELATION_CHANGE(RetractedSubtypeOf)

lib/Sema/CSBindings.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,17 @@ BindingSet::BindingSet(ConstraintSystem &CS, TypeVariableType *TypeVar,
6161
addLiteralRequirement(constraint);
6262
break;
6363

64-
case ConstraintKind::Defaultable:
65-
case ConstraintKind::FallbackType:
66-
// Do these in a separate pass.
67-
if (isDirectRequirement(CS, TypeVar, constraint))
68-
addDefault(constraint);
69-
break;
70-
7164
default:
7265
break;
7366
}
7467
}
7568

69+
for (auto *constraint : info.Defaults) {
70+
// Do these in a separate pass.
71+
if (isDirectRequirement(CS, TypeVar, constraint))
72+
addDefault(constraint);
73+
}
74+
7675
for (auto &entry : info.AdjacentVars)
7776
AdjacentVars.insert(entry.first);
7877
}
@@ -2090,9 +2089,12 @@ void PotentialBindings::infer(ConstraintSystem &CS,
20902089
case ConstraintKind::SameShape:
20912090
case ConstraintKind::MaterializePackExpansion:
20922091
case ConstraintKind::LiteralConformsTo:
2092+
// Constraints from which we can't do anything.
2093+
break;
2094+
20932095
case ConstraintKind::Defaultable:
20942096
case ConstraintKind::FallbackType:
2095-
// Constraints from which we can't do anything.
2097+
Defaults.push_back(constraint);
20962098
break;
20972099

20982100
// For now let's avoid inferring protocol requirements from
@@ -2237,6 +2239,10 @@ void PotentialBindings::retract(ConstraintSystem &CS,
22372239
llvm::remove_if(Protocols, CALLBACK(RetractedProtocol)),
22382240
Protocols.end());
22392241

2242+
Defaults.erase(
2243+
llvm::remove_if(Defaults, CALLBACK(RetractedDefault)),
2244+
Defaults.end());
2245+
22402246
#define PAIR_CALLBACK(ChangeKind) \
22412247
[&](std::pair<TypeVariableType *, Constraint *> pair) { \
22422248
if (pair.second == constraint) { \

lib/Sema/CSTrail.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,11 @@ void SolverTrail::Change::undo(ConstraintSystem &cs) const {
543543
.Protocols.push_back(TheConstraint.Constraint);
544544
break;
545545

546+
case ChangeKind::RetractedDefault:
547+
cg[TheConstraint.TypeVar].getPotentialBindings()
548+
.Defaults.push_back(TheConstraint.Constraint);
549+
break;
550+
546551
case ChangeKind::RetractedAdjacentVar:
547552
cg[BindingRelation.TypeVar].getPotentialBindings()
548553
.AdjacentVars.emplace_back(BindingRelation.OtherTypeVar,

0 commit comments

Comments
 (0)