forked from apple/swift
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[unittests/Sema] Add a simple integer literal type inference test
- Loading branch information
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters