Skip to content

Commit a1e8887

Browse files
committed
Enable indexing for refs to synthesized declarations.
Based on feedback in PR #69460, enabling indexing for synthesized decls because they are usable by users and make sense to appear in the indexstore. Sets `synthesized` on some additional decls: - derived `hashInto(...)` - Objc properties and methods derived from Objc protocols #67446
1 parent f7911e6 commit a1e8887

12 files changed

+108
-35
lines changed

lib/ClangImporter/ImportDecl.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -9151,6 +9151,9 @@ ClangImporter::Implementation::importMirroredDecl(const clang::NamedDecl *decl,
91519151

91529152
auto updateMirroredDecl = [&](Decl *result) {
91539153
result->setImplicit();
9154+
if (auto VD = dyn_cast<ValueDecl>(result)) {
9155+
VD->setSynthesized();
9156+
}
91549157

91559158
// Map the Clang attributes onto Swift attributes.
91569159
importAttributes(decl, result);

lib/Index/Index.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,8 @@ class IndexSwiftASTWalker : public SourceEntityWalker {
10851085
D = cast<VarDecl>(D)->getCanonicalVarDecl();
10861086
}
10871087

1088-
if (D->isImplicit() && !shouldIndexImplicitDecl(D, IsRef))
1088+
if (!D->isSynthesized() && D->isImplicit() &&
1089+
!shouldIndexImplicitDecl(D, IsRef))
10891090
return false;
10901091

10911092
// Do not handle non-public imported decls.

lib/Sema/DerivedConformanceComparable.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ deriveComparable_lt(
251251
/*ThrownType=*/Type(),
252252
/*GenericParams=*/nullptr, params, boolTy, parentDC);
253253
comparableDecl->setUserAccessible(false);
254+
comparableDecl->setSynthesized();
254255

255256
// Add the @_implements(Comparable, < (_:_:)) attribute
256257
if (generatedIdentifier != C.Id_LessThanOperator) {

lib/Sema/DerivedConformanceEquatableHashable.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ deriveEquatable_eq(
410410
/*Throws=*/false, /*ThrownType=*/Type(),
411411
/*GenericParams=*/nullptr, params, boolTy, parentDC);
412412
eqDecl->setUserAccessible(false);
413+
eqDecl->setSynthesized();
413414

414415
// Add the @_implements(Equatable, ==(_:_:)) attribute
415416
if (generatedIdentifier != C.Id_EqualsOperator) {
@@ -546,6 +547,7 @@ deriveHashable_hashInto(
546547
/*Async=*/false,
547548
/*Throws=*/false, /*ThrownType=*/Type(),
548549
/*GenericParams=*/nullptr, params, returnType, parentDC);
550+
hashDecl->setSynthesized();
549551
hashDecl->setBodySynthesizer(bodySynthesizer);
550552
hashDecl->copyFormalAccessFrom(derived.Nominal,
551553
/*sourceIsParentContext=*/true);

test/AutoDiff/Sema/DerivedConformances/derived_differentiable.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ struct GenericTangentVectorMember<T: Differentiable>: Differentiable,
1313
// CHECK-AST: internal var x: T.TangentVector
1414
// CHECK-AST: internal static func + (lhs: GenericTangentVectorMember<T>, rhs: GenericTangentVectorMember<T>) -> GenericTangentVectorMember<T>
1515
// CHECK-AST: internal static func - (lhs: GenericTangentVectorMember<T>, rhs: GenericTangentVectorMember<T>) -> GenericTangentVectorMember<T>
16-
// CHECK-AST: @_implements(Equatable, ==(_:_:)) internal static func __derived_struct_equals(_ a: GenericTangentVectorMember<T>, _ b: GenericTangentVectorMember<T>) -> Bool
1716
// CHECK-AST: internal typealias TangentVector = GenericTangentVectorMember<T>
17+
// CHECK-AST: @_implements(Equatable, ==(_:_:)) internal static func __derived_struct_equals(_ a: GenericTangentVectorMember<T>, _ b: GenericTangentVectorMember<T>) -> Bool
1818
// CHECK-AST: internal init(x: T.TangentVector)
1919
// CHECK-AST: internal static var zero: GenericTangentVectorMember<T> { get }
2020

test/Index/index_imported_objc.swift

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// REQUIRES: objc_interop
2+
3+
// RUN: %empty-directory(%t)
4+
// RUN: %empty-directory(%t/mods)
5+
// RUN: split-file %s %t
6+
7+
// RUN: %target-swift-ide-test -print-indexed-symbols -enable-objc-interop -source-filename %t/ObjcUser.swift -Xcc -fmodule-map-file=%t/module.modulemap | %FileCheck -dump-input=always %t/ObjcUser.swift
8+
9+
//--- objc_decls.h
10+
#import <Foundation/Foundation.h>
11+
@protocol MemberAdding<NSObject>
12+
@property int protocolAddedProperty;
13+
-(void)protocolAddedMethod;
14+
@end
15+
16+
@interface ObjCClass : NSObject
17+
@property int baseClassProperty;
18+
@end
19+
20+
@interface ObjCClass (category) <MemberAdding>
21+
@property int categoryAddedProperty;
22+
@end
23+
24+
//--- module.modulemap
25+
module objc_decls {
26+
header "objc_decls.h"
27+
export *
28+
}
29+
30+
//--- ObjcUser.swift
31+
import objc_decls
32+
func test() { // CHECK: [[@LINE]]:6 | function/Swift | test() | [[s:.*]] | Def | rel: 0
33+
let c = ObjCClass()
34+
let _ = c.baseClassProperty
35+
// CHECK: [[@LINE-1]]:13 | instance-property/Swift | baseClassProperty | c:objc(cs)ObjCClass(py)baseClassProperty | Ref,Read,Dyn,RelRec,RelCont | rel: 2
36+
let _ = c.categoryAddedProperty
37+
// CHECK: [[@LINE-1]]:13 | instance-property/Swift | categoryAddedProperty | c:objc(cs)ObjCClass(py)categoryAddedProperty | Ref,Read,Dyn,RelRec,RelCont | rel: 2
38+
// CHECK: [[@LINE-2]]:13 | instance-method/acc-get/Swift | getter:categoryAddedProperty | c:@CM@objc_decls@@objc(cs)ObjCClass(im)categoryAddedProperty | Ref,Call,Dyn,Impl,RelRec,RelCall,RelCont | rel: 2
39+
let _ = c.protocolAddedProperty
40+
// CHECK: [[@LINE-1]]:13 | instance-property/Swift | protocolAddedProperty | c:objc(pl)MemberAdding(py)protocolAddedProperty | Ref,Read,Dyn,RelRec,RelCont | rel: 2
41+
// CHECK: [[@LINE-2]]:13 | instance-method/acc-get/Swift | getter:protocolAddedProperty | c:@CM@objc_decls@@objc(cs)ObjCClass(im)protocolAddedProperty | Ref,Call,Dyn,Impl,RelRec,RelCall,RelCont | rel: 2
42+
c.protocolAddedMethod()
43+
// CHECK: [[@LINE-1]]:5 | instance-method/Swift | protocolAddedMethod() | c:objc(pl)MemberAdding(im)protocolAddedMethod | Ref,Call,Dyn,RelRec,RelCall,RelCont | rel: 2
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %empty-directory(%t)
2+
//
3+
// RUN: %target-swift-ide-test -print-indexed-symbols -source-filename %s | %FileCheck -check-prefix=CHECK %s
4+
5+
public struct CustomFoo: Equatable, Hashable {
6+
public let a: Int
7+
public let b: String
8+
}
9+
func testFoo() {
10+
var hasher = Hasher()
11+
let f = CustomFoo(a: 0, b: "b")
12+
// CHECK: [[@LINE+1]]:7 | instance-method/Swift | hash(into:) | s:14swift_ide_test9CustomFooV4hash4intoys6HasherVz_tF | {{.*}}Ref
13+
f.hash(into: &hasher)
14+
hasher.finalize()
15+
// CHECK: [[@LINE+1]]:11 | static-method/Swift | __derived_struct_equals(_:_:) | s:14swift_ide_test9CustomFooV23__derived_struct_equalsySbAC_ACtFZ | {{.*}}Ref
16+
_ = f == CustomFoo(a: 0, b: "b")
17+
}

test/Index/roles-contained.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,11 @@ struct SomeStruct {
319319
// CHECK-NEXT: RelCont | static-property/Swift | staticProperty | {{.*}}
320320

321321
lazy var lazyProperty: Int = { 1 }()
322-
// CHECK: [[@LINE-1]]:28 | struct/Swift | Int | {{.*}} | Ref,RelCont | rel: 1
322+
// CHECK: [[@LINE-1]]:14 | instance-method/acc-get/Swift | getter:lazyProperty | s:14swift_ide_test10SomeStructV12lazyPropertySivg | Def,Impl,RelChild,RelAcc | rel: 1
323+
// CHECK-NEXT: RelChild,RelAcc | instance-property/Swift | lazyProperty | s:14swift_ide_test10SomeStructV12lazyPropertySivp
324+
// CHECK: [[@LINE-3]]:14 | instance-method/acc-set/Swift | setter:lazyProperty | s:14swift_ide_test10SomeStructV12lazyPropertySivs | Def,Impl,RelChild,RelAcc | rel: 1
325+
// CHECK-NEXT: RelChild,RelAcc | instance-property/Swift | lazyProperty | s:14swift_ide_test10SomeStructV12lazyPropertySivp
326+
// CHECK: [[@LINE-5]]:28 | struct/Swift | Int | {{.*}} | Ref,RelCont | rel: 1
323327
// CHECK-NEXT: RelCont | instance-property/Swift | lazyProperty | {{.*}}
324328

325329
@Wrapped

test/ModuleInterface/derived_conformances_nonisolated.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
let y: String
1212

1313
// CHECK: nonisolated public static func == (a: Library.X1, b: Library.X1) -> Swift.Bool
14-
// CHECK: nonisolated public func hash(into hasher: inout Swift.Hasher)
1514
// CHECK: nonisolated public func encode(to encoder: any Swift.Encoder) throws
15+
// CHECK: nonisolated public func hash(into hasher: inout Swift.Hasher)
1616
// CHECK: nonisolated public var hashValue: Swift.Int
1717
// CHECK: nonisolated public init(from decoder: any Swift.Decoder) throws
1818
}

test/SILGen/synthesized_conformance_class.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ final class Final<T> {
1010
// CHECK: init(x: T)
1111
// CHECK: enum CodingKeys : CodingKey {
1212
// CHECK: case x
13-
// CHECK: @_implements(Equatable, ==(_:_:)) static func __derived_enum_equals(_ a: Final<T>.CodingKeys, _ b: Final<T>.CodingKeys) -> Bool
14-
// CHECK: func hash(into hasher: inout Hasher)
1513
// CHECK: init?(stringValue: String)
1614
// CHECK: init?(intValue: Int)
15+
// CHECK: @_implements(Equatable, ==(_:_:)) static func __derived_enum_equals(_ a: Final<T>.CodingKeys, _ b: Final<T>.CodingKeys) -> Bool
16+
// CHECK: func hash(into hasher: inout Hasher)
1717
// CHECK: var hashValue: Int { get }
1818
// CHECK: var intValue: Int? { get }
1919
// CHECK: var stringValue: String { get }
@@ -30,10 +30,10 @@ class Nonfinal<T> {
3030
// CHECK: init(x: T)
3131
// CHECK: enum CodingKeys : CodingKey {
3232
// CHECK: case x
33-
// CHECK: @_implements(Equatable, ==(_:_:)) static func __derived_enum_equals(_ a: Nonfinal<T>.CodingKeys, _ b: Nonfinal<T>.CodingKeys) -> Bool
34-
// CHECK: func hash(into hasher: inout Hasher)
3533
// CHECK: init?(stringValue: String)
3634
// CHECK: init?(intValue: Int)
35+
// CHECK: @_implements(Equatable, ==(_:_:)) static func __derived_enum_equals(_ a: Nonfinal<T>.CodingKeys, _ b: Nonfinal<T>.CodingKeys) -> Bool
36+
// CHECK: func hash(into hasher: inout Hasher)
3737
// CHECK: var hashValue: Int { get }
3838
// CHECK: var intValue: Int? { get }
3939
// CHECK: var stringValue: String { get }
@@ -54,10 +54,10 @@ class Nonfinal<T> {
5454

5555
// Make sure that CodingKeys members are actually emitted.
5656

57-
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}21__derived_enum_equalsySbAFyx_G_AHtFZ : $@convention(method) <T> (Final<T>.CodingKeys, Final<T>.CodingKeys, @thin Final<T>.CodingKeys.Type) -> Bool {
58-
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}4hash4intoys6HasherVz_tF : $@convention(method) <T> (@inout Hasher, Final<T>.CodingKeys) -> () {
5957
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}11stringValueAFyx_GSgSS_tcfC : $@convention(method) <T> (@owned String, @thin Final<T>.CodingKeys.Type) -> Optional<Final<T>.CodingKeys> {
6058
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}8intValueAFyx_GSgSi_tcfC : $@convention(method) <T> (Int, @thin Final<T>.CodingKeys.Type) -> Optional<Final<T>.CodingKeys> {
59+
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}21__derived_enum_equalsySbAFyx_G_AHtFZ : $@convention(method) <T> (Final<T>.CodingKeys, Final<T>.CodingKeys, @thin Final<T>.CodingKeys.Type) -> Bool {
60+
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}4hash4intoys6HasherVz_tF : $@convention(method) <T> (@inout Hasher, Final<T>.CodingKeys) -> () {
6161
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}9hashValueSivg : $@convention(method) <T> (Final<T>.CodingKeys) -> Int {
6262
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}8intValueSiSgvg : $@convention(method) <T> (Final<T>.CodingKeys) -> Optional<Int> {
6363
// CHECK-LABEL: sil private [ossa] @$s29synthesized_conformance_class5FinalC10CodingKeys{{.*}}11stringValueSSvg : $@convention(method) <T> (Final<T>.CodingKeys) -> @owned String {

test/SILGen/synthesized_conformance_struct.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ struct Struct<T> {
99
// CHECK: @_hasStorage var x: T { get set }
1010
// CHECK: enum CodingKeys : CodingKey {
1111
// CHECK: case x
12+
// CHECK: init?(stringValue: String)
13+
// CHECK: init?(intValue: Int)
1214
// CHECK-FRAGILE: @_implements(Equatable, ==(_:_:)) static func __derived_enum_equals(_ a: Struct<T>.CodingKeys, _ b: Struct<T>.CodingKeys) -> Bool
1315
// CHECK-RESILIENT: static func == (a: Struct<T>.CodingKeys, b: Struct<T>.CodingKeys) -> Bool
1416
// CHECK: func hash(into hasher: inout Hasher)
15-
// CHECK: init?(stringValue: String)
16-
// CHECK: init?(intValue: Int)
1717
// CHECK: var hashValue: Int { get }
1818
// CHECK: var intValue: Int? { get }
1919
// CHECK: var stringValue: String { get }

test/decl/enum/derived_hashable_equatable.swift

+24-23
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,6 @@ enum HasAssociatedValues: Hashable {
5252
// CHECK: case c
5353
case c
5454

55-
// CHECK: internal func hash(into hasher: inout Hasher) {
56-
// CHECK-NEXT: switch self {
57-
// CHECK-NEXT: case .a(let a0):
58-
// CHECK-NEXT: hasher.combine(0)
59-
// CHECK-NEXT: hasher.combine(a0)
60-
// CHECK-NEXT: case .b(let a0):
61-
// CHECK-NEXT: hasher.combine(1)
62-
// CHECK-NEXT: hasher.combine(a0)
63-
// CHECK-NEXT: case .c:
64-
// CHECK-NEXT: hasher.combine(2)
65-
// CHECK-NEXT: }
66-
// CHECK-NEXT: }
67-
6855
// CHECK: @_implements(Equatable, ==(_:_:)) internal static func __derived_enum_equals(_ a: HasAssociatedValues, _ b: HasAssociatedValues) -> Bool {
6956
// CHECK-NEXT: switch (a, b) {
7057
// CHECK-NEXT: case (.a(let l0), .a(let r0)):
@@ -84,6 +71,19 @@ enum HasAssociatedValues: Hashable {
8471
// CHECK-NEXT: }
8572
// CHECK-NEXT: }
8673

74+
// CHECK: internal func hash(into hasher: inout Hasher) {
75+
// CHECK-NEXT: switch self {
76+
// CHECK-NEXT: case .a(let a0):
77+
// CHECK-NEXT: hasher.combine(0)
78+
// CHECK-NEXT: hasher.combine(a0)
79+
// CHECK-NEXT: case .b(let a0):
80+
// CHECK-NEXT: hasher.combine(1)
81+
// CHECK-NEXT: hasher.combine(a0)
82+
// CHECK-NEXT: case .c:
83+
// CHECK-NEXT: hasher.combine(2)
84+
// CHECK-NEXT: }
85+
// CHECK-NEXT: }
86+
8787
// CHECK: internal var hashValue: Int {
8888
// CHECK-NEXT: get {
8989
// CHECK-NEXT: return _hashValue(for: self)
@@ -145,16 +145,6 @@ enum HasAssociatedValuesAndUnavailableElement: Hashable {
145145
@available(*, unavailable)
146146
case b(String)
147147

148-
// CHECK: internal func hash(into hasher: inout Hasher) {
149-
// CHECK-NEXT: switch self {
150-
// CHECK-NEXT: case .a(let a0):
151-
// CHECK-NEXT: hasher.combine(0)
152-
// CHECK-NEXT: hasher.combine(a0)
153-
// CHECK-NEXT: case .b:
154-
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}()
155-
// CHECK-NEXT: }
156-
// CHECK-NEXT: }
157-
158148
// CHECK: @_implements(Equatable, ==(_:_:)) internal static func __derived_enum_equals(_ a: HasAssociatedValuesAndUnavailableElement, _ b: HasAssociatedValuesAndUnavailableElement) -> Bool {
159149
// CHECK-NEXT: switch (a, b) {
160150
// CHECK-NEXT: case (.a(let l0), .a(let r0)):
@@ -169,6 +159,17 @@ enum HasAssociatedValuesAndUnavailableElement: Hashable {
169159
// CHECK-NEXT: }
170160
// CHECK-NEXT: }
171161

162+
163+
// CHECK: internal func hash(into hasher: inout Hasher) {
164+
// CHECK-NEXT: switch self {
165+
// CHECK-NEXT: case .a(let a0):
166+
// CHECK-NEXT: hasher.combine(0)
167+
// CHECK-NEXT: hasher.combine(a0)
168+
// CHECK-NEXT: case .b:
169+
// CHECK-NEXT: _diagnoseUnavailableCodeReached{{.*}}()
170+
// CHECK-NEXT: }
171+
// CHECK-NEXT: }
172+
172173
// CHECK: internal var hashValue: Int {
173174
// CHECK-NEXT: get {
174175
// CHECK-NEXT: return _hashValue(for: self)

0 commit comments

Comments
 (0)