Skip to content

Commit

Permalink
fix: fix CallWithMetadata inside DestructuredArgs with `create_bi…
Browse files Browse the repository at this point in the history
…ndings = false`
  • Loading branch information
AayushSabharwal committed Feb 4, 2025
1 parent ab62dbe commit 7ca5fb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,16 @@ CallWithMetadata(f) = CallWithMetadata(f, nothing)

SymbolicIndexingInterface.symbolic_type(::Type{<:CallWithMetadata}) = ScalarSymbolic()

# HACK:
# A `DestructuredArgs` with `create_bindings = false` doesn't create a `Let` block, and
# instead adds the assignments to the rewrites dictionary. This is problematic, because
# if the `DestructuredArgs` contains a `CallWithMetadata` the key in the `Dict` will be
# a `CallWithMetadata` which won't match against the operation of the called symbolic.
# This is the _only_ hook we have and relies on the `DestructuredArgs` being converted
# into a list of `Assignment`s before being addded to the `Dict` inside `toexpr(::Let, st)`.

Check warning on line 300 in src/variable.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"addded" should be "added".
# The callable symbolic is unwrapped so it matches the operation of the called version.
SymbolicUtils.Code.Assignment(f::CallWithMetadata, x) = SymbolicUtils.Code.Assignment(f.f, x)

function Base.show(io::IO, c::CallWithMetadata)
show(io, c.f)
print(io, "")
Expand Down
6 changes: 6 additions & 0 deletions test/build_function.jl
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,9 @@ end
@test res isa Let
@test !isempty(res.pairs)
end

@testset "`CallWithMetadata` in `DestructuredArgs` with `create_bindings = false`" begin
@variables x f(..)
fn = build_function(f(x), DestructuredArgs([f]; create_bindings = false), x; expression = Val{false})
@test fn([isodd], 3)
end

0 comments on commit 7ca5fb5

Please sign in to comment.