<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; @code_typed true ^ -1
CodeInfo(
1 ─ %1 = Base.slt_int(p, 0)::Bool
└──      goto #5 if not %1
2 ─ %3 = Base.not_int(x)::Bool
└──      goto #4 if not %3
3 ─      invoke Base.throw_domerr_powbysq(_2::Bool, _3::Int64)::Union{}
└──      unreachable
4 ─      nothing::Nothing
5 ┄ %8 = (p === 0)::Bool
│   %9 = Base.or_int(%8, x)::Bool
└──      goto #6
6 ─      return %9
) =&gt; Bool

# as expected
julia&gt; a = true ^ 1; println(a, &quot; &quot;, typeof(a))
true Bool

# ???
julia&gt; a = true ^ -1; println(a, &quot; &quot;, typeof(a))
1.0 Float64

# strangely, these forms work
julia&gt; a = true ^ Int64(-1); println(a, &quot; &quot;, typeof(a))
true Bool
julia&gt; a = true ^ -Int64(1); println(a, &quot; &quot;, typeof(a))
true Bool"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">@code_typed</span> <span class="pl-c1">true</span> <span class="pl-k">^</span> <span class="pl-k">-</span><span class="pl-c1">1</span>
<span class="pl-c1">CodeInfo</span>(
<span class="pl-c1">1</span> ─ <span class="pl-k">%</span><span class="pl-c1">1</span> <span class="pl-k">=</span> Base<span class="pl-k">.</span><span class="pl-c1">slt_int</span>(p, <span class="pl-c1">0</span>)<span class="pl-k">::</span><span class="pl-c1">Bool</span>
└──      goto <span class="pl-c"><span class="pl-c">#</span>5 if not %1</span>
<span class="pl-c1">2</span> ─ <span class="pl-k">%</span><span class="pl-c1">3</span> <span class="pl-k">=</span> Base<span class="pl-k">.</span><span class="pl-c1">not_int</span>(x)<span class="pl-k">::</span><span class="pl-c1">Bool</span>
└──      goto <span class="pl-c"><span class="pl-c">#</span>4 if not %3</span>
<span class="pl-c1">3</span> ─      invoke Base<span class="pl-k">.</span><span class="pl-c1">throw_domerr_powbysq</span>(_2<span class="pl-k">::</span><span class="pl-c1">Bool</span>, _3<span class="pl-k">::</span><span class="pl-c1">Int64</span>)<span class="pl-k">::</span><span class="pl-c1">Union{}</span>
└──      unreachable
<span class="pl-c1">4</span> ─      <span class="pl-c1">nothing</span><span class="pl-k">::</span><span class="pl-c1">Nothing</span>
<span class="pl-c1">5</span> ┄ <span class="pl-k">%</span><span class="pl-c1">8</span> <span class="pl-k">=</span> (p <span class="pl-k">===</span> <span class="pl-c1">0</span>)<span class="pl-k">::</span><span class="pl-c1">Bool</span>
│   <span class="pl-k">%</span><span class="pl-c1">9</span> <span class="pl-k">=</span> Base<span class="pl-k">.</span><span class="pl-c1">or_int</span>(<span class="pl-k">%</span><span class="pl-c1">8</span>, x)<span class="pl-k">::</span><span class="pl-c1">Bool</span>
└──      goto <span class="pl-c"><span class="pl-c">#</span>6</span>
<span class="pl-c1">6</span> ─      <span class="pl-k">return</span> <span class="pl-k">%</span><span class="pl-c1">9</span>
) <span class="pl-k">=&gt;</span> Bool

<span class="pl-c"><span class="pl-c">#</span> as expected</span>
julia<span class="pl-k">&gt;</span> a <span class="pl-k">=</span> <span class="pl-c1">true</span> <span class="pl-k">^</span> <span class="pl-c1">1</span>; <span class="pl-c1">println</span>(a, <span class="pl-s"><span class="pl-pds">"</span> <span class="pl-pds">"</span></span>, <span class="pl-c1">typeof</span>(a))
<span class="pl-c1">true</span> Bool

<span class="pl-c"><span class="pl-c">#</span> ???</span>
julia<span class="pl-k">&gt;</span> a <span class="pl-k">=</span> <span class="pl-c1">true</span> <span class="pl-k">^</span> <span class="pl-k">-</span><span class="pl-c1">1</span>; <span class="pl-c1">println</span>(a, <span class="pl-s"><span class="pl-pds">"</span> <span class="pl-pds">"</span></span>, <span class="pl-c1">typeof</span>(a))
<span class="pl-c1">1.0</span> Float64

