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

@views macro causes module compilation failure

Open prittjam opened this issue 3 years ago • 3 comments

If I used @views on a function that uses Tullio, the module won't compile. Perhaps this is known or intended, but I don't understand the behavior. Removal of @views allows compilation.

prittjam avatar Sep 21 '22 19:09 prittjam

I wouldn't call this intended but it is unfortunately expected. Macros are expanded outermost first (the opposite order to functions) and thus @views changes code before @tullio sees it, and produces something it has no idea how to digest:

julia> @macroexpand1 @views f(x) = @tullio _[i] := x[i]^2
:(f(x) = begin
          #= REPL[203]:1 =#
          #= REPL[203]:1 =# @tullio let var"##a#421" = _, var"##i#422" = i
                  var"##a#421"[var"##i#422"] = (Base.maybeview)(var"##a#421", var"##i#422"):(Base.maybeview)(x, i) ^ 2
              end
      end)

Macros can opt out of this, by expanding their arguments. If @views did this, it would act on the code @tullio produces, and would be less likely to cause trouble. But it doesn't.

mcabbott avatar Sep 21 '22 23:09 mcabbott

Thanks for the response. I think this would be a common gotcha and maybe worth putting in the docs.

prittjam avatar Sep 22 '22 09:09 prittjam

Maybe this macro should notice Base.maybeview, and thrown an informative error. That wouldn't be so hard to implement.

mcabbott avatar Apr 28 '23 13:04 mcabbott