Skip to content

Commit 47e303a

Browse files
committed
[CodeCompletion] Fix a crash in context type analysis for tuple expr
When completing inside tuple expressions, the context tuple type may have fewer number of elements. In such cases, we cannot provide the expected type. rdar://problem/61668779
1 parent 142791b commit 47e303a

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,9 @@ class ExprContextAnalyzer {
762762
unsigned Position = 0;
763763
bool HasName;
764764
if (getPositionInArgs(*DC, Parent, ParsedExpr, Position, HasName)) {
765-
recordPossibleType(tupleT->getElementType(Position));
765+
// The expected type may have fewer number of elements.
766+
if (Position < tupleT->getNumElements())
767+
recordPossibleType(tupleT->getElementType(Position));
766768
}
767769
break;
768770
}

test/IDE/complete_call_arg.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ARG_PARAMFLAG_IUO | %FileCheck %s -check-prefix=ARG_PARAMFLAG_IUO
102102
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=ARG_PARAMFLAG_VARIADIC | %FileCheck %s -check-prefix=ARG_PARAMFLAG_VARIADIC
103103

104+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TUPLEELEM_1 | %FileCheck %s -check-prefix=TUPLEELEM_1
105+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TUPLEELEM_2 | %FileCheck %s -check-prefix=TUPLEELEM_2
104106

105107
var i1 = 1
106108
var i2 = 2
@@ -816,3 +818,14 @@ func testPamrameterFlags(_: Int, inoutArg: inout Int, autoclosureArg: @autoclosu
816818
// ARG_PARAMFLAG_VARIADIC-DAG: Pattern/ExprSpecific: {#variadicArg: Int...#}[#Int#];
817819
// ARG_PARAMFLAG_VARIADIC: End completions
818820
}
821+
822+
func testTupleElement(arg: (SimpleEnum, SimpleEnum)) {
823+
testTupleElement(arg: (.foo, .#^TUPLEELEM_1^#))
824+
// TUPLEELEM_1: Begin completions, 3 items
825+
// TUPLEELEM_1-DAG: Decl[EnumElement]/ExprSpecific/TypeRelation[Identical]: foo[#SimpleEnum#]; name=foo
826+
// TUPLEELEM_1-DAG: Decl[EnumElement]/ExprSpecific/TypeRelation[Identical]: bar[#SimpleEnum#]; name=bar
827+
// TUPLEELEM_1-DAG: Decl[EnumElement]/ExprSpecific/TypeRelation[Identical]: baz[#SimpleEnum#]; name=baz
828+
// TUPLEELEM_1: End completions
829+
testTupleElement(arg: (.foo, .bar, .#^TUPLEELEM_2^#))
830+
// TUPLEELEM_2-NOT: Begin completions
831+
}

0 commit comments

Comments
 (0)