Skip to content

Commit

Permalink
Translate new construction of NamedTuple (#156)
Browse files Browse the repository at this point in the history
* translate new construction of NamedTuple

* Update src/tapedfunction.jl

* Update Project.toml

Co-authored-by: Hong Ge <[email protected]>
  • Loading branch information
KDr2 and yebai authored Aug 19, 2022
1 parent 22d0d46 commit bafaa1f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ uuid = "6f1fad26-d15e-5dc8-ae53-837a1d7b8c9f"
license = "MIT"
desc = "Tape based task copying in Turing"
repo = "https://github.com/TuringLang/Libtask.jl.git"
version = "0.8.1"
version = "0.8.2"

[deps]
FunctionWrappers = "069b7b12-0de2-55c6-9aab-29f3d0a68a2e"
Expand Down
11 changes: 10 additions & 1 deletion src/tapedfunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,15 @@ function translate!!(var::IRVar, line::Core.SlotNumber,
return Instruction(func, input, output)
end

function translate!!(var::IRVar, line::NTuple{N, Symbol},
bindings::Bindings, isconst::Bool, ir) where {N}
# for syntax (; x, y, z), see Turing.jl#1873
func = identity
input = (bind_var!(line, bindings, ir),)
output = bind_var!(var, bindings, ir)
return Instruction(func, input, output)
end

function translate!!(var::IRVar, line::Core.TypedSlot,
bindings::Bindings, isconst::Bool, ir)
input_box = bind_var!(Core.SlotNumber(line.id), bindings, ir)
Expand Down Expand Up @@ -437,7 +446,7 @@ function translate!!(var::IRVar, line::Expr,
end
end

function translate!!(var, line, bindings, ir)
function translate!!(var, line, bindings, isconst, ir)
@error "Unknown IR code: " typeof(var) var typeof(line) line
throw(ErrorException("Unknown IR code"))
end
Expand Down
11 changes: 11 additions & 0 deletions test/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@
ttask = TapedTask(f, 2)
@test consume(ttask) == 1
end

@testset "Issue-Turing-1873, NamedTuple syntax" begin
function g(x, y)
c = x + y
return (; c, x, y)
end

tf = Libtask.TapedFunction(g, 1, 2)
r = tf(1, 2)
@test r == (c=3, x=1, y=2)
end
end

2 comments on commit bafaa1f

@yebai
Copy link
Member

@yebai yebai commented on bafaa1f Aug 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/66548

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.8.2 -m "<description of version>" bafaa1f553d5b2f09773690145b730b47497f164
git push origin v0.8.2

Please sign in to comment.