Skip to content

WIP : Allow @inferred to accept do-block syntax (fix #59114) #59152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 3 additions & 26 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions stdlib/Test/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down