Skip to content

Commit ba7df40

Browse files
committed
Sema: Avoid copying BindingSets
They're stored inside the ConstraintGraphNode now, so returning a `const BindingSet *` from within determineBestBindings() should be safe.
1 parent da8b721 commit ba7df40

File tree

9 files changed

+22
-21
lines changed

9 files changed

+22
-21
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ class BindingSet {
385385
BindingSet(ConstraintSystem &CS, TypeVariableType *TypeVar,
386386
const PotentialBindings &info);
387387

388+
BindingSet(BindingSet &&other) = default;
389+
390+
BindingSet(const BindingSet &other) = delete;
391+
388392
ConstraintSystem &getConstraintSystem() const { return CS; }
389393

390394
TypeVariableType *getTypeVariable() const { return TypeVar; }

include/swift/Sema/ConstraintSystem.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5270,7 +5270,7 @@ class ConstraintSystem {
52705270

52715271
/// Determine whether given type variable with its set of bindings is viable
52725272
/// to be attempted on the next step of the solver.
5273-
std::optional<BindingSet> determineBestBindings(
5273+
const BindingSet *determineBestBindings(
52745274
llvm::function_ref<void(const BindingSet &)> onCandidate);
52755275

52765276
/// Get bindings for the given type variable based on current
@@ -6200,7 +6200,7 @@ class TypeVarBindingProducer : public BindingProducer<TypeVariableBinding> {
62006200
public:
62016201
using Element = TypeVariableBinding;
62026202

6203-
TypeVarBindingProducer(BindingSet &bindings);
6203+
TypeVarBindingProducer(const BindingSet &bindings);
62046204

62056205
/// Retrieve a set of bindings available in the current state.
62066206
ArrayRef<Binding> getCurrentBindings() const { return Bindings; }

lib/Sema/CSBindings.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,7 @@ bool BindingSet::operator<(const BindingSet &other) {
11801180
return isPotentiallyIncomplete() < other.isPotentiallyIncomplete();
11811181
}
11821182

1183-
std::optional<BindingSet> ConstraintSystem::determineBestBindings(
1183+
const BindingSet *ConstraintSystem::determineBestBindings(
11841184
llvm::function_ref<void(const BindingSet &)> onCandidate) {
11851185
// Look for potential type variable bindings.
11861186
BindingSet *bestBindings = nullptr;
@@ -1238,10 +1238,7 @@ std::optional<BindingSet> ConstraintSystem::determineBestBindings(
12381238
bestBindings = &bindings;
12391239
}
12401240

1241-
if (!bestBindings)
1242-
return std::nullopt;
1243-
1244-
return std::optional(*bestBindings);
1241+
return bestBindings;
12451242
}
12461243

12471244
/// Find the set of type variables that are inferable from the given type.

lib/Sema/CSStep.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ StepResult ComponentStep::take(bool prevFailed) {
263263
SmallString<64> potentialBindings;
264264
llvm::raw_svector_ostream bos(potentialBindings);
265265

266-
auto bestBindings = CS.determineBestBindings([&](const BindingSet &bindings) {
266+
const auto *bestBindings = CS.determineBestBindings([&](const BindingSet &bindings) {
267267
if (CS.isDebugMode() && bindings.hasViableBindings()) {
268268
bos.indent(CS.solverState->getCurrentIndent() + 2);
269269
bos << "(";

lib/Sema/CSStep.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ class TypeVariableStep final : public BindingStep<TypeVarBindingProducer> {
540540
bool SawFirstLiteralConstraint = false;
541541

542542
public:
543-
TypeVariableStep(BindingContainer &bindings,
543+
TypeVariableStep(const BindingContainer &bindings,
544544
SmallVectorImpl<Solution> &solutions)
545545
: BindingStep(bindings.getConstraintSystem(), {bindings}, solutions),
546546
TypeVar(bindings.getTypeVariable()) {}

lib/Sema/ConstraintSystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5309,7 +5309,7 @@ ConstraintSystem::inferKeyPathLiteralCapability(KeyPathExpr *keyPath) {
53095309
return success(mutability, isSendable);
53105310
}
53115311

5312-
TypeVarBindingProducer::TypeVarBindingProducer(BindingSet &bindings)
5312+
TypeVarBindingProducer::TypeVarBindingProducer(const BindingSet &bindings)
53135313
: BindingProducer(bindings.getConstraintSystem(),
53145314
bindings.getTypeVariable()->getImpl().getLocator()),
53155315
TypeVar(bindings.getTypeVariable()), CanBeNil(bindings.canBeNil()) {

unittests/Sema/BindingInferenceTests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ TEST_F(SemaTest, TestTransitiveProtocolInference) {
196196
cs.getConstraintLocator({}, LocatorPathElt::ContextualType(
197197
CTP_Initialization)));
198198

199-
auto bindings = inferBindings(cs, typeVar);
199+
auto &bindings = inferBindings(cs, typeVar);
200200
ASSERT_TRUE(bindings.getConformanceRequirements().empty());
201201
ASSERT_TRUE(bool(bindings.TransitiveProtocols));
202202
verifyProtocolInferenceResults(*bindings.TransitiveProtocols,
@@ -218,7 +218,7 @@ TEST_F(SemaTest, TestTransitiveProtocolInference) {
218218
cs.addConstraint(ConstraintKind::Conversion, typeVar, GPT1,
219219
cs.getConstraintLocator({}));
220220

221-
auto bindings = inferBindings(cs, typeVar);
221+
auto &bindings = inferBindings(cs, typeVar);
222222
ASSERT_TRUE(bindings.getConformanceRequirements().empty());
223223
ASSERT_TRUE(bool(bindings.TransitiveProtocols));
224224
verifyProtocolInferenceResults(*bindings.TransitiveProtocols,
@@ -281,10 +281,10 @@ TEST_F(SemaTest, TestComplexTransitiveProtocolInference) {
281281
cs.addConstraint(ConstraintKind::Equal, typeVar1, typeVar5, nilLocator);
282282
cs.addConstraint(ConstraintKind::Conversion, typeVar5, typeVar6, nilLocator);
283283

284-
auto bindingsForT1 = inferBindings(cs, typeVar1);
285-
auto bindingsForT2 = inferBindings(cs, typeVar2);
286-
auto bindingsForT3 = inferBindings(cs, typeVar3);
287-
auto bindingsForT5 = inferBindings(cs, typeVar5);
284+
auto &bindingsForT1 = inferBindings(cs, typeVar1);
285+
auto &bindingsForT2 = inferBindings(cs, typeVar2);
286+
auto &bindingsForT3 = inferBindings(cs, typeVar3);
287+
auto &bindingsForT5 = inferBindings(cs, typeVar5);
288288

289289
ASSERT_TRUE(bool(bindingsForT1.TransitiveProtocols));
290290
verifyProtocolInferenceResults(*bindingsForT1.TransitiveProtocols,
@@ -335,7 +335,7 @@ TEST_F(SemaTest, TestTransitiveProtocolInferenceThroughEquivalenceChains) {
335335
cs.addConstraint(ConstraintKind::ConformsTo, typeVar2, protocolTy0, nilLocator);
336336
cs.addConstraint(ConstraintKind::ConformsTo, typeVar3, protocolTy1, nilLocator);
337337

338-
auto bindings = inferBindings(cs, typeVar0);
338+
auto &bindings = inferBindings(cs, typeVar0);
339339

340340
ASSERT_TRUE(bool(bindings.TransitiveProtocols));
341341
verifyProtocolInferenceResults(*bindings.TransitiveProtocols,

unittests/Sema/SemaFixture.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ ProtocolType *SemaTest::createProtocol(llvm::StringRef protocolName,
124124
return ProtocolType::get(PD, parent, Context);
125125
}
126126

127-
BindingSet SemaTest::inferBindings(ConstraintSystem &cs,
128-
TypeVariableType *typeVar) {
127+
const BindingSet &SemaTest::inferBindings(ConstraintSystem &cs,
128+
TypeVariableType *typeVar) {
129129
for (auto *typeVar : cs.getTypeVariables()) {
130130
auto &node = cs.getConstraintGraph()[typeVar];
131131
node.resetBindingSet();

unittests/Sema/SemaFixture.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ class SemaTest : public SemaTestBase {
8080
ProtocolType *createProtocol(llvm::StringRef protocolName,
8181
Type parent = Type());
8282

83-
static BindingSet inferBindings(ConstraintSystem &cs,
84-
TypeVariableType *typeVar);
83+
static const BindingSet &inferBindings(ConstraintSystem &cs,
84+
TypeVariableType *typeVar);
8585
};
8686

8787
} // end namespace unittest

0 commit comments

Comments
 (0)