Skip to content

Use a generated self-argument name in @grad_from_chainrules - #336

Closed
mwhincup wants to merge 1 commit into
JuliaDiff:masterfrom
mwhincup:fix/grad-from-chainrules-func-name
Closed

mwhincup wants to merge 1 commit into
JuliaDiff:masterfrom
mwhincup:fix/grad-from-chainrules-func-name

Conversation

@mwhincup

Copy link
Copy Markdown

Fixes #334.

Since 1.18.8 (#328) the methods generated by @grad_from_chainrules name their self-argument func:

(func::ReverseDiff._selftype(f))(args...; kwargs...) where {...} = ReverseDiff.track(func, ...)

Macro hygiene does not rename the self-argument of a callable-object method, so a rule with an argument also named func produced a signature with two arguments called func and failed with syntax: function argument name not unique: "func". This breaks the precompilation of ImplicitAD.jl, which declares ReverseDiff.@grad_from_chainrules _provide_rule(func, x::ReverseDiff.TrackedArray, p, mode, jacobian, jvp, vjp).

Change: @grad_from_chainrules binds the self-argument to a name from gensym(:func) and uses it in both generated methods, so no argument name can collide with it. Only the callable method needs it (hygiene does rename the func argument of track), but one name for both keeps them consistent.

@grad is not changed: its track(func::..., ...) method is an ordinary method, whose argument hygiene renames, and an argument named func is already covered by its tests (f15 in MacrosTests.jl). On 1.18.8 we tried 13 argument names (f, func, args, kwargs, tp, output_value, back, output, closure, cls_args, instruction, input, self) with both macros. Only func with @grad_from_chainrules failed.

Test: a testset "argument named func" next to "argument names" in test/ChainRulesTests.jl, with a rule f_func(func, x). On master the file fails to load with the error above; with the change it passes.

Tests run: the full test suite with this change passes: 12792 passed, 9 broken (the existing @test_broken in derivatives/ArrayFunctionTests.jl), 0 failed, on Julia 1.12.5, Linux x86_64 (Pkg.test(test_args=["--jobs=4"])).

This PR does not conflict with #332 (git merge-tree is clean).

…iff#334)

Since 1.18.8 the methods generated by `@grad_from_chainrules` name their
self-argument `func`. Macro hygiene does not rename the self-argument of
a callable-object method, so a rule with an argument that is also named
`func`, such as ImplicitAD's `_provide_rule(func, x, ...)`, failed with
"function argument name not unique". The self-argument now has a name
from `gensym`.

Fixes JuliaDiff#334

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@devmotion

Copy link
Copy Markdown
Member

Thanks, but I'll close this in favour of #335.

@devmotion devmotion closed this Nov 13, 2026
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

Successfully merging this pull request may close these issues.

1.18.8: @grad_from_chainrules fails when a rule argument is named func ("function argument name not unique"), breaks ImplicitAD

2 participants