Skip to content

Error compiling @differentiable computed property (within a struct) if defined in a different file from its use site. #63169

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

Closed
fibrechannelscsi opened this issue Jan 23, 2023 · 1 comment · Fixed by #63988
Assignees
Labels
accessors Feature → declarations: Variable (property) accessors AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself multiple files Flag: An issue whose reproduction requires multiple files type checker Area → compiler: Semantic analysis unexpected error Bug: Unexpected error

Comments

@fibrechannelscsi
Copy link
Contributor

fibrechannelscsi commented Jan 23, 2023

Description
If we have a struct containing a @differentiable computed property (with explicit get/set directives) in the same file that it is used, then the program compiles successfully. However, If this struct is in a different file, the compilation fails. The error message is as follows:

reproducer.swift:12:59: error: expression is not differentiable
var localStruct = TestStruct(); localStruct.g = x; return localStruct.g
                                              ^

reproducer.swift:12:59: note: cannot differentiate functions that have not been marked '@differentiable' and that are defined in other files
var localStruct = TestStruct(); localStruct.g = x; return localStruct.g
                                              ^

Steps to reproduce
This reproducer requires two Swift files, in the same directory (for example, insides Sources/ProjectName).

File 1:

import _Differentiation
import Foundation
public struct MyReproducer {
    func compute() {
        @differentiable(reverse)
        func assignF(_ x: Double) -> Double {
            var localStruct = TestStruct(); localStruct.f(x); return localStruct.g
        }
        
        @differentiable(reverse)
        func assignG(_ x: Double) -> Double {
            var localStruct = TestStruct(); localStruct.g = x; return localStruct.g
        }
    }
}

File 2:

public struct TestStruct: Differentiable {
    private var _g: Double = 0
    @differentiable(reverse)
    var g: Double {
        get {self._g}
        set(newValue) {self._g = 3.0*newValue*newValue}
    }
    @differentiable(reverse)
    mutating func f(_ x: Double) {
        self._g = 3.0*x*x
    }
}

Place the above code into two separate files, and compile in Debug mode.

Note: Placing TestStruct inside File 1, albeit outside MyReproducer, will cause the compilation to succeed.

Expected behavior
The project containing the two files should compile successfully, even when TestStruct and MyReproducer are in different files.

  • Swift compiler version info: Fails with (seemingly) everything from 2022-02-03a to 2023-01-19a.
  • Xcode version info: 13.3 (13E113)
  • Deployment target: iOS 12.3.1, ARM64.

Additional context
Similar to: #55170

@fibrechannelscsi fibrechannelscsi added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Jan 23, 2023
@AnthonyLatsis AnthonyLatsis added compiler The Swift compiler itself type checker Area → compiler: Semantic analysis multiple files Flag: An issue whose reproduction requires multiple files AutoDiff accessors Feature → declarations: Variable (property) accessors and removed triage needed This issue needs more specific labels labels Jan 23, 2023
@asl
Copy link
Contributor

asl commented Feb 27, 2023

Apparently, we do not resolve differential attributes for accessors properly. We explicitly resolve only for getters. There is old FIXME in resolveDifferentiableAttrOriginalFunction:

    // TODO(TF-129): Forward `@differentiable` attributes to setters after
    // differentiation supports inout parameters.

@asl asl self-assigned this Feb 27, 2023
asl added a commit to asl/swift that referenced this issue Mar 1, 2023
@asl asl closed this as completed in #63988 Mar 6, 2023
asl added a commit that referenced this issue Mar 6, 2023
@AnthonyLatsis AnthonyLatsis added the unexpected error Bug: Unexpected error label Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accessors Feature → declarations: Variable (property) accessors AutoDiff bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself multiple files Flag: An issue whose reproduction requires multiple files type checker Area → compiler: Semantic analysis unexpected error Bug: Unexpected error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants