Skip to content
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

Code completion: Stop suggesting old-style protocol metatype syntax in Swift 6 mode #65699

Open
AnthonyLatsis opened this issue May 5, 2023 · 9 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. code completion Area → source tooling: code completion existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values good first issue Good for newcomers metatypes Feature → types: Metatypes source tooling Area: IDE support, SourceKit, and other source tooling swift 5.9 types Feature: types unexpected behavior Bug: Unexpected behavior or incorrect output

Comments

@AnthonyLatsis
Copy link
Collaborator

AnthonyLatsis commented May 5, 2023

In Swift 6 mode, [.]Protocol1 is no longer a valid suggestion. Per SE-0335, we should suggest two distinct [.]Type1 completions for a plain protocol/composition — one aiming at any P.Type and another aiming at (any P).Type — that also insert a missing any prefix, and parenthesize the acted-upon existential in the second case.

Test case (should not succeed):

// RUN: %empty-directory(%t)
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t -swift-version 6

protocol P {}

let _: P#^COMPLETE1?check=META^#
let _: any P#^COMPLETE2?check=META^#
let _: (any P)#^COMPLETE3?check=META^#

// META: Keyword/None: .Protocol[#(any P).Type#]; name=Protocol

Environment

Footnotes

  1. [x] means x is optional 2

@AnthonyLatsis AnthonyLatsis added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. good first issue Good for newcomers code completion Area → source tooling: code completion source tooling Area: IDE support, SourceKit, and other source tooling existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values metatypes Feature → types: Metatypes unexpected behavior Bug: Unexpected behavior or incorrect output types Feature: types labels May 5, 2023
@Rajveer100
Copy link
Contributor

@AnthonyLatsis
Could you give me a brief insight on this?

@AnthonyLatsis AnthonyLatsis changed the title Code completion: Stop suggesting old-style protocol metatype syntax Code completion: Stop suggesting old-style protocol metatype syntax in Swift 6 mode May 10, 2023
@AnthonyLatsis
Copy link
Collaborator Author

AnthonyLatsis commented May 10, 2023

@Rajveer100 After updating the description, I am no longer positive this can be qualified as a beginner-friendly issue, but here is some tips:

  • Code completion can be tested using the swift-ide-test executable found in the same place as swift-frontend (see test/IDE for example invocations).
  • In code completion mode, this executable searches for code completion tokens spelled #^TOKEN_NAME^#.
  • The places the Protocol completion is suggested are CompletionLookup::getPostfixKeywordCompletions and CompletionLookup::getTypeCompletions.

@AnthonyLatsis
Copy link
Collaborator Author

@ahoppen @rintaro Please review the good first issue status.

@ahoppen
Copy link
Member

ahoppen commented May 10, 2023

Yes, I agree that this could be a good first issue.

@aniket-singh-01
Copy link

aniket-singh-01 commented May 11, 2023

Hi Sir,
Can you assign this to me as I think this could solve it.

protocol P {}
P#^COMPLETE?check=META^#
// META: Keyword/CurrNominal: .Protocol[#(any P).Type#]; name=Protocol

changes to

protocol P {}
P.#^COMPLETE?check=META^#
// META: Keyword/CurrNominal: .Type[#any P.Type#]; name=Type
// META: Keyword/CurrNominal: .Type[#(any P).Type#]; name=Type

@AnthonyLatsis
Copy link
Collaborator Author

Note that a solution should also handle the any P and (any P) cases. Only the existential metatype completion should be suggested for any P, and only the singleton metatype completion — always spelled using any syntax, not only in Swift 6 mode — should be suggested for (any P).

@aniket-singh-01
Copy link

// META: Keyword/CurrNominal: .Type[#any P.Type#]; name=Type
// META: Keyword/CurrNominal: .Type[#(any P).Type#]; name=Type
The completion suggestions include .Type[#any P.Type#], which represents the existential metatype for any P, and .Type[#(any P).Type#], which represents the singleton metatype for (any P).

isn't this correct Sir?

@AnthonyLatsis
Copy link
Collaborator Author

AnthonyLatsis commented May 12, 2023

Your completion suggestions are fine for P#^COMPLETE^#, but not for any P#^COMPLETE^# or (any P)#^COMPLETE^#. Only one .Type completion is reasonable for those two. They should be addressed because they also produce a .Protocol completion suggestion:

  • any P.Protocol is not a sensible type at all.
  • (any P).Protocol is valid pre-Swift 6 but a mixture of the old and new syntaxes, which I believe we do not want to suggest.

@aniket-singh-01
Copy link

any P.#^ANY_COMPLETE?check=META^#
// META: Keyword/CurrNominal: .Type[#any P.Type#]; name=Type

(any P).#^PAREN_COMPLETE?check=META^#
// No completion suggestion

When we trigger code completion at any P.#^ANY_COMPLETE?check=META^#, the completion suggestion is .Type[#any P.Type#], which represents the existential metatype for any P.

There is no completion suggestion provided for (any P)#^COMPLETE?check=META^# because any P.Protocol is not a sensible type and (any P).Protocol is a mixture of the old and new syntaxe

I hope it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. code completion Area → source tooling: code completion existentials Feature: values of types like `any Collection`, `Any` and `AnyObject`; type-erased values good first issue Good for newcomers metatypes Feature → types: Metatypes source tooling Area: IDE support, SourceKit, and other source tooling swift 5.9 types Feature: types unexpected behavior Bug: Unexpected behavior or incorrect output
Projects
None yet
Development

No branches or pull requests

4 participants