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

[SR-13152] Add diagnostic for derivative registration instance/static method mismatch #54274

Closed
dan-zheng opened this issue Nov 27, 2019 · 2 comments
Assignees
Labels
AutoDiff compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation

Comments

@dan-zheng
Copy link
Collaborator

Previous ID SR-13152
Radar None
Original Reporter @dan-zheng
Type Sub-task
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Sub-task, AutoDiff, DiagnosticsQoI
Assignee @vguerra
Priority Medium

md5: 65c5d8981325b3779edb5f7276072344

Parent-Task:

  • SR-13149 @derivative and @transpose type-checking diagnostic improvements

is duplicated by:

Issue Description:

Example using @differentiable attribute:

struct Foo: Differentiable {
  @differentiable(vjp: vjpInit)
  init(_ x: Float) {}
}
extension Foo {
  func vjpInit(_ x: Float) -> (value: Self, pullback: (TangentVector) -> Float) {
    fatalError()
  }
}
tf-1015.swift:2:24: error: 'vjpInit' does not have expected type '(Foo.Type) -> (Float) -> (Foo, (Foo.TangentVector) -> Float.TangentVector)' (aka '(Foo.Type) -> (Float) -> (Foo, (Foo.TangentVector) -> Float)')
  @differentiable(vjp: vjpInit)
                       ^

Example using @derivative attribute:

struct Foo: Differentiable {
  init(_ x: Float) {}
}
extension Foo {
  @derivative(of: init)
  func vjpInit(_ x: Float) -> (value: Self, pullback: (TangentVector) -> Float) {
    fatalError()
  }
}
tf-1015.swift:5:19: error: could not find function 'init' with expected type '(Foo) -> (Float) -> Foo'
  @derivative(of: init)
                  ^

These diagnostics are technically correct, but aren't very understandable or actionable.

We could detect instance/static method mismatches for derivative registration and produce a more specific diagnostic ("did you mean to make 'vjpInit' a static function?").

Example:

tf-1015.swift:5:19: error: could not find function 'init' with expected type '(Foo) -> (Float) -> Foo'
  @derivative(of: init)
                  ^
tf-1015.swift:6:19: note: did you mean to make 'vjpInit' a static function?
  func vjpInit(_ x: Float) -> (value: Self, pullback: (TangentVector) -> Float) {
  ^
  static
@vguerra
Copy link
Contributor

vguerra commented Feb 24, 2021

hello @dan-zheng, @rxwei!

I tried to improve the diagnostics for this case as I have encountered it several times already. I pushed a draft here: #36128 .. unfortunately I dont have the rights to put reviewers ... would you mind having a look at it please?

Thanks!

@vguerra
Copy link
Contributor

vguerra commented Mar 3, 2021

PR has been merged.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
AutoDiff compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation
Projects
None yet
Development

No branches or pull requests

2 participants