Skip to content
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

[compiler] Teach type inference that GotoIfNot can throw #48583

Merged
merged 1 commit into from
Feb 7, 2023
Merged
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
9 changes: 9 additions & 0 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2893,6 +2893,7 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
empty!(frame.pclimitations)
@goto find_next_bb
end
orig_condt = condt
if !(isa(condt, Const) || isa(condt, Conditional)) && isa(condx, SlotNumber)
# if this non-`Conditional` object is a slot, we form and propagate
# the conditional constraint on it
Expand Down Expand Up @@ -2924,6 +2925,14 @@ function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
handle_control_backedge!(interp, frame, currpc, stmt.dest)
@goto branch
else
if !⊑(𝕃ᵢ, orig_condt, Bool)
merge_effects!(interp, frame, EFFECTS_THROWS)
if !hasintersect(widenconst(orig_condt), Bool)
ssavaluetypes[currpc] = Bottom
@goto find_next_bb
end
end

# We continue with the true branch, but process the false
# branch here.
if isa(condt, Conditional)
Expand Down
8 changes: 8 additions & 0 deletions test/compiler/effects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -743,3 +743,11 @@ end

@test Base.ismutationfree(Type{Union{}})
@test Core.Compiler.is_total(Base.infer_effects(typejoin, ()))


# GotoIfNot should properly mark itself as throwing when given a non-Bool
# https://github.com/JuliaLang/julia/pull/48583
gotoifnot_throw_check_48583(x) = x ? x : 0
@test !Core.Compiler.is_nothrow(Base.infer_effects(gotoifnot_throw_check_48583, (Missing,)))
@test !Core.Compiler.is_nothrow(Base.infer_effects(gotoifnot_throw_check_48583, (Any,)))
@test Core.Compiler.is_nothrow(Base.infer_effects(gotoifnot_throw_check_48583, (Bool,)))