@@ -3824,7 +3824,7 @@ Type Solution::simplifyTypeForCodeCompletion(Type Ty) const {
3824
3824
3825
3825
// Replace all type variables (which must come from placeholders) by their
3826
3826
// 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 {
3828
3828
// Code completion depends on generic parameter type being represented in
3829
3829
// terms of `ArchetypeType` since it's easy to extract protocol requirements
3830
3830
// from it.
@@ -3841,6 +3841,29 @@ Type Solution::simplifyTypeForCodeCompletion(Type Ty) const {
3841
3841
return archetype;
3842
3842
}
3843
3843
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
+
3844
3867
// When applying the logic below to get contextual types inside result
3845
3868
// builders, the code completion type variable is connected by a one-way
3846
3869
// constraint to a type variable in the buildBlock call, but that is not the
0 commit comments