Skip to content

Commit afd3be9

Browse files
committed
[CodeCompletion] Improve context type analysis for ternary expressions
This improves code-completion for non-type-checked ternary expressions.
1 parent 1e8c644 commit afd3be9

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,18 @@ class ExprContextAnalyzer {
645645
}
646646
break;
647647
}
648+
case ExprKind::If: {
649+
auto *IE = cast<IfExpr>(Parent);
650+
if (SM.rangeContains(IE->getCondExpr()->getSourceRange(),
651+
ParsedExpr->getSourceRange())) {
652+
recordPossibleType(Context.getBoolDecl()->getDeclaredInterfaceType());
653+
break;
654+
}
655+
ExprContextInfo ternaryCtxtInfo(DC, Parent);
656+
for (auto ternaryT : ternaryCtxtInfo.getPossibleTypes())
657+
recordPossibleType(ternaryT);
658+
break;
659+
}
648660
case ExprKind::Assign: {
649661
auto *AE = cast<AssignExpr>(Parent);
650662

@@ -861,7 +873,7 @@ class ExprContextAnalyzer {
861873
case ExprKind::Assign:
862874
case ExprKind::Array:
863875
case ExprKind::Dictionary:
864-
return true;
876+
case ExprKind::If:
865877
case ExprKind::UnresolvedMember:
866878
return true;
867879
case ExprKind::Tuple: {

test/IDE/complete_unresolved_members.swift

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@
119119
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TERNARY_2 | %FileCheck %s -check-prefix=UNRESOLVED_3
120120
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TERNARY_3 | %FileCheck %s -check-prefix=UNRESOLVED_3
121121
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TERNARY_4 | %FileCheck %s -check-prefix=UNRESOLVED_3
122+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TERNARY_5 | %FileCheck %s -check-prefix=UNRESOLVED_3
123+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TERNARY_6 | %FileCheck %s -check-prefix=UNRESOLVED_3
124+
// RUN: %target-swift-ide-test -code-completion -source-filename %s -code-completion-token=TERNARY_CONDITION | %FileCheck %s -check-prefix=TERNARY_CONDITION
122125

123126
enum SomeEnum1 {
124127
case South
@@ -738,12 +741,23 @@ func testTypeParamInContextType() {
738741
// TYPEPARAM_IN_CONTEXTTYPE_1: End completions
739742
}
740743

741-
func testTernaryOperator() {
742-
let _: SomeEnum1 = true ? .#^TERNARY_1^#
744+
func testTernaryOperator(cond: Bool) {
745+
let _: SomeEnum1 = cond ? .#^TERNARY_1^#
743746
func sync(){}
744-
let _: SomeEnum1 = true ? .#^TERNARY_2^# :
747+
let _: SomeEnum1 = cond ? .#^TERNARY_2^# :
745748
func sync(){}
746-
let _: SomeEnum1 = true ? .#^TERNARY_3^# : .South
749+
let _: SomeEnum1 = cond ? .#^TERNARY_3^# : .South
747750
func sync(){}
748-
let _: SomeEnum1 = true ? .South : .#^TERNARY_4^#
751+
let _: SomeEnum1 = cond ? .South : .#^TERNARY_4^#
752+
}
753+
754+
func testTernaryOperator2(cond: Bool) {
755+
let _: SomeEnum1 = cond ? .#^TERNARY_5^# : .bogus
756+
func sync(){}
757+
let _: SomeEnum1 = cond ? .bogus : .#^TERNARY_6^#
758+
func sync(){}
759+
let _: SomeEnum1 = .#^TERNARY_CONDITION^# ? .bogus : .bogus
760+
// TERNARY_CONDITION: Begin completions
761+
// TERNARY_CONDITION-DAG: Decl[Constructor]/CurrNominal/TypeRelation[Identical]: init()[#Bool#]; name=init()
762+
// TERNARY_CONDITION: End completions
749763
}

0 commit comments

Comments
 (0)