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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
|
// RUN: %target-swift-frontend -typecheck -verify -primary-file %s %S/Inputs/struct_differentiable_other_module.swift
import _Differentiation
// Verify that a type `T` conforms to `AdditiveArithmetic`.
func assertConformsToAdditiveArithmetic<T>(_: T.Type)
where T: AdditiveArithmetic {}
struct Empty: Differentiable {}
func testEmpty() {
assertConformsToAdditiveArithmetic(Empty.TangentVector.self)
}
struct EmptyWithConcreteNonmutatingMoveAlong: Differentiable {
typealias TangentVector = Empty.TangentVector
func move(by _: TangentVector) {}
static func proof_that_i_have_nonmutating_move_along() {
let empty = Self()
empty.move(by: .init())
}
}
protocol DifferentiableWithNonmutatingMoveAlong: Differentiable {}
extension DifferentiableWithNonmutatingMoveAlong {
func move(by _: TangentVector) {}
}
struct EmptyWithInheritedNonmutatingMoveAlong: DifferentiableWithNonmutatingMoveAlong {
typealias TangentVector = Empty.TangentVector
static func proof_that_i_have_nonmutating_move_along() {
let empty = Self()
empty.move(by: .init())
}
}
class EmptyClass: Differentiable {}
func testEmptyClass() {
assertConformsToAdditiveArithmetic(EmptyClass.TangentVector.self)
}
// Test interaction with `AdditiveArithmetic` derived conformances.
// Previously, this crashed due to duplicate memberwise initializer synthesis.
struct EmptyAdditiveArithmetic: AdditiveArithmetic, Differentiable {}
// Test structs with `let` stored properties.
// Derived conformances fail because `mutating func move` requires all stored
// properties to be mutable.
struct ImmutableStoredProperties: Differentiable {
var okay: Float
// expected-warning @+1 {{stored property 'nondiff' has no derivative because 'Int' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
let nondiff: Int
// expected-warning @+1 {{synthesis of the 'Differentiable.move(by:)' requirement for 'ImmutableStoredProperties' requires all stored properties not marked with '@noDerivative' to be mutable or have a non-mutating 'move(by:)'; use 'var' instead, or add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
let diff: Float
let nonmutatingMoveAlongStruct: EmptyWithConcreteNonmutatingMoveAlong
let inheritedNonmutatingMoveAlongStruct: EmptyWithInheritedNonmutatingMoveAlong
let diffClass: EmptyClass // No error on class-bound `let` with a non-mutating `move(by:)`.
}
func testImmutableStoredProperties() {
_ = ImmutableStoredProperties.TangentVector(
okay: 1,
nonmutatingMoveAlongStruct: Empty.TangentVector(),
inheritedNonmutatingMoveAlongStruct: Empty.TangentVector(),
diffClass: EmptyClass.TangentVector())
}
struct MutableStoredPropertiesWithInitialValue: Differentiable {
var x = Float(1)
var y = Double(1)
}
// Test struct with both an empty constructor and memberwise initializer.
struct AllMixedStoredPropertiesHaveInitialValue: Differentiable {
// expected-warning @+1 {{synthesis of the 'Differentiable.move(by:)' requirement for 'AllMixedStoredPropertiesHaveInitialValue' requires all stored properties not marked with '@noDerivative' to be mutable or have a non-mutating 'move(by:)'; use 'var' instead, or add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
let x = Float(1)
var y = Float(1)
// Memberwise initializer should be `init(y:)` since `x` is immutable.
static func testMemberwiseInitializer() {
_ = AllMixedStoredPropertiesHaveInitialValue(y: 1)
}
}
struct HasCustomConstructor: Differentiable {
var x = Float(1)
var y = Float(1)
// Custom constructor should not affect synthesis.
init(x: Float, y: Float, z: Bool) {}
}
struct Simple: AdditiveArithmetic, Differentiable {
var w: Float
var b: Float
}
func testSimple() {
var simple = Simple(w: 1, b: 1)
simple.move(by: simple)
}
// Test type with mixed members.
struct Mixed: AdditiveArithmetic, Differentiable {
var simple: Simple
var float: Float
}
func testMixed(_ simple: Simple) {
var mixed = Mixed(simple: simple, float: 1)
mixed.move(by: mixed)
}
// Test type with manual definition of vector space types to `Self`.
struct VectorSpacesEqualSelf: AdditiveArithmetic, Differentiable {
var w: Float
var b: Float
typealias TangentVector = VectorSpacesEqualSelf
}
// Test generic type with vector space types to `Self`.
struct GenericVectorSpacesEqualSelf<T>: AdditiveArithmetic, Differentiable
where T: Differentiable, T == T.TangentVector {
var w: T
var b: T
}
func testGenericVectorSpacesEqualSelf() {
var genericSame = GenericVectorSpacesEqualSelf<Double>(w: 1, b: 1)
genericSame.move(by: genericSame)
}
// Test nested type.
struct Nested: AdditiveArithmetic, Differentiable {
var simple: Simple
var mixed: Mixed
var generic: GenericVectorSpacesEqualSelf<Double>
}
func testNested(
_ simple: Simple, _ mixed: Mixed,
_ genericSame: GenericVectorSpacesEqualSelf<Double>
) {
var nested = Nested(simple: simple, mixed: mixed, generic: genericSame)
nested.move(by: nested)
}
// Test type that does not conform to `AdditiveArithmetic` but whose members do.
// Thus, `Self` cannot be used as `TangentVector` or `TangentVector`.
// Vector space structs types must be synthesized.
// Note: it would be nice to emit a warning if conforming `Self` to
// `AdditiveArithmetic` is possible.
struct AllMembersAdditiveArithmetic: Differentiable {
var w: Float
var b: Float
}
// Test type whose properties are not all differentiable.
struct DifferentiableSubset: Differentiable {
var w: Float
var b: Float
@noDerivative var flag: Bool
@noDerivative let technicallyDifferentiable: Float = .pi
}
func testDifferentiableSubset() {
_ = DifferentiableSubset.TangentVector(w: 1, b: 1)
_ = DifferentiableSubset.TangentVector(w: 1, b: 1)
}
// Test nested type whose properties are not all differentiable.
struct NestedDifferentiableSubset: Differentiable {
var x: DifferentiableSubset
var mixed: Mixed
@noDerivative var technicallyDifferentiable: Float
}
// Test type that uses synthesized vector space types but provides custom
// method.
struct HasCustomMethod: Differentiable {
var simple: Simple
var mixed: Mixed
var generic: GenericVectorSpacesEqualSelf<Double>
mutating func move(by offset: TangentVector) {
print("Hello world")
simple.move(by: offset.simple)
mixed.move(by: offset.mixed)
generic.move(by: offset.generic)
}
}
// Test type with user-defined memberwise initializer.
struct TF_25: Differentiable {
public var bar: Float
public init(bar: Float) {
self.bar = bar
}
}
// Test user-defined memberwise initializer.
struct TF_25_Generic<T: Differentiable>: Differentiable {
public var bar: T
public init(bar: T) {
self.bar = bar
}
}
// Test initializer that is not a memberwise initializer because of stored property name vs parameter label mismatch.
struct HasCustomNonMemberwiseInitializer<T: Differentiable>: Differentiable {
var value: T
init(randomLabel value: T) { self.value = value }
}
// Test type with generic environment.
struct HasGenericEnvironment<Scalar: FloatingPoint & Differentiable>: Differentiable
{
var x: Float
}
// Test type with generic members that conform to `Differentiable`.
struct GenericSynthesizeAllStructs<T: Differentiable>: Differentiable {
var w: T
var b: T
}
// Test type in generic context.
struct A<T: Differentiable> {
struct B<U: Differentiable, V>: Differentiable {
struct InGenericContext: Differentiable {
@noDerivative var a: A
var b: B
var t: T
var u: U
}
}
}
// Test extension.
struct Extended {
var x: Float
}
extension Extended: Differentiable {}
// Test extension of generic type.
struct GenericExtended<T> {
var x: T
}
extension GenericExtended: Differentiable where T: Differentiable {}
// Test constrained extension of generic type.
struct GenericConstrained<T> {
var x: T
}
extension GenericConstrained: Differentiable
where T: Differentiable {}
struct TF_260<T: Differentiable>: Differentiable, AdditiveArithmetic {
var x: T.TangentVector
}
// TF-269: Test crash when differentiation properties have no getter.
// Related to access levels and associated type inference.
public protocol TF_269_Layer: Differentiable {
associatedtype Input: Differentiable
associatedtype Output: Differentiable
func applied(to input: Input) -> Output
}
public struct TF_269: TF_269_Layer {
public var filter: Float
public typealias Activation = @differentiable(reverse) (Output) -> Output
@noDerivative public let activation: Activation
public func applied(to input: Float) -> Float {
return input
}
}
// Test errors.
// Test manually customizing vector space types.
// These should fail. Synthesis is semantically unsupported if vector space
// types are customized.
// expected-error @+1 {{type 'VectorSpaceTypeAlias' does not conform to protocol 'Differentiable'}}
struct VectorSpaceTypeAlias: AdditiveArithmetic, Differentiable {
var w: Float
var b: Float
typealias TangentVector = Simple
}
// expected-error @+1 {{type 'VectorSpaceCustomStruct' does not conform to protocol 'Differentiable'}}
struct VectorSpaceCustomStruct: AdditiveArithmetic, Differentiable {
var w: Float
var b: Float
struct TangentVector: AdditiveArithmetic, Differentiable {
var w: Float.TangentVector
var b: Float.TangentVector
typealias TangentVector = VectorSpaceCustomStruct.TangentVector
}
}
struct StaticNoDerivative: Differentiable {
@noDerivative static var s: Bool = true
}
struct StaticMembersShouldNotAffectAnything: AdditiveArithmetic, Differentiable
{
static var x: Bool = true
static var y: Bool = false
}
struct ImplicitNoDerivative: Differentiable {
var a: Float
var b: Bool // expected-warning {{stored property 'b' has no derivative because 'Bool' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
}
struct ImplicitNoDerivativeWithSeparateTangent: Differentiable {
var x: DifferentiableSubset
var b: Bool // expected-warning {{stored property 'b' has no derivative because 'Bool' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}} {{3-3=@noDerivative }}
}
// TF-1018: verify that `@noDerivative` warnings are always silenceable, even
// when the `Differentiable` conformance context is not the nominal type
// declaration.
struct ExtensionDifferentiableNoDerivative<T> {
// expected-warning @+2 {{stored property 'x' has no derivative because 'T' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
// expected-warning @+1 {{stored property 'y' has no derivative because 'T' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
var x, y: T
// expected-warning @+1 {{stored property 'nondiff' has no derivative because 'Bool' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
var nondiff: Bool
}
extension ExtensionDifferentiableNoDerivative: Differentiable {}
struct ExtensionDifferentiableNoDerivativeFixed<T> {
@noDerivative var x, y: T
@noDerivative var nondiff: Bool
}
extension ExtensionDifferentiableNoDerivativeFixed: Differentiable {}
struct ConditionalDifferentiableNoDerivative<T> {
var x, y: T
// expected-warning @+1 {{stored property 'nondiff' has no derivative because 'Bool' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
var nondiff: Bool
}
extension ConditionalDifferentiableNoDerivative: Differentiable
where T: Differentiable {}
struct ConditionalDifferentiableNoDerivativeFixed<T> {
var x, y: T
@noDerivative var nondiff: Bool
}
extension ConditionalDifferentiableNoDerivativeFixed: Differentiable
where T: Differentiable {}
// TF-265: Test invalid initializer (that uses a non-existent type).
struct InvalidInitializer: Differentiable {
init(filterShape: (Int, Int, Int, Int), blah: NonExistentType) {} // expected-error {{cannot find type 'NonExistentType' in scope}}
}
// Test memberwise initializer synthesis.
struct NoMemberwiseInitializerExtended<T> {
var value: T
init(_ value: T) {
self.value = value
}
}
extension NoMemberwiseInitializerExtended: Equatable, AdditiveArithmetic
where T: AdditiveArithmetic {}
extension NoMemberwiseInitializerExtended: Differentiable
where T: Differentiable & AdditiveArithmetic {}
// https://github.com/apple/swift/issues/55238
// Test interaction with `@differentiable` and `@derivative` type-checking.
struct S_55238: Differentiable {
var x: Float
@differentiable(reverse)
func method() -> Float { x }
@derivative(of: method)
func vjpMethod() -> (value: Float, pullback: (Float) -> TangentVector) { fatalError() }
// Test usage of synthesized `TangentVector` type.
// This should not produce an error: "reference to invalid associated type 'TangentVector'".
mutating func move(by offset: TangentVector) {}
}
// Test property wrappers.
@propertyWrapper
struct ImmutableWrapper<Value> {
private var value: Value
var wrappedValue: Value { value }
}
@propertyWrapper
struct Wrapper<Value> {
var wrappedValue: Value
}
@propertyWrapper
class ClassWrapper<Value> {
var wrappedValue: Value
init(wrappedValue: Value) { self.wrappedValue = wrappedValue }
}
struct Generic<T> {}
extension Generic: Differentiable where T: Differentiable {}
struct WrappedProperties: Differentiable {
// expected-warning @+1 {{synthesis of the 'Differentiable.move(by:)' requirement for 'WrappedProperties' requires 'wrappedValue' in property wrapper 'ImmutableWrapper' to be mutable or have a non-mutating 'move(by:)'; add an explicit '@noDerivative' attribute}}
@ImmutableWrapper var immutableInt: Generic<Int>
// expected-warning @+1 {{stored property 'mutableInt' has no derivative because 'Generic<Int>' does not conform to 'Differentiable'; add an explicit '@noDerivative' attribute}}
@Wrapper var mutableInt: Generic<Int>
@Wrapper var float: Generic<Float>
@ClassWrapper var float2: Generic<Float>
// https://github.com/apple/swift/issues/55517
// Test `@differentiable` wrapped property.
@differentiable(reverse) @Wrapper var float3: Generic<Float>
@noDerivative @ImmutableWrapper var nondiff: Generic<Int>
static func testTangentMemberwiseInitializer() {
_ = TangentVector(float: .init(), float2: .init(), float3: .init())
}
}
// Verify that cross-file derived conformances are disallowed.
extension OtherFileNonconforming: Differentiable {}
// expected-error @-1 {{extension outside of file declaring struct 'OtherFileNonconforming' prevents automatic synthesis of 'move(by:)' for protocol 'Differentiable'}}
extension GenericOtherFileNonconforming: Differentiable {}
// expected-error @-1 {{extension outside of file declaring generic struct 'GenericOtherFileNonconforming' prevents automatic synthesis of 'move(by:)' for protocol 'Differentiable'}}
|