<span class="pl-c"><span class="pl-c">#</span> strangely, these forms work</span>
julia<span class="pl-k">&gt;</span> a <span class="pl-k">=</span> <span class="pl-c1">true</span> <span class="pl-k">^</span> <span class="pl-c1">Int64</span>(<span class="pl-k">-</span><span class="pl-c1">1</span>); <span class="pl-c1">println</span>(a, <span class="pl-s"><span class="pl-pds">"</span> <span class="pl-pds">"</span></span>, <span class="pl-c1">typeof</span>(a))
<span class="pl-c1">true</span> Bool
julia<span class="pl-k">&gt;</span> a <span class="pl-k">=</span> <span class="pl-c1">true</span> <span class="pl-k">^</span> <span class="pl-k">-</span><span class="pl-c1">Int64</span>(<span class="pl-c1">1</span>); <span class="pl-c1">println</span>(a, <span class="pl-s"><span class="pl-pds">"</span> <span class="pl-pds">"</span></span>, <span class="pl-c1">typeof</span>(a))
<span class="pl-c1">true</span> Bool</pre></div>
<p dir="auto">Cross-posting from discourse: <a href="https://discourse.julialang.org/t/incorrect-results-from-which-and-code-warntype-for-literal-powers/2630" rel="nofollow">https://discourse.julialang.org/t/incorrect-results-from-which-and-code-warntype-for-literal-powers/2630</a></p>
<p dir="auto">I'm trying to use the new literal exponent behavior introduced in <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="206335084" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/20530" data-hovercard-type="pull_request" data-hovercard-url="/JuliaLang/julia/pull/20530/hovercard" href="https://github.com/JuliaLang/julia/pull/20530">#20530</a> and <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="211863927" data-permission-text="Title is private" data-url="https://github.com/JuliaLang/julia/issues/20889" data-hovercard-type="pull_request" data-hovercard-url="/JuliaLang/julia/pull/20889/hovercard" href="https://github.com/JuliaLang/julia/pull/20889">#20889</a>. I think I'm correctly overloading <code class="notranslate">literal_pow</code>, but various macros like <code class="notranslate">@which</code>, <code class="notranslate">@code_warntype</code> and others all seem to be showing me the wrong behavior.</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; versioninfo()
Julia Version 0.6.0-pre.alpha.136
Commit 6eeabd8 (2017-03-13 00:18 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i7-2860QM CPU @ 2.50GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, sandybridge)

julia&gt; immutable MyType
           x::Int
       end

julia&gt; import Base: literal_pow

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

julia&gt; m = MyType(1)
MyType(1)

julia&gt; m^2
3"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">versioninfo</span>()
Julia Version <span class="pl-c1">0.6</span>.<span class="pl-c1">0</span><span class="pl-k">-</span>pre<span class="pl-k">.</span>alpha.<span class="pl-c1">136</span>
Commit <span class="pl-c1">6</span>eeabd8 (<span class="pl-c1">2017</span><span class="pl-k">-</span><span class="pl-c1">03</span><span class="pl-k">-</span><span class="pl-c1">13</span> <span class="pl-c1">00</span><span class="pl-k">:</span><span class="pl-c1">18</span> UTC)
Platform Info<span class="pl-k">:</span>
  OS<span class="pl-k">:</span> macOS (x86_64<span class="pl-k">-</span>apple<span class="pl-k">-</span>darwin13.<span class="pl-c1">4.0</span>)
  CPU<span class="pl-k">:</span> <span class="pl-c1">Intel</span>(R) <span class="pl-c1">Core</span>(TM) i7<span class="pl-k">-</span><span class="pl-c1">2860</span>QM CPU @ <span class="pl-c1">2.50</span>GHz
  WORD_SIZE<span class="pl-k">:</span> <span class="pl-c1">64</span>
  BLAS<span class="pl-k">:</span> libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK<span class="pl-k">:</span> libopenblas64_
  LIBM<span class="pl-k">:</span> libopenlibm
  LLVM<span class="pl-k">:</span> libLLVM<span class="pl-k">-</span><span class="pl-c1">3.9</span>.<span class="pl-c1">1</span> (ORCJIT, sandybridge)

julia<span class="pl-k">&gt;</span> immutable MyType
           x<span class="pl-k">::</span><span class="pl-c1">Int</span>
       <span class="pl-k">end</span>

julia<span class="pl-k">&gt;</span> <span class="pl-k">import</span> Base<span class="pl-k">:</span> literal_pow

julia<span class="pl-k">&gt;</span> <span class="pl-en">literal_pow</span>(<span class="pl-k">^</span>, m<span class="pl-k">::</span><span class="pl-c1">MyType</span>, <span class="pl-k">::</span><span class="pl-c1">Type{Val{p}}</span>) <span class="pl-k">where</span> p <span class="pl-k">=</span> m<span class="pl-k">.</span>x <span class="pl-k">+</span> p
literal_pow (generic <span class="pl-k">function</span> with <span class="pl-c1">6</span> methods)

