Skip to content

Commit b5d9339

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 1f092d6 commit b5d9339

File tree

2 files changed

+305
-0
lines changed

2 files changed

+305
-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+
}

test/NameLookup/module_selector.swift

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

0 commit comments

Comments
 (0)