Skip to content

Allow @testable witnesses to satisfy protocol requirements. #4654

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
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
23 changes: 20 additions & 3 deletions lib/Sema/TypeCheckProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,14 +1239,31 @@ checkWitnessAccessibility(const DeclContext *&requiredAccessScope,
}
}

if (!witness->isAccessibleFrom(requiredAccessScope))
return true;
const DeclContext *actualScopeToCheck = requiredAccessScope;
if (!witness->isAccessibleFrom(actualScopeToCheck)) {
// Special case: if we have `@testable import` of the witness's module,
// allow the witness to match if it would have matched for just this file.
// That is, if '@testable' allows us to see the witness here, it should
// allow us to see it anywhere, because any other client could also add
// their own `@testable import`.
if (auto parentFile = dyn_cast<SourceFile>(DC->getModuleScopeContext())) {
const Module *witnessModule = witness->getModuleContext();
if (parentFile->getParentModule() != witnessModule &&
parentFile->hasTestableImport(witnessModule) &&
witness->isAccessibleFrom(parentFile)) {
actualScopeToCheck = parentFile;
}
}

if (actualScopeToCheck == requiredAccessScope)
return true;
}

if (requirement->isSettable(DC)) {
*isSetter = true;

auto ASD = cast<AbstractStorageDecl>(witness);
if (!ASD->isSetterAccessibleFrom(requiredAccessScope))
if (!ASD->isSetterAccessibleFrom(actualScopeToCheck))
return true;
}

Expand Down
6 changes: 6 additions & 0 deletions test/NameBinding/Inputs/has_accessibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ public struct StructWithPrivateSetter {
public private(set) var x = 0
public init() {}
}

public protocol HasDefaultImplementation {}
extension HasDefaultImplementation {
internal func foo() {}
}
internal class InternalBase {}
18 changes: 14 additions & 4 deletions test/NameBinding/accessibility.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// RUN: rm -rf %t && mkdir -p %t
// RUN: cp %s %t/main.swift

// RUN: %target-swift-frontend -parse -primary-file %t/main.swift %S/Inputs/accessibility_other.swift -module-name accessibility -enable-source-import -I %S/Inputs -sdk "" -enable-access-control -verify
// RUN: %target-swift-frontend -parse -primary-file %t/main.swift %S/Inputs/accessibility_other.swift -module-name accessibility -enable-source-import -I %S/Inputs -sdk "" -disable-access-control -D DEFINE_VAR_FOR_SCOPED_IMPORT -D ACCESS_DISABLED

// RUN: %target-swift-frontend -emit-module -o %t %S/Inputs/has_accessibility.swift -D DEFINE_VAR_FOR_SCOPED_IMPORT -enable-testing
// RUN: %target-swift-frontend -parse -primary-file %t/main.swift %S/Inputs/accessibility_other.swift -module-name accessibility -I %t -sdk "" -enable-access-control -verify
// RUN: %target-swift-frontend -parse -primary-file %t/main.swift %S/Inputs/accessibility_other.swift -module-name accessibility -I %t -sdk "" -disable-access-control -D ACCESS_DISABLED
Expand Down Expand Up @@ -105,9 +102,9 @@ protocol MethodProto {
}

extension OriginallyEmpty : MethodProto {}
// TESTABLE-NOT: :[[@LINE-1]]:{{[^:]+}}:
#if !ACCESS_DISABLED
extension HiddenMethod : MethodProto {} // expected-error {{type 'HiddenMethod' does not conform to protocol 'MethodProto'}}
// TESTABLE-NOT: :[[@LINE-1]]:{{[^:]+}}:

extension Foo : MethodProto {} // expected-error {{type 'Foo' does not conform to protocol 'MethodProto'}}
#endif
Expand Down Expand Up @@ -163,3 +160,16 @@ private struct PrivateConformerByLocalTypeBad : TypeProto {
}
#endif

public protocol Fooable {
func foo() // expected-note * {{protocol requires function 'foo()'}}
}