julia<span class="pl-k">&gt;</span> m <span class="pl-k">=</span> <span class="pl-c1">MyType</span>(<span class="pl-c1">1</span>)
<span class="pl-c1">MyType</span>(<span class="pl-c1">1</span>)

julia<span class="pl-k">&gt;</span> m<span class="pl-k">^</span><span class="pl-c1">2</span>
<span class="pl-c1">3</span></pre></div>
<p dir="auto">That's all good so far. But none of the introspection macros seem to work for <code class="notranslate">m^2</code>:</p>
<div class="highlight highlight-source-julia notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="julia&gt; @which m^2
^(x, p::Integer) in Base at intfuncs.jl:196

julia&gt; @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&gt; @code_lowered m^2
CodeInfo(:(begin
        nothing
        return (Base.power_by_squaring)(x, p)
    end))

julia&gt; using Base.Test

julia&gt; @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"><pre class="notranslate">julia<span class="pl-k">&gt;</span> <span class="pl-c1">@which</span> m<span class="pl-k">^</span><span class="pl-c1">2</span>
<span class="pl-k">^</span>(x, p<span class="pl-k">::</span><span class="pl-c1">Integer</span>) <span class="pl-k">in</span> Base at intfuncs<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">196</span>

julia<span class="pl-k">&gt;</span> <span class="pl-c1">@code_warntype</span> m<span class="pl-k">^</span><span class="pl-c1">2</span>
Variables<span class="pl-k">:</span>
  <span class="pl-c"><span class="pl-c">#</span>self#::Base.#^</span>
  x<span class="pl-k">::</span><span class="pl-c1">MyType</span>
  p<span class="pl-k">::</span><span class="pl-c1">Int64</span>

Body<span class="pl-k">:</span>
  <span class="pl-k">begin</span>
      <span class="pl-k">return</span> <span class="pl-k">$</span>(<span class="pl-c1">Expr</span>(<span class="pl-c1">:invoke</span>, MethodInstance <span class="pl-k">for</span> <span class="pl-c1">power_by_squaring</span>(<span class="pl-k">::</span><span class="pl-c1">MyType</span>, <span class="pl-k">::</span><span class="pl-c1">Int64</span>), :(Base<span class="pl-k">.</span>power_by_squaring), :(x), :(p)))
  <span class="pl-k">end</span><span class="pl-k">::</span><span class="pl-c1">Any</span>

julia<span class="pl-k">&gt;</span> <span class="pl-c1">@code_lowered</span> m<span class="pl-k">^</span><span class="pl-c1">2</span>
<span class="pl-c1">CodeInfo</span>(:(<span class="pl-k">begin</span>
        <span class="pl-c1">nothing</span>
        <span class="pl-k">return</span> (Base<span class="pl-k">.</span>power_by_squaring)(x, p)
    <span class="pl-k">end</span>))

julia<span class="pl-k">&gt;</span> <span class="pl-k">using</span> Base<span class="pl-k">.</span>Test

julia<span class="pl-k">&gt;</span> <span class="pl-c1">@inferred</span> m<span class="pl-k">^</span><span class="pl-c1">2</span>
ERROR<span class="pl-k">:</span> MethodError<span class="pl-k">:</span> no method matching <span class="pl-k">*</span>(<span class="pl-k">::</span><span class="pl-c1">MyType</span>, <span class="pl-k">::</span><span class="pl-c1">MyType</span>)
Closest candidates are<span class="pl-k">:</span>
  <span class="pl-k">*</span>(<span class="pl-k">::</span><span class="pl-c1">Any</span>, <span class="pl-k">::</span><span class="pl-c1">Any</span>, <span class="pl-k">::</span><span class="pl-c1">Any</span>, <span class="pl-k">::</span><span class="pl-c1">Any...</span>) at operators<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">424</span>
Stacktrace<span class="pl-k">:</span>
 [<span class="pl-c1">1</span>] <span class="pl-c1">power_by_squaring</span>(<span class="pl-k">::</span><span class="pl-c1">MyType</span>, <span class="pl-k">::</span><span class="pl-c1">Int64</span>) at <span class="pl-k">./</span>intfuncs<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">166</span>
 [<span class="pl-c1">2</span>] <span class="pl-k">^</span>(<span class="pl-k">::</span><span class="pl-c1">MyType</span>, <span class="pl-k">::</span><span class="pl-c1">Int64</span>) at <span class="pl-k">./</span>intfuncs<span class="pl-k">.</span>jl<span class="pl-k">:</span><span class="pl-c1">196</span></pre></div>
<p dir="auto">are the special lowering rules not being applied inside these macros?</p>