Skip to content

Commit f107a15

Browse files
committed
[CodeCompletion] Skip to '=' after completion in 'if'/'guard' pattern
So that the parser can parse the subject expresion which provides the expected type of the pattern. rdar://problem/56802036
1 parent 94ff157 commit f107a15

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/Parse/ParseStmt.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,8 +1503,13 @@ Parser::parseStmtConditionElement(SmallVectorImpl<StmtConditionElement> &result,
15031503
}
15041504

15051505
ThePattern = parseOptionalPatternTypeAnnotation(ThePattern);
1506-
if (ThePattern.hasCodeCompletion())
1506+
if (ThePattern.hasCodeCompletion()) {
15071507
Status.setHasCodeCompletion();
1508+
1509+
// Skip to '=' so that the completion can see the expected type of the
1510+
// pattern which is determined by the initializer.
1511+
skipUntilDeclStmtRBrace(tok::equal, tok::l_brace);
1512+
}
15081513

15091514
if (ThePattern.isNull()) {
15101515
// Recover by creating AnyPattern.

test/IDE/complete_stmt_controlling_expr.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@
120120
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GUARD_LET_BIND_7 | %FileCheck %s -check-prefix=FOOSTRUCT_LOCALVAL
121121
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GUARD_LET_BIND_8 | %FileCheck %s -check-prefix=FOOSTRUCT_LOCALVAL
122122

123+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GUARD_CASE_PATTERN_1| %FileCheck %s -check-prefix=OPTIONAL_FOOSTRUCT
124+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=GUARD_CASE_PATTERN_2| %FileCheck %s -check-prefix=OPTIONAL_FOOSTRUCT
125+
123126
struct FooStruct {
124127
var instanceVar : Int
125128
init(_: Int = 0) { }
@@ -612,7 +615,12 @@ func testGuardLetBinding7(x: FooStruct?) {
612615
func testGuardLetBinding8(_ x: FooStruct?) {
613616
guard let boundVal = x, let other = testGuardLetBinding8(#^GUARD_LET_BIND_8^#) else {}
614617
}
615-
618+
func testGuardCase(x:FooStruct?) {
619+
guard case .#^GUARD_CASE_PATTERN_1^# = x {}
620+
}
621+
func testGuardCase(x:FooStruct?) {
622+
guard case .#^GUARD_CASE_PATTERN_2^#some() = x {}
623+
}
616624

617625
// FOOSTRUCT_DOT: Begin completions
618626
// FOOSTRUCT_DOT-DAG: Decl[InstanceVar]/CurrNominal: instanceVar[#Int#];
@@ -635,3 +643,12 @@ func testGuardLetBinding8(_ x: FooStruct?) {
635643
// FOOSTRUCT_LOCALVAL: Begin completions
636644
// FOOSTRUCT_LOCALVAL-DAG: Decl[LocalVar]/Local{{(/TypeRelation\[Convertible\])?}}: boundVal[#FooStruct#];
637645
// FOOSTRUCT_LOCALVAL: End completions
646+
647+
// OPTIONAL_FOOSTRUCT: Begin completions, 5 items
648+
// OPTIONAL_FOOSTRUCT-DAG: Keyword[nil]/None/Erase[1]: nil[#FooStruct?#]; name=nil
649+
// OPTIONAL_FOOSTRUCT-DAG: Decl[EnumElement]/CurrNominal/IsSystem: none[#Optional<FooStruct>#]; name=none
650+
// OPTIONAL_FOOSTRUCT-DAG: Decl[EnumElement]/CurrNominal/IsSystem: some({#FooStruct#})[#Optional<FooStruct>#]; name=some(FooStruct)
651+
// FIXME: 'FooStruct' members should not be shown.
652+
// OPTIONAL_FOOSTRUCT-DAG: Decl[Constructor]/CurrNominal/TypeRelation[Convertible]: init()[#FooStruct#]; name=init()
653+
// OPTIONAL_FOOSTRUCT-DAG: Decl[Constructor]/CurrNominal/TypeRelation[Convertible]: init({#Int#})[#FooStruct#]; name=init(Int)
654+
// OPTIONAL_FOOSTRUCT: End completions

0 commit comments

Comments
 (0)