Incorrect results from `@which` and `@code_warntype` for literal powers


#1

I’m trying to use the new literal exponent behavior introduced in https://github.com/JuliaLang/julia/pull/20530 and https://github.com/JuliaLang/julia/pull/20889. I think I’m correctly overloading literal_pow, but various macros like @which, @code_warntype and others all seem to be showing me the wrong behavior.

julia> immutable MyType
           x::Int
       end

julia> import Base: literal_pow

julia> literal_pow(^, m::MyType, ::Type{Val{p}}) where p = m.x + p
literal_pow (generic function with 6 methods)

julia> m = MyType(1)
MyType(1)

julia> m^2
3

That’s all good so far. But none of the introspection macros seem to work for m^2:

julia> @which m^2
^(x, p::Integer) in Base at intfuncs.jl:196

julia> @code_warntype m^2
Variables:
  #self#::Base.#^
  x::MyType
  p::Int64

Body:
  begin
      return $(Expr(:invoke, MethodInstance for power_by_squaring(::MyType, ::Int64), :(Base.power_by_squaring), :(x), :(p)))
  end::Any

julia> @code_lowered m^2
CodeInfo(:(begin
        nothing
        return (Base.power_by_squaring)(x, p)
    end))

julia> using Base.Test

julia> @inferred m^2
ERROR: MethodError: no method matching *(::MyType, ::MyType)
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:424
Stacktrace:
 [1] power_by_squaring(::MyType, ::Int64) at ./intfuncs.jl:166
 [2] ^(::MyType, ::Int64) at ./intfuncs.jl:196

are the special lowering rules not being applied inside the macros?


#2

Issue created: https://github.com/JuliaLang/julia/issues/21014