Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoDiff] Guard let optional unwrapping is not differentiable #79051

Open
loonatick-src opened this issue Jan 30, 2025 · 2 comments
Open

[AutoDiff] Guard let optional unwrapping is not differentiable #79051

loonatick-src opened this issue Jan 30, 2025 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@loonatick-src
Copy link

loonatick-src commented Jan 30, 2025

Description

Closely related to #75280: the reproducer provided there now results in a compilation error instead of crash in Swift 6.0.3:

import _Differentiation
@differentiable(reverse) func a<F, A>(_ f: Optional<F>, c: @differentiable(reverse) (F) -> A) -> Optional<A> where F: Differentiable, A: Differentiable
{
    guard let f else {return nil}
    return c(f)
}
<source>:2:2: error: function is not differentiable
1 | import _Differentiation
2 | @differentiable(reverse) func a<F, A>(_ f: Optional<F>, c: @differentiable(reverse) (F) -> A) -> Optional<A> where F: Differentiable, A: Differentiable
  |  |                            `- note: when differentiating this function definition
  |  `- error: function is not differentiable
3 | {
4 |     guard let f else {return nil}
  |                              `- note: expression is not differentiable
5 |     return c(f)
6 | }
Compiler returned: 1

But, the following semantically equivalent function compiles successfully.

import _Differentiation
@differentiable(reverse) func a<F, A>(_ f: Optional<F>, c: @differentiable(reverse) (F) -> A) -> Optional<A> where F: Differentiable, A: Differentiable
{
    guard let f else {
        let a: Optional<A> = nil
        return a
    }
    return c(f)
}

Compiler explorer link

Reproduction

// main.swift
import _Differentiation
@differentiable(reverse) func a<F, A>(_ f: Optional<F>, c: @differentiable(reverse) (F) -> A) -> Optional<A> where F: Differentiable, A: Differentiable
{
    guard let f else {return nil}
    return c(f)
}

Using Swift version 6.0.3, run swiftc main.swift.

Expected behavior

Both should compile successfully

Environment

Tested on the following platforms.

Swift version 6.0.3 (swift-6.0.3-RELEASE)
Target: x86_64-unknown-linux-gnu
swift-driver version: 1.115.1 Apple Swift version 6.0.3 (swiftlang-6.0.3.1.10 clang-1600.0.30.1)
Target: arm64-apple-macosx14.0

Additional information

No response

@loonatick-src loonatick-src added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jan 30, 2025
@loonatick-src
Copy link
Author

@JaapWijnen

@JaapWijnen
Copy link
Contributor

tagging @asl

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants