|
| 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 | +} |
0 commit comments