Skip to content

Commit fec7a0b

Browse files
committed
[IDE] Skip walking serialized internal top level decls in SemaAnnotator
`SemaAnnotator` always attempts to retrieve the location of the decl. This requires generating the USR, which needs to resolve the type. But that's invalid in the presence of `@_implementationOnly`. This most commonly comes up during index while building as even though it skip internal decls, by that point it's too late (`SemaAnnotator` has already tried retrieving the location). Other uses of `SourceEntityWalker` should be unaffected as they only run over the current decls in the current module. For now skip walking internal top level decls from serialized modules in `SemaAnnotator`. Consider expanding this to most, if not all, `ASTWalker` clients in the future - it's unlikely `SemaAnnotator` is the only one with this problem. Resolves rdar://91279771.
1 parent 4ebf04a commit fec7a0b

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

include/swift/AST/ASTWalker.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ class ASTWalker {
235235
/// until eventually we can remove this altogether.
236236
virtual bool shouldWalkAccessorsTheOldWay() { return false; }
237237

238+
/// Whether to walk internal top level decls in serialized modules.
239+
///
240+
/// TODO: Consider changing this to false by default.
241+
virtual bool shouldWalkSerializedTopLevelInternalDecls() { return true; }
242+
238243
/// walkToParameterListPre - This method is called when first visiting a
239244
/// ParameterList, before walking into its parameters. If it returns false,
240245
/// the subtree is skipped.

lib/AST/Module.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2916,7 +2916,17 @@ bool FileUnit::walk(ASTWalker &walker) {
29162916
getTopLevelDecls(Decls);
29172917
llvm::SaveAndRestore<ASTWalker::ParentTy> SAR(walker.Parent,
29182918
getParentModule());
2919+
2920+
bool SkipInternal = getKind() == FileUnitKind::SerializedAST &&
2921+
!walker.shouldWalkSerializedTopLevelInternalDecls();
29192922
for (Decl *D : Decls) {
2923+
if (SkipInternal) {
2924+
if (auto *VD = dyn_cast<ValueDecl>(D)) {
2925+
if (!VD->isAccessibleFrom(nullptr))
2926+
continue;
2927+
}
2928+
}
2929+
29202930
#ifndef NDEBUG
29212931
PrettyStackTraceDecl debugStack("walking into decl", D);
29222932
#endif

lib/IDE/SourceEntityWalker.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class SemaAnnotator : public ASTWalker {
5454
bool shouldWalkIntoGenericParams() override {
5555
return SEWalker.shouldWalkIntoGenericParams();
5656
}
57+
58+
bool shouldWalkSerializedTopLevelInternalDecls() override {
59+
return false;
60+
}
61+
5762
bool walkToDeclPre(Decl *D) override;
5863
bool walkToDeclPreProper(Decl *D);
5964
std::pair<bool, Expr *> walkToExprPre(Expr *E) override;

0 commit comments

Comments
 (0)