File: superfluously-public-imports.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (312 lines) | stat: -rw-r--r-- 13,514 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// RUN: %empty-directory(%t)
// RUN: split-file --leading-lines %s %t

/// Build the libraries.
// RUN: %target-swift-frontend -emit-module %t/DepUsedFromInlinableCode.swift -o %t
// RUN: %target-swift-frontend -emit-module %t/DepUsedInSignature.swift -o %t
// RUN: %target-swift-frontend -emit-module %t/Exportee.swift -o %t
// RUN: %target-swift-frontend -emit-module %t/Exporter.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/ConformanceBaseTypes.swift -o %t
// RUN: %target-swift-frontend -emit-module %t/ConformanceDefinition.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/AliasesBase.swift -o %t
// RUN: %target-swift-frontend -emit-module %t/Aliases.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/ExtensionA.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/ExtensionB.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/PropertyWrapper.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/ExtendedDefinitionPublic.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/ExtendedDefinitionNonPublic.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/UnusedImport.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/UnusedPackageImport.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/ExtendedPackageTypeImport.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/ImportNotUseFromAPI.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/ImportUsedInPackage.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/ExportedUnused.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/SPIOnlyUsedInSPI.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/RetroactiveConformance.swift -o %t -I %t

/// Check diagnostics.
// RUN: %target-swift-frontend -typecheck %t/Client.swift -I %t \
// RUN:   -package-name pkg -Rmodule-api-import \
// RUN:   -enable-upcoming-feature InternalImportsByDefault -verify \
// RUN:   -experimental-spi-only-imports
// RUN: %target-swift-frontend -typecheck %t/ClientOfClangModules.swift -I %t \
// RUN:   -package-name pkg -Rmodule-api-import \
// RUN:   -enable-upcoming-feature InternalImportsByDefault -verify
// RUN: %target-swift-frontend -typecheck %t/Client_Swift5.swift -I %t \
// RUN:   -swift-version 5 -verify

//--- DepUsedFromInlinableCode.swift
public struct TypeUsedFromInlinableCode {}
public func funcUsedFromInlinableCode() {}

public func funcUsedFromDefaultValue() -> Int { 42 }

//--- DepUsedInSignature.swift
public struct TypeUsedInSignature {}
public protocol ComposedProtoA {}
public protocol ComposedProtoB {}

//--- Exportee.swift
public struct ExportedType {}

//--- Exporter.swift
@_exported import Exportee

//--- ConformanceBaseTypes.swift
public protocol Proto {}
public struct ConformingType {
    public init () {}
}

//--- ConformanceDefinition.swift
import ConformanceBaseTypes
extension ConformingType : Proto  {}

//--- AliasesBase.swift
open class Clazz {}

//--- Aliases.swift
import AliasesBase
public typealias ClazzAlias = Clazz

//--- ExtensionA.swift
import ConformanceBaseTypes
extension ConformingType {
    public func extFuncA() {}
}

//--- ExtensionB.swift
import ConformanceBaseTypes
extension ConformingType {
    public func extFuncB() {}
}

//--- PropertyWrapper.swift
@propertyWrapper
public struct MyPropertyWrapper<T> {
  public var wrappedValue: T

  public init(wrappedValue value: T) { self.wrappedValue = value }
  public init(_ value: T) { self.wrappedValue = value }
}

//--- ExtendedDefinitionPublic.swift
public struct PublicExtendedType {}

//--- ExtendedDefinitionNonPublic.swift
public struct NonPublicExtendedType {}

//--- UnusedImport.swift

//--- UnusedPackageImport.swift
//--- ExtendedPackageTypeImport.swift

public struct ExtendedPackageType {}

//--- ImportNotUseFromAPI.swift
public struct NotAnAPIType {}
public func notAnAPIFunc() -> NotAnAPIType { return NotAnAPIType() }

//--- ImportUsedInPackage.swift
public struct PackageType {}
public func packageFunc() -> PackageType { return PackageType() }

//--- ExportedUnused.swift

//--- SPIOnlyUsedInSPI.swift
public struct ToUseFromSPI {}

//--- RetroactiveConformance.swift
public struct Extended {
  public var count: Int { 42 }
}

//--- Client_Swift5.swift
/// No diagnostics should be raised on the implicit access level.
import UnusedImport
public import UnusedImport // expected-warning {{public import of 'UnusedImport' was not used in public declarations or inlinable code}} {{1-7=internal}}

//--- Client.swift
public import DepUsedFromInlinableCode
public import DepUsedInSignature
public import Exporter
public import ConformanceBaseTypes
public import ConformanceDefinition
public import AliasesBase
public import Aliases
public import ExtensionA
public import ExtensionB
public import PropertyWrapper
public import ExtendedDefinitionPublic
public import ExtendedDefinitionNonPublic // expected-warning {{public import of 'ExtendedDefinitionNonPublic' was not used in public declarations or inlinable code}} {{1-8=}}

