Skip to content

Commit 69d1c16

Browse files
authored
Merge pull request swiftlang#70225 from ahoppen/ahoppen/non-default-literal-completion
[CodeCompletion] Don’t increase score for non-default literal types when performing member completion on a literal
2 parents d66f233 + 485a06e commit 69d1c16

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lib/Sema/CSRanking.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,23 @@ static bool shouldIgnoreScoreIncreaseForCodeCompletion(
8888
return true;
8989
}
9090

91-
// The sibling argument is the code completion expression, this allows e.g.
92-
// non-default literal values in sibling arguments.
93-
// E.g. we allow a 1 to be a double in
94-
// foo(1, #^COMPLETE^#)
9591
if (auto parent = cs.getParentExpr(expr)) {
92+
// The sibling argument is the code completion expression, this allows e.g.
93+
// non-default literal values in sibling arguments.
94+
// E.g. we allow a 1 to be a double in
95+
// foo(1, #^COMPLETE^#)
9696
if (exprHasCodeCompletionAsArgument(parent, cs)) {
9797
return true;
9898
}
99+
// If we are completing a member of a literal, consider completion results
100+
// for all possible literal types. E.g. show completion results for `let a:
101+
// Double = 1.#^COMPLETE^#
102+
if (isa_and_nonnull<CodeCompletionExpr>(parent) &&
103+
kind == SK_NonDefaultLiteral) {
104+
return true;
105+
}
99106
}
107+
100108
return false;
101109
}
102110

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %batch-code-completion
2+
3+
extension Double {
4+
var doubleOnlyMember: Double { self }
5+
}
6+
7+
struct CustomInt: ExpressibleByIntegerLiteral {
8+
public init() {}
9+
public init(integerLiteral value: Int) { }
10+
var customIntMember: CustomInt { .init() }
11+
}
12+
13+
func test() {
14+
let double: Double = 2.#^DOUBLE_CONTEXTUAL_TYPE^#
15+
// DOUBLE_CONTEXTUAL_TYPE: Decl[InstanceVar]/CurrNominal/TypeRelation[Convertible]: doubleOnlyMember[#Double#];
16+
17+
let int: Int = 2.#^INT_CONTEXTUAL_TYPE^#
18+
// INT_CONTEXTUAL_TYPE: Decl[InstanceVar]/CurrNominal: doubleOnlyMember[#Double#];
19+
20+
let customInt: CustomInt = 2.#^CUSTOM_INT^#
21+
// CUSTOM_INT-NOT: customIntMember
22+
}

0 commit comments

Comments
 (0)