diff --git a/stdlib/Test/src/Test.jl b/stdlib/Test/src/Test.jl index 09594e5be0025..b915c58c782df 100644 --- a/stdlib/Test/src/Test.jl +++ b/stdlib/Test/src/Test.jl @@ -2143,36 +2143,13 @@ macro inferred(allow, ex) _inferred(ex, __module__, allow) end function _inferred(ex, mod, allow = :(Union{})) - if Meta.isexpr(ex, :ref) - ex = Expr(:call, :getindex, ex.args...) - end - Meta.isexpr(ex, :call)|| error("@inferred requires a call expression") - farg = ex.args[1] - if isa(farg, Symbol) && farg !== :.. && first(string(farg)) == '.' - farg = Symbol(string(farg)[2:end]) - ex = Expr(:call, GlobalRef(Test, :_materialize_broadcasted), - farg, ex.args[2:end]...) - end + Meta.isexpr(ex, (:call, :ref, :do)) || error("@inferred requires a call expression") result = let ex = ex quote let allow = $(esc(allow)) allow isa Type || throw(ArgumentError("@inferred requires a type as second argument")) - $(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]))) - inftype = $(gen_call_with_extracted_types(mod, Base.infer_return_type, :($(ex.args[1])($(args)...; $(kwargs)...)); is_source_reflection = false)) - end - else - # No keywords - quote - args = ($([esc(ex.args[i]) for i = 2:length(ex.args)]...),) - result = $(esc(ex.args[1]))(args...) - inftype = Base.infer_return_type($(esc(ex.args[1])), Base.typesof(args...)) - end - end) + result = $(esc(ex)) + inftype = $(gen_call_with_extracted_types(mod, Base.infer_return_type, ex; is_source_reflection = false)) rettype = result isa Type ? Type{result} : typeof(result) rettype <: allow || rettype == typesplit(inftype, allow) || error("return type $rettype does not match inferred return type $inftype") result diff --git a/stdlib/Test/test/runtests.jl b/stdlib/Test/test/runtests.jl index 0d6486a26e33e..58dffa31a11db 100644 --- a/stdlib/Test/test/runtests.jl +++ b/stdlib/Test/test/runtests.jl @@ -745,6 +745,10 @@ uninferable_kwtest(x; y=1) = 2x+y @test (@inferred uninferable_kwtest(1)) == 3 @test (@inferred uninferable_kwtest(1; y=2)) == 4 +# Issue #59114 +# @inferred with do-blocks +@test @inferred(map([1,2]) do x x end) == [1, 2] + @test_throws ErrorException @testset "$(error())" for i in 1:10 end @test_throws ErrorException @testset "$(error())" begin