/// Repeat some imports to make sure we report all of them.
public import UnusedImport // expected-warning {{public import of 'UnusedImport' was not used in public declarations or inlinable code}} {{1-8=}}
// expected-note @-1 {{imported 'public' here}}
public import UnusedImport // expected-warning {{public import of 'UnusedImport' was not used in public declarations or inlinable code}} {{1-8=}}
package import UnusedImport // expected-warning {{package import of 'UnusedImport' was not used in package declarations}} {{1-9=}}
// expected-warning @-1 {{module 'UnusedImport' is imported as 'public' from the same file; this 'package' access level will be ignored}}

package import UnusedPackageImport // expected-warning {{package import of 'UnusedPackageImport' was not used in package declarations}} {{1-9=}}
package import ExtendedPackageTypeImport
public import ImportNotUseFromAPI // expected-warning {{public import of 'ImportNotUseFromAPI' was not used in public declarations or inlinable code}} {{1-8=}}
public import ImportUsedInPackage // expected-warning {{public import of 'ImportUsedInPackage' was not used in public declarations or inlinable code}} {{1-7=package}}

@_exported public import ExportedUnused
@_spiOnly public import SPIOnlyUsedInSPI
public import RetroactiveConformance

public func useInSignature(_ a: TypeUsedInSignature) {} // expected-remark {{struct 'TypeUsedInSignature' is imported via 'DepUsedInSignature'}}
public func exportedTypeUseInSignature(_ a: ExportedType) {} // expected-remark {{struct 'ExportedType' is imported via 'Exporter', which reexports definition from 'Exportee'}}

public func useInDefaultValue(_ a: Int = funcUsedFromDefaultValue()) {}
// expected-remark @-1 {{struct 'Int' is imported via 'Swift'}}
// expected-remark @-2 {{global function 'funcUsedFromDefaultValue()' is imported via 'DepUsedFromInlinableCode'}}

public func genericType(_ a: Array<TypeUsedInSignature>) {}
// expected-remark @-1 {{generic struct 'Array' is imported via 'Swift'}}
// expected-remark @-2 {{struct 'TypeUsedInSignature' is imported via 'DepUsedInSignature'}}

public func protocolComposition(_ a: any ComposedProtoA & ComposedProtoB) {}
// expected-remark @-1 {{protocol 'ComposedProtoA' is imported via 'DepUsedInSignature'}}
// expected-remark @-2 {{protocol 'ComposedProtoB' is imported via 'DepUsedInSignature'}}

public func useConformance(_ a: any Proto = ConformingType()) {}
// expected-remark @-1 {{protocol 'Proto' is imported via 'ConformanceBaseTypes'}}
// expected-remark @-2 {{conformance of 'ConformingType' to protocol 'Proto' used here is imported via 'ConformanceDefinition'}}
// expected-remark @-3 {{struct 'ConformingType' is imported via 'ConformanceBaseTypes'}}
// expected-remark @-4 {{initializer 'init()' is imported via 'ConformanceBaseTypes'}}

@usableFromInline internal func usableFromInlineFunc(_ a: TypeUsedInSignature) {}

@inlinable
public func publicFuncUsesPrivate() {
  let _: TypeUsedFromInlinableCode // expected-remark {{struct 'TypeUsedFromInlinableCode' is imported via 'DepUsedFromInlinableCode'}}
  let _: ExportedType // expected-remark {{struct 'ExportedType' is imported via 'Exporter', which reexports definition from 'Exportee'}}
  funcUsedFromInlinableCode() // expected-remark {{global function 'funcUsedFromInlinableCode()' is imported via 'DepUsedFromInlinableCode'}}

  let _: Array<TypeUsedInSignature>
  // expected-remark @-1 {{generic struct 'Array' is imported via 'Swift'}}
  // expected-remark @-2 {{struct 'TypeUsedInSignature' is imported via 'DepUsedInSignature'}}

  let _: any ComposedProtoA & ComposedProtoB
  // expected-remark @-1 {{protocol 'ComposedProtoA' is imported via 'DepUsedInSignature'}}
  // expected-remark @-2 {{protocol 'ComposedProtoB' is imported via 'DepUsedInSignature'}}

  let _: any Proto = ConformingType()
  // expected-remark @-1 {{protocol 'Proto' is imported via 'ConformanceBaseTypes'}}
  // expected-remark @-2 {{conformance of 'ConformingType' to protocol 'Proto' used here is imported via 'ConformanceDefinition'}}
  // expected-remark @-3 {{struct 'ConformingType' is imported via 'ConformanceBaseTypes'}}
  // expected-remark @-4 {{initializer 'init()' is imported via 'ConformanceBaseTypes'}}

  let _: ClazzAlias
  // expected-remark @-1 {{type alias 'ClazzAlias' is imported via 'Aliases'}}
  // expected-remark @-2 2 {{typealias underlying type class 'Clazz' is imported via 'AliasesBase'}}

  let x = ConformingType()
  // expected-remark @-1 {{struct 'ConformingType' is imported via 'ConformanceBaseTypes'}}
  // expected-remark @-2 {{initializer 'init()' is imported via 'ConformanceBaseTypes'}}
  x.extFuncA() // expected-remark {{instance method 'extFuncA()' is imported via 'ExtensionA'}}
  x.extFuncB() // expected-remark {{instance method 'extFuncB()' is imported via 'ExtensionB'}}
}

