Skip to content

Commit

Permalink
define InteractiveUtils.@infer_[return|exception]_type (#56398)
Browse files Browse the repository at this point in the history
Also simplifies the definitions of `@code_typed` and the other similar
macros.
  • Loading branch information
aviatesk authored Nov 1, 2024
1 parent 7715cf2 commit 040174c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
40 changes: 13 additions & 27 deletions stdlib/InteractiveUtils/src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

# macro wrappers for various reflection functions

import Base: typesof, insert!, replace_ref_begin_end!, infer_effects, code_ircode
using Base: typesof, insert!, replace_ref_begin_end!,
infer_return_type, infer_exception_type, infer_effects, code_ircode

# defined in Base so it's possible to time all imports, including InteractiveUtils and its deps
# via. `Base.@time_imports` etc.
Expand Down Expand Up @@ -225,35 +226,20 @@ macro which(ex0::Symbol)
return :(which($__module__, $ex0))
end

for fname in [:code_warntype, :code_llvm, :code_native, :infer_effects]
@eval begin
macro ($fname)(ex0...)
gen_call_with_extracted_types_and_kwargs(__module__, $(Expr(:quote, fname)), ex0)
end
end
end

macro code_typed(ex0...)
thecall = gen_call_with_extracted_types_and_kwargs(__module__, :code_typed, ex0)
quote
local results = $thecall
length(results) == 1 ? results[1] : results
for fname in [:code_warntype, :code_llvm, :code_native,
:infer_return_type, :infer_effects, :infer_exception_type]
@eval macro ($fname)(ex0...)
gen_call_with_extracted_types_and_kwargs(__module__, $(QuoteNode(fname)), ex0)
end
end

macro code_lowered(ex0...)
thecall = gen_call_with_extracted_types_and_kwargs(__module__, :code_lowered, ex0)
quote
local results = $thecall
length(results) == 1 ? results[1] : results
end
end

macro code_ircode(ex0...)
thecall = gen_call_with_extracted_types_and_kwargs(__module__, :code_ircode, ex0)
quote
local results = $thecall
length(results) == 1 ? results[1] : results
for fname in [:code_typed, :code_lowered, :code_ircode]
@eval macro ($fname)(ex0...)
thecall = gen_call_with_extracted_types_and_kwargs(__module__, $(QuoteNode(fname)), ex0)
quote
local results = $thecall
length(results) == 1 ? results[1] : results
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions stdlib/InteractiveUtils/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,8 @@ end
end

@test Base.infer_effects(sin, (Int,)) == InteractiveUtils.@infer_effects sin(42)
@test Base.infer_return_type(sin, (Int,)) == InteractiveUtils.@infer_return_type sin(42)
@test Base.infer_exception_type(sin, (Int,)) == InteractiveUtils.@infer_exception_type sin(42)
@test first(InteractiveUtils.@code_ircode sin(42)) isa Core.Compiler.IRCode
@test first(InteractiveUtils.@code_ircode optimize_until="Inlining" sin(42)) isa Core.Compiler.IRCode

Expand Down

0 comments on commit 040174c

Please sign in to comment.