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

[IDE] Resolve [.]Type completion for (any P). to produce singleton metatype instead of existential metatype. #73163

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 7 additions & 2 deletions lib/IDE/CompletionLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2440,8 +2440,13 @@ void CompletionLookup::getPostfixKeywordCompletions(Type ExprType,
if (instanceTy->isAnyExistentialType()) {
addKeyword("Protocol", MetatypeType::get(instanceTy),
SemanticContextKind::CurrentNominal);
addKeyword("Type", ExistentialMetatypeType::get(instanceTy),
SemanticContextKind::CurrentNominal);
if (instanceTy->hasParenSugar()) {
addKeyword("Type", MetatypeType::get(instanceTy),
SemanticContextKind::CurrentNominal);
} else {
addKeyword("Type", ExistentialMetatypeType::get(instanceTy),
SemanticContextKind::CurrentNominal);
}
Comment on lines +2443 to +2449
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, this is the right direction. But there really is no guarantee that the semantic type will retain the parentheses. A better option is to check the parsed type representation, which is meant to preserve the structure of the written type. You can get it from the type expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wouldn't this check ensure that?

if (isa<TypeExpr>(ParsedExpr)) {
  // ...
}

Copy link
Collaborator

@AnthonyLatsis AnthonyLatsis Apr 24, 2024

Choose a reason for hiding this comment

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

I think you misunderstand. I am suggesting to check for parentheses in the type representation as opposed to the semantic type. The type representation is stored in the type expression.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The type representation doesn't actually have a function for checking this, rather has a function to retrieve the expression without the parenthesis, do we want to introduce this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the existing function is optimal here.

} else {
addKeyword("Type", MetatypeType::get(instanceTy),
SemanticContextKind::CurrentNominal);
Expand Down
23 changes: 23 additions & 0 deletions test/IDE/issue-65843.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t

protocol P {}

(any P).#^COMPLETE?^#
// COMPLETE: Begin completions, 3 items
// COMPLETE: Keyword[self]/CurrNominal: self[#(any P).Type#]; name=self
// COMPLETE: Keyword/CurrNominal: Protocol[#(any P).Type#]; name=Protocol
// COMPLETE: Keyword/CurrNominal: Type[#(any P).Type#]; name=Type


P.Type.#^PROTOCOLTYPE_DOT_1?^#
// PROTOCOLTYPE_DOT_1: Begin completions, 3 items
// PROTOCOLTYPE_DOT_1: Keyword[self]/CurrNominal: self[#(any P.Type).Type#]; name=self
// PROTOCOLTYPE_DOT_1: Keyword/CurrNominal: Protocol[#(any P.Type).Type#]; name=Protocol
// PROTOCOLTYPE_DOT_1: Keyword/CurrNominal: Type[#any P.Type.Type#]; name=Type

(P).Type.#^PROTOCOLTYPE_DOT_2?^#
// PROTOCOLTYPE_DOT_2: Begin completions, 3 items
// PROTOCOLTYPE_DOT_2: Keyword[self]/CurrNominal: self[#(any (P).Type).Type#]; name=self
// PROTOCOLTYPE_DOT_2: Keyword/CurrNominal: Protocol[#(any (P).Type).Type#]; name=Protocol
// PROTOCOLTYPE_DOT_2: Keyword/CurrNominal: Type[#any (P).Type.Type#]; name=Type