Skip to content

Commit aee92ff

Browse files
authored
SE-0025: Parsing and basic completion for 'fileprivate'. (#3391)
Right now 'fileprivate' is parsed as an alias for 'private' (or perhaps vice versa, since the semantics of 'private' haven't changed yet). This allows us to migrate code to 'fileprivate' without waiting for the full implementation.
1 parent 609d1fc commit aee92ff

File tree

12 files changed

+298
-145
lines changed

12 files changed

+298
-145
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ DECL_ATTR(private, Accessibility,
192192
OnClass | OnProtocol | OnVar | OnSubscript | OnConstructor |
193193
DeclModifier | NotSerialized,
194194
/* Not serialized */ 46)
195+
DECL_ATTR_ALIAS(fileprivate, Accessibility)
195196
DECL_ATTR_ALIAS(internal, Accessibility)
196197
DECL_ATTR_ALIAS(public, Accessibility)
197198

include/swift/IDE/CodeCompletion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class CodeCompletionStringChunk {
6363

6464
public:
6565
enum class ChunkKind {
66-
/// "internal", "private" or "public".
66+
/// "public", "internal", "fileprivate", or "private".
6767
AccessControlKeyword,
6868

6969
/// such as @"availability".

include/swift/Parse/Tokens.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ DECL_KEYWORD(typealias)
9090
DECL_KEYWORD(associatedtype)
9191
DECL_KEYWORD(var)
9292

93+
DECL_KEYWORD(fileprivate)
9394
DECL_KEYWORD(internal)
9495
DECL_KEYWORD(private)
9596
DECL_KEYWORD(public)

lib/IDE/CodeCompletion.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4062,8 +4062,9 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {
40624062

40634063
bool missingDeclIntroducer = !hasVarIntroducer && !hasFuncIntroducer;
40644064
bool missingAccess = !isKeywordSpecified("private") &&
4065-
!isKeywordSpecified("public") &&
4066-
!isKeywordSpecified("internal");
4065+
!isKeywordSpecified("fileprivate") &&
4066+
!isKeywordSpecified("internal") &&
4067+
!isKeywordSpecified("public");
40674068
bool missingOverride = Reason == DeclVisibilityKind::MemberOfSuper &&
40684069
!isKeywordSpecified("override");
40694070

lib/Parse/ParseDecl.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,9 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,
502502

503503
auto access = llvm::StringSwitch<Accessibility>(AttrName)
504504
.Case("private", Accessibility::Private)
505-
.Case("public", Accessibility::Public)
506-
.Case("internal", Accessibility::Internal);
505+
.Case("fileprivate", Accessibility::Private) // FIXME: For migration purposes.
506+
.Case("internal", Accessibility::Internal)
507+
.Case("public", Accessibility::Public);
507508

508509
if (!consumeIf(tok::l_paren)) {
509510
// Normal accessibility attribute.
@@ -1680,11 +1681,13 @@ static bool isStartOfOperatorDecl(const Token &Tok, const Token &Tok2) {
16801681
static bool isKeywordPossibleDeclStart(const Token &Tok) {
16811682
switch (Tok.getKind()) {
16821683
case tok::at_sign:
1684+
case tok::kw_associatedtype:
16831685
case tok::kw_case:
16841686
case tok::kw_class:
16851687
case tok::kw_deinit:
16861688
case tok::kw_enum:
16871689
case tok::kw_extension:
1690+
case tok::kw_fileprivate:
16881691
case tok::kw_func:
16891692
case tok::kw_import:
16901693
case tok::kw_init:
@@ -1698,7 +1701,6 @@ static bool isKeywordPossibleDeclStart(const Token &Tok) {
16981701
case tok::kw_struct:
16991702
case tok::kw_subscript:
17001703
case tok::kw_typealias:
1701-
case tok::kw_associatedtype:
17021704
case tok::kw_var:
17031705
case tok::pound_if:
17041706
case tok::identifier:
@@ -1940,6 +1942,7 @@ ParserStatus Parser::parseDecl(ParseDeclOptions Flags,
19401942
}
19411943

19421944
case tok::kw_private:
1945+
case tok::kw_fileprivate:
19431946
case tok::kw_internal:
19441947
case tok::kw_public:
19451948
// We still model these specifiers as attributes.

test/Sema/accessibility.swift

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,49 @@ internal protocol InternalProto {
99
func internalReq()
1010
}
1111

12+
fileprivate protocol FilePrivateProto {
13+
func filePrivateReq()
14+
}
15+
1216
// expected-note@+1 * {{type declared here}}
1317
private protocol PrivateProto {
1418
func privateReq()
1519
}
1620

17-
public struct PublicStruct: PublicProto, InternalProto, PrivateProto {
21+
public struct PublicStruct: PublicProto, InternalProto, FilePrivateProto, PrivateProto {
1822
private func publicReq() {} // expected-error {{method 'publicReq()' must be declared public because it matches a requirement in public protocol 'PublicProto'}} {{3-10=public}}
1923
private func internalReq() {} // expected-error {{method 'internalReq()' must be declared internal because it matches a requirement in internal protocol 'InternalProto'}} {{3-10=internal}}
24+
private func filePrivateReq() {}
2025
private func privateReq() {}
2126

2227
public var publicVar = 0
2328
}
2429

2530
// expected-note@+1 * {{type declared here}}
26-
internal struct InternalStruct: PublicProto, InternalProto, PrivateProto {
31+
internal struct InternalStruct: PublicProto, InternalProto, FilePrivateProto, PrivateProto {
2732
private func publicReq() {} // expected-error {{method 'publicReq()' must be as accessible as its enclosing type because it matches a requirement in protocol 'PublicProto'}} {{3-10=internal}}
2833
private func internalReq() {} // expected-error {{method 'internalReq()' must be declared internal because it matches a requirement in internal protocol 'InternalProto'}} {{3-10=internal}}
34+
private func filePrivateReq() {}
2935
private func privateReq() {}
3036

3137
public var publicVar = 0 // expected-warning {{declaring a public var for an internal struct}} {{3-9=internal}}
3238
}
3339

3440
// expected-note@+1 * {{type declared here}}
35-
private struct PrivateStruct: PublicProto, InternalProto, PrivateProto {
41+
fileprivate struct FilePrivateStruct: PublicProto, InternalProto, FilePrivateProto, PrivateProto {
3642
private func publicReq() {}
3743
private func internalReq() {}
44+
private func filePrivateReq() {}
45+
private func privateReq() {}
46+
47+
public var publicVar = 0 // expected-warning {{declaring a public var for a private struct}} {{3-9=private}}
48+
}
49+
50+
// expected-note@+1 * {{type declared here}}
51+
private struct PrivateStruct: PublicProto, InternalProto, FilePrivateProto, PrivateProto {
52+
private func publicReq() {}
53+
private func internalReq() {}
54+
private func filePrivateReq() {}
3855
private func privateReq() {}
3956

4057
public var publicVar = 0 // expected-warning {{declaring a public var for a private struct}} {{3-9=private}}
@@ -48,6 +65,10 @@ extension InternalStruct {
4865
public init(x: Int) { self.init() } // expected-warning {{declaring a public initializer for an internal struct}} {{3-9=internal}}
4966
}
5067

68+
extension FilePrivateStruct {
69+
public init(x: Int) { self.init() } // expected-warning {{declaring a public initializer for a private struct}} {{3-9=private}}
70+
}
71+
5172
extension PrivateStruct {
5273
public init(x: Int) { self.init() } // expected-warning {{declaring a public initializer for a private struct}} {{3-9=private}}
5374
}
@@ -64,6 +85,10 @@ private extension PublicStruct {
6485
public func extMemberPrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
6586
private func extImplPrivate() {}
6687
}
88+
fileprivate extension PublicStruct {
89+
public func extMemberFilePrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
90+
private func extImplFilePrivate() {}
91+
}
6792
public extension InternalStruct { // expected-error {{extension of internal struct cannot be declared public}} {{1-8=}}
6893
public func extMemberPublic() {} // expected-warning {{declaring a public instance method for an internal struct}} {{3-9=internal}}
6994
private func extImplPublic() {}
@@ -72,10 +97,30 @@ internal extension InternalStruct {
7297
public func extMemberInternal() {} // expected-warning {{declaring a public instance method in an internal extension}} {{3-9=internal}}
7398
private func extImplInternal() {}
7499
}
100+
fileprivate extension InternalStruct {
101+
public func extMemberFilePrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
102+
private func extImplFilePrivate() {}
103+
}
75104
private extension InternalStruct {
76105
public func extMemberPrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
77106
private func extImplPrivate() {}
78107
}
108+
public extension FilePrivateStruct { // expected-error {{extension of private struct cannot be declared public}} {{1-8=}}
109+
public func extMemberPublic() {} // expected-warning {{declaring a public instance method for a private struct}} {{3-9=private}}
110+
private func extImplPublic() {}
111+
}
112+
internal extension FilePrivateStruct { // expected-error {{extension of private struct cannot be declared internal}} {{1-10=}}
113+
public func extMemberInternal() {} // expected-warning {{declaring a public instance method in an internal extension}} {{3-9=internal}}
114+
private func extImplInternal() {}
115+
}
116+
fileprivate extension FilePrivateStruct {
117+
public func extMemberFilePrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
118+
private func extImplFilePrivate() {}
119+
}
120+
private extension FilePrivateStruct {
121+
public func extMemberPrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
122+
private func extImplPrivate() {}
123+
}
79124
public extension PrivateStruct { // expected-error {{extension of private struct cannot be declared public}} {{1-8=}}
80125
public func extMemberPublic() {} // expected-warning {{declaring a public instance method for a private struct}} {{3-9=private}}
81126
private func extImplPublic() {}
@@ -84,6 +129,10 @@ internal extension PrivateStruct { // expected-error {{extension of private stru
84129
public func extMemberInternal() {} // expected-warning {{declaring a public instance method in an internal extension}} {{3-9=internal}}
85130
private func extImplInternal() {}
86131
}
132+
fileprivate extension PrivateStruct {
133+
public func extMemberFilePrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
134+
private func extImplFilePrivate() {}
135+
}
87136
private extension PrivateStruct {
88137
public func extMemberPrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
89138
private func extImplPrivate() {}
@@ -152,12 +201,14 @@ internal class InternalSubPrivateSet: Base {
152201

153202
public typealias PublicTA1 = PublicStruct
154203
public typealias PublicTA2 = InternalStruct // expected-error {{type alias cannot be declared public because its underlying type uses an internal type}}
155-
public typealias PublicTA3 = PrivateStruct // expected-error {{type alias cannot be declared public because its underlying type uses a private type}}
204+
public typealias PublicTA3 = FilePrivateStruct // expected-error {{type alias cannot be declared public because its underlying type uses a private type}}
205+
public typealias PublicTA4 = PrivateStruct // expected-error {{type alias cannot be declared public because its underlying type uses a private type}}
156206

157207
// expected-note@+1 {{type declared here}}
158208
internal typealias InternalTA1 = PublicStruct
159209
internal typealias InternalTA2 = InternalStruct
160-
internal typealias InternalTA3 = PrivateStruct // expected-error {{type alias cannot be declared internal because its underlying type uses a private type}}
210+
internal typealias InternalTA3 = FilePrivateStruct // expected-error {{type alias cannot be declared internal because its underlying type uses a private type}}
211+
internal typealias InternalTA4 = PrivateStruct // expected-error {{type alias cannot be declared internal because its underlying type uses a private type}}
161212

162213
public typealias PublicFromInternal = InternalTA1 // expected-error {{type alias cannot be declared public because its underlying type uses an internal type}}
163214

test/SourceKit/CodeComplete/complete_override.swift.response

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@
7474
key.associated_usrs: "s:FC17complete_override4Base1fFT5getMeSi1bSd_T_",
7575
key.modulename: "complete_override"
7676
},
77+
{
78+
key.kind: source.lang.swift.keyword,
79+
key.name: "fileprivate",
80+
key.sourcetext: "fileprivate",
81+
key.description: "fileprivate",
82+
key.typename: "",
83+
key.context: source.codecompletion.context.none,
84+
key.num_bytes_to_erase: 0
85+
},
7786
{
7887
key.kind: source.lang.swift.keyword,
7988
key.name: "final",

test/SourceKit/DocumentStructure/Inputs/access.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ class DefAccess {
33
public var pubProp : Int = 0
44
private var privProp : Int = 0
55
internal func intFunc() {}
6+
fileprivate func fpFunc() {}
67
}
78

89
public class PubAccess {
910
var defProp : Int = 0
1011
public var pubProp : Int = 0
1112
private var privProp : Int = 0
1213
internal func intFunc() {}
14+
fileprivate func fpFunc() {}
1315

1416
class Nested {
1517
func defFunc() {}
@@ -21,14 +23,16 @@ internal class IntAccess {
2123
public var pubProp : Int = 0
2224
private var privProp : Int = 0
2325
internal func intFunc() {}
26+
fileprivate func fpFunc() {}
2427
}
2528

2629
private class PrivAccess {
2730
var defProp : Int = 0
2831
public var pubProp : Int = 0
2932
private var privProp : Int = 0
3033
internal func intFunc() {}
31-
34+
fileprivate func fpFunc() {}
35+
3236
class Nested {
3337
func defFunc() {}
3438
}
@@ -50,6 +54,10 @@ public private(set) var pubPrivSetProp : Int {
5054
get { return 0; }
5155
set { }
5256
}
57+
public fileprivate(set) var pubFPSetProp : Int {
58+
get { return 0; }
59+
set { }
60+
}
5361
public internal(set) var pubIntSetProp : Int {
5462
get { return 0; }
5563
set { }

0 commit comments

Comments
 (0)