@@ -63,9 +63,10 @@ swift::ide::makeCodeCompletionMemoryBuffer(const llvm::MemoryBuffer *origBuf,
6363}
6464
6565namespace {
66- // / Returns index number of \p D in \p Decls . If it's not found, returns ~0.
66+ // / Returns index number of \p D in \p Decls . If it's not found, returns
67+ // / \c nullopt.
6768template <typename Range>
68- unsigned findIndexInRange (Decl *D, const Range &Decls) {
69+ std::optional< unsigned > findIndexInRange (Decl *D, const Range &Decls) {
6970 unsigned N = 0 ;
7071 for (auto I = Decls.begin (), E = Decls.end (); I != E; ++I) {
7172 if ((*I)->isImplicit ())
@@ -74,7 +75,7 @@ unsigned findIndexInRange(Decl *D, const Range &Decls) {
7475 return N;
7576 ++N;
7677 }
77- return ~ 0U ;
78+ return std:: nullopt ;
7879}
7980
8081// / Return the element at \p N in \p Decls .
@@ -106,7 +107,7 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
106107 if (!D)
107108 return nullptr ;
108109 auto *parentDC = newDC->getParent ();
109- unsigned N = ~ 0U ;
110+ std::optional< unsigned > N ;
110111
111112 if (auto accessor = dyn_cast<AccessorDecl>(D)) {
112113 // The AST for accessors is like:
@@ -117,7 +118,9 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
117118 if (!storage)
118119 return nullptr ;
119120 auto accessorN = findIndexInRange (accessor, storage->getAllAccessors ());
120- IndexStack.push_back (accessorN);
121+ if (!accessorN)
122+ return nullptr ;
123+ IndexStack.push_back (*accessorN);
121124 D = storage;
122125 }
123126
@@ -134,11 +137,10 @@ static DeclContext *getEquivalentDeclContextFromSourceFile(DeclContext *DC,
134137 }
135138
136139 // Not found in the decl context tree.
137- if (N == ~ 0U ) {
140+ if (!N)
138141 return nullptr ;
139- }
140142
141- IndexStack.push_back (N);
143+ IndexStack.push_back (* N);
142144 newDC = parentDC;
143145 } while (!newDC->isModuleScopeContext ());
144146
0 commit comments