Skip to content

[Index] Add the dynamic role for calls to class methods #36484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions lib/Index/Index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1348,22 +1348,34 @@ static bool isSuperRefExpr(Expr *E) {
}

static bool isDynamicCall(Expr *BaseE, ValueDecl *D) {
// The call is 'dynamic' if the method is not of a struct/enum and the
// receiver is not 'super'. Note that if the receiver is 'super' that
// does not mean that the call is statically determined (an extension
// method may have injected itself in the super hierarchy).
// For our purposes 'dynamic' means that the method call cannot invoke
// a method in a subclass.
// "dynamic" here is not Swift's "dynamic" modifier, but rathar "can call a
// function in a conformance/subclass". Eg. if the receiver is "super", an
// extension may have injected itself in the parent hierarchy - this call
// would not be "dynamic" for our purposes (though would be Swift's
// "dynamic"), but it is also not statically determined.
auto TyD = getNominalParent(D);
if (!TyD)
return false;
if (isa<StructDecl>(TyD) || isa<EnumDecl>(TyD))
return false;
if (isSuperRefExpr(BaseE))
return false;
if (BaseE->getType()->is<MetatypeType>())
// `SomeType.staticOrClassMethod()`
if (isa<TypeExpr>(BaseE))
return false;

// `type(of: foo).staticOrClassMethod()`, not "dynamic" if the instance type
// is a struct/enum or if it is a class and the function is a static method
// (rather than a class method).
if (auto IT = BaseE->getType()->getAs<MetatypeType>()) {
auto InstanceType = IT->getInstanceType();
if (InstanceType->getStructOrBoundGenericStruct() ||
InstanceType->getEnumOrBoundGenericEnum())
return false;
if (InstanceType->getClassOrBoundGenericClass() && D->isFinal())
return false;
}

return true;
}

