Skip to content

Unused arg in TapedTask #148

Closed
Closed
@FredericWantiez

Description

@FredericWantiez

There is a weird edge case when defining a TapedTask with arguments but not using them in the body of the function. For example:

function f(x)
    produce(1)
end

trace = TapedTask(f, 2)
consume(trace)

yields an empty sequence error:

ERROR: LoadError: ArgumentError: reducing over an empty collection is not allowed
Stacktrace:
  [1] _empty_reduce_error()
    @ Base ./reduce.jl:301
  [2] reduce_empty(op::Function, #unused#::Type{Int64})
    @ Base ./reduce.jl:311
  [3] reduce_empty(op::Base.BottomRF{typeof(max)}, #unused#::Type{Int64})
    @ Base ./reduce.jl:330
  [4] reduce_empty_iter
    @ ./reduce.jl:357 [inlined]
  [5] reduce_empty_iter
    @ ./reduce.jl:356 [inlined]
  [6] foldl_impl
    @ ./reduce.jl:49 [inlined]
  [7] mapfoldl_impl
    @ ./reduce.jl:44 [inlined]
  [8] #mapfoldl#244
    @ ./reduce.jl:162 [inlined]
  [9] mapfoldl
    @ ./reduce.jl:162 [inlined]
 [10] #mapreduce#248
    @ ./reduce.jl:289 [inlined]
 [11] mapreduce
    @ ./reduce.jl:289 [inlined]
 [12] #maximum#258
    @ ./reduce.jl:737 [inlined]
 [13] maximum
    @ ./reduce.jl:737 [inlined]
 [14] translate!(tape::Vector{Libtask.AbstractInstruction}, ir::Core.CodeInfo)
    @ Libtask ~/Documents/projects/turing/AdvancedPS.jl/Libtask.jl/src/tapedfunction.jl:320

The same runs fine if the arg is used within the function body:

function f(x)
    produce(x)
end

trace = TapedTask(f, 2)
consume(trace) # 2

The issue seems to be the way the arg_slots are constructed here:

arg_binding_slots = fill(0, maximum(keys(slots)))
for (k, v) in slots
arg_binding_slots[k] = v
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions