Skip to content

Commit f06d22f

Browse files
authored
bugfix on dealing with args (#149)
1 parent 8ea2899 commit f06d22f

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

Diff for: src/tapedfunction.jl

+12-3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ end
133133
struct NOOPInstruction <: AbstractInstruction end
134134

135135
@inline result(t::TapedFunction) = t.binding_values[t.retval_binding_slot]
136+
@inline function _arg(tf::TapedFunction, i::Int; default=nothing)
137+
length(tf.arg_binding_slots) < i && return default
138+
tf.arg_binding_slots[i] > 0 && return tf.binding_values[tf.arg_binding_slots[i]]
139+
return default
140+
end
141+
@inline function _arg!(tf::TapedFunction, i::Int, v)
142+
length(tf.arg_binding_slots) >= i &&
143+
tf.arg_binding_slots[i] > 0 && _update_var!(tf, tf.arg_binding_slots[i], v)
144+
end
136145

137146
function (tf::TapedFunction)(args...; callback=nothing, continuation=false)
138147
if !continuation # reset counter and retval_binding_slot to run from the start
@@ -143,10 +152,10 @@ function (tf::TapedFunction)(args...; callback=nothing, continuation=false)
143152
# set args
144153
if tf.counter <= 1
145154
# The first slot in `binding_values` is assumed to be `tf.func`.
146-
tf.arg_binding_slots[1] > 0 && _update_var!(tf, tf.arg_binding_slots[1], tf.func)
155+
_arg!(tf, 1, tf.func)
147156
for i in 1:length(args) # the subsequent arg_binding_slots are arguments
148157
slot = i + 1
149-
tf.arg_binding_slots[slot] > 0 && _update_var!(tf, tf.arg_binding_slots[slot], args[i])
158+
_arg!(tf, slot, args[i])
150159
end
151160
end
152161

@@ -314,7 +323,7 @@ function translate!(tape::RawTape, ir::Core.CodeInfo)
314323
for (k, v) in bcache
315324
isa(k, Core.SlotNumber) && (slots[k.id] = v)
316325
end
317-
arg_binding_slots = fill(0, maximum(keys(slots)))
326+
arg_binding_slots = fill(0, maximum(keys(slots); init=0))
318327
for (k, v) in slots
319328
arg_binding_slots[k] = v
320329
end

Diff for: src/tapedtask.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ function Base.copy(t::TapedTask; args=())
174174
# the task is running, we find the real args from the copied binding_values
175175
map(1:length(t.args)) do i
176176
s = i + 1
177-
tf.arg_binding_slots[s] > 0 ? tf.binding_values[tf.arg_binding_slots[s]] : t.args[i]
177+
_arg(tf, s; default=t.args[i])
178178
end
179179
else
180180
# the task is not started yet, but no args is given

Diff for: test/tapedtask.jl

+9
Original file line numberDiff line numberDiff line change
@@ -199,5 +199,14 @@
199199
ttask3 = copy(ttask; args=(4,))
200200
@test consume(ttask3) == 5
201201
end
202+
203+
@testset "Issue-148, unused argument" begin
204+
function f(x)
205+
produce(1)
206+
end
207+
208+
ttask = TapedTask(f, 2)
209+
@test consume(ttask) == 1
210+
end
202211
end
203212
end

0 commit comments

Comments
 (0)