Skip to content

[CodeCompletion] Record key path component types in the constraint system solution #38389

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
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
7 changes: 7 additions & 0 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -1185,6 +1185,10 @@ class Solution {
/// The node -> type mappings introduced by this solution.
llvm::DenseMap<ASTNode, Type> nodeTypes;

/// The key path component types introduced by this solution.
llvm::DenseMap<std::pair<const KeyPathExpr *, unsigned>, TypeBase *>
keyPathComponentTypes;

/// Contextual types introduced by this solution.
std::vector<std::pair<ASTNode, ContextualTypeInfo>> contextualTypes;

Expand Down Expand Up @@ -1300,6 +1304,9 @@ class Solution {
/// Retrieve the type of the given node, as recorded in this solution.
Type getType(ASTNode node) const;

/// Retrieve the type of the \p ComponentIndex-th component in \p KP.
Type getType(const KeyPathExpr *KP, unsigned ComponentIndex) const;

/// Retrieve the type of the given node as recorded in this solution
/// and resolve all of the type variables in contains to form a fully
/// "resolved" concrete type.
Expand Down
10 changes: 8 additions & 2 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8900,8 +8900,9 @@ bool Solution::hasType(ASTNode node) const {
}

bool Solution::hasType(const KeyPathExpr *KP, unsigned ComponentIndex) const {
auto &cs = getConstraintSystem();
return cs.hasType(KP, ComponentIndex);
assert(KP && "Expected non-null key path parameter!");
return keyPathComponentTypes.find(std::make_pair(KP, ComponentIndex))
!= keyPathComponentTypes.end();
}

Type Solution::getType(ASTNode node) const {
Expand All @@ -8913,6 +8914,11 @@ Type Solution::getType(ASTNode node) const {
return cs.getType(node);
}

Type Solution::getType(const KeyPathExpr *KP, unsigned I) const {
assert(hasType(KP, I) && "Expected type to have been set!");
return keyPathComponentTypes.find(std::make_pair(KP, I))->second;
}

Type Solution::getResolvedType(ASTNode node) const {
return simplifyType(getType(node));
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ Solution ConstraintSystem::finalize() {
for (auto &nodeType : NodeTypes) {
solution.nodeTypes.insert(nodeType);
}
for (auto &keyPathComponentType : KeyPathComponentTypes) {
solution.keyPathComponentTypes.insert(keyPathComponentType);
}

// Remember contextual types.
solution.contextualTypes.assign(
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckCodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1347,7 +1347,7 @@ void KeyPathTypeCheckCompletionCallback::sawSolution(
} else {
// We are completing after a component. Get the previous component's result
// type.
BaseType = S.simplifyType(CS.getType(KeyPath, ComponentIndex - 1));
BaseType = S.simplifyType(S.getType(KeyPath, ComponentIndex - 1));
}

// If ExpectedTy is a duplicate of any other result, ignore this solution.
Expand Down
29 changes: 29 additions & 0 deletions test/IDE/complete_sr14916.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %swift-ide-test -code-completion -code-completion-token COMPLETE -source-filename %s | %FileCheck %s

struct Foo {
var bar: Int
}

protocol View2 {}
struct EmptyView: View2 {}

@resultBuilder public struct ViewBuilder2 {
public static func buildBlock(_ content: EmptyView) -> EmptyView { fatalError() }
}

public struct List2 {
public init(selection: Int?, @ViewBuilder2 content: () -> EmptyView)
public init(selection: String?, @ViewBuilder2 content: () -> EmptyView)
}

func foo(kp: (Foo) -> String) {}

func foo() {
List2 {
foo(kp: \.self#^COMPLETE^#)
// CHECK: Begin completions, 1 items
// CHECK-NEXT: Decl[InstanceVar]/CurrNominal: .bar[#Int#];
// CHECK-NEXT: End completions
}
.unknownMethod()
}