Skip to content

Commit 0957f85

Browse files
committed
Sema: Fix bug with private access scopes in Swift 3 mode
In Swift 3 mode, the canonical private DeclContext should not look through extensions. The only way I can reproduce this is with a missing warning and we don't really care about missing warnings in Swift 3 mode. However, the next patch in this PR regresses more things so let's fix it properly.
1 parent 1a861df commit 0957f85

File tree

3 files changed

+7
-0
lines changed

3 files changed

+7
-0
lines changed

lib/AST/DeclContext.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,9 @@ IterableDeclContext::castDeclToIterableDeclContext(const Decl *D) {
821821
/// declaration or extension, the supplied context is returned.
822822
static const DeclContext *
823823
getPrivateDeclContext(const DeclContext *DC, const SourceFile *useSF) {
824+
if (DC->getASTContext().isSwiftVersion3())
825+
return DC;
826+
824827
auto NTD = DC->getAsNominalTypeOrNominalTypeExtensionContext();
825828
if (!NTD)
826829
return DC;

test/Compatibility/accessibility_private.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ extension Container {
171171
extension Container {
172172
private struct VeryPrivateStruct { // expected-note * {{type declared here}}
173173
private typealias VeryPrivateType = Int // expected-note * {{type declared here}}
174+
private struct VeryPrivateInnerStruct {}
174175
var privateVar: VeryPrivateType { fatalError() } // expected-warning {{property should be declared private because its type uses a private type}}
175176
var privateVar2 = VeryPrivateType() // expected-warning {{property should be declared private because its type 'Container.VeryPrivateStruct.VeryPrivateType' (aka 'Int') uses a private type}}
177+
var privateVar3 = VeryPrivateInnerStruct() // expected-warning {{property should be declared private because its type 'Container.VeryPrivateStruct.VeryPrivateInnerStruct' uses a private type}}
176178
typealias PrivateAlias = VeryPrivateType // expected-warning {{type alias should be declared private because its underlying type uses a private type}}
177179
subscript(_: VeryPrivateType) -> Void { return () } // expected-warning {{subscript should be declared private because its index uses a private type}}
178180
func privateMethod(_: VeryPrivateType) -> Void {} // expected-warning {{method should be declared private because its parameter uses a private type}} {{none}}

test/Sema/accessibility_private.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,10 @@ extension Container {
171171
extension Container {
172172
private struct VeryPrivateStruct { // expected-note * {{type declared here}}
173173
private typealias VeryPrivateType = Int // expected-note * {{type declared here}}
174+
private struct VeryPrivateInnerStruct {}
174175
var privateVar: VeryPrivateType { fatalError() } // expected-error {{property must be declared private because its type uses a private type}}
175176
var privateVar2 = VeryPrivateType() // expected-error {{property must be declared private because its type 'Container.VeryPrivateStruct.VeryPrivateType' (aka 'Int') uses a private type}}
177+
var privateVar3 = VeryPrivateInnerStruct() // expected-error {{property must be declared private because its type 'Container.VeryPrivateStruct.VeryPrivateInnerStruct' uses a private type}}
176178
typealias PrivateAlias = VeryPrivateType // expected-error {{type alias must be declared private because its underlying type uses a private type}}
177179
subscript(_: VeryPrivateType) -> Void { return () } // expected-error {{subscript must be declared private because its index uses a private type}}
178180
func privateMethod(_: VeryPrivateType) -> Void {} // expected-error {{method must be declared private because its parameter uses a private type}} {{none}}

0 commit comments

Comments
 (0)