From 90314dd777507e0b97294510046adb77983ccd2e Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Date: Fri, 11 Mar 2022 06:13:01 +0900 Subject: [PATCH] make `Base.return_types` interface similar to `code_typed` (#44533) Especially, it should be more consistent with the other reflection utilities defined in reflection.jl if the optional `interp::AbstractInterpreter` argument is passed as keyword one. --- base/reflection.jl | 5 +++-- test/compiler/AbstractInterpreter.jl | 14 +++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/base/reflection.jl b/base/reflection.jl index 95fb81c8859d60..484dc8b5866642 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -1291,11 +1291,12 @@ function code_typed_opaque_closure(@nospecialize(closure::Core.OpaqueClosure); end end -function return_types(@nospecialize(f), @nospecialize(types=default_tt(f)), interp=Core.Compiler.NativeInterpreter()) +function return_types(@nospecialize(f), @nospecialize(types=default_tt(f)); + world = get_world_counter(), + interp = Core.Compiler.NativeInterpreter(world)) ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions") types = to_tuple_type(types) rt = [] - world = get_world_counter() for match in _methods(f, types, -1, world)::Vector match = match::Core.MethodMatch meth = func_for_method_checked(match.method, types, match.sparams) diff --git a/test/compiler/AbstractInterpreter.jl b/test/compiler/AbstractInterpreter.jl index 25ab8056fa649d..de0e628f964a52 100644 --- a/test/compiler/AbstractInterpreter.jl +++ b/test/compiler/AbstractInterpreter.jl @@ -43,30 +43,30 @@ CC.method_table(interp::MTOverlayInterp) = CC.OverlayMethodTable(CC.get_world_co strangesin(x) = sin(x) @overlay OverlayedMT strangesin(x::Float64) = iszero(x) ? nothing : cos(x) -@test Base.return_types((Float64,); MTOverlayInterp()) do x +@test Base.return_types((Float64,); interp=MTOverlayInterp()) do x strangesin(x) end |> only === Union{Float64,Nothing} -@test Base.return_types((Any,); MTOverlayInterp()) do x +@test Base.return_types((Any,); interp=MTOverlayInterp()) do x Base.@invoke strangesin(x::Float64) end |> only === Union{Float64,Nothing} # fallback to the internal method table -@test Base.return_types((Int,), MTOverlayInterp()) do x +@test Base.return_types((Int,); interp=MTOverlayInterp()) do x cos(x) end |> only === Float64 -@test Base.return_types((Any,); MTOverlayInterp()) do x +@test Base.return_types((Any,); interp=MTOverlayInterp()) do x Base.@invoke cos(x::Float64) end |> only === Float64 # not fully covered overlay method match overlay_match(::Any) = nothing @overlay OverlayedMT overlay_match(::Int) = missing -@test Base.return_types((Any,), MTOverlayInterp()) do x +@test Base.return_types((Any,); interp=MTOverlayInterp()) do x overlay_match(x) end |> only === Union{Nothing,Missing} # partial pure/concrete evaluation -@test Base.return_types((), MTOverlayInterp()) do +@test Base.return_types(; interp=MTOverlayInterp()) do isbitstype(Int) ? nothing : missing end |> only === Nothing Base.@assume_effects :terminates_globally function issue41694(x) @@ -78,6 +78,6 @@ Base.@assume_effects :terminates_globally function issue41694(x) end return res end -@test Base.return_types((), MTOverlayInterp()) do +@test Base.return_types(; interp=MTOverlayInterp()) do issue41694(3) == 6 ? nothing : missing end |> only === Nothing