Skip to content

Commit a185d21

Browse files
committed
[ASTPrinter] Print custom attributes only if it's accessible
1 parent b611901 commit a185d21

File tree

2 files changed

+78
-11
lines changed

2 files changed

+78
-11
lines changed

lib/AST/Attr.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,18 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
845845
break;
846846
}
847847
case DAK_Custom: {
848+
849+
auto attr = cast<CustomAttr>(this);
850+
if (auto type = attr->getType()) {
851+
// Print custom attributes only if the attribute decl is accessible.
852+
// FIXME: rdar://85477478 They should be rejected.
853+
if (auto attrDecl = type->getNominalOrBoundGenericNominal()) {
854+
if (attrDecl->getFormalAccess() < Options.AccessFilter) {
855+
return false;
856+
}
857+
}
858+
}
859+
848860
if (!Options.IsForSwiftInterface)
849861
break;
850862
// For Swift interface, we should print result builder attributes

test/SourceKit/InterfaceGen/gen_custom_attributes.swift

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,26 @@
88
// RUN: %sourcekitd-test -req=interface-gen -module MyModule -- -target %target-triple -I %t | %FileCheck %s --check-prefix=SWIFTMODULE
99
// RUN: %sourcekitd-test -req=doc-info -module MyModule -- -target %target-triple -I %t | %FileCheck %s --check-prefix=DOCINFO
1010

11+
@available(SwiftStdlib 5.1, *)
12+
@globalActor
13+
public struct PublicActor {
14+
public actor MyActor { }
15+
public static let shared = MyActor()
16+
}
17+
18+
@available(SwiftStdlib 5.1, *)
19+
@globalActor
20+
struct InternalActor {
21+
actor MyActor { }
22+
static let shared = MyActor()
23+
}
24+
1125
@propertyWrapper
12-
public struct OrLess {
26+
public struct PublicOrLess {
1327
private var threshold: Int
1428
private var number: Int = 0
15-
init(wrappedValue: Int, threshold: Int) {
29+
30+
public init(wrappedValue: Int, threshold: Int) {
1631
self.threshold = threshold
1732
self.wrappedValue = wrappedValue
1833
}
@@ -22,38 +37,78 @@ public struct OrLess {
2237
}
2338
}
2439

40+
@propertyWrapper
41+
struct InternalOrLess {
42+
private var threshold: Int
43+
private var number: Int = 0
44+
45+
init(wrappedValue: Int, threshold: Int) {
46+
self.threshold = threshold
47+
self.wrappedValue = wrappedValue
48+
}
49+
var wrappedValue: Int {
50+
get { return number }
51+
set { number = min(newValue, 12) }
52+
}
53+
}
54+
2555
@resultBuilder
26-
public struct IntBuilder {
56+
public struct PublicBuilder {
2757
public static func buildBlock(_ val: Int) -> Int { val }
2858
}
2959

60+
@resultBuilder
61+
struct InternalBuilder {
62+
static func buildBlock(_ val: Int) -> Int { val }
63+
}
64+
65+
3066
public struct TestStruct {
3167

3268
@MainActor public func mainActorMethod() {}
3369
@MainActor(unsafe) public func mainActorUnsafeMethod() {}
3470

35-
@OrLess(threshold: 12) public var twelveOrLess = 13
71+
@available(SwiftStdlib 5.1, *)
72+
@PublicActor public func publicActorMethod() {}
73+
@available(SwiftStdlib 5.1, *)
74+
@InternalActor public func internalActorMethod() {}
75+
76+
@PublicOrLess(threshold: 12) public var publicOrLess = 13
77+
@InternalOrLess(threshold: 42) public var internalOrLess = 56
3678

37-
@IntBuilder public var intValue: Int { 1 }
79+
@PublicBuilder public var publicBuilderVal: Int { 1 }
80+
@InternalBuilder public var internalBuilderVal: Int { 1 }
3881
}
3982

4083
// SWIFTSOURCE: public struct TestStruct {
4184
// SWIFTSOURCE: @MainActor public func mainActorMethod()
4285
// SWIFTSOURCE: @MainActor public func mainActorUnsafeMethod()
43-
// SWIFTSOURCE: @MyModule.OrLess public var twelveOrLess: Int { get set }
44-
// SWIFTSOURCE: @MyModule.IntBuilder public var intValue: Int { get }
86+
// SWIFTSOURCE: @MyModule.PublicActor public func publicActorMethod()
87+
// SWIFTSOURCE: @MyModule.InternalActor public func internalActorMethod()
88+
// SWIFTSOURCE: @MyModule.PublicOrLess public var publicOrLess: Int { get set }
89+
// SWIFTSOURCE: @MyModule.InternalOrLess public var internalOrLess: Int { get set }
90+
// SWIFTSOURCE: @MyModule.PublicBuilder public var publicBuilderVal: Int { get }
91+
// SWIFTSOURCE: @MyModule.InternalBuilder public var internalBuilderVal: Int { get }
4592
// SWIFTSOURCE: }
4693

4794
// SWIFTMODULE: public struct TestStruct {
4895
// SWIFTMODULE: @MainActor public func mainActorMethod()
4996
// SWIFTMODULE: @MainActor public func mainActorUnsafeMethod()
50-
// SWIFTMODULE: @MyModule.OrLess public var twelveOrLess: Int
51-
// SWIFTMODULE: @MyModule.IntBuilder public var intValue: Int { get }
97+
// SWIFTMODULE: @MyModule.PublicActor public func publicActorMethod()
98+
// SWIFTMODULE: public func internalActorMethod()
99+
// SWIFTMODULE: @MyModule.PublicOrLess public var publicOrLess: Int
100+
// SWIFTMODULE: public var internalOrLess: Int
101+
// SWIFTMODULE: @MyModule.PublicBuilder public var publicBuilderVal: Int { get }
102+
// SWIFTMODULE: public var internalBuilderVal: Int { get }
52103
// SWIFTMODULE: }
53104

54105
// DOCINFO: struct TestStruct {
55106
// DOCINFO: @MainActor func mainActorMethod()
56107
// DOCINFO: @MainActor func mainActorUnsafeMethod()
57-
// DOCINFO: @MyModule.OrLess var twelveOrLess: Int
58-
// DOCINFO: @MyModule.IntBuilder var intValue: Int { get }
108+
// DOCINFO: @MyModule.PublicActor func publicActorMethod()
109+
// DOCINFO: func internalActorMethod()
110+
// DOCINFO: @MyModule.PublicOrLess var publicOrLess: Int
111+
// DOCINFO: var internalOrLess: Int
112+
// DOCINFO: @MyModule.PublicBuilder var publicBuilderVal: Int { get }
113+
// DOCINFO: var internalBuilderVal: Int { get }
59114
// DOCINFO: }

0 commit comments

Comments
 (0)