|
| 1 | +/// Test @_spiOnly exportability type-checking |
| 2 | + |
| 3 | +// RUN: %empty-directory(%t) |
| 4 | +// RUN: split-file %s %t |
| 5 | + |
| 6 | +/// Generate dependencies. |
| 7 | +// RUN: %target-swift-frontend -emit-module %t/PublicLib.swift \ |
| 8 | +// RUN: -module-name PublicLib -emit-module-path %t/PublicLib.swiftmodule \ |
| 9 | +// RUN: -swift-version 5 -enable-library-evolution |
| 10 | +// RUN: %target-swift-frontend -emit-module %t/SPIOnlyImportedLib.swift \ |
| 11 | +// RUN: -module-name SPIOnlyImportedLib \ |
| 12 | +// RUN: -emit-module-path %t/SPIOnlyImportedLib.swiftmodule \ |
| 13 | +// RUN: -swift-version 5 -enable-library-evolution -I %t |
| 14 | + |
| 15 | +/// Test the client. |
| 16 | +// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t -verify \ |
| 17 | +// RUN: -experimental-spi-only-imports |
| 18 | +// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t -verify \ |
| 19 | +// RUN: -experimental-spi-only-imports \ |
| 20 | +// RUN: -enable-library-evolution |
| 21 | + |
| 22 | +/// Generate the swiftinterface of the working code and verify it. |
| 23 | +// RUN: %target-swift-emit-module-interface(%t/Client.swiftinterface) \ |
| 24 | +// RUN: %t/Client.swift -I %t -DSKIP_ERRORS \ |
| 25 | +// RUN: -experimental-spi-only-imports |
| 26 | +// RUN: %target-swift-typecheck-module-from-interface(%t/Client.swiftinterface) -I %t |
| 27 | + |
| 28 | + |
| 29 | +//--- PublicLib.swift |
| 30 | + |
| 31 | +public struct PublicType { |
| 32 | + public init() {} |
| 33 | +} |
| 34 | + |
| 35 | +public protocol PublicProtocol {} |
| 36 | +public func conformanceUse(_ a: PublicProtocol) {} |
| 37 | + |
| 38 | +@propertyWrapper |
| 39 | +public struct PublicPropertyWrapper<T> { |
| 40 | + public var wrappedValue: T |
| 41 | + |
| 42 | + public init(wrappedValue value: T) { self.wrappedValue = value } |
| 43 | + public init(_ value: T) { self.wrappedValue = value } |
| 44 | +} |
| 45 | + |
| 46 | +//--- SPIOnlyImportedLib.swift |
| 47 | +import PublicLib |
| 48 | + |
| 49 | +public func spiOnlyFunc() -> String { fatalError() } |
| 50 | + |
| 51 | +public protocol SPIOnlyProto { |
| 52 | +} |
| 53 | + |
| 54 | +public struct SPIOnlyStruct { |
| 55 | + public init() {} |
| 56 | + public func structMethod() {} |
| 57 | +} |
| 58 | + |
| 59 | +public class SPIOnlyClass<P: PublicProtocol> { |
| 60 | + public init(p: P) {} |
| 61 | + public func classMethod() {} |
| 62 | +} |
| 63 | + |
| 64 | +public enum SPIOnlyEnum { |
| 65 | + case A |
| 66 | + case B |
| 67 | + public func enumMethod() {} |
| 68 | +} |
| 69 | + |
| 70 | +extension PublicType { |
| 71 | + public func spiOnlyExtensionMethod() {} |
| 72 | +} |
| 73 | + |
| 74 | +extension PublicType : PublicProtocol {} |
| 75 | + |
| 76 | +@propertyWrapper |
| 77 | +public struct SPIOnlyPropertyWrapper<T> { |
| 78 | + public var wrappedValue: T |
| 79 | + |
| 80 | + public init(wrappedValue value: T) { self.wrappedValue = value } |
| 81 | + public init(_ value: T) { self.wrappedValue = value } |
| 82 | +} |
| 83 | + |
| 84 | +//--- Client.swift |
| 85 | + |
| 86 | +import PublicLib |
| 87 | +@_spiOnly import SPIOnlyImportedLib |
| 88 | + |
| 89 | +#if !SKIP_ERRORS |
| 90 | +public func publicUser<T: SPIOnlyProto>(_ a: SPIOnlyStruct, t: T) -> SPIOnlyStruct { fatalError() } |
| 91 | +// expected-error @-1 2 {{cannot use struct 'SPIOnlyStruct' here; 'SPIOnlyImportedLib' was imported for SPI only}} |
| 92 | +// expected-error @-2 {{cannot use protocol 'SPIOnlyProto' here; 'SPIOnlyImportedLib' was imported for SPI only}} |
| 93 | +#endif |
| 94 | + |
| 95 | +@_spi(X) public func spiUser(_ a: SPIOnlyStruct) -> SPIOnlyStruct { fatalError() } |
| 96 | + |
| 97 | +internal func internalUser(_ a: SPIOnlyStruct) -> SPIOnlyStruct { fatalError() } |
| 98 | + |
| 99 | +#if !SKIP_ERRORS |
| 100 | +@inlinable |
| 101 | +public func publicInlinableUser() { |
| 102 | + _ = spiOnlyFunc() // expected-error {{global function 'spiOnlyFunc()' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 103 | + |
| 104 | + var x: SPIOnlyStruct // expected-error {{struct 'SPIOnlyStruct' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 105 | + x = SPIOnlyStruct() // expected-error {{struct 'SPIOnlyStruct' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 106 | + // expected-error @-1 {{initializer 'init()' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 107 | + x.structMethod() // expected-error {{instance method 'structMethod()' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 108 | + |
| 109 | + var c: SPIOnlyClass<PublicType> // expected-error {{generic class 'SPIOnlyClass' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 110 | + c = SPIOnlyClass(p: PublicType()) // expected-error {{generic class 'SPIOnlyClass' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 111 | + // expected-error @-1 {{initializer 'init(p:)' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 112 | + c.classMethod() // expected-error {{instance method 'classMethod()' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 113 | + |
| 114 | + var e: SPIOnlyEnum // expected-error {{enum 'SPIOnlyEnum' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 115 | + e = .A // expected-error {{enum case 'A' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 116 | + e.enumMethod() // expected-error {{instance method 'enumMethod()' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 117 | + |
| 118 | + let p: PublicType = PublicType() |
| 119 | + p.spiOnlyExtensionMethod() // expected-error {{instance method 'spiOnlyExtensionMethod()' cannot be used in an '@inlinable' function because 'SPIOnlyImportedLib' was imported for SPI only}} |
| 120 | +} |
| 121 | +#endif |
| 122 | + |
| 123 | +@inlinable @_spi(X) |
| 124 | +public func spiInlinableUser() { |
| 125 | + _ = spiOnlyFunc() |
| 126 | + |
| 127 | + var x: SPIOnlyStruct |
| 128 | + x = SPIOnlyStruct() |
| 129 | + x.structMethod() |
| 130 | + |
| 131 | + let p: PublicType = PublicType() |
| 132 | + p.spiOnlyExtensionMethod() |
| 133 | +} |
| 134 | + |
| 135 | +public func implementationDetailsUser() { |
| 136 | + _ = spiOnlyFunc() |
| 137 | + |
| 138 | + var x: SPIOnlyStruct |
| 139 | + x = SPIOnlyStruct() |
| 140 | + x.structMethod() |
| 141 | + |
| 142 | + let p: PublicType = PublicType() |
| 143 | + p.spiOnlyExtensionMethod() |
| 144 | +} |
| 145 | + |
| 146 | +public struct ClientStruct { |
| 147 | +#if !SKIP_ERRORS |
| 148 | + public var a: SPIOnlyStruct // expected-error {{cannot use struct 'SPIOnlyStruct' here; 'SPIOnlyImportedLib' was imported for SPI only}} |
| 149 | + @SPIOnlyPropertyWrapper(42) public var aWrapped: Any // expected-error {{cannot use generic struct 'SPIOnlyPropertyWrapper' as property wrapper here; 'SPIOnlyImportedLib' was imported for SPI only}} |
| 150 | +#endif |
| 151 | + @PublicPropertyWrapper(SPIOnlyStruct()) public var bWrapped: Any |
| 152 | +} |
0 commit comments