JuliaDynamics / ResumableFunctions.jl

C# style generators a.k.a. semi-coroutines for Julia.
Other
160 stars 19 forks source link

Tests not passing with 1.1 #30

Closed tlienart closed 5 years ago

tlienart commented 5 years ago

Related to the Mega-Issue https://github.com/JuliaLang/julia/issues/30374.

test_for

@resumable function test_for(a::Int=0; b::Int=a+1) :: Int
  for i in 1:10
    @yield a
    a, b = b, a+b
  end
end
collect(test_for(4))

this fails on 1.1. However this works:

@resumable function test_for(a::Int=0; b::Int=a+1, n::Int=10) :: Int
  for i in 1:n
    @yield a
    a, b = b, a+b
  end
end
collect(test_for(4)) # works

An issue seems to be with the transform_for with the line $next = iterate... where it looks like the right-hand-side gives a nothing and the left-hand side expects a Tuple{Int, Int} (I don't know enough about macros to know what causes this assignment to fail). The following "fixes" it:

function transform_for(expr, ui8::BoxedUInt8)
  @capture(expr, for element_ in iterator_ body__ end) || return expr
  ui8.n += one(UInt8)
  next = Symbol("_iteratornext_", ui8.n)
  state = Symbol("_iterstate_", ui8.n)
  iterator_value = Symbol("_iterator_", ui8.n)
  quote
    $iterator_value = $iterator
    $next = iterate($iterator_value)
    while $next != nothing
      ($element, $state) = $next
      $(body...)
      tmp = iterate($iterator_value, $state) # <---
      tmp === nothing && break               # <---
      $next = tmp                            # <---
    end
  end
end

test_try

@resumable function test_try(io)
  try
    a = 1
    @yield a
    a = 2
    c = @yield a
    println(io,c)
  catch except
    println(io,except)
    d = @yield
    println(io,d)
  finally
    println(io,"Always")
  end
end

fails with error syntax: Attempt to jump into catch block. Not sure what's going on there.

BenLauwens commented 5 years ago

Thanks!

test_for should work now in master test_try is modified, Julia v1.1 does no longer support @goto into a catch statement... so I removed this feature...

Can you check?

tlienart commented 5 years ago

Great, yes tests pass, there's just a couple of warnings

┌ Warning: @resumable function contains return statement with value!
└ @ ResumableFunctions ~/.julia/dev/ResumableFunctions/src/utils.jl:121
┌ Warning: @resumable function contains return statement with value!
└ @ ResumableFunctions ~/.julia/dev/ResumableFunctions/src/utils.jl:121

but that's it. Closing the issue!