Skip to content

Commit

Permalink
make Base.return_types interface similar to code_typed (#44533)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
aviatesk committed Mar 14, 2022
1 parent 44ebba2 commit 9375f3b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
5 changes: 3 additions & 2 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions test/compiler/AbstractInterpreter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -78,13 +78,13 @@ 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

# disable partial pure/concrete evaluation when tainted by any overlayed call
Base.@assume_effects :total totalcall(f, args...) = f(args...)
@test Base.return_types(MTOverlayInterp()) do
@test Base.return_types(; interp=MTOverlayInterp()) do
if totalcall(strangesin, 1.0) == cos(1.0)
return nothing
else
Expand Down

0 comments on commit 9375f3b

Please sign in to comment.