Skip to content

Commit 1aecea1

Browse files
committed
[unittest/Sema] Add a helper method to infer bindings for a given type variable
This mimics what `determineBestBindings` does but without sorting.
1 parent 5dc9919 commit 1aecea1

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

unittests/Sema/SemaFixture.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "swift/ClangImporter/ClangImporter.h"
2323
#include "swift/Serialization/SerializedModuleLoader.h"
2424
#include "swift/Subsystems.h"
25+
#include "llvm/ADT/DenseMap.h"
2526

2627
using namespace swift;
2728
using namespace swift::unittest;
@@ -74,3 +75,27 @@ Type SemaTest::getStdlibType(StringRef name) const {
7475

7576
return Type();
7677
}
78+
79+
ConstraintSystem::PotentialBindings
80+
SemaTest::inferBindings(ConstraintSystem &cs, TypeVariableType *typeVar) {
81+
llvm::SmallDenseMap<TypeVariableType *, ConstraintSystem::PotentialBindings>
82+
cache;
83+
84+
for (auto *typeVar : cs.getTypeVariables()) {
85+
if (!typeVar->getImpl().hasRepresentativeOrFixed())
86+
cache.insert({typeVar, cs.inferBindingsFor(typeVar, /*finalize=*/false)});
87+
}
88+
89+
for (auto *typeVar : cs.getTypeVariables()) {
90+
auto cachedBindings = cache.find(typeVar);
91+
if (cachedBindings == cache.end())
92+
continue;
93+
94+
auto &bindings = cachedBindings->getSecond();
95+
bindings.finalize(cs, cache);
96+
}
97+
98+
auto result = cache.find(typeVar);
99+
assert(result != cache.end());
100+
return result->second;
101+
}

unittests/Sema/SemaFixture.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@
1818
#include "swift/Basic/LangOptions.h"
1919
#include "swift/Basic/Platform.h"
2020
#include "swift/Basic/SourceManager.h"
21-
#include "llvm/ADT/StringRef.h"
21+
#include "swift/Sema/ConstraintSystem.h"
2222
#include "llvm/ADT/SmallString.h"
23+
#include "llvm/ADT/StringRef.h"
2324
#include "llvm/Support/Host.h"
2425
#include "llvm/Support/Path.h"
2526
#include "gtest/gtest.h"
2627
#include <string>
2728

29+
using namespace swift::constraints;
30+
2831
namespace swift {
2932
namespace unittest {
3033

@@ -62,6 +65,9 @@ class SemaTest : public SemaTestBase {
6265

6366
protected:
6467
Type getStdlibType(StringRef name) const;
68+
69+
static ConstraintSystem::PotentialBindings
70+
inferBindings(ConstraintSystem &cs, TypeVariableType *typeVar);
6571
};
6672

6773
} // end namespace unittest

0 commit comments

Comments
 (0)