Skip to content

Commit 1c00cd9

Browse files
committed
[CodeCompletion] Fix dot completion with non-nominals
Completion after dot inside an init (or any other parent expr with a nominal type) was incorrectly looking at the types of parent expressions whenever the base type was not a nominal (even an lvalue of a nominal wasn't working). This code to look at the type of the parent was never correct, and fortunately the type-checking issues that prompted it to be added in the first place have since been fixed, so we can just delete it. rdar://problem/25773358
1 parent 864bb61 commit 1c00cd9

File tree

2 files changed

+28
-23
lines changed

2 files changed

+28
-23
lines changed

lib/IDE/CodeCompletion.cpp

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4887,37 +4887,17 @@ void CodeCompletionCallbacksImpl::doneParsing() {
48874887

48884888
case CompletionKind::DotExpr: {
48894889
Lookup.setHaveDot(DotLoc);
4890-
Type OriginalType = *ExprType;
4891-
Type ExprType = OriginalType;
4892-
4893-
// If there is no nominal type in the expr, try to find nominal types
4894-
// in the ancestors of the expr.
4895-
if (!OriginalType->getAnyNominal()) {
4896-
ExprParentFinder Walker(ParsedExpr, [&](ASTNode Node) {
4897-
if (auto E = Node.dyn_cast<Expr *>()) {
4898-
return E->getType() && E->getType()->getAnyNominal();
4899-
} else {
4900-
return false;
4901-
}
4902-
});
4903-
CurDeclContext->walkContext(Walker);
4904-
if (auto PCE = Walker.ParentClosest.dyn_cast<Expr *>()) {
4905-
ExprType = PCE->getType();
4906-
} else {
4907-
ExprType = OriginalType;
4908-
}
4909-
}
49104890

4911-
if (isDynamicLookup(ExprType))
4891+
if (isDynamicLookup(*ExprType))
49124892
Lookup.setIsDynamicLookup();
4913-
Lookup.initializeArchetypeTransformer(CurDeclContext, ExprType);
4893+
Lookup.initializeArchetypeTransformer(CurDeclContext, *ExprType);
49144894

49154895
CodeCompletionTypeContextAnalyzer TypeAnalyzer(CurDeclContext, ParsedExpr);
49164896
llvm::SmallVector<Type, 2> PossibleTypes;
49174897
if (TypeAnalyzer.Analyze(PossibleTypes)) {
49184898
Lookup.setExpectedTypes(PossibleTypes);
49194899
}
4920-
Lookup.getValueExprCompletions(ExprType, ReferencedDecl.getDecl());
4900+
Lookup.getValueExprCompletions(*ExprType, ReferencedDecl.getDecl());
49214901
break;
49224902
}
49234903

test/IDE/complete_value_expr.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@
171171
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GENERIC_TYPEALIAS_2 | FileCheck %s -check-prefix=GENERIC_TYPEALIAS_2
172172

173173
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DEPRECATED_1 | FileCheck %s -check-prefix=DEPRECATED_1
174+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DOT_EXPR_NON_NOMINAL_1 | FileCheck %s -check-prefix=DOT_EXPR_NON_NOMINAL_1
175+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=DOT_EXPR_NON_NOMINAL_2 | FileCheck %s -check-prefix=DOT_EXPR_NON_NOMINAL_2
174176

175177
// Test code completion of expressions that produce a value.
176178

@@ -1890,3 +1892,26 @@ struct Deprecated {
18901892
}
18911893
}
18921894
// DEPRECATED_1: Decl[InstanceMethod]/CurrNominal/NotRecommended: deprecated({#x: Deprecated#})[#Void#];
1895+
1896+
struct Person {
1897+
var firstName: String
1898+
}
1899+
class Other { var nameFromOther: Int = 1 }
1900+
class TestDotExprWithNonNominal {
1901+
var other: Other
1902+
1903+
func test1() {
1904+
let person = Person(firstName: other.#^DOT_EXPR_NON_NOMINAL_1^#)
1905+
// DOT_EXPR_NON_NOMINAL_1-NOT: Instance
1906+
// DOT_EXPR_NON_NOMINAL_1: Decl[InstanceVar]/CurrNominal: nameFromOther[#Int#];
1907+
// DOT_EXPR_NON_NOMINAL_1-NOT: Instance
1908+
}
1909+
func test2() {
1910+
let person = Person(firstName: 1.#^DOT_EXPR_NON_NOMINAL_2^#)
1911+
// DOT_EXPR_NON_NOMINAL_2-NOT: other
1912+
// DOT_EXPR_NON_NOMINAL_2-NOT: firstName
1913+
// DOT_EXPR_NON_NOMINAL_2: Decl[InstanceVar]/CurrNominal: hashValue[#Int#];
1914+
// DOT_EXPR_NON_NOMINAL_2-NOT: other
1915+
// DOT_EXPR_NON_NOMINAL_2-NOT: firstName
1916+
}
1917+
}

0 commit comments

Comments
 (0)