Expand Down Expand Up @@ -1431,17 +1443,22 @@ bool IndexSwiftASTWalker::initFuncRefIndexSymbol(ValueDecl *D, SourceLoc Loc,
if (!BaseE || BaseE == CurrentE)
return false;

if (isDynamicCall(BaseE, D))
Info.roles |= (unsigned)SymbolRole::Dynamic;

if (Type ReceiverTy = BaseE->getType()) {
if (auto LVT = ReceiverTy->getAs<LValueType>())
ReceiverTy = LVT->getObjectType();
else if (auto MetaT = ReceiverTy->getAs<MetatypeType>())
ReceiverTy = MetaT->getInstanceType();

// TODO: Handle generics and composed protocols
if (auto OpenedTy = ReceiverTy->getAs<OpenedArchetypeType>())
ReceiverTy = OpenedTy->getOpenedExistentialType();

if (auto TyD = ReceiverTy->getAnyNominal()) {
if (addRelation(Info, (SymbolRoleSet) SymbolRole::RelationReceivedBy, TyD))
return true;
if (isDynamicCall(BaseE, D))
Info.roles |= (unsigned)SymbolRole::Dynamic;
}
}

Expand Down
101 changes: 101 additions & 0 deletions test/Index/index_static_funcs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// RUN: %empty-directory(%t)
//
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s | %FileCheck %s

protocol SwiftProto {
// CHECK: [[@LINE-1]]:10 | protocol/Swift | SwiftProto | [[Proto_USR:.*]] | Def | rel: 0
static func staticMethod()
// CHECK: [[@LINE-1]]:15 | static-method/Swift | staticMethod() | [[ProtoStaticMethod_USR:.*]] | Def,RelChild | rel: 1
}

protocol SwiftProtoSame {
// CHECK: [[@LINE-1]]:10 | protocol/Swift | SwiftProtoSame | [[ProtoSame_USR:.*]] | Def | rel: 0
static func staticMethod()
// CHECK: [[@LINE-1]]:15 | static-method/Swift | staticMethod() | [[ProtoSameStaticMethod_USR:.*]] | Def,RelChild | rel: 1
}

protocol SwiftProtoOther {}

protocol SwiftProtoComposed: SwiftProtoSame, SwiftProto, SwiftProtoOther {}

class SwiftClass: SwiftProtoComposed {
// CHECK: [[@LINE-1]]:7 | class/Swift | SwiftClass | [[Class_USR:.*]] | Def | rel: 0
static func staticMethod() {}
// CHECK: [[@LINE-1]]:15 | static-method/Swift | staticMethod() | [[ClassStaticMethod_USR:.*]] | Def,RelChild,RelOver | rel: 3
class func classMethod() {}
// CHECK: [[@LINE-1]]:14 | class-method/Swift | classMethod() | [[ClassClassMethod_USR:.*]] | Def,Dyn,RelChild | rel: 1
}

struct SwiftStruct: SwiftProtoComposed {
// CHECK: [[@LINE-1]]:8 | struct/Swift | SwiftStruct | [[Struct_USR:.*]] | Def | rel: 0
static func staticMethod() {}
// CHECK: [[@LINE-1]]:15 | static-method/Swift | staticMethod() | [[StructStaticMethod_USR:.*]] | Def,RelChild,RelOver | rel: 3
}

enum SwiftEnum: SwiftProtoComposed {
// CHECK: [[@LINE-1]]:6 | enum/Swift | SwiftEnum | [[Enum_USR:.*]] | Def | rel: 0
static func staticMethod() {}
// CHECK: [[@LINE-1]]:15 | static-method/Swift | staticMethod() | [[EnumStaticMethod_USR:.*]] | Def,RelChild,RelOver | rel: 3
}

func directCalls() {
SwiftClass.staticMethod()
// CHECK: [[@LINE-1]]:14 | static-method/Swift | staticMethod() | [[ClassStaticMethod_USR]] | Ref,Call,RelRec,RelCall,RelCont | rel: 2
// CHECK-DAG: RelRec | class/Swift | SwiftClass | [[Class_USR]]
SwiftClass.classMethod()
// CHECK: [[@LINE-1]]:14 | class-method/Swift | classMethod() | [[ClassClassMethod_USR]] | Ref,Call,RelRec,RelCall,RelCont | rel: 2
// CHECK-DAG: RelRec | class/Swift | SwiftClass | [[Class_USR]]

SwiftStruct.staticMethod()
// CHECK: [[@LINE-1]]:15 | static-method/Swift | staticMethod() | [[StructStaticMethod_USR]] | Ref,Call,RelRec,RelCall,RelCont | rel: 2
// CHECK-DAG: RelRec | struct/Swift | SwiftStruct | [[Struct_USR]]

SwiftEnum.staticMethod()
// CHECK: [[@LINE-1]]:13 | static-method/Swift | staticMethod() | [[EnumStaticMethod_USR]] | Ref,Call,RelRec,RelCall,RelCont | rel: 2
// CHECK-DAG: RelRec | enum/Swift | SwiftEnum | [[Enum_USR]]
}

func typeofClass(c: SwiftClass) {
type(of: c).staticMethod()
// CHECK: [[@LINE-1]]:15 | static-method/Swift | staticMethod() | [[ClassStaticMethod_USR]] | Ref,Call,RelRec,RelCall,RelCont | rel: 2
// CHECK: RelRec | class/Swift | SwiftClass | [[Class_USR]]
type(of: c).classMethod()
// CHECK: [[@LINE-1]]:15 | class-method/Swift | classMethod() | [[ClassClassMethod_USR]] | Ref,Call,Dyn,RelRec,RelCall,RelCont | rel: 2
// CHECK: RelRec | class/Swift | SwiftClass | [[Class_USR]]
}

func typeofStruct(s: SwiftStruct) {
type(of: s).staticMethod()
// CHECK: [[@LINE-1]]:15 | static-method/Swift | staticMethod() | [[StructStaticMethod_USR]] | Ref,Call,RelRec,RelCall,RelCont | rel: 2
// CHECK: RelRec | struct/Swift | SwiftStruct | [[Struct_USR]]
}

func typeofEnum(e: SwiftEnum) {
type(of: e).staticMethod()
// CHECK: [[@LINE-1]]:15 | static-method/Swift | staticMethod() | [[EnumStaticMethod_USR]] | Ref,Call,RelRec,RelCall,RelCont | rel: 2
// CHECK: RelRec | enum/Swift | SwiftEnum | [[Enum_USR]]
}

func typeofProtocol(proto: SwiftProto) {
type(of: proto).staticMethod()
// CHECK: [[@LINE-1]]:19 | static-method/Swift | staticMethod() | [[ProtoStaticMethod_USR]] | Ref,Call,Dyn,RelRec,RelCall,RelCont | rel: 2
// CHECK: RelRec | protocol/Swift | SwiftProto | [[Proto_USR]]
}

// FIXME: Add the ReceivedBy relation for generics
func genericSingle<T>(proto: T) where T: SwiftProto {
type(of: proto).staticMethod()
// CHECK: [[@LINE-1]]:19 | static-method/Swift | staticMethod() | [[ProtoStaticMethod_USR]] | Ref,Call,Dyn,RelCall,RelCont | rel: 1
}

// FIXME: The composed cases currently picks one of the USRs, should we output
// multiple occurrences?
func genericComposedType<T>(proto: T) where T: SwiftProtoComposed {
type(of: proto).staticMethod()
// CHECK: [[@LINE-1]]:19 | static-method/Swift | staticMethod() | [[ProtoStaticMethod_USR]] | Ref,Call,Dyn,RelCall,RelCont | rel: 1
}

func genericComposedWhere<T>(proto: T) where T: SwiftProto & SwiftProtoSame & SwiftProtoOther {
type(of: proto).staticMethod()
// CHECK: [[@LINE-1]]:19 | static-method/Swift | staticMethod() | [[ProtoSameStaticMethod_USR]] | Ref,Call,Dyn,RelCall,RelCont | rel: 1
}