Skip to content

[CodeCompletion] Don’t compute type relations if the contextual type is Any #59021

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
May 24, 2022
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
22 changes: 22 additions & 0 deletions lib/IDE/CodeCompletionResultType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,34 @@ TypeRelation USRBasedTypeContext::ContextualType::typeRelation(

// MARK: - CodeCompletionResultType

/// Returns \c true if \p Ty is the 'Any' type or some type that is sufficiently
/// similar to Any, like the 'Any' metatype or an optional type wrapping 'Any'.
static bool isEssentiallyAnyType(Type Ty) {
while (true) {
if (auto MT = Ty->getAs<AnyMetatypeType>()) {
Ty = MT->getInstanceType();
} else if (auto OT = Ty->getOptionalObjectType()) {
Ty = OT;
} else {
break;
}
}
return Ty->isAny();
}

static TypeRelation calculateTypeRelation(Type Ty, Type ExpectedTy,
const DeclContext &DC) {
if (Ty.isNull() || ExpectedTy.isNull() || Ty->is<ErrorType>() ||
ExpectedTy->is<ErrorType>())
return TypeRelation::Unrelated;

/// Computing type relations to 'Any' is not very enlightning because
/// everything would be convertible to it. If the contextual type is 'Any',
/// just report all type relations as 'Unknown'.
if (isEssentiallyAnyType(ExpectedTy)) {
return TypeRelation::Unknown;
}

// Equality/Conversion of GenericTypeParameterType won't account for
// requirements – ignore them
if (!Ty->hasTypeParameter() && !ExpectedTy->hasTypeParameter()) {
Expand Down
2 changes: 1 addition & 1 deletion test/IDE/complete_at_top_level.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ var stringInterp = "\(#^STRING_INTERP_3?check=STRING_INTERP^#)"
_ = "" + "\(#^STRING_INTERP_4?check=STRING_INTERP^#)" + ""
// STRING_INTERP: Begin completions
// STRING_INTERP-DAG: Decl[InstanceMethod]/CurrNominal/Flair[ArgLabels]/IsSystem: ['(']{#(value): T#}[')'][#Void#];
// STRING_INTERP-DAG: Decl[Struct]/CurrModule/TypeRelation[Convertible]: FooStruct[#FooStruct#]; name=FooStruct
// STRING_INTERP-DAG: Decl[Struct]/CurrModule: FooStruct[#FooStruct#]; name=FooStruct
// STRING_INTERP-DAG: Decl[FreeFunction]/CurrModule/TypeRelation[Invalid]: fooFunc1()[#Void#];
// STRING_INTERP-DAG: Decl[FreeFunction]/CurrModule: optStr()[#String?#];
// STRING_INTERP-DAG: Decl[GlobalVar]/Local: fooObject[#FooStruct#];
Expand Down
35 changes: 30 additions & 5 deletions test/IDE/complete_type_any.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ANY_IN_FUNC_PARAM | %FileCheck %s -check-prefix=ANY_IN_FUNC_PARAM
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ANY_IN_VAR_TYPE | %FileCheck %s -check-prefix=ANY_IN_VAR_TYPE
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ANY_METATYPE_VARIABLE | %FileCheck %s -check-prefix=ANY_METATYPE_VARIABLE
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ANY_METATYPE_MEMBER | %FileCheck %s -check-prefix=ANY_METATYPE_MEMBER
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ANY_IN_TYPEALIAS | %FileCheck %s -check-prefix=ANY_IN_TYPEALIAS
// RUN: %empty-directory(%t)
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t

func testAnyInParamList(a: #^ANY_IN_FUNC_PARAM^#
// ANY_IN_FUNC_PARAM: Begin completions
Expand Down Expand Up @@ -31,4 +28,32 @@ typealias A = #^ANY_IN_TYPEALIAS^#
// ANY_IN_TYPEALIAS-DAG: Keyword/None: Any[#Any#]; name=Any
// ANY_IN_TYPEALIAS: End completions

func testRdar64812321() {
func foo<T>(x: T) {}
func foo(x: Any.Type) {}

struct MyStruct {}
let myStruct = MyStruct()

foo(x: #^ANY_RELATIONSHIP^#)
// The MyStruct type should not be preferred over the myStruct instance.

// ANY_RELATIONSHIP: Begin completions
// ANY_RELATIONSHIP-DAG: Decl[LocalVar]/Local: myStruct[#MyStruct#]; name=myStruct
// ANY_RELATIONSHIP-DAG: Decl[Struct]/Local: MyStruct[#MyStruct#]; name=MyStruct
// ANY_RELATIONSHIP: End completions
}

func testRdar84684686() {
func foo(_ x: Any?) {}

struct S {
static func bar(x: Int) -> Int { x }
}

// We should suggest a function call to `bar` here (i.e. `bar(x: <#Int#>)`), not a function reference (i.e. `bar(x:)`)
foo(S.#^ANY_PREFERS_FUNCTION_CALL^#)
// ANY_PREFERS_FUNCTION_CALL: Begin completions
// ANY_PREFERS_FUNCTION_CALL-DAG: Decl[StaticMethod]/CurrNominal: bar({#x: Int#})[#Int#]; name=bar(x:)
// ANY_PREFERS_FUNCTION_CALL: End completions
}