Skip to content

Commit

Permalink
fix macros @which, @edit, @functionloc, @less for `literal_po…
Browse files Browse the repository at this point in the history
…w` case. (#53713)

The macros `@which`, `@edit`, `@functionloc`, `@less` from `InteractiveUtils`, if
applied to the case of literal powers, like `a^12` or `2^-1` used to direct the
user to function `^`, while the compiler generates code for `Base.literal_pow`.

Now the user is shown the code the compiler generates.

Fixes #53691
Fixes #43337
Fixes #21014

Co-authored-by: Matt Bauman <mbauman@juliahub.com>
  • Loading branch information
KlausC and mbauman committed Apr 3, 2024
1 parent 12c9391 commit 286e339
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
5 changes: 5 additions & 0 deletions stdlib/InteractiveUtils/src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ function gen_call_with_extracted_types(__module__, fcn, ex0, kws=Expr[])
$(kws...))
end
elseif ex0.head === :call
if ex0.args[1] === :^ && length(ex0.args) >= 3 && isa(ex0.args[3], Int)
return Expr(:call, fcn, :(Base.literal_pow),
Expr(:call, typesof, esc(ex0.args[1]), esc(ex0.args[2]),
esc(Val(ex0.args[3]))))
end
return Expr(:call, fcn, esc(ex0.args[1]),
Expr(:call, typesof, map(esc, ex0.args[2:end])...),
kws...)
Expand Down
38 changes: 37 additions & 1 deletion stdlib/InteractiveUtils/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,50 @@ let x..y = 0
@test (@which 1..2).name === :..
end

# issue #53691
let a = -1
@test (@which 2^a).name === :^
@test (@which 2^0x1).name === :^
end

let w = Vector{Any}(undef, 9)
@testset "@which x^literal" begin
w[1] = @which 2^0
w[2] = @which 2^1
w[3] = @which 2^2
w[4] = @which 2^3
w[5] = @which 2^-1
w[6] = @which 2^-2
w[7] = @which 2^10
w[8] = @which big(2.0)^1
w[9] = @which big(2.0)^-1
@test all(getproperty.(w, :name) .=== :literal_pow)
@test length(Set(w)) == length(w) # all methods distinct
end
end

# PR 53713
if Int === Int64
# literal_pow only for exponents x: -2^63 <= x < 2^63 #53860 (all Int)
@test (@which 2^-9223372036854775809).name === :^
@test (@which 2^-9223372036854775808).name === :literal_pow
@test (@which 2^9223372036854775807).name === :literal_pow
@test (@which 2^9223372036854775808).name === :^
elseif Int === Int32
# literal_pow only for exponents x: -2^31 <= x < 2^31 #53860 (all Int)
@test (@which 2^-2147483649).name === :^
@test (@which 2^-2147483648).name === :literal_pow
@test (@which 2^2147483647).name === :literal_pow
@test (@which 2^2147483648).name === :^
end

# issue #13464
try
@which x = 1
error("unexpected")
catch err13464
@test startswith(err13464.msg, "expression is not a function call")
end

module MacroTest
export @macrotest
macro macrotest(x::Int, y::Symbol) end
Expand Down

0 comments on commit 286e339

Please sign in to comment.