|
| 1 | +//===--- ConstraintSystemDumpTests.cpp ------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 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/Sema/ConstraintSystem.h" |
| 15 | + |
| 16 | +using namespace swift; |
| 17 | +using namespace swift::unittest; |
| 18 | + |
| 19 | +TEST_F(SemaTest, DumpConstraintSystemBasic) { |
| 20 | + ConstraintSystemOptions options; |
| 21 | + ConstraintSystem cs(DC, options); |
| 22 | + |
| 23 | + auto *emptyLoc = cs.getConstraintLocator({}); |
| 24 | + |
| 25 | + auto *t0 = cs.createTypeVariable(emptyLoc, TVO_CanBindToLValue); |
| 26 | + auto *t1 = cs.createTypeVariable(emptyLoc, 0); |
| 27 | + auto *t2 = cs.createTypeVariable( |
| 28 | + cs.getConstraintLocator(emptyLoc, ConstraintLocator::GenericArgument), |
| 29 | + TVO_CanBindToHole | TVO_CanBindToPack); |
| 30 | + |
| 31 | + cs.addUnsolvedConstraint(Constraint::create( |
| 32 | + cs, ConstraintKind::Bind, t2, |
| 33 | + TupleType::get({Type(t0), Type(t1)}, Context), emptyLoc)); |
| 34 | + |
| 35 | + std::string expectedOutput = |
| 36 | + R"(Score: <default 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0> |
| 37 | +Type Variables: |
| 38 | + $T0 [can bind to: lvalue] [adjacent to: $T1, $T2] [potential bindings: <none>] @ locator@ [] |
| 39 | + $T1 [adjacent to: $T0, $T2] [potential bindings: <none>] @ locator@ [] |
| 40 | + $T2 [can bind to: hole, pack] [potential bindings: <none>] @ locator@ [ → generic argument #0] |
| 41 | +Inactive Constraints: |
| 42 | + $T2 bind ($T0, $T1) @ locator@ [] |
| 43 | +)"; |
| 44 | + |
| 45 | + std::string actualOutput; |
| 46 | + { |
| 47 | + llvm::raw_string_ostream os(actualOutput); |
| 48 | + cs.print(os); |
| 49 | + |
| 50 | + // Remove locator addresses. |
| 51 | + std::string adjustedOutput; |
| 52 | + const auto size = actualOutput.size(); |
| 53 | + size_t pos = 0; |
| 54 | + while (pos < size) { |
| 55 | + auto addr_pos = actualOutput.find("0x", pos); |
| 56 | + if (addr_pos == std::string::npos) { |
| 57 | + adjustedOutput += actualOutput.substr(pos, std::string::npos); |
| 58 | + break; |
| 59 | + } else { |
| 60 | + adjustedOutput += actualOutput.substr(pos, addr_pos - pos); |
| 61 | + } |
| 62 | + |
| 63 | + pos = actualOutput.find(' ', addr_pos); |
| 64 | + } |
| 65 | + |
| 66 | + actualOutput = adjustedOutput; |
| 67 | + } |
| 68 | + |
| 69 | + EXPECT_EQ(expectedOutput, actualOutput); |
| 70 | +} |
0 commit comments