Skip to content

Commit 6c61e60

Browse files
committed
[CodeCompletion] Skip typechecking preceding top level decls
1 parent 0a72d02 commit 6c61e60

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

lib/IDE/ExprContextAnalysis.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,22 +104,7 @@ void swift::ide::typeCheckContextUntil(DeclContext *DC, SourceLoc Loc) {
104104
DC = DC->getParent();
105105

106106
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(DC)) {
107-
// Typecheck all 'TopLevelCodeDecl's up to the target one.
108-
// In theory, this is not needed, but it fails to resolve the type of
109-
// 'guard'ed variable. e.g.
110-
//
111-
// guard value = something() else { fatalError() }
112-
// <complete>
113-
// Here, 'value' is '<error type>' unless we explicitly typecheck the
114-
// 'guard' statement.
115-
SourceFile *SF = DC->getParentSourceFile();
116-
for (auto *D : SF->getTopLevelDecls()) {
117-
if (auto Code = dyn_cast<TopLevelCodeDecl>(D)) {
118-
typeCheckTopLevelCodeDecl(Code);
119-
if (Code == TLCD)
120-
break;
121-
}
122-
}
107+
typeCheckTopLevelCodeDecl(TLCD);
123108
} else {
124109
typeCheckContextImpl(DC, Loc);
125110
}

test/IDE/complete_skipbody.swift

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
// RUN: %swift-ide-test -code-completion -source-filename %s -code-completion-token COMPLETE -debug-forbid-typecheck-prefix FORBIDDEN | %FileCheck %s
1+
// RUN: %swift-ide-test -code-completion -source-filename %s -code-completion-token FUNCTIONBODY -debug-forbid-typecheck-prefix FORBIDDEN | %FileCheck %s
2+
// RUN: %swift-ide-test -code-completion -source-filename %s -code-completion-token TOPLEVEL -debug-forbid-typecheck-prefix FORBIDDEN | %FileCheck %s
3+
24
struct FORBIDDEN_Struct {
35
func FORBIDDEN_method() -> Int? { 1 }
46
}
@@ -8,6 +10,13 @@ struct MyStruct {
810
var y: Int { 1 }
911
}
1012

13+
let globalUnrelated = FORBIDDEN_Struct();
14+
15+
guard let globalValueOpt = MyStruct() as MyStruct?? else {
16+
let localUnrelated = FORBIDDEN_Struct();
17+
fatalError()
18+
}
19+
1120
func test(valueOptOpt: MyStruct??) {
1221

1322
let FORBIDDEN_localVar = 1
@@ -20,15 +29,27 @@ func test(valueOptOpt: MyStruct??) {
2029
}
2130

2231
guard let value = valueOpt else {
32+
let FORBIDDEN_localVar2 = 1
2333
return
2434
}
2535

2636
if (value.x == 1) {
2737
let unrelated2 = FORBIDDEN_Struct()
28-
_ = value.#^COMPLETE^#
38+
_ = value.#^FUNCTIONBODY^#
2939
}
3040
}
3141

42+
let globalValue = globalValueOpt!
43+
44+
let FORBIDDEN_localVar = 1
45+
46+
switch glovalValue.x {
47+
case let x where x < 2:
48+
if x == globalValue.#^TOPLEVEL^# {}
49+
default:
50+
break
51+
}
52+
3253
// CHECK: Begin completions, 3 items
3354
// CHECK-DAG: Keyword[self]/CurrNominal: self[#MyStruct#]; name=self
3455
// CHECK-DAG: Decl[InstanceVar]/CurrNominal: x[#Int#]; name=x

0 commit comments

Comments
 (0)