Skip to content

Commit 5f15bf4

Browse files
authored
Merge pull request #7072 from nathawes/rdar30118572-swift-3.1
[indexer] Fix crash in initVarRefIndexSymbols by handling references from 'import var ...'
2 parents ca89e8b + 51b3e5c commit 5f15bf4

File tree

5 files changed

+42
-13
lines changed

5 files changed

+42
-13
lines changed

lib/AST/SourceEntityWalker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ bool SemaAnnotator::handleImports(ImportDecl *Import) {
365365
auto Decls = Import->getDecls();
366366
if (Decls.size() == 1) {
367367
// FIXME: ImportDecl should store a DeclNameLoc.
368+
// FIXME: Handle overloaded funcs too by passing a reference for each?
368369
if (!passReference(Decls.front(), Type(), DeclNameLoc(Import->getEndLoc()),
369370
SemaReferenceKind::DeclRef))
370371
return false;

lib/Index/Index.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
361361
bool initIndexSymbol(ValueDecl *D, SourceLoc Loc, bool IsRef,
362362
IndexSymbol &Info);
363363
bool initFuncDeclIndexSymbol(ValueDecl *D, IndexSymbol &Info);
364-
bool initCallRefIndexSymbol(Expr *CurrentE, Expr *ParentE, ValueDecl *D,
364+
bool initFuncRefIndexSymbol(Expr *CurrentE, Expr *ParentE, ValueDecl *D,
365365
SourceLoc Loc, IndexSymbol &Info);
366366
bool initVarRefIndexSymbols(Expr *CurrentE, ValueDecl *D, SourceLoc Loc,
367367
IndexSymbol &Info);
@@ -662,15 +662,15 @@ bool IndexSwiftASTWalker::reportPseudoAccessor(AbstractStorageDecl *D,
662662
if (IsRef) {
663663
IndexSymbol Info;
664664

665-
// initCallRefIndexSymbol uses the top of the entities stack as the caller,
665+
// initFuncRefIndexSymbol uses the top of the entities stack as the caller,
666666
// but in this case the top of the stack is the referenced
667667
// AbstractStorageDecl.
668668
assert(getParentDecl() == D);
669669
auto PreviousTop = EntitiesStack.pop_back_val();
670-
bool initCallFailed = initCallRefIndexSymbol(ExprStack.back(), getParentExpr(), D, Loc, Info);
670+
bool initFailed = initFuncRefIndexSymbol(getCurrentExpr(), getParentExpr(), D, Loc, Info);
671671
EntitiesStack.push_back(PreviousTop);
672672

673-
if (initCallFailed)
673+
if (initFailed)
674674
return true; // continue walking.
675675
if (updateInfo(Info))
676676
return true;
@@ -793,7 +793,7 @@ bool IndexSwiftASTWalker::reportRef(ValueDecl *D, SourceLoc Loc,
793793
return true; // keep walking
794794

795795
if (isa<AbstractFunctionDecl>(D)) {
796-
if (initCallRefIndexSymbol(getCurrentExpr(), getParentExpr(), D, Loc, Info))
796+
if (initFuncRefIndexSymbol(getCurrentExpr(), getParentExpr(), D, Loc, Info))
797797
return true;
798798
} else if (isa<AbstractStorageDecl>(D)) {
799799
if (initVarRefIndexSymbols(getCurrentExpr(), D, Loc, Info))
@@ -809,11 +809,10 @@ bool IndexSwiftASTWalker::reportRef(ValueDecl *D, SourceLoc Loc,
809809
}
810810

811811
// Report the accessors that were utilized.
812-
if (isa<AbstractStorageDecl>(D)) {
812+
if (AbstractStorageDecl *ASD = dyn_cast<AbstractStorageDecl>(D)) {
813813
bool UsesGetter = Info.roles & (SymbolRoleSet)SymbolRole::Read;
814814
bool UsesSetter = Info.roles & (SymbolRoleSet)SymbolRole::Write;
815815

816-
AbstractStorageDecl *ASD = cast<AbstractStorageDecl>(D);
817816
if (UsesGetter)
818817
if (!reportPseudoAccessor(ASD, AccessorKind::IsGetter, /*IsRef=*/true,
819818
Loc))
@@ -947,15 +946,23 @@ static bool isDynamicCall(Expr *BaseE, ValueDecl *D) {
947946
return true;
948947
}
949948

950-
bool IndexSwiftASTWalker::initCallRefIndexSymbol(Expr *CurrentE, Expr *ParentE,
949+
bool IndexSwiftASTWalker::initFuncRefIndexSymbol(Expr *CurrentE, Expr *ParentE,
951950
ValueDecl *D, SourceLoc Loc,
952951
IndexSymbol &Info) {
953-
if (!ParentE)
954-
return true;
955952

956953
if (initIndexSymbol(D, Loc, /*IsRef=*/true, Info))
957954
return true;
958955

956+
if (!CurrentE)
957+
return false;
958+
959+
// FIXME: the below check maintains existing indexing behaviour with
960+
// pseudo/accessor output but seems incorrect. E.g otherGlobal in:
961+
// let global = otherGlobal
962+
// will not have a parent expression so no accessor call is reported
963+
if (!ParentE)
964+
return true;
965+
959966
Info.roles |= (unsigned)SymbolRole::Call;
960967

961968
Decl *ParentD = getParentDecl();
@@ -995,10 +1002,13 @@ bool IndexSwiftASTWalker::initCallRefIndexSymbol(Expr *CurrentE, Expr *ParentE,
9951002

9961003
bool IndexSwiftASTWalker::initVarRefIndexSymbols(Expr *CurrentE, ValueDecl *D, SourceLoc Loc, IndexSymbol &Info) {
9971004

998-
if (!(CurrentE->getReferencedDecl() == D))
1005+
if (initIndexSymbol(D, Loc, /*IsRef=*/true, Info))
9991006
return true;
10001007

1001-
if (initIndexSymbol(D, Loc, /*IsRef=*/true, Info))
1008+
if (!CurrentE)
1009+
return false;
1010+
1011+
if (!(CurrentE->getReferencedDecl() == D))
10021012
return true;
10031013

10041014
AccessKind Kind = CurrentE->hasLValueAccessKind() ? CurrentE->getLValueAccessKind() : AccessKind::Read;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public func importedFunc() {}
2+
public var importedGlobal: Int = 0

test/Index/roles.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s | %FileCheck %s
1+
// RUN: rm -rf %t
2+
// RUN: mkdir -p %t
3+
//
4+
// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/imported_swift_module.swift
5+
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s -I %t | %FileCheck %s
6+
7+
import func imported_swift_module.importedFunc
8+
// CHECK: [[@LINE-1]]:35 | function/Swift | importedFunc() | s:F21imported_swift_module12importedFuncFT_T_ | Ref | rel: 0
9+
import var imported_swift_module.importedGlobal
10+
// CHECK: [[@LINE-1]]:34 | variable/Swift | importedGlobal | s:v21imported_swift_module14importedGlobalSi | Ref | rel: 0
211

312
// Definition
413
let x = 2

test/SourceKit/Indexing/index_func_import.swift.response

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
}
2626
],
2727
key.entities: [
28+
{
29+
key.kind: source.lang.swift.ref.function.free,
30+
key.name: "globalFunc()",
31+
key.usr: "s:F11test_module10globalFuncFT_T_",
32+
key.line: 8,
33+
key.column: 25
34+
},
2835
{
2936
key.kind: source.lang.swift.decl.function.free,
3037
key.name: "test()",

0 commit comments

Comments
 (0)