JuliaFolds2 / OhMyThreads.jl

Simple multithreading in julia
https://juliafolds2.github.io/OhMyThreads.jl/
MIT License
139 stars 9 forks source link

Macro parsing incorrectly with `OhMyThreads.@set` #107

Closed Moelf closed 4 months ago

Moelf commented 5 months ago
julia> function f()
           for i in [1,2,3]
               # check something
               OhMyThreads.@tasks for j in [1,2,3]
                   OhMyThreads.@set begin
                       scheduler = :static
                       ntasks = 8
                   end
                   j += 2
                   # do sutff
                   end
               end
           end
ERROR: LoadError: The @set macro may only be used inside of a @tasks block.

but this works:

julia> function f()
           for i in [1,2,3]
               # check something
               OhMyThreads.@tasks for j in [1,2,3]
                   @set begin
                       scheduler = :static
                       ntasks = 8
                   end
                   j += 2
                   # do sutff
                   end
               end
           end
f (generic function with 1 method)
MasonProtter commented 5 months ago

Yeah, the @set macro is not a real macro. All it does is throw an error. It's only ever meant to be used as something that @tasks detects and handles, so when you write OhMyThreads.@set, it gets confused.

We can make it catch this particular pattern too, but it'll always be possible to confuse it, e.g. you could do

const var"@my_set" = var"@set"

and then try and use use @my_set inside @tasks and you'll get this same error.

carstenbauer commented 5 months ago

What Mason said. But we should probably cover this particular pattern, i.e. OhMyThreads.@macroname, as well.

Moelf commented 5 months ago

I'm aware of the mechanism that caused it and had seen the stub code in the macro definition. Not asking why though