-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathauthoritative-import-priority.swift
209 lines (159 loc) · 6.94 KB
/
authoritative-import-priority.swift
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
/// Point to the most relevant import relative to the target decl.
// RUN: %empty-directory(%t)
// RUN: split-file --leading-lines %s %t
/// Build the Swift libraries.
// RUN: %target-swift-frontend -emit-module %t/SwiftPublicNameCore.swift -o %t \
// RUN: -public-module-name SwiftPublicName
// RUN: %target-swift-frontend -emit-module %t/SwiftPublicName.swift -o %t -I %t
// RUN: %target-swift-frontend -emit-module %t/MixedDep.swift -o %t -I %t
/// Client testing order between indirect imports of the same priority.
// RUN: %target-swift-frontend -typecheck -I %t \
// RUN: %t/OrderClient_FileA.swift %t/OrderClient_FileB.swift \
// RUN: -enable-upcoming-feature InternalImportsByDefault \
// RUN: -Rmodule-api-import -verify
/// Client testing order of preference for more levels of imports.
// RUN: %target-swift-frontend -typecheck -I %t \
// RUN: %t/ExportedClient_FileExported.swift %t/ExportedClient_FileA.swift \
// RUN: %t/ExportedClient_FileB.swift %t/ExportedClient_FileC.swift \
// RUN: %t/ExportedClient_FileD_via_underlying.swift \
// RUN: -import-underlying-module -module-name ExportedClient \
// RUN: -enable-upcoming-feature InternalImportsByDefault \
// RUN: -Rmodule-api-import -verify
// Same without the underlying clang module.
// RUN: %target-swift-frontend -typecheck -I %t \
// RUN: %t/ExportedClient_FileExported.swift %t/ExportedClient_FileA.swift \
// RUN: %t/ExportedClient_FileB.swift %t/ExportedClient_FileC.swift \
// RUN: %t/ExportedClient_FileD_via_exported.swift \
// RUN: -module-name ExportedClient \
// RUN: -enable-upcoming-feature InternalImportsByDefault \
// RUN: -Rmodule-api-import -verify
/// Client testing -public-module-name ordering.
// RUN: %target-swift-frontend -typecheck -I %t \
// RUN: %t/SwiftLibClient_FileA.swift %t/SwiftLibClient_FileB.swift \
// RUN: -enable-upcoming-feature InternalImportsByDefault \
// RUN: -Rmodule-api-import -verify
/// Client testing import of the overlay vs an unrelated module.
// RUN: %target-swift-frontend -typecheck -I %t \
// RUN: %t/OverlayClient_FileA.swift %t/OverlayClient_FileB.swift \
// RUN: -enable-upcoming-feature InternalImportsByDefault \
// RUN: -Rmodule-api-import -verify
// REQUIRES: swift_feature_InternalImportsByDefault
//--- module.modulemap
module FarClangDep {
header "FarClangDep.h"
}
module MixedDep {
export *
header "MixedDep.h"
}
module IndirectClangDepA {
export *
header "IndirectClangDepA.h"
}
module IndirectClangDepB {
export *
header "IndirectClangDepB.h"
}
module LibCore {
export *
export_as Lib
header "LibCore.h"
}
module Lib {
export *
header "Lib.h"
}
module NotLib {
export *
header "NotLib.h"
}
module ExportedClient {
export *
header "ExportedClient.h"
}
//--- FarClangDep.h
struct FarClangType{};
//--- MixedDep.h
struct UnderlyingType{};
//--- IndirectClangDepA.h
#include <FarClangDep.h>
#include <MixedDep.h>
//--- IndirectClangDepB.h
#include <FarClangDep.h>
#include <MixedDep.h>
//--- LibCore.h
struct ExportedType {};
//--- Lib.h
#include <LibCore.h>
//--- NotLib.h
#include <LibCore.h>
//--- ExportedClient.h
#include <LibCore.h>
//--- SwiftPublicNameCore.swift
public struct SwiftStruct {}
//--- SwiftPublicName.swift
@_exported import SwiftPublicNameCore
//--- MixedDep.swift
@_exported import MixedDep
//--- OrderClient_FileA.swift
/// Between indirect imports, prefer the first one.
public import IndirectClangDepA
public import IndirectClangDepB // expected-warning {{public import of 'IndirectClangDepB' was not used in public declarations or inlinable code}}
public func useTypesB(a: FarClangType) {}
// expected-remark @-1 {{struct 'FarClangType' is imported via 'IndirectClangDepA', which reexports definition from 'FarClangDep'}}
//--- OrderClient_FileB.swift
/// Still prefer the first one after changing the order.
public import IndirectClangDepB
public import IndirectClangDepA // expected-warning {{public import of 'IndirectClangDepA' was not used in public declarations or inlinable code}}
public func useTypesC(a: FarClangType) {}
// expected-remark @-1 {{struct 'FarClangType' is imported via 'IndirectClangDepB', which reexports definition from 'FarClangDep'}}
//--- ExportedClient_FileExported.swift
@_exported public import ExportedClient
//--- ExportedClient_FileA.swift
/// Prefer the defining module.
public import NotLib // expected-warning {{public import of 'NotLib' was not used in public declarations or inlinable code}}
public import LibCore // We should warn here.
public import Lib
public func useTypesA(a: ExportedType) {}
// expected-remark @-1 {{struct 'ExportedType' is imported via 'Lib', which reexports definition from 'LibCore'}}
//--- ExportedClient_FileB.swift
/// Then prefer the export_as module.
public import NotLib // expected-warning {{public import of 'NotLib' was not used in public declarations or inlinable code}}
public import Lib
public func useTypesB(a: ExportedType) {}
// expected-remark @-1 {{struct 'ExportedType' is imported via 'Lib'}}
//--- ExportedClient_FileC.swift
/// Then prefer any local import.
public import NotLib
public func useTypesC(a: ExportedType) {}
// expected-remark @-1 {{struct 'ExportedType' is imported via 'NotLib', which reexports definition from 'LibCore'}}
//--- ExportedClient_FileD_via_underlying.swift
/// Then use the import of the underling clang module.
public func useTypesD(a: ExportedType) {}
// expected-remark @-1 {{struct 'ExportedType' is imported via 'ExportedClient', which reexports definition from 'LibCore'}}
//--- ExportedClient_FileD_via_exported.swift
/// Finally use the @_exported import from the local module.
public func useTypesD(a: ExportedType) {}
// It would be nice to have a remark even without an import to point to.
//--- SwiftLibClient_FileA.swift
/// Prefer the import matching public-module-name.
public import SwiftPublicNameCore // We should warn here.
public import SwiftPublicName
public func useTypesA(a: SwiftStruct) {}
// expected-remark @-1 {{struct 'SwiftStruct' is imported via 'SwiftPublicName', which reexports definition from 'SwiftPublicName'}}
//--- SwiftLibClient_FileB.swift
/// Fallback on read definition site.
public import SwiftPublicNameCore
public func useTypesB(a: SwiftStruct) {}
// expected-remark @-1 {{struct 'SwiftStruct' is imported via 'SwiftPublicName'}}
//--- OverlayClient_FileA.swift
/// Prefer a Swift overlay to an unrelated 3rd module.
public import IndirectClangDepA // expected-warning {{public import of 'IndirectClangDepA' was not used in public declarations or inlinable code}}
public import MixedDep
public func useTypesA(a: UnderlyingType) {}
// expected-remark @-1 {{struct 'UnderlyingType' is imported via 'MixedDep', which reexports definition from 'MixedDep'}}
//--- OverlayClient_FileB.swift
/// Fallback on any reexporter.
public import IndirectClangDepA
public func useTypesB(a: UnderlyingType) {}
// expected-remark @-1 {{struct 'UnderlyingType' is imported via 'IndirectClangDepA', which reexports definition from 'MixedDep'}}