Skip to content

[5.7][CodeCompletion] Allow references to top-level functions with error parameters #42240

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
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
6 changes: 5 additions & 1 deletion lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,11 @@ namespace {

// If declaration is invalid, let's turn it into a potential hole
// and keep generating constraints.
if (!knownType && E->getDecl()->isInvalid()) {
// For code completion, we still resolve the overload and replace error
// types inside the function decl with placeholders
// (in getTypeOfReference) so we can match non-error param types.
if (!knownType && E->getDecl()->isInvalid() &&
!CS.isForCodeCompletion()) {
auto *hole = CS.createTypeVariable(locator, TVO_CanBindToHole);
(void)CS.recordFix(AllowRefToInvalidDecl::create(CS, locator));
CS.setType(E, hole);
Expand Down
70 changes: 38 additions & 32 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,38 @@ AnyFunctionType *ConstraintSystem::adjustFunctionTypeForConcurrency(
fnType, decl, dc, numApplies, isMainDispatchQueue, GetClosureType{*this});
}

/// For every parameter in \p type that has an error type, replace that
/// parameter's type by a placeholder type, where \p value is the declaration
/// that declared \p type. This is useful for code completion so we can match
/// the types we do know instead of bailing out completely because \p type
/// contains an error type.
static Type replaceParamErrorTypeByPlaceholder(Type type, ValueDecl *value) {
if (!type->is<AnyFunctionType>() || !isa<AbstractFunctionDecl>(value)) {
return type;
}
auto funcType = type->castTo<AnyFunctionType>();
auto funcDecl = cast<AbstractFunctionDecl>(value);

auto declParams = funcDecl->getParameters();
auto typeParams = funcType->getParams();
assert(declParams->size() == typeParams.size());
SmallVector<AnyFunctionType::Param, 4> newParams;
newParams.reserve(declParams->size());
for (auto i : indices(typeParams)) {
AnyFunctionType::Param param = typeParams[i];
if (param.getPlainType()->is<ErrorType>()) {
auto paramDecl = declParams->get(i);
auto placeholder =
PlaceholderType::get(paramDecl->getASTContext(), paramDecl);
newParams.push_back(param.withType(placeholder));
} else {
newParams.push_back(param);
}
}
assert(newParams.size() == declParams->size());
return FunctionType::get(newParams, funcType->getResult());
}

std::pair<Type, Type>
ConstraintSystem::getTypeOfReference(ValueDecl *value,
FunctionRefKind functionRefKind,
Expand Down Expand Up @@ -1458,6 +1490,12 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
openedType->getAs<FunctionType>(),
locator);

if (isForCodeCompletion() && openedType->hasError()) {
// In code completion, replace error types by placeholder types so we can
// match the types we know instead of bailing out completely.
openedType = replaceParamErrorTypeByPlaceholder(openedType, value);
}

// If we opened up any type variables, record the replacements.
recordOpenedTypes(locator, replacements);

Expand Down Expand Up @@ -1950,38 +1988,6 @@ Type constraints::typeEraseOpenedExistentialReference(
});
}

/// For every parameter in \p type that has an error type, replace that
/// parameter's type by a placeholder type, where \p value is the declaration
/// that declared \p type. This is useful for code completion so we can match
/// the types we do know instead of bailing out completely because \p type
/// contains an error type.
static Type replaceParamErrorTypeByPlaceholder(Type type, ValueDecl *value) {
if (!type->is<AnyFunctionType>() || !isa<AbstractFunctionDecl>(value)) {
return type;
}
auto funcType = type->castTo<AnyFunctionType>();
auto funcDecl = cast<AbstractFunctionDecl>(value);

auto declParams = funcDecl->getParameters();
auto typeParams = funcType->getParams();
assert(declParams->size() == typeParams.size());
SmallVector<AnyFunctionType::Param, 4> newParams;
newParams.reserve(declParams->size());
for (auto i : indices(typeParams)) {
AnyFunctionType::Param param = typeParams[i];
if (param.getPlainType()->is<ErrorType>()) {
auto paramDecl = declParams->get(i);
auto placeholder =
PlaceholderType::get(paramDecl->getASTContext(), paramDecl);
newParams.push_back(param.withType(placeholder));
} else {
newParams.push_back(param);
}
}
assert(newParams.size() == declParams->size());
return FunctionType::get(newParams, funcType->getResult());
}

std::pair<Type, Type>
ConstraintSystem::getTypeOfMemberReference(
Type baseTy, ValueDecl *value, DeclContext *useDC,
Expand Down
11 changes: 11 additions & 0 deletions test/IDE/complete_call_arg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1371,3 +1371,14 @@ func testDynamicMemberSubscriptLookup() {
// DYNAMIC_MEMBER_SUBSCRIPT_LOOKUP-DAG: Decl[LocalVar]/Local/TypeRelation[Identical]: index[#Int#]; name=index
// DYNAMIC_MEMBER_SUBSCRIPT_LOOKUP-DAG: Pattern/CurrNominal/Flair[ArgLabels]: ['[']{#keyPath: KeyPath<Binding<MyStruct>, Value>#}[']'][#Value#]; name=keyPath:
// DYNAMIC_MEMBER_SUBSCRIPT_LOOKUP: End completions

func testTopLevelFuncWithErrorParam() {
enum A { case a }
func foo(x: A, b: Undefined) {}

foo(x: .#^TOP_LEVEL_FUNC_WITH_ERROR_PARAM^#)
// TOP_LEVEL_FUNC_WITH_ERROR_PARAM: Begin completions, 2 items
// TOP_LEVEL_FUNC_WITH_ERROR_PARAM-DAG: Decl[EnumElement]/CurrNominal/Flair[ExprSpecific]/TypeRelation[Identical]: a[#A#]; name=a
// TOP_LEVEL_FUNC_WITH_ERROR_PARAM-DAG: Decl[InstanceMethod]/CurrNominal/TypeRelation[Invalid]: hash({#(self): A#})[#(into: inout Hasher) -> Void#]; name=hash(:)
// TOP_LEVEL_FUNC_WITH_ERROR_PARAM: End completions
}