Skip to content

[Autodiff] Derivative Registration for the Get and Set Accessors #32614

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

Merged
merged 19 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3668,6 +3668,8 @@ static AbstractFunctionDecl *findAbstractFunctionDecl(
missingAccessor = true;
} else
candidate = asd->getAccessor(AccessorKind::Get);
} else if (accessorKind != None) {
missingAccessor = true;
}
if (!candidate) {
notFunction = true;
Expand All @@ -3688,8 +3690,9 @@ static AbstractFunctionDecl *findAbstractFunctionDecl(
}
resolvedCandidate = candidate;
}

// If function declaration was resolved, return it.
if (resolvedCandidate)
if (resolvedCandidate && !missingAccessor)
return resolvedCandidate;

// Otherwise, emit the appropriate diagnostic and return nullptr.
Expand Down
49 changes: 48 additions & 1 deletion test/AutoDiff/Sema/derivative_attr_type_checking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ extension Struct where T: Differentiable & AdditiveArithmetic {
return (x, { _ in .zero })
}


// expected-error @+1 {{'subscript' does not have a 'set' accessor}}
@derivative(of: subscript.set)
mutating func vjpSubscriptSetter(_ newValue: Float) -> (
value: (), pullback: (inout TangentVector) -> Float
Expand Down Expand Up @@ -1077,3 +1077,50 @@ func internal_original_fileprivate_derivative(_ x: Float) -> Float { x }
fileprivate func _internal_original_fileprivate_derivative(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

// Test invalid reference to an accessor of a non-storage declaration.

func function(_ x: Float) -> Float {
x
}

// expected-error @+1 {{'function' does not have a 'get' accessor}}
@derivative(of: function(_:).get)
func vjpFunction(_ x: Float) -> (value: Float, pullback: (Float) -> Float) {
fatalError()
}

// Test ambiguity that exists when Type function name is the same
// as an accessor label.

extension Float {
// Original function name conflicts with an accessor name ("set").
func set() -> Float {
self
}

// Original function name does not conflict with an accessor name.
func method() -> Float {
self
}

// Test ambiguous parse.
// Expected:
// - Base type: `Float`
// - Declaration name: `set`
// - Accessor kind: <none>
// Actual:
// - Base type: <none>
// - Declaration name: `Float`
// - Accessor kind: `set`
// expected-error @+1 {{cannot find 'Float' in scope}}
@derivative(of: Float.set)
func jvpSet() -> (value: Float, differential: (Float) -> Float) {
fatalError()
}

@derivative(of: Float.method)
func jvpMethod() -> (value: Float, differential: (Float) -> Float) {
fatalError()
}
}