@@ -395,3 +395,43 @@ TEST_F(SemaTest, TestNoDoubleVoidClosureResultInference) {
395395
396396 verifyInference (closureResultWithoutVoid, 3 );
397397}
398+
399+ TEST_F (SemaTest, TestSupertypeInferenceWithDefaults) {
400+ ConstraintSystemOptions options;
401+ ConstraintSystem cs (DC, options);
402+
403+ auto *genericArg = cs.createTypeVariable (
404+ cs.getConstraintLocator ({}, ConstraintLocator::GenericArgument),
405+ /* options=*/ 0 );
406+
407+ // KeyPath<String, Int> i.e. \.utf8.count or something similar
408+ auto keyPath =
409+ BoundGenericType::get (Context.getKeyPathDecl (), /* parent=*/ Type (),
410+ {getStdlibType (" String" ), getStdlibType (" Int" )});
411+
412+ cs.addConstraint (ConstraintKind::Conversion, keyPath, genericArg,
413+ cs.getConstraintLocator ({}));
414+
415+ cs.addConstraint (ConstraintKind::Defaultable, genericArg, Context.TheAnyType ,
416+ cs.getConstraintLocator ({}));
417+
418+ auto bindings = cs.getBindingsFor (genericArg);
419+ TypeVarBindingProducer producer (bindings);
420+
421+ llvm::SmallVector<Type, 4 > inferredTypes;
422+ while (auto binding = producer ()) {
423+ ASSERT_TRUE (binding.has_value ());
424+ inferredTypes.push_back (binding->getType ());
425+ }
426+
427+ // The inference should produce 4 types: KeyPath<String, Int>,
428+ // PartialKeyPath<String>, AnyKeyPath and Any - in that order.
429+
430+ ASSERT_EQ (inferredTypes.size (), 4 );
431+ ASSERT_TRUE (inferredTypes[0 ]->isEqual (keyPath));
432+ ASSERT_TRUE (inferredTypes[1 ]->isEqual (
433+ BoundGenericType::get (Context.getPartialKeyPathDecl (),
434+ /* parent=*/ Type (), {getStdlibType (" String" )})));
435+ ASSERT_TRUE (inferredTypes[2 ]->isEqual (getStdlibType (" AnyKeyPath" )));
436+ ASSERT_TRUE (inferredTypes[3 ]->isEqual (Context.TheAnyType ));
437+ }
0 commit comments