Skip to content

Commit 7197c9e

Browse files
authored
Test: use infer_return_type for @inferred instead of return_types (#54752)
The implementation can be slightly more simply.
1 parent ce38f08 commit 7197c9e

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

stdlib/Test/src/Test.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,7 @@ function get_testset_depth()
19001900
return length(testsets)
19011901
end
19021902

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

19061906
"""
@@ -1982,25 +1982,24 @@ function _inferred(ex, mod, allow = :(Union{}))
19821982
quote
19831983
let allow = $(esc(allow))
19841984
allow isa Type || throw(ArgumentError("@inferred requires a type as second argument"))
1985-
$(if any(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex.args)
1985+
$(if any(@nospecialize(a)->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex.args)
19861986
# Has keywords
19871987
args = gensym()
19881988
kwargs = gensym()
19891989
quote
19901990
$(esc(args)), $(esc(kwargs)), result = $(esc(Expr(:call, _args_and_call, ex.args[2:end]..., ex.args[1])))
1991-
inftypes = $(gen_call_with_extracted_types(mod, Base.return_types, :($(ex.args[1])($(args)...; $(kwargs)...))))
1991+
inftype = $(gen_call_with_extracted_types(mod, Base.infer_return_type, :($(ex.args[1])($(args)...; $(kwargs)...))))
19921992
end
19931993
else
19941994
# No keywords
19951995
quote
19961996
args = ($([esc(ex.args[i]) for i = 2:length(ex.args)]...),)
19971997
result = $(esc(ex.args[1]))(args...)
1998-
inftypes = Base.return_types($(esc(ex.args[1])), Base.typesof(args...))
1998+
inftype = Base.infer_return_type($(esc(ex.args[1])), Base.typesof(args...))
19991999
end
20002000
end)
2001-
@assert length(inftypes) == 1
20022001
rettype = result isa Type ? Type{result} : typeof(result)
2003-
rettype <: allow || rettype == typesplit(inftypes[1], allow) || error("return type $rettype does not match inferred return type $(inftypes[1])")
2002+
rettype <: allow || rettype == typesplit(inftype, allow) || error("return type $rettype does not match inferred return type $inftype")
20042003
result
20052004
end
20062005
end

0 commit comments

Comments
 (0)