@@ -3824,7 +3824,7 @@ Type Solution::simplifyTypeForCodeCompletion(Type Ty) const {
38243824
38253825 // Replace all type variables (which must come from placeholders) by their
38263826 // generic parameters. Because we call into simplifyTypeImpl
3827- Ty = CS.simplifyTypeImpl (Ty, [&CS](TypeVariableType *typeVar) -> Type {
3827+ Ty = CS.simplifyTypeImpl (Ty, [&CS, this ](TypeVariableType *typeVar) -> Type {
38283828 // Code completion depends on generic parameter type being represented in
38293829 // terms of `ArchetypeType` since it's easy to extract protocol requirements
38303830 // from it.
@@ -3841,6 +3841,29 @@ Type Solution::simplifyTypeForCodeCompletion(Type Ty) const {
38413841 return archetype;
38423842 }
38433843
3844+ // Sometimes the type variable itself doesn't have have an originator that
3845+ // can be replaced by an archetype but one of its equivalent type variable
3846+ // does.
3847+ // Search thorough all equivalent type variables, looking for one that can
3848+ // be replaced by a generic parameter.
3849+ std::vector<std::pair<TypeVariableType *, Type>> bindings (
3850+ typeBindings.begin (), typeBindings.end ());
3851+ // Make sure we iterate the bindings in a deterministic order.
3852+ llvm::sort (bindings, [](const std::pair<TypeVariableType *, Type> &lhs,
3853+ const std::pair<TypeVariableType *, Type> &rhs) {
3854+ return lhs.first ->getID () < rhs.first ->getID ();
3855+ });
3856+ for (auto binding : bindings) {
3857+ if (auto placeholder = binding.second ->getAs <PlaceholderType>()) {
3858+ if (placeholder->getOriginator ().dyn_cast <TypeVariableType *>() ==
3859+ typeVar) {
3860+ if (auto archetype = getTypeVarAsArchetype (binding.first )) {
3861+ return archetype;
3862+ }
3863+ }
3864+ }
3865+ }
3866+
38443867 // When applying the logic below to get contextual types inside result
38453868 // builders, the code completion type variable is connected by a one-way
38463869 // constraint to a type variable in the buildBlock call, but that is not the
0 commit comments