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

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Feb 22, 2023

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.

…e by an 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.
@ahoppen ahoppen requested review from xedin and rintaro February 22, 2023 22:01
@ahoppen ahoppen requested a review from hborla as a code owner February 22, 2023 22:01
@ahoppen
Copy link
Member Author

ahoppen commented Feb 22, 2023

@swift-ci Please smoke test

@ahoppen
Copy link
Member Author

ahoppen commented Feb 22, 2023

@swift-ci Please SourceKit stress test

@ahoppen
Copy link
Member Author

ahoppen commented Feb 23, 2023

@swift-ci Please smoke test macOS

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.

@ahoppen ahoppen merged commit 2108674 into swiftlang:main Mar 1, 2023
@ahoppen ahoppen deleted the ahoppen/equivalent-type-vars branch March 1, 2023 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants