Skip to content

Commit e4f82ea

Browse files
committed
[IDE] Pass whether we have applied self to replaceParamErrorTypeByPlaceholder
1 parent c94210c commit e4f82ea

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,30 +1526,35 @@ AnyFunctionType *ConstraintSystem::adjustFunctionTypeForConcurrency(
15261526
/// that declared \p type. This is useful for code completion so we can match
15271527
/// the types we do know instead of bailing out completely because \p type
15281528
/// contains an error type.
1529-
static Type replaceParamErrorTypeByPlaceholder(Type type, ValueDecl *value) {
1529+
static Type replaceParamErrorTypeByPlaceholder(Type type, ValueDecl *value, bool hasAppliedSelf) {
15301530
if (!type->is<AnyFunctionType>() || !isa<AbstractFunctionDecl>(value)) {
15311531
return type;
15321532
}
15331533
auto funcType = type->castTo<AnyFunctionType>();
15341534
auto funcDecl = cast<AbstractFunctionDecl>(value);
15351535

1536-
auto declParams = funcDecl->getParameters();
1536+
SmallVector<ParamDecl *> declParams;
1537+
if (hasAppliedSelf) {
1538+
declParams.append(funcDecl->getParameters()->begin(), funcDecl->getParameters()->end());
1539+
} else {
1540+
declParams.push_back(funcDecl->getImplicitSelfDecl());
1541+
}
15371542
auto typeParams = funcType->getParams();
1538-
assert(declParams->size() == typeParams.size());
1543+
assert(declParams.size() == typeParams.size());
15391544
SmallVector<AnyFunctionType::Param, 4> newParams;
1540-
newParams.reserve(declParams->size());
1545+
newParams.reserve(declParams.size());
15411546
for (auto i : indices(typeParams)) {
15421547
AnyFunctionType::Param param = typeParams[i];
15431548
if (param.getPlainType()->is<ErrorType>()) {
1544-
auto paramDecl = declParams->get(i);
1549+
auto paramDecl = declParams[i];
15451550
auto placeholder =
15461551
PlaceholderType::get(paramDecl->getASTContext(), paramDecl);
15471552
newParams.push_back(param.withType(placeholder));
15481553
} else {
15491554
newParams.push_back(param);
15501555
}
15511556
}
1552-
assert(newParams.size() == declParams->size());
1557+
assert(newParams.size() == declParams.size());
15531558
return FunctionType::get(newParams, funcType->getResult());
15541559
}
15551560

@@ -1620,7 +1625,7 @@ ConstraintSystem::getTypeOfReference(ValueDecl *value,
16201625
if (isForCodeCompletion() && openedType->hasError()) {
16211626
// In code completion, replace error types by placeholder types so we can
16221627
// match the types we know instead of bailing out completely.
1623-
openedType = replaceParamErrorTypeByPlaceholder(openedType, value);
1628+
openedType = replaceParamErrorTypeByPlaceholder(openedType, value, /*hasAppliedSelf=*/true);
16241629
}
16251630

16261631
// If we opened up any type variables, record the replacements.
@@ -2288,7 +2293,7 @@ Type ConstraintSystem::getMemberReferenceTypeFromOpenedType(
22882293
if (isForCodeCompletion() && type->hasError()) {
22892294
// In code completion, replace error types by placeholder types so we can
22902295
// match the types we know instead of bailing out completely.
2291-
type = replaceParamErrorTypeByPlaceholder(type, value);
2296+
type = replaceParamErrorTypeByPlaceholder(type, value, hasAppliedSelf);
22922297
}
22932298

22942299
return type;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-ide-test -batch-code-completion -source-filename %s -filecheck %raw-FileCheck -completion-output-dir %t
3+
4+
func myGlobalFunction() -> Invalid {}
5+
6+
struct Test {
7+
func myInstanceMethod() -> Invalid {}
8+
9+
func testInstanceMethod() {
10+
Test.myInstanceMethod#^INSTANCE_METHOD^#
11+
// Check that we don't crash
12+
// INSTANCE_METHOD-NOT: Begin completions
13+
}
14+
15+
func testGlobalFunctionMethod() {
16+
myGlobalFunction#^GLOBAL_FUNCTION^#
17+
// Check that we don't crash
18+
// GLOBAL_FUNCTION: Begin completions
19+
// GLOBAL_FUNCTION: Keyword[self]/CurrNominal: .self[#_#]
20+
// GLOBAL_FUNCTION: End completions
21+
}
22+
23+
func testLocalFunction() {
24+
func myLocalFunction() -> Invalid {}
25+
myLocalFunction#^LOCAL_FUNCTION^#
26+
// LOCAL_FUNCTION: Begin completions
27+
// LOCAL_FUNCTION: Keyword[self]/CurrNominal: .self[#_#]
28+
// LOCAL_FUNCTION: End completions
29+
}
30+
}
31+

0 commit comments

Comments
 (0)