Skip to content

Commit 33ee653

Browse files
authored
Merge pull request #58614 from ahoppen/pr/complete-type-with-same-type-requirement
[CodeCompletion] Offer suggestions if a nested type is followed by a same type requirement
2 parents 6f05a95 + a35f185 commit 33ee653

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

lib/Parse/ParseGeneric.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,23 @@ ParserStatus Parser::parseGenericWhereClause(
350350
SecondType = makeParserResult(new (Context) ErrorTypeRepr(PreviousLoc));
351351

352352
// Add the requirement
353-
Requirements.push_back(RequirementRepr::getSameType(FirstType.get(),
354-
EqualLoc,
355-
SecondType.get()));
353+
if (FirstType.hasCodeCompletion()) {
354+
// If the first type has a code completion token, don't record a same
355+
// type constraint because otherwise if we have
356+
// K.#^COMPLETE^# == Foo
357+
// we parse this as
358+
// K == Foo
359+
// and thus simplify K to Foo. But we didn't want to state that K is Foo
360+
// but that K has a member of type Foo.
361+
// FIXME: The proper way to fix this would be to represent the code
362+
// completion token in the TypeRepr.
363+
Requirements.push_back(RequirementRepr::getTypeConstraint(
364+
FirstType.get(), EqualLoc,
365+
new (Context) ErrorTypeRepr(SecondType.get()->getLoc())));
366+
} else {
367+
Requirements.push_back(RequirementRepr::getSameType(
368+
FirstType.get(), EqualLoc, SecondType.get()));
369+
}
356370
} else if (FirstType.hasCodeCompletion()) {
357371
// Recover by adding dummy constraint.
358372
Requirements.push_back(RequirementRepr::getTypeConstraint(

test/IDE/complete_where_clause.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=EXT_ASSOC_MEMBER_1 | %FileCheck %s -check-prefix=EXT_ASSOC_MEMBER
4242
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=EXT_ASSOC_MEMBER_2 | %FileCheck %s -check-prefix=EXT_ASSOC_MEMBER
4343
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=EXT_SECONDTYPE | %FileCheck %s -check-prefix=EXT_SECONDTYPE
44+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=WHERE_CLAUSE_WITH_EQUAL | %FileCheck %s -check-prefix=WHERE_CLAUSE_WITH_EQUAL
4445

4546
class A1<T1, T2, T3> {}
4647

@@ -264,3 +265,10 @@ extension WithAssoc where Int == #^EXT_SECONDTYPE^#
264265
// EXT_SECONDTYPE: Begin completions
265266
// EXT_SECONDTYPE-DAG: Decl[AssociatedType]/CurrNominal: T;
266267
// EXT_SECONDTYPE: End completions
268+
269+
func foo<K: WithAssoc>(_ key: K.Type) where K.#^WHERE_CLAUSE_WITH_EQUAL^# == S1 {}
270+
271+
// WHERE_CLAUSE_WITH_EQUAL: Begin completions, 2 items
272+
// WHERE_CLAUSE_WITH_EQUAL-DAG: Decl[AssociatedType]/CurrNominal: T;
273+
// WHERE_CLAUSE_WITH_EQUAL-DAG: Keyword/None: Type[#K.Type#];
274+
// WHERE_CLAUSE_WITH_EQUAL: End completions

0 commit comments

Comments
 (0)