public struct StructUsingPropertyWrapper {
  @MyPropertyWrapper(42) public var wrapped: Any // expected-remark 2 {{generic struct 'MyPropertyWrapper' is imported via 'PropertyWrapper'}}
}

extension PublicExtendedType { // expected-remark 2 {{struct 'PublicExtendedType' is imported via 'ExtendedDefinitionPublic'}}
    public func foo() {}
}

extension NonPublicExtendedType {
    func foo() {}
}

public struct Struct { // expected-remark {{implicitly used struct 'Int' is imported via 'Swift'}}
  public var propWithInferredIntType = 42
  public var propWithExplicitType: String = "Text" // expected-remark {{struct 'String' is imported via 'Swift'}}
}

public func publicFunction() {
    let _: NotAnAPIType = notAnAPIFunc()
}

internal func internalFunc(a: NotAnAPIType = notAnAPIFunc()) {}
func implicitlyInternalFunc(a: NotAnAPIType = notAnAPIFunc()) {}

// For package decls we only remark on types used in signatures, not for inlinable code.
package func packageFunc(a: PackageType = packageFunc()) {} // expected-remark {{struct 'PackageType' is imported via 'ImportUsedInPackage'}}

@_spi(X)
public func spiFunc(a: ToUseFromSPI) {} // expected-remark {{struct 'ToUseFromSPI' is imported via 'SPIOnlyUsedInSPI'}}

public protocol Countable {
  var count: Int { get } // expected-remark {{struct 'Int' is imported via 'Swift'}}
}

extension Extended: Countable { // expected-remark {{struct 'Extended' is imported via 'RetroactiveConformance'}}
}

extension ExtendedPackageType { // expected-remark {{struct 'ExtendedPackageType' is imported via 'ExtendedPackageTypeImport'}}
  package func useExtendedPackageType() { }
}

/// Tests for imports of clang modules.
//--- module.modulemap
module ClangSimpleUnused {
    header "ClangSimpleUnused.h"
}
module ClangSimple {
    header "ClangSimple.h"
}

module ClangSubmodule {
    header "ClangSubmodule.h"

    module ClangSubmoduleSubmodule {
      header "ClangSubmoduleSubmodule.h"
    }
}

module ClangSubmoduleUnused {
    header "ClangSubmoduleUnused.h"

    module ClangSubmoduleUnsuedSubmodule {
      header "ClangSubmoduleUnusedSubmodule.h"
    }
}

module ClangTopModule {
  header "ClangTopModule.h"
  module ClangTopModuleSubmodule {
    header "ClangTopModuleSubmodule.h"
  }
}

//--- ClangSimpleUnused.h
//--- ClangSimple.h
struct ClangSimpleType {};

//--- ClangSubmodule.h
//--- ClangSubmoduleSubmodule.h
struct ClangSubmoduleSubmoduleType {};

//--- ClangSubmoduleUnused.h
//--- ClangSubmoduleUnusedSubmodule.h

//--- ClangTopModule.h
struct ClangTopModuleType {};
//--- ClangTopModuleSubmodule.h

//--- ClientOfClangModules.swift
public import ClangSimple
public import ClangSimpleUnused // expected-warning {{public import of 'ClangSimpleUnused' was not used in public declarations or inlinable code}}
public import ClangSubmodule.ClangSubmoduleSubmodule
public import ClangSubmoduleUnused.ClangSubmoduleUnsuedSubmodule // expected-warning {{public import of 'ClangSubmoduleUnused' was not used in public declarations or inlinable code}}

// Only the top-level module is used, but we can't detect whether the submodule was used or not.
public import ClangTopModule.ClangTopModuleSubmodule

public func clangUser(a: ClangSimpleType) {} // expected-remark {{struct 'ClangSimpleType' is imported via 'ClangSimple'}}
public func clangUser(a: ClangSubmoduleSubmoduleType) {} // expected-remark {{struct 'ClangSubmoduleSubmoduleType' is imported via 'ClangSubmodule'}}
public func clangUser(a: ClangTopModuleType) {} // expected-remark {{struct 'ClangTopModuleType' is imported via 'ClangTopModule'}}