Skip to content

[IDE] Inspect all equivalent type variables to replace a type variable by an archetype #63849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3814,7 +3814,7 @@ Type Solution::simplifyTypeForCodeCompletion(Type Ty) const {

// Replace all type variables (which must come from placeholders) by their
// generic parameters. Because we call into simplifyTypeImpl
Ty = CS.simplifyTypeImpl(Ty, [&CS](TypeVariableType *typeVar) -> Type {
Ty = CS.simplifyTypeImpl(Ty, [&CS, this](TypeVariableType *typeVar) -> Type {
// Code completion depends on generic parameter type being represented in
// terms of `ArchetypeType` since it's easy to extract protocol requirements
// from it.
Expand All @@ -3831,6 +3831,29 @@ Type Solution::simplifyTypeForCodeCompletion(Type Ty) const {
return archetype;
}

// Sometimes the type variable itself doesn't have have an originator that
// can be replaced by an archetype but one of its equivalent type variable
// does.
// Search thorough all equivalent type variables, looking for one that can
// be replaced by a generic parameter.
std::vector<std::pair<TypeVariableType *, Type>> bindings(
typeBindings.begin(), typeBindings.end());
// Make sure we iterate the bindings in a deterministic order.
llvm::sort(bindings, [](const std::pair<TypeVariableType *, Type> &lhs,
const std::pair<TypeVariableType *, Type> &rhs) {
return lhs.first->getID() < rhs.first->getID();
});
for (auto binding : bindings) {
if (auto placeholder = binding.second->getAs<PlaceholderType>()) {
if (placeholder->getOriginator().dyn_cast<TypeVariableType *>() ==
typeVar) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't quite understand what is going on here... Could you please elaborate on what you are trying to archive?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The motivation is what we are already doing above: If we have a placeholder whose originator is a type variable that represents a generic parameter, we want to replace that placeholder by the generic parameter before printing code completion results because for example in the test case I modified MyStruct<T> looks more useful than MyStruct<_>.

But, again in the test case below, we are receiving the following solution

--- Solution ---
Fixed score: <default 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0>
Type variables:
  $T0 as <<placeholder for $T8>> @ locator@0x138048218 [Type@/Users/alex/swift-src/swift/test/IDE/complete_subscript.swift:40:11 -> generic parameter 'T']
  $T1 as Int @ locator@0x1380482f0 [CodeCompletion@/Users/alex/swift-src/swift/test/IDE/complete_subscript.swift:40:20]
  $T2 as MyStruct<<<placeholder for $T8>>> @ locator@0x1380483a8 [Subscript@/Users/alex/swift-src/swift/test/IDE/complete_subscript.swift:40:19 -> function result]
  $T3 as (Int, <<placeholder for $T8>>) -> MyStruct<<<placeholder for $T8>>> @ locator@0x138048380 [Subscript@/Users/alex/swift-src/swift/test/IDE/complete_subscript.swift:40:19 -> subscript member]
  $T7 as <<placeholder for $T8>> @ locator@0x138048ab8 [Subscript@/Users/alex/swift-src/swift/test/IDE/complete_subscript.swift:40:19 -> subscript member -> generic parameter 'T']
  $T8 as <<placeholder for $T8>> @ locator@0x138048d78 [Subscript@/Users/alex/swift-src/swift/test/IDE/complete_subscript.swift:40:19 -> apply argument -> comparing call argument #1 to parameter #1 -> synthesized argument #1]

And the argument we want to look up has type T8. Now T8 doesn’t represent a generic parameter and thus we were outputting _ for the placeholder in the results. But actually looking at the solution, you can see that T8 has the same type as T0 (because T0 is bound to T8) and T0 represents a generic parameter. Thus, for printing purposes we can replace the placeholder for T8 by the generic parameter that T0 represents, which is T.

Does this make sense?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this transformation should happen in ConstraintSystem::finalize while we are forming a complete solution in code completion mode instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, I’ll do this in a follow up PR and filed rdar://106082607 so we don’t forget about it.

if (auto archetype = getTypeVarAsArchetype(binding.first)) {
return archetype;
}
}
}
}

// When applying the logic below to get contextual types inside result
// builders, the code completion type variable is connected by a one-way
// constraint to a type variable in the buildBlock call, but that is not the
Expand Down
2 changes: 1 addition & 1 deletion test/IDE/complete_subscript.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func test1() {

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

let _ = MyStruct<Int> #^METATYPE_INT^#
Expand Down