Skip to content

Commit 52b771d

Browse files
committed
[AutoDiff] Add SR-12526 negative test.
Add negative test for SR-12526: `@derivative` attribute cross-module deserialization crash.
1 parent 0c1d4b5 commit 52b771d

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
public struct Struct {
2+
public func method(_ x: Float) -> Float { x }
3+
4+
public static func +(_ lhs: Self, rhs: Self) -> Self {
5+
lhs
6+
}
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import _Differentiation
2+
import a
3+
4+
extension Struct: Differentiable {
5+
public struct TangentVector: Differentiable & AdditiveArithmetic {}
6+
public mutating func move(along _: TangentVector) {}
7+
8+
@usableFromInline
9+
@derivative(of: method, wrt: x)
10+
func vjpMethod(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
11+
(x, { $0 })
12+
}
13+
14+
@usableFromInline
15+
@derivative(of: +)
16+
static func vjpAdd(_ lhs: Self, rhs: Self) -> (
17+
value: Self, pullback: (TangentVector) -> (TangentVector, TangentVector)
18+
) {
19+
(lhs + rhs, { v in (v, v) })
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module -primary-file %S/Inputs/a.swift -emit-module-path %t/a.swiftmodule
3+
// RUN: %target-swift-frontend -emit-module -primary-file %S/Inputs/b.swift -emit-module-path %t/b.swiftmodule -I %t
4+
// RUN: not --crash %target-swift-frontend-typecheck -verify -I %t %s
5+
6+
// SR-12526: Fix cross-module deserialization crash involving `@derivative` attribute.
7+
8+
import a
9+
import b
10+
11+
func foo(_ s: Struct) {
12+
_ = Struct()
13+
_ = s.method(1)
14+
}

0 commit comments

Comments
 (0)