Skip to content

Commit 1ecabaf

Browse files
committed
[NFC] Add skeleton of module selector lookup test
The diagnostics in this test will evolve as we implement pieces of the feature.
1 parent 12b582a commit 1ecabaf

File tree

2 files changed

+316
-0
lines changed

2 files changed

+316
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// swift-interface-format-version: 1.0
2+
// swift-module-flags: -module-name ModuleSelectorTestingKit -swift-version 5
3+
4+
import Swift
5+
6+
// These types are identical; we just use them to test with different scope
7+
// selectors.
8+
9+
public struct A {
10+
public init(value: Swift.Int)
11+
public dynamic mutating func negate()
12+
public var magnitude: Swift.UInt
13+
}
14+
15+
public struct B {
16+
public init(value: Swift.Int)
17+
public dynamic mutating func negate()
18+
public var magnitude: Swift.UInt
19+
}
20+
21+
public struct C {
22+
public init(value: Int)
23+
public dynamic mutating func negate()
24+
public var magnitude: Swift.UInt
25+
}
26+
27+
public struct D {
28+
public init(value: Int)
29+
public dynamic mutating func negate()
30+
public var magnitude: Swift.UInt
31+
}
32+
33+
@propertyWrapper
34+
public struct available {
35+
public init() {}
36+
public var wrappedValue: Int { 0 }
37+
}
38+
39+
@_functionBuilder
40+
public struct MyBuilder {
41+
public static func buildBlock()
42+
}
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
// RUN: %target-typecheck-verify-swift -verify-ignore-unrelated -sdk %clang-importer-sdk -module-name main -I %S/Inputs -enable-experimental-feature ModuleSelector
2+
// RUN: %target-typecheck-verify-swift -verify-ignore-unrelated -sdk %clang-importer-sdk -module-name main -I %S/Inputs -enable-experimental-feature ModuleSelector -enable-experimental-feature ParserASTGen
3+
4+
// REQUIRES: swift_feature_ModuleSelector, swift_feature_ParserASTGen
5+
6+
// FIXME: This test doesn't really cover:
7+
//
8+
// * Whether X::foo finds foos in X's re-exports
9+
// * Whether we handle access paths correctly
10+
// * Interaction with ClangImporter
11+
// * Cross-import overlays
12+
// * Key path dynamic member lookup
13+
// * Custom type attributes (and coverage of type attrs generally is sparse)
14+
// * Macros
15+
//
16+
// It also might not cover all combinations of name lookup paths and inputs.
17+
18+
import ModuleSelectorTestingKit
19+
20+
import ctypes.bits
21+
import struct ModuleSelectorTestingKit::A
22+
23+
let magnitude: Never = fatalError()
24+
25+
// Test correct code using `A`
26+
27+
extension ModuleSelectorTestingKit::A {}
28+
29+
extension A: @retroactive Swift::Equatable {
30+
@_implements(Swift::Equatable, ==(_:_:))
31+
public static func equals(_: ModuleSelectorTestingKit::A, _: ModuleSelectorTestingKit::A) -> Swift::Bool {
32+
Swift::fatalError()
33+
}
34+
35+
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
36+
// @_derivative(of:)
37+
38+
@_dynamicReplacement(for: ModuleSelectorTestingKit::negate())
39+
mutating func myNegate() {
40+
let fn: (Swift::Int, Swift::Int) -> Swift::Int = (Swift::+)
41+
42+
let magnitude: Int.Swift::Magnitude = main::magnitude
43+
44+
_ = (fn, magnitude)
45+
46+
if Swift::Bool.Swift::random() {
47+
self.ModuleSelectorTestingKit::negate()
48+
}
49+
else {
50+
self = ModuleSelectorTestingKit::A(value: .Swift::min)
51+
self = A.ModuleSelectorTestingKit::init(value: .min)
52+
}
53+
54+
self.main::myNegate()
55+
56+
Swift::fatalError()
57+
58+
_ = \ModuleSelectorTestingKit::A.magnitude
59+
_ = \A.ModuleSelectorTestingKit::magnitude
60+
}
61+
62+
// FIXME: Can we test @convention(witness_method:)?
63+
}
64+
65+
// Test resolution of main:: using `B`
66+
67+
extension main::B {}
68+
69+
extension B: @retroactive main::Equatable {
70+
71+
@_implements(main::Equatable, ==(_:_:))
72+
73+
public static func equals(_: main::B, _: main::B) -> main::Bool {
74+
main::fatalError()
75+
}
76+
77+
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
78+
// @_derivative(of:)
79+
80+
@_dynamicReplacement(for: main::negate())
81+
82+
mutating func myNegate() {
83+
let fn: (main::Int, main::Int) -> main::Int =
84+
(main::+)
85+
86+
let magnitude: Int.main::Magnitude = main::magnitude
87+
88+
_ = (fn, magnitude)
89+
90+
if main::Bool.main::random() {
91+
92+
main::negate()
93+
}
94+
else {
95+
self = main::B(value: .main::min)
96+
97+
self = B.main::init(value: .min)
98+
}
99+
100+
self.main::myNegate()
101+
102+
main::fatalError()
103+
104+
_ = \main::A.magnitude
105+
_ = \A.main::magnitude
106+
}
107+
108+
// FIXME: Can we test @convention(witness_method:)?
109+
}
110+
111+
// Test resolution of ModuleSelectorTestingKit:: using `C`
112+
113+
extension ModuleSelectorTestingKit::C {}
114+
115+
extension C: @retroactive ModuleSelectorTestingKit::Equatable {
116+
117+
@_implements(ModuleSelectorTestingKit::Equatable, ==(_:_:))
118+
119+
public static func equals(_: ModuleSelectorTestingKit::C, _: ModuleSelectorTestingKit::C) -> ModuleSelectorTestingKit::Bool {
120+
ModuleSelectorTestingKit::fatalError()
121+
}
122+
123+
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
124+
// @_derivative(of:)
125+
126+
@_dynamicReplacement(for: ModuleSelectorTestingKit::negate())
127+
128+
mutating func myNegate() {
129+
let fn: (ModuleSelectorTestingKit::Int, ModuleSelectorTestingKit::Int) -> ModuleSelectorTestingKit::Int =
130+
(ModuleSelectorTestingKit::+)
131+
132+
let magnitude: Int.ModuleSelectorTestingKit::Magnitude = ModuleSelectorTestingKit::magnitude
133+
134+
_ = (fn, magnitude)
135+
136+
if ModuleSelectorTestingKit::Bool.ModuleSelectorTestingKit::random() {
137+
138+
ModuleSelectorTestingKit::negate()
139+
}
140+
else {
141+
self = ModuleSelectorTestingKit::C(value: .ModuleSelectorTestingKit::min)
142+
143+
self = C.ModuleSelectorTestingKit::init(value: .min)
144+
}
145+
146+
self.ModuleSelectorTestingKit::myNegate()
147+
148+
ModuleSelectorTestingKit::fatalError()
149+
150+
_ = \ModuleSelectorTestingKit::A.magnitude
151+
_ = \A.ModuleSelectorTestingKit::magnitude
152+
}
153+
154+
// FIXME: Can we test @convention(witness_method:)?
155+
}
156+
157+
// Test resolution of Swift:: using `D`
158+
159+
extension Swift::D {}
160+
161+
extension D: @retroactive Swift::Equatable {
162+
163+
@_implements(Swift::Equatable, ==(_:_:))
164+
public static func equals(_: Swift::D, _: Swift::D) -> Swift::Bool {
165+
Swift::fatalError()
166+
}
167+
168+
// FIXME: Add tests with autodiff @_differentiable(jvp:vjp:) and
169+
// @_derivative(of:)
170+
171+
@_dynamicReplacement(for: Swift::negate())
172+
173+
mutating func myNegate() {
174+
175+
let fn: (Swift::Int, Swift::Int) -> Swift::Int =
176+
(Swift::+)
177+
178+
let magnitude: Int.Swift::Magnitude = Swift::magnitude
179+
180+
_ = (fn, magnitude)
181+
182+
if Swift::Bool.Swift::random() {
183+
184+
Swift::negate()
185+
}
186+
else {
187+
self = Swift::D(value: .Swift::min)
188+
189+
self = D.Swift::init(value: .min)
190+
}
191+
192+
self.Swift::myNegate()
193+
194+
Swift::fatalError()
195+
196+
_ = \Swift::A.magnitude
197+
_ = \A.Swift::magnitude
198+
}
199+
200+
// FIXME: Can we test @convention(witness_method:)?
201+
}
202+
203+
let mog: Never = fatalError()
204+
205+
func localVarsCantBeAccessedByModuleSelector() {
206+
let mag: Int.Swift::Magnitude = main::mag
207+
// expected-error@-1 {{use of local variable 'mag' before its declaration}}
208+
// expected-note@-2 {{'mag' declared here}}
209+
210+
let mog: Never = main::mog
211+
}
212+
213+
struct AvailableUser {
214+
@available(macOS 10.15, *) var use1: String { "foo" }
215+
216+
@main::available() var use2
217+
218+
@ModuleSelectorTestingKit::available() var use4
219+
// no-error
220+
221+
@Swift::available() var use5
222+
}
223+
224+
func builderUser2(@main::MyBuilder fn: () -> Void) {}
225+
226+
func builderUser3(@ModuleSelectorTestingKit::MyBuilder fn: () -> Void) {}
227+
228+
func builderUser4(@Swift::MyBuilder fn: () -> Void) {}
229+
230+
// Error cases
231+
232+
func decl1(
233+
p1: main::A,
234+
label p2: inout A,
235+
label p3: @escaping () -> A
236+
) {
237+
switch Optional(main::p2) {
238+
case Optional.some(let decl1i):
239+
// expected-warning@-1 {{immutable value 'decl1i' was never used; consider replacing with '_' or removing it}}
240+
break
241+
case .none:
242+
break
243+
}
244+
245+
switch Optional(main::p2) {
246+
case let Optional.some(decl1j):
247+
// expected-warning@-1 {{immutable value 'decl1j' was never used; consider replacing with '_' or removing it}}
248+
break
249+
case .none:
250+
break
251+
}
252+
253+
switch Optional(main::p2) {
254+
case let decl1k?:
255+
// expected-warning@-1 {{immutable value 'decl1k' was never used; consider replacing with '_' or removing it}}
256+
break
257+
case .none:
258+
break
259+
}
260+
}
261+
262+
typealias decl5 = main::Bool
263+
264+
func badModuleNames() {
265+
NonexistentModule::print()
266+
267+
_ = "foo".NonexistentModule::count
268+
269+
let x: NonexistentModule::MyType = NonexistentModule::MyType()
270+
// expected-error@-1 {{cannot find type 'MyType' in scope}}
271+
272+
let y: A.NonexistentModule::MyChildType = fatalError()
273+
// expected-error@-1 {{'MyChildType' is not a member type of struct 'ModuleSelectorTestingKit.A'}}
274+
}

0 commit comments

Comments
 (0)