Skip to content

Commit 4cc1db0

Browse files
authored
Merge pull request #31567 from rintaro/5.3-ide-completion-crash-rdar62679517
[5.3][CodeCompletion] Make getEquivalentDeclContextFromSourceFile safer
2 parents 7cac40e + 5c2be9d commit 4cc1db0

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

lib/IDE/CompletionInstance.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
100100
if (!D)
101101
return nullptr;
102102
auto *parentDC = newDC->getParent();
103-
unsigned N;
103+
unsigned N = ~0U;
104104

105105
if (auto accessor = dyn_cast<AccessorDecl>(D)) {
106106
// The AST for accessors is like:
@@ -140,22 +140,22 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
140140
newDC = SF;
141141
do {
142142
auto N = IndexStack.pop_back_val();
143-
Decl *D;
143+
Decl *D = nullptr;
144144
if (auto parentSF = dyn_cast<SourceFile>(newDC))
145145
D = getElementAt(parentSF->getTopLevelDecls(), N);
146146
else if (auto parentIDC = dyn_cast<IterableDeclContext>(newDC->getAsDecl()))
147147
D = getElementAt(parentIDC->getMembers(), N);
148148
else
149149
llvm_unreachable("invalid DC kind for finding equivalent DC (query)");
150150

151-
if (auto storage = dyn_cast<AbstractStorageDecl>(D)) {
151+
if (auto storage = dyn_cast_or_null<AbstractStorageDecl>(D)) {
152152
if (IndexStack.empty())
153153
return nullptr;
154154
auto accessorN = IndexStack.pop_back_val();
155155
D = getElementAt(storage->getAllAccessors(), accessorN);
156156
}
157157

158-
newDC = dyn_cast<DeclContext>(D);
158+
newDC = dyn_cast_or_null<DeclContext>(D);
159159
if (!newDC)
160160
return nullptr;
161161
} while (!IndexStack.empty());
@@ -354,7 +354,7 @@ bool CompletionInstance::performCachedOperationIfPossible(
354354

355355
DeclContext *DC =
356356
getEquivalentDeclContextFromSourceFile(newInfo.ParentContext, oldSF);
357-
if (!DC)
357+
if (!DC || !isa<AbstractFunctionDecl>(DC))
358358
return false;
359359

360360
// OK, we can perform fast completion for this. Update the orignal delayed

0 commit comments

Comments
 (0)