#if !ACCESS_DISABLED
internal struct FooImpl: Fooable, HasDefaultImplementation {} // expected-error {{type 'FooImpl' does not conform to protocol 'Fooable'}}
public struct PublicFooImpl: Fooable, HasDefaultImplementation {} // expected-error {{type 'PublicFooImpl' does not conform to protocol 'Fooable'}}
// TESTABLE-NOT: method 'foo()'

internal class TestableSub: InternalBase {} // expected-error {{undeclared type 'InternalBase'}}
public class TestablePublicSub: InternalBase {} // expected-error {{undeclared type 'InternalBase'}}
// TESTABLE-NOT: undeclared type 'InternalBase'
#endif
8 changes: 8 additions & 0 deletions test/SILGen/Inputs/TestableMultifileHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
public protocol HasDefaultFoo {}
extension HasDefaultFoo {
internal func foo() {}
}

internal class Base {
func foo() {}
}
43 changes: 43 additions & 0 deletions test/SILGen/testable-multifile-other.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// This test is paired with testable-multifile.swift.

// RUN: rm -rf %t && mkdir -p %t
// RUN: %target-swift-frontend -emit-module %S/Inputs/TestableMultifileHelper.swift -enable-testing -o %t

// RUN: %target-swift-frontend -emit-silgen -I %t %s %S/testable-multifile.swift -module-name main | %FileCheck %s
// RUN: %target-swift-frontend -emit-silgen -I %t %S/testable-multifile.swift %s -module-name main | %FileCheck %s
// RUN: %target-swift-frontend -emit-silgen -I %t -primary-file %s %S/testable-multifile.swift -module-name main | %FileCheck %s

// Just make sure we don't crash later on.
// RUN: %target-swift-frontend -emit-ir -I %t -primary-file %s %S/testable-multifile.swift -module-name main -o /dev/null
// RUN: %target-swift-frontend -emit-ir -I %t -O -primary-file %s %S/testable-multifile.swift -module-name main -o /dev/null

func use<F: Fooable>(_ f: F) { f.foo() }
func test(internalFoo: FooImpl, publicFoo: PublicFooImpl) {
use(internalFoo)
use(publicFoo)

internalFoo.foo()
publicFoo.foo()
}

// CHECK-LABEL: sil hidden @_TF4main4testFT11internalFooVS_7FooImpl9publicFooVS_13PublicFooImpl_T_
// CHECK: [[USE_1:%.+]] = function_ref @_TF4main3useuRxS_7FooablerFxT_
// CHECK: = apply [[USE_1]]<FooImpl>({{%.+}}) : $@convention(thin) <τ_0_0 where τ_0_0 : Fooable> (@in τ_0_0) -> ()
// CHECK: [[USE_2:%.+]] = function_ref @_TF4main3useuRxS_7FooablerFxT_
// CHECK: = apply [[USE_2]]<PublicFooImpl>({{%.+}}) : $@convention(thin) <τ_0_0 where τ_0_0 : Fooable> (@in τ_0_0) -> ()
// CHECK: [[IMPL_1:%.+]] = function_ref @_TFE23TestableMultifileHelperPS_13HasDefaultFoo3foofT_T_
// CHECK: = apply [[IMPL_1]]<FooImpl>({{%.+}}) : $@convention(method) <τ_0_0 where τ_0_0 : HasDefaultFoo> (@in_guaranteed τ_0_0) -> ()
// CHECK: [[IMPL_2:%.+]] = function_ref @_TFE23TestableMultifileHelperPS_13HasDefaultFoo3foofT_T_
// CHECK: = apply [[IMPL_2]]<PublicFooImpl>({{%.+}}) : $@convention(method) <τ_0_0 where τ_0_0 : HasDefaultFoo> (@in_guaranteed τ_0_0) -> ()
// CHECK: {{^}$}}

func test(internalSub: Sub, publicSub: PublicSub) {
internalSub.foo()
publicSub.foo()
}

// CHECK-LABEL: sil hidden @_TF4main4testFT11internalSubCS_3Sub9publicSubCS_9PublicSub_T_
// CHECK: = class_method %0 : $Sub, #Sub.foo!1
// CHECK: = class_method %1 : $PublicSub, #PublicSub.foo!1
// CHECK: {{^}$}}

