Polyester.jl icon indicating copy to clipboard operation
Polyester.jl copied to clipboard

Destructuring not recognized

Open henry2004y opened this issue 2 years ago • 6 comments

Hi,

I've encountered a case where Polyester.jl does not know how to handle the destructuring syntax introduced in Julia 1.7:

using Polyester

struct A
   a
   b
end

x = A(1,2)

Threads.@threads for i in 1:2
   (; a, b) = x
end

@batch for i in 1:2
   (; a, b) = x
end

The @batch loop returns

ERROR: LoadError: "Don't know how to handle:\n \$(Expr(:parameters, :a, :b))"

henry2004y avatar Apr 10 '22 20:04 henry2004y

bump, do we need a pass in the macro to transform this into a bunch of getproperty?

Moelf avatar Oct 03 '22 18:10 Moelf

bump, do we need a pass in the macro to transform this into a bunch of getproperty?

Yeah, that'd be a reasonable approach. The alternative would be handling Expr(:parameters,...), where those parameters would be added to the symbols defined in the loop.

Want to take a stab at it?

chriselrod avatar Oct 03 '22 19:10 chriselrod

https://github.com/JuliaSIMD/Polyester.jl/blob/f49a3081f0005e920fa7542cd3ce4710925efddb/src/closure.jl#L18-L30 can handle

julia> y = (1,2);

julia> @batch for i in 1:2
          (a, b) = y
       end

so it should be straightforward to adapt to support (; a, b) = x.

Although preprocessing this into getproperty calls would work too, and not require as much looking into the internals, at the cost of requiring an extra pass over the Expr making macro expansion slower.

chriselrod avatar Oct 03 '22 20:10 chriselrod

where even is this signature defined

  define_tup!(defined, a) 

Moelf avatar Oct 04 '22 18:10 Moelf

also this doesn't quite work, this logic assumes each element in ex.args gets replaced by a gensym

but when a is a :parameters expression, it needs to inflate into multiple entries in ex.args

Moelf avatar Oct 04 '22 18:10 Moelf

where even is this signature defined

  define_tup!(defined, a) 

That method does not exist. That is a bug.

chriselrod avatar Oct 04 '22 20:10 chriselrod