diff --git a/include/swift/Sema/ConstraintSystem.h b/include/swift/Sema/ConstraintSystem.h index 17f804123a6c3..e3e2735b60ea6 100644 --- a/include/swift/Sema/ConstraintSystem.h +++ b/include/swift/Sema/ConstraintSystem.h @@ -4942,6 +4942,7 @@ class ConstraintSystem { Optional checkTypeOfBinding(TypeVariableType *typeVar, Type type) const; Optional determineBestBindings(); +public: /// Infer bindings for the given type variable based on current /// state of the constraint system. PotentialBindings inferBindingsFor(TypeVariableType *typeVar, diff --git a/unittests/Sema/BindingInferenceTests.cpp b/unittests/Sema/BindingInferenceTests.cpp new file mode 100644 index 0000000000000..f4b71d921a28c --- /dev/null +++ b/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()); +} diff --git a/unittests/Sema/CMakeLists.txt b/unittests/Sema/CMakeLists.txt index 270b08eff4b34..1257490d9183e 100644 --- a/unittests/Sema/CMakeLists.txt +++ b/unittests/Sema/CMakeLists.txt @@ -1,6 +1,7 @@ add_swift_unittest(swiftSemaTests - SemaFixture.cpp) + SemaFixture.cpp + BindingInferenceTests.cpp) target_link_libraries(swiftSemaTests PRIVATE