Skip to content

Commit

Permalink
Test: use infer_return_type for @inferred instead of `return_type…
Browse files Browse the repository at this point in the history
…s` (#54752)

The implementation can be slightly more simply.
  • Loading branch information
aviatesk authored Jun 10, 2024
1 parent ce38f08 commit 7197c9e
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1900,7 +1900,7 @@ function get_testset_depth()
return length(testsets)
end

_args_and_call(args...; kwargs...) = (args[1:end-1], kwargs, args[end](args[1:end-1]...; kwargs...))
_args_and_call((args..., f)...; kwargs...) = (args, kwargs, f(args...; kwargs...))
_materialize_broadcasted(f, args...) = Broadcast.materialize(Broadcast.broadcasted(f, args...))

"""
Expand Down Expand Up @@ -1982,25 +1982,24 @@ function _inferred(ex, mod, allow = :(Union{}))
quote
let allow = $(esc(allow))
allow isa Type || throw(ArgumentError("@inferred requires a type as second argument"))
$(if any(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex.args)
$(if any(@nospecialize(a)->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex.args)
# Has keywords
args = gensym()
kwargs = gensym()
quote
$(esc(args)), $(esc(kwargs)), result = $(esc(Expr(:call, _args_and_call, ex.args[2:end]..., ex.args[1])))
inftypes = $(gen_call_with_extracted_types(mod, Base.return_types, :($(ex.args[1])($(args)...; $(kwargs)...))))
inftype = $(gen_call_with_extracted_types(mod, Base.infer_return_type, :($(ex.args[1])($(args)...; $(kwargs)...))))
end
else
# No keywords
quote
args = ($([esc(ex.args[i]) for i = 2:length(ex.args)]...),)
result = $(esc(ex.args[1]))(args...)
inftypes = Base.return_types($(esc(ex.args[1])), Base.typesof(args...))
inftype = Base.infer_return_type($(esc(ex.args[1])), Base.typesof(args...))
end
end)
@assert length(inftypes) == 1
rettype = result isa Type ? Type{result} : typeof(result)
rettype <: allow || rettype == typesplit(inftypes[1], allow) || error("return type $rettype does not match inferred return type $(inftypes[1])")
rettype <: allow || rettype == typesplit(inftype, allow) || error("return type $rettype does not match inferred return type $inftype")
result
end
end
Expand Down

0 comments on commit 7197c9e

Please sign in to comment.