Skip to content

SE-0025: Parsing and basic completion for 'fileprivate'. #3391

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
Jul 7, 2016
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
1 change: 1 addition & 0 deletions include/swift/AST/Attr.def
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ DECL_ATTR(private, Accessibility,
OnClass | OnProtocol | OnVar | OnSubscript | OnConstructor |
DeclModifier | NotSerialized,
/* Not serialized */ 46)
DECL_ATTR_ALIAS(fileprivate, Accessibility)
DECL_ATTR_ALIAS(internal, Accessibility)
DECL_ATTR_ALIAS(public, Accessibility)

Expand Down
2 changes: 1 addition & 1 deletion include/swift/IDE/CodeCompletion.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class CodeCompletionStringChunk {

public:
enum class ChunkKind {
/// "internal", "private" or "public".
/// "public", "internal", "fileprivate", or "private".
AccessControlKeyword,

/// such as @"availability".
Expand Down
1 change: 1 addition & 0 deletions include/swift/Parse/Tokens.def
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ DECL_KEYWORD(typealias)
DECL_KEYWORD(associatedtype)
DECL_KEYWORD(var)

DECL_KEYWORD(fileprivate)
DECL_KEYWORD(internal)
DECL_KEYWORD(private)
DECL_KEYWORD(public)
Expand Down
5 changes: 3 additions & 2 deletions lib/IDE/CodeCompletion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4062,8 +4062,9 @@ class CompletionOverrideLookup : public swift::VisibleDeclConsumer {

bool missingDeclIntroducer = !hasVarIntroducer && !hasFuncIntroducer;
bool missingAccess = !isKeywordSpecified("private") &&
!isKeywordSpecified("public") &&
!isKeywordSpecified("internal");
!isKeywordSpecified("fileprivate") &&
!isKeywordSpecified("internal") &&
!isKeywordSpecified("public");
bool missingOverride = Reason == DeclVisibilityKind::MemberOfSuper &&
!isKeywordSpecified("override");

Expand Down
9 changes: 6 additions & 3 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -502,8 +502,9 @@ bool Parser::parseNewDeclAttribute(DeclAttributes &Attributes, SourceLoc AtLoc,

auto access = llvm::StringSwitch<Accessibility>(AttrName)
.Case("private", Accessibility::Private)
.Case("public", Accessibility::Public)
.Case("internal", Accessibility::Internal);
.Case("fileprivate", Accessibility::Private) // FIXME: For migration purposes.
.Case("internal", Accessibility::Internal)
.Case("public", Accessibility::Public);

if (!consumeIf(tok::l_paren)) {
// Normal accessibility attribute.
Expand Down Expand Up @@ -1680,11 +1681,13 @@ static bool isStartOfOperatorDecl(const Token &Tok, const Token &Tok2) {
static bool isKeywordPossibleDeclStart(const Token &Tok) {
switch (Tok.getKind()) {
case tok::at_sign:
case tok::kw_associatedtype:
case tok::kw_case:
case tok::kw_class:
case tok::kw_deinit:
case tok::kw_enum:
case tok::kw_extension:
case tok::kw_fileprivate:
case tok::kw_func:
case tok::kw_import:
case tok::kw_init:
Expand All @@ -1698,7 +1701,6 @@ static bool isKeywordPossibleDeclStart(const Token &Tok) {
case tok::kw_struct:
case tok::kw_subscript:
case tok::kw_typealias:
case tok::kw_associatedtype:
case tok::kw_var:
case tok::pound_if:
case tok::identifier:
Expand Down Expand Up @@ -1940,6 +1942,7 @@ ParserStatus Parser::parseDecl(ParseDeclOptions Flags,
}

case tok::kw_private:
case tok::kw_fileprivate:
case tok::kw_internal:
case tok::kw_public:
// We still model these specifiers as attributes.
Expand Down
61 changes: 56 additions & 5 deletions test/Sema/accessibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,49 @@ internal protocol InternalProto {
func internalReq()
}

fileprivate protocol FilePrivateProto {
func filePrivateReq()
}

// expected-note@+1 * {{type declared here}}
private protocol PrivateProto {
func privateReq()
}

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

public var publicVar = 0
}

// expected-note@+1 * {{type declared here}}
internal struct InternalStruct: PublicProto, InternalProto, PrivateProto {
internal struct InternalStruct: PublicProto, InternalProto, FilePrivateProto, PrivateProto {
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}}
private func internalReq() {} // expected-error {{method 'internalReq()' must be declared internal because it matches a requirement in internal protocol 'InternalProto'}} {{3-10=internal}}
private func filePrivateReq() {}
private func privateReq() {}

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

// expected-note@+1 * {{type declared here}}
private struct PrivateStruct: PublicProto, InternalProto, PrivateProto {
fileprivate struct FilePrivateStruct: PublicProto, InternalProto, FilePrivateProto, PrivateProto {
private func publicReq() {}
private func internalReq() {}
private func filePrivateReq() {}
private func privateReq() {}

public var publicVar = 0 // expected-warning {{declaring a public var for a private struct}} {{3-9=private}}
}

// expected-note@+1 * {{type declared here}}
private struct PrivateStruct: PublicProto, InternalProto, FilePrivateProto, PrivateProto {
private func publicReq() {}
private func internalReq() {}
private func filePrivateReq() {}
private func privateReq() {}

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

extension FilePrivateStruct {
public init(x: Int) { self.init() } // expected-warning {{declaring a public initializer for a private struct}} {{3-9=private}}
}

extension PrivateStruct {
public init(x: Int) { self.init() } // expected-warning {{declaring a public initializer for a private struct}} {{3-9=private}}
}
Expand All @@ -64,6 +85,10 @@ private extension PublicStruct {
public func extMemberPrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
private func extImplPrivate() {}
}
fileprivate extension PublicStruct {
public func extMemberFilePrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
private func extImplFilePrivate() {}
}
public extension InternalStruct { // expected-error {{extension of internal struct cannot be declared public}} {{1-8=}}
public func extMemberPublic() {} // expected-warning {{declaring a public instance method for an internal struct}} {{3-9=internal}}
private func extImplPublic() {}
Expand All @@ -72,10 +97,30 @@ internal extension InternalStruct {
public func extMemberInternal() {} // expected-warning {{declaring a public instance method in an internal extension}} {{3-9=internal}}
private func extImplInternal() {}
}
fileprivate extension InternalStruct {
public func extMemberFilePrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
private func extImplFilePrivate() {}
}
private extension InternalStruct {
public func extMemberPrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
private func extImplPrivate() {}
}
public extension FilePrivateStruct { // expected-error {{extension of private struct cannot be declared public}} {{1-8=}}
public func extMemberPublic() {} // expected-warning {{declaring a public instance method for a private struct}} {{3-9=private}}
private func extImplPublic() {}
}
internal extension FilePrivateStruct { // expected-error {{extension of private struct cannot be declared internal}} {{1-10=}}
public func extMemberInternal() {} // expected-warning {{declaring a public instance method in an internal extension}} {{3-9=internal}}
private func extImplInternal() {}
}
fileprivate extension FilePrivateStruct {
public func extMemberFilePrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
private func extImplFilePrivate() {}
}
private extension FilePrivateStruct {
public func extMemberPrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
private func extImplPrivate() {}
}
public extension PrivateStruct { // expected-error {{extension of private struct cannot be declared public}} {{1-8=}}
public func extMemberPublic() {} // expected-warning {{declaring a public instance method for a private struct}} {{3-9=private}}
private func extImplPublic() {}
Expand All @@ -84,6 +129,10 @@ internal extension PrivateStruct { // expected-error {{extension of private stru
public func extMemberInternal() {} // expected-warning {{declaring a public instance method in an internal extension}} {{3-9=internal}}
private func extImplInternal() {}
}
fileprivate extension PrivateStruct {
public func extMemberFilePrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
private func extImplFilePrivate() {}
}
private extension PrivateStruct {
public func extMemberPrivate() {} // expected-warning {{declaring a public instance method in a private extension}} {{3-9=private}}
private func extImplPrivate() {}
Expand Down Expand Up @@ -152,12 +201,14 @@ internal class InternalSubPrivateSet: Base {

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

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

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

Expand Down
9 changes: 9 additions & 0 deletions test/SourceKit/CodeComplete/complete_override.swift.response
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@
key.associated_usrs: "s:FC17complete_override4Base1fFT5getMeSi1bSd_T_",
key.modulename: "complete_override"
},
{
key.kind: source.lang.swift.keyword,
key.name: "fileprivate",
key.sourcetext: "fileprivate",
key.description: "fileprivate",
key.typename: "",
key.context: source.codecompletion.context.none,
key.num_bytes_to_erase: 0
},
{
key.kind: source.lang.swift.keyword,
key.name: "final",
Expand Down
10 changes: 9 additions & 1 deletion test/SourceKit/DocumentStructure/Inputs/access.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ class DefAccess {
public var pubProp : Int = 0
private var privProp : Int = 0
internal func intFunc() {}
fileprivate func fpFunc() {}
}

public class PubAccess {
var defProp : Int = 0
public var pubProp : Int = 0
private var privProp : Int = 0
internal func intFunc() {}
fileprivate func fpFunc() {}

class Nested {
func defFunc() {}
Expand All @@ -21,14 +23,16 @@ internal class IntAccess {
public var pubProp : Int = 0
private var privProp : Int = 0
internal func intFunc() {}
fileprivate func fpFunc() {}
}

private class PrivAccess {
var defProp : Int = 0
public var pubProp : Int = 0
private var privProp : Int = 0
internal func intFunc() {}

fileprivate func fpFunc() {}

class Nested {
func defFunc() {}
}
Expand All @@ -50,6 +54,10 @@ public private(set) var pubPrivSetProp : Int {
get { return 0; }
set { }
}
public fileprivate(set) var pubFPSetProp : Int {
get { return 0; }
set { }
}
public internal(set) var pubIntSetProp : Int {
get { return 0; }
set { }
Expand Down
Loading