Skip to content

Commit

Permalink
inference: handle cases where :the_exception is used independently
Browse files Browse the repository at this point in the history
`Expr(:the_exception)` is handled by the interpreter in all cases,
however, inference assumes it is only used within frames containing
`try/catch`. This commit corrects that assumption.
  • Loading branch information
aviatesk committed Dec 16, 2024
1 parent 13f446e commit 3bd969a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Compiler/src/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3240,8 +3240,16 @@ function abstract_eval_throw_undef_if_not(interp::AbstractInterpreter, e::Expr,
end

function abstract_eval_the_exception(::AbstractInterpreter, sv::InferenceState)
(;handlers, handler_at) = sv.handler_info::HandlerInfo
return the_exception_info(handlers[handler_at[sv.currpc][2]].exct)
(;handler_info) = sv
if handler_info === nothing
return the_exception_info(Any)
end
(;handlers, handler_at) = handler_info
handler_id = handler_at[sv.currpc][2]
if handler_id === 0
return the_exception_info(Any)
end
return the_exception_info(handlers[handler_id].exct)
end
abstract_eval_the_exception(::AbstractInterpreter, ::IRInterpretationState) = the_exception_info(Any)
the_exception_info(@nospecialize t) = RTEffects(t, Union{}, Effects(EFFECTS_TOTAL; consistent=ALWAYS_FALSE))
Expand Down
14 changes: 14 additions & 0 deletions Compiler/test/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6125,3 +6125,17 @@ function func_swapglobal!_must_throw(x)
end
@test Base.infer_return_type(func_swapglobal!_must_throw, (Int,); interp=SwapGlobalInterp()) === Union{}
@test !Compiler.is_effect_free(Base.infer_effects(func_swapglobal!_must_throw, (Int,); interp=SwapGlobalInterp()) )

@eval get_exception() = $(Expr(:the_exception))
@test Base.infer_return_type() do
get_exception()
end <: Any
@test @eval Base.infer_return_type((Float64,)) do x
out = $(Expr(:the_exception))
try
out = sin(x)
catch
out = $(Expr(:the_exception))
end
return out
end == Union{Float64,DomainError}

0 comments on commit 3bd969a

Please sign in to comment.