Skip to content

Commit 286e339

Browse files
KlausCmbauman
andauthored
fix macros @which, @edit, @functionloc, @less for literal_pow 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>
1 parent 12c9391 commit 286e339

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

stdlib/InteractiveUtils/src/macros.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ function gen_call_with_extracted_types(__module__, fcn, ex0, kws=Expr[])
106106
$(kws...))
107107
end
108108
elseif ex0.head === :call
109+
if ex0.args[1] === :^ && length(ex0.args) >= 3 && isa(ex0.args[3], Int)
110+
return Expr(:call, fcn, :(Base.literal_pow),
111+
Expr(:call, typesof, esc(ex0.args[1]), esc(ex0.args[2]),
112+
esc(Val(ex0.args[3]))))
113+
end
109114
return Expr(:call, fcn, esc(ex0.args[1]),
110115
Expr(:call, typesof, map(esc, ex0.args[2:end])...),
111116
kws...)

stdlib/InteractiveUtils/test/runtests.jl

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,14 +279,50 @@ let x..y = 0
279279
@test (@which 1..2).name === :..
280280
end
281281

282+
# issue #53691
283+
let a = -1
284+
@test (@which 2^a).name === :^
285+
@test (@which 2^0x1).name === :^
286+
end
287+
288+
let w = Vector{Any}(undef, 9)
289+
@testset "@which x^literal" begin
290+
w[1] = @which 2^0
291+
w[2] = @which 2^1
292+
w[3] = @which 2^2
293+
w[4] = @which 2^3
294+
w[5] = @which 2^-1
295+
w[6] = @which 2^-2
296+
w[7] = @which 2^10
297+
w[8] = @which big(2.0)^1
298+
w[9] = @which big(2.0)^-1
299+
@test all(getproperty.(w, :name) .=== :literal_pow)
300+
@test length(Set(w)) == length(w) # all methods distinct
301+
end
302+
end
303+
304+
# PR 53713
305+
if Int === Int64
306+
# literal_pow only for exponents x: -2^63 <= x < 2^63 #53860 (all Int)
307+
@test (@which 2^-9223372036854775809).name === :^
308+
@test (@which 2^-9223372036854775808).name === :literal_pow
309+
@test (@which 2^9223372036854775807).name === :literal_pow
310+
@test (@which 2^9223372036854775808).name === :^
311+
elseif Int === Int32
312+
# literal_pow only for exponents x: -2^31 <= x < 2^31 #53860 (all Int)
313+
@test (@which 2^-2147483649).name === :^
314+
@test (@which 2^-2147483648).name === :literal_pow
315+
@test (@which 2^2147483647).name === :literal_pow
316+
@test (@which 2^2147483648).name === :^
317+
end
318+
282319
# issue #13464
283320
try
284321
@which x = 1
285322
error("unexpected")
286323
catch err13464
287324
@test startswith(err13464.msg, "expression is not a function call")
288325
end
289-
290326
module MacroTest
291327
export @macrotest
292328
macro macrotest(x::Int, y::Symbol) end

0 commit comments

Comments
 (0)