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-13150] Unhelpful compiler error message: ambiguous reference in @derivative attribute #55582

Open
swift-ci opened this issue Jul 2, 2020 · 0 comments
Assignees

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Jul 2, 2020

Previous ID SR-13150
Radar None
Original Reporter saeta (JIRA User)
Type Sub-task
Additional Detail from JIRA
Votes 0
Component/s
Labels Sub-task
Assignee @dan-zheng
Priority Medium

md5: d1c22ceb1cbc545cbe3b8f0aeb6f7c42

Parent-Task:

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

Issue Description:

When the compiler encounters an ambiguous reference, the compiler should print out where the ambiguity is coming from to help guide the user to understand what's going wrong.

File:

import TensorFlow

protocol Foo {
  func foo(_ x: Int) -> Self
}

protocol Bar: Foo, Differentiable {
  @differentiable(wrt: self)
  func foo(_ x: Int) -> Self
}

extension KeyPathIterable {
  func foo(_ x: Int) -> Self { self }
}

// This allows KeyPathIterable things to conform to Bar easily.
extension KeyPathIterable where Self: Differentiable {
  // Note: thanks to retroactive conformances, you don't even need this,
  // but if you do accidentally (or intentionally) write it, we should give
  // a nicer compiler error message.
  @differentiable(wrt: self)
  func foo(_ x: Int) -> Self { self }


  @derivative(of: foo(_:), wrt: self)
  func _vjpFoo(_ x: Int) -> (value: Self, pullback: (TangentVector) -> TangentVector) {
    fatalError("Unimplemented")
  }
}

Compiler output:

saeta@saeta:~/tmp/repros$ swiftc  foo.swift 
foo.swift:25:19: error: ambiguous reference to 'foo' in '@derivative' attribute
  @derivative(of: foo(_:), wrt: self)
                  ^

Desired output (something like the following which indicates where the ambiguities are coming from):

foo.swift:25:19: error: ambiguous reference to 'foo' in '@derivative' attribute
  @derivative(of: foo(_:), wrt: self)
                  ^
foo.swift:13:8: note: first potential reference here:
  func foo(_ x: Int) -> Self { self }
       ^
foo.swift:22:8: note: second potential reference here:
  func foo(_ x: Int) -> Self { self }
       ^

Note: as the comment says, the user don't actually need to write two implementations of the foo function, as retroactive differentiability is exactly what you want. Context: this came up while working on eaplatanios/swift-rl#6

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant