|
13 | 13 | #include "SemaFixture.h"
|
14 | 14 | #include "swift/AST/Expr.h"
|
15 | 15 | #include "swift/Sema/ConstraintSystem.h"
|
| 16 | +#include "llvm/ADT/SmallPtrSet.h" |
16 | 17 |
|
17 | 18 | using namespace swift;
|
18 | 19 | using namespace swift::unittest;
|
@@ -44,3 +45,44 @@ TEST_F(SemaTest, TestIntLiteralBindingInference) {
|
44 | 45 | ASSERT_TRUE(binding.BindingType->isEqual(getStdlibType("Int")));
|
45 | 46 | ASSERT_TRUE(binding.hasDefaultedLiteralProtocol());
|
46 | 47 | }
|
| 48 | + |
| 49 | +TEST_F(SemaTest, TestTransitiveProtocolInference) { |
| 50 | + ConstraintSystemOptions options; |
| 51 | + ConstraintSystem cs(DC, options); |
| 52 | + |
| 53 | + auto *PD1 = |
| 54 | + new (Context) ProtocolDecl(DC, SourceLoc(), SourceLoc(), |
| 55 | + Context.getIdentifier("P1"), /*Inherited=*/{}, |
| 56 | + /*trailingWhere=*/nullptr); |
| 57 | + PD1->setImplicit(); |
| 58 | + |
| 59 | + auto *protocolTy1 = ProtocolType::get(PD1, Type(), Context); |
| 60 | + |
| 61 | + auto *GPT = cs.createTypeVariable(cs.getConstraintLocator({}), |
| 62 | + /*options=*/TVO_CanBindToNoEscape); |
| 63 | + |
| 64 | + cs.addConstraint( |
| 65 | + ConstraintKind::ConformsTo, GPT, protocolTy1, |
| 66 | + cs.getConstraintLocator({}, LocatorPathElt::TypeParameterRequirement( |
| 67 | + 0, RequirementKind::Conformance))); |
| 68 | + |
| 69 | + // First, let's try inferring through a single conversion |
| 70 | + // relationship. |
| 71 | + { |
| 72 | + auto *typeVar = cs.createTypeVariable(cs.getConstraintLocator({}), |
| 73 | + /*options=*/0); |
| 74 | + |
| 75 | + cs.addConstraint( |
| 76 | + ConstraintKind::Conversion, typeVar, GPT, |
| 77 | + cs.getConstraintLocator({}, LocatorPathElt::ContextualType())); |
| 78 | + |
| 79 | + auto bindings = inferBindings(cs, typeVar); |
| 80 | + ASSERT_TRUE(bindings.Protocols.empty()); |
| 81 | + |
| 82 | + const auto &inferredProtocols = bindings.TransitiveProtocols; |
| 83 | + ASSERT_TRUE(bool(inferredProtocols)); |
| 84 | + ASSERT_EQ(inferredProtocols->size(), (unsigned)1); |
| 85 | + ASSERT_TRUE( |
| 86 | + (*inferredProtocols->begin())->getSecondType()->isEqual(protocolTy1)); |
| 87 | + } |
| 88 | +} |
0 commit comments