Repository navigation
Conversation
Julia's hygiene does not rename a definition's name or self argument if an escaped argument of that definition has the same name. So `@grad_from_chainrules k(func, x::TrackedArray)` failed with "function argument name not unique", and `@grad function k(closure, x)` with "cannot add method to function argument closure". Use a gensym for these two names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #335 +/- ##
=======================================
Coverage 94.77% 94.78%
=======================================
Files 19 19
Lines 1703 1705 +2
=======================================
+ Hits 1614 1616 +2
Misses 89 89 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
devmotion
force-pushed
the
dmw/self-name-collision
branch
from
November 13, 2026 14:19
202e204 to
ee0b544
Compare
Member
Author
|
Reported the underlying hygiene bug upstream: JuliaLang/julia#64945. |
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since #328,
@grad_from_chainrules k(func, x::TrackedArray)fails withsyntax: function argument name not unique: "func", which breaks ImplicitAD and its dependents (#334).The self argument
funcis not escaped, so macro hygiene should rename it. It doesn't, because of a quirk in Julia's hygiene (src/macroexpand.scm):safe-llist-keyword-args: "count escaped argument names as 'keywords' to prevent renaming").(func::T)(…)or a localfunction closure(…), is resolved with that same mapping. So an internal name there that coincides with an escaped argument name stays unrenamed as well, and collides.Minimal example without ReverseDiff:
Trying every internal name as an argument name in both macros found one more case: an untyped argument named
closurein@gradfails withcannot add method to function argument closure. The existing test missed it because typed arguments are escaped as a whole::expression, which hygiene does not pin. Contrary to the guess in #334,funcworks in@grad. All other internal names (args,kwargs,tp,back,output, …) work in both macros.Hygiene alone cannot fix this: each macro has to put one of its own names in such a position. Dropping the self name and calling
$fin the body would let an argument named like the function shadow it again, which #328 avoided on purpose. So this PR uses agensymfor exactly these two names: the self argument in@grad_from_chainrulesand the local function in@grad.Regression tests: an untyped argument
funcin@grad_from_chainrules(as in ImplicitAD), and untypedclosureandfuncin@grad.Fixes #334.
🤖 Generated with Claude Code