|
| 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