Skip to content

Commit

Permalink
[unittests/Sema] Add a simple integer literal type inference test
Browse files Browse the repository at this point in the history
  • Loading branch information
xedin committed Oct 13, 2020
1 parent b2c31c3 commit dc7c9c2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/swift/Sema/ConstraintSystem.h
Expand Up @@ -4942,6 +4942,7 @@ class ConstraintSystem {
Optional<Type> checkTypeOfBinding(TypeVariableType *typeVar, Type type) const;
Optional<PotentialBindings> determineBestBindings();

public:
/// Infer bindings for the given type variable based on current
/// state of the constraint system.
PotentialBindings inferBindingsFor(TypeVariableType *typeVar,
Expand Down
46 changes: 46 additions & 0 deletions unittests/Sema/BindingInferenceTests.cpp
@@ -0,0 +1,46 @@
//===--- BindingInferenceTests.cpp ----------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "SemaFixture.h"
#include "swift/AST/Expr.h"
#include "swift/Sema/ConstraintSystem.h"

using namespace swift;
using namespace swift::unittest;
using namespace swift::constraints;

TEST_F(SemaTest, TestIntLiteralBindingInference) {
ConstraintSystemOptions options;
options |= ConstraintSystemFlags::AllowUnresolvedTypeVariables;

ConstraintSystem cs(DC, options);

auto *intLiteral = IntegerLiteralExpr::createFromUnsigned(Context, 42);

auto *literalTy = cs.createTypeVariable(cs.getConstraintLocator(intLiteral),
/*options=*/0);

cs.addConstraint(
ConstraintKind::LiteralConformsTo, literalTy,
Context.getProtocol(KnownProtocolKind::ExpressibleByIntegerLiteral)
->getDeclaredInterfaceType(),
cs.getConstraintLocator(intLiteral));

auto bindings = cs.inferBindingsFor(literalTy);

ASSERT_EQ(bindings.Bindings.size(), (unsigned)1);

const auto &binding = bindings.Bindings.front();

ASSERT_TRUE(binding.BindingType->isEqual(getStdlibType("Int")));
ASSERT_TRUE(binding.hasDefaultedLiteralProtocol());
}
3 changes: 2 additions & 1 deletion unittests/Sema/CMakeLists.txt
@@ -1,6 +1,7 @@

add_swift_unittest(swiftSemaTests
SemaFixture.cpp)
SemaFixture.cpp
BindingInferenceTests.cpp)

target_link_libraries(swiftSemaTests
PRIVATE
Expand Down

0 comments on commit dc7c9c2

Please sign in to comment.