Skip to content

Commit 887baba

Browse files
committed
Allow @inferred to accept do-block syntax (fix #59114)
Simplified code by leveraging `gen_call_with_extracted_types` for special case handling.
1 parent 9f69961 commit 887baba

File tree

2 files changed

+7
-26
lines changed

2 files changed

+7
-26
lines changed

stdlib/Test/src/Test.jl

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,36 +2143,13 @@ macro inferred(allow, ex)
21432143
_inferred(ex, __module__, allow)
21442144
end
21452145
function _inferred(ex, mod, allow = :(Union{}))
2146-
if Meta.isexpr(ex, :ref)
2147-
ex = Expr(:call, :getindex, ex.args...)
2148-
end
2149-
Meta.isexpr(ex, :call)|| error("@inferred requires a call expression")
2150-
farg = ex.args[1]
2151-
if isa(farg, Symbol) && farg !== :.. && first(string(farg)) == '.'
2152-
farg = Symbol(string(farg)[2:end])
2153-
ex = Expr(:call, GlobalRef(Test, :_materialize_broadcasted),
2154-
farg, ex.args[2:end]...)
2155-
end
2146+
Meta.isexpr(ex, (:call, :ref, :do)) || error("@inferred requires a call expression")
21562147
result = let ex = ex
21572148
quote
21582149
let allow = $(esc(allow))
21592150
allow isa Type || throw(ArgumentError("@inferred requires a type as second argument"))
2160-
$(if any(@nospecialize(a)->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex.args)
2161-
# Has keywords
2162-
args = gensym()
2163-
kwargs = gensym()
2164-
quote
2165-
$(esc(args)), $(esc(kwargs)), result = $(esc(Expr(:call, _args_and_call, ex.args[2:end]..., ex.args[1])))
2166-
inftype = $(gen_call_with_extracted_types(mod, Base.infer_return_type, :($(ex.args[1])($(args)...; $(kwargs)...)); is_source_reflection = false))
2167-
end
2168-
else
2169-
# No keywords
2170-
quote
2171-
args = ($([esc(ex.args[i]) for i = 2:length(ex.args)]...),)
2172-
result = $(esc(ex.args[1]))(args...)
2173-
inftype = Base.infer_return_type($(esc(ex.args[1])), Base.typesof(args...))
2174-
end
2175-
end)
2151+
result = $(esc(ex))
2152+
inftype = $(gen_call_with_extracted_types(mod, Base.infer_return_type, ex; is_source_reflection = false))
21762153
rettype = result isa Type ? Type{result} : typeof(result)
21772154
rettype <: allow || rettype == typesplit(inftype, allow) || error("return type $rettype does not match inferred return type $inftype")
21782155
result

stdlib/Test/test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,10 @@ uninferable_kwtest(x; y=1) = 2x+y
745745
@test (@inferred uninferable_kwtest(1)) == 3
746746
@test (@inferred uninferable_kwtest(1; y=2)) == 4
747747

748+
# Issue #59114
749+
# @inferred with do-blocks
750+
@test @inferred(map([1,2]) do x x end) == [1, 2]
751+
748752
@test_throws ErrorException @testset "$(error())" for i in 1:10
749753
end
750754
@test_throws ErrorException @testset "$(error())" begin

0 commit comments

Comments
 (0)