Skip to content

Commit dc7c9c2

Browse files
committed
[unittests/Sema] Add a simple integer literal type inference test
1 parent b2c31c3 commit dc7c9c2

File tree

3 files changed

+49
-1
lines changed

3 files changed

+49
-1
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4942,6 +4942,7 @@ class ConstraintSystem {
49424942
Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type) const;
49434943
Optional<PotentialBindings> determineBestBindings();
49444944

4945+
public:
49454946
/// Infer bindings for the given type variable based on current
49464947
/// state of the constraint system.
49474948
PotentialBindings inferBindingsFor(TypeVariableType *typeVar,
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===--- BindingInferenceTests.cpp ----------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "SemaFixture.h"
14+
#include "swift/AST/Expr.h"
15+
#include "swift/Sema/ConstraintSystem.h"
16+
17+
using namespace swift;
18+
using namespace swift::unittest;
19+
using namespace swift::constraints;
20+
21+
TEST_F(SemaTest, TestIntLiteralBindingInference) {
22+
ConstraintSystemOptions options;
23+
options |= ConstraintSystemFlags::AllowUnresolvedTypeVariables;
24+
25+
ConstraintSystem cs(DC, options);
26+
27+
auto *intLiteral = IntegerLiteralExpr::createFromUnsigned(Context, 42);
28+
29+
auto *literalTy = cs.createTypeVariable(cs.getConstraintLocator(intLiteral),
30+
/*options=*/0);
31+
32+
cs.addConstraint(
33+
ConstraintKind::LiteralConformsTo, literalTy,
34+
Context.getProtocol(KnownProtocolKind::ExpressibleByIntegerLiteral)
35+
->getDeclaredInterfaceType(),
36+
cs.getConstraintLocator(intLiteral));
37+
38+
auto bindings = cs.inferBindingsFor(literalTy);
39+
40+
ASSERT_EQ(bindings.Bindings.size(), (unsigned)1);
41+
42+
const auto &binding = bindings.Bindings.front();
43+
44+
ASSERT_TRUE(binding.BindingType->isEqual(getStdlibType("Int")));
45+
ASSERT_TRUE(binding.hasDefaultedLiteralProtocol());
46+
}

unittests/Sema/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
add_swift_unittest(swiftSemaTests
3-
SemaFixture.cpp)
3+
SemaFixture.cpp
4+
BindingInferenceTests.cpp)
45

56
target_link_libraries(swiftSemaTests
67
PRIVATE

0 commit comments

Comments
 (0)