Comprehension not working in @generated function in v0.6

I upgraded from v0.5 to v0.6 and using a comprehension in a generated function gives the following error:

generated function body is not pure. this likely means it contains a closure or comprehension.

The following is a minimum working example:

      @generated function _test{T,N}(s::AbstractArray{T,N})
           :([1 for i=1:size(s,2)])
      end
    
      a = ones(1,2)
      b=_test(a)

and leads to the expected result in v0.5, but gives the following error in v0.6:

generated function body is not pure. this likely means it contains a closure or comprehension.

It seems to me that the comprehension [… for … ] is the problem.

Is this a planned deprecation or a bug? If this is a planned depreciation, how to get around it?

(For reference, the error occurs in the TimeWarp.jl package, dba.jl, l.147)

Thanks

The manual for v0.6 says generated functions cannot (currently) define untyped generators. This seems to work for your example:

@generated function _test{T,N}(s::AbstractArray{T,N})
       :(Int[1 for i=1:size(s,2)])
end
1 Like

This is a known regression. You can workaround it by putting the comprehension in a different function. It can of course get complicated if the comprehension depends on the input types but you can get around it most of the time by nesting another generated function in the comprehension.

2 Likes

Thank you, this works!