Skip to content

[SR-12793] Bad interaction between @differentiable type-checking and Differentiable derived conformances #55238

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
dan-zheng opened this issue May 12, 2020 · 1 comment
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself

Comments

@dan-zheng
Copy link
Contributor

Previous ID SR-12793
Radar None
Original Reporter @dan-zheng
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @dan-zheng
Priority Medium

md5: 2982851f9d316dc7f87ad75961ebafac

Issue Description:

import _Differentiation

struct Struct: Differentiable {
  var x: Float

  @differentiable // either this
  func method() -> Float { x }

  @derivative(of: method) // or this is sufficient to trigger error
  func vjpMethod() -> (value: Float, pullback: (Float) -> TangentVector) { fatalError() }

  mutating func move(along direction: TangentVector) {}
}
$ swiftc test.swift
test.swift:12:39: error: reference to invalid associated type 'TangentVector' of type 'Struct'
  mutating func move(along direction: TangentVector) {}
                                      ^

This occurs because @differentiable and @derivative attribute type-checking currently tries to resolve Differentiable.TangentVector type witnesses, which is ad-hoc logic that messes up Differentiable derived conformances.

  // Try to get the `TangentVector` type witness, in case the conformance has
  // not been fully checked.
  Type tanType = conf.getTypeWitnessByName(type, ctx.Id_TangentVector);
  if (tanType.isNull() || tanType->hasError())
    return ProtocolConformanceRef();
  return conf;

Attribute type-checking should be changed to drop TangentVector type witness resolution. This should simplify code and make things less hacky.

@dan-zheng
Copy link
Contributor Author

Fixed in #31727

The problematic code (forcibly resolving Differentiable.TangentVector type witnesses) has been removed. Derivative function type calculation now returns llvm::Expected, checking type witnesses on-demand.

@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
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler itself
Projects
None yet
Development

No branches or pull requests

1 participant