Skip to content

Commit 2108674

Browse files
authored
Merge pull request #63849 from ahoppen/ahoppen/equivalent-type-vars
[IDE] Inspect all equivalent type variables to replace a type variable by an archetype
2 parents 78749d8 + 8951373 commit 2108674

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

test/IDE/complete_subscript.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func test1() {
3939

4040
let _ = MyStruct[#^METATYPE_UNRESOLVED_BRACKET^#
4141
// METATYPE_UNRESOLVED_BRACKET: Begin completions
42-
// METATYPE_UNRESOLVED_BRACKET-DAG: Decl[Subscript]/CurrNominal/Flair[ArgLabels]: ['[']{#(x): Int#}, {#static: _#}[']'][#MyStruct<_>#];
42+
// METATYPE_UNRESOLVED_BRACKET-DAG: Decl[Subscript]/CurrNominal/Flair[ArgLabels]: ['[']{#(x): Int#}, {#static: T#}[']'][#MyStruct<T>#];
4343
// METATYPE_UNRESOLVED_BRACKET: End completions
4444

4545
let _ = MyStruct<Int> #^METATYPE_INT^#

0 commit comments

Comments
 (0)