67 changes: 67 additions & 0 deletions test/SILGen/testable-multifile.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This test is paired with testable-multifile-other.swift.

// RUN: rm -rf %t && mkdir -p %t
// RUN: %target-swift-frontend -emit-module %S/Inputs/TestableMultifileHelper.swift -enable-testing -o %t

// RUN: %target-swift-frontend -emit-silgen -I %t %s %S/testable-multifile-other.swift -module-name main | %FileCheck %s
// RUN: %target-swift-frontend -emit-silgen -I %t %S/testable-multifile-other.swift %s -module-name main | %FileCheck %s
// RUN: %target-swift-frontend -emit-silgen -I %t -primary-file %s %S/testable-multifile-other.swift -module-name main | %FileCheck %s

// Just make sure we don't crash later on.
// RUN: %target-swift-frontend -emit-ir -I %t -primary-file %s %S/testable-multifile-other.swift -module-name main -o /dev/null
// RUN: %target-swift-frontend -emit-ir -I %t -O -primary-file %s %S/testable-multifile-other.swift -module-name main -o /dev/null

@testable import TestableMultifileHelper

public protocol Fooable {
func foo()
}

struct FooImpl: Fooable, HasDefaultFoo {}
public struct PublicFooImpl: Fooable, HasDefaultFoo {}

// CHECK-LABEL: sil{{.*}} @_TTWV4main7FooImplS_7FooableS_FS1_3foofT_T_ : $@convention(witness_method) (@in_guaranteed FooImpl) -> () {
// CHECK: function_ref @_TFE23TestableMultifileHelperPS_13HasDefaultFoo3foofT_T_
// CHECK: {{^}$}}

// CHECK-LABEL: sil{{.*}} @_TTWV4main13PublicFooImplS_7FooableS_FS1_3foofT_T_ : $@convention(witness_method) (@in_guaranteed PublicFooImpl) -> () {
// CHECK: function_ref @_TFE23TestableMultifileHelperPS_13HasDefaultFoo3foofT_T_
// CHECK: {{^}$}}

private class PrivateSub: Base {
fileprivate override func foo() {}
}
class Sub: Base {
internal override func foo() {}
}
public class PublicSub: Base {
public override func foo() {}
}

// CHECK-LABEL: sil_vtable PrivateSub {
// CHECK-NEXT: #Base.foo!1: _TFC4mainP33_F1525133BD493492AD72BF10FBCB1C5210PrivateSub3foofT_T_
// CHECK-NEXT: #Base.init!initializer.1: _TFC4mainP33_F1525133BD493492AD72BF10FBCB1C5210PrivateSubcfT_S0_
// CHECK-NEXT: #PrivateSub.deinit!deallocator: _TFC4mainP33_F1525133BD493492AD72BF10FBCB1C5210PrivateSubD
// CHECK-NEXT: }

// CHECK-LABEL: sil_vtable Sub {
// CHECK-NEXT: #Base.foo!1: _TFC4main3Sub3foofT_T_
// CHECK-NEXT: #Base.init!initializer.1: _TFC4main3SubcfT_S0_
// CHECK-NEXT: #Sub.deinit!deallocator: _TFC4main3SubD
// CHECK-NEXT: }

// CHECK-LABEL: sil_vtable PublicSub {
// CHECK-NEXT: #Base.foo!1: _TFC4main9PublicSub3foofT_T_
// CHECK-NEXT: #Base.init!initializer.1: _TFC4main9PublicSubcfT_S0_
// CHECK-NEXT: #PublicSub.deinit!deallocator: _TFC4main9PublicSubD
// CHECK-NEXT: }



// CHECK-LABEL: sil_witness_table FooImpl: Fooable module main {
// CHECK-NEXT: method #Fooable.foo!1: @_TTWV4main7FooImplS_7FooableS_FS1_3foofT_T_
// CHECK-NEXT: }

// CHECK-LABEL: sil_witness_table [fragile] PublicFooImpl: Fooable module main {
// CHECK-NEXT: method #Fooable.foo!1: @_TTWV4main13PublicFooImplS_7FooableS_FS1_3foofT_T_
// CHECK-NEXT: }