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

Use Core.throw("unreachable") for unreachables instead of ReturnNode() #115

Merged
merged 3 commits into from
Oct 16, 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
6 changes: 5 additions & 1 deletion src/ir/wrap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ function IRCode(ir::IR)
x = get(defs, br.args[1], br.args[1]) |> unvars
push!(stmts, ReturnNode(x))
elseif br == unreachable
push!(stmts, ReturnNode())
@static if VERSION >= v"1.10"
push!(stmts, ReturnNode())
else
push!(stmts, Expr(:call, GlobalRef(Core, :throw), "unreachable"))
end
elseif br.condition == nothing
push!(stmts, GotoNode(br.block))
else
Expand Down
2 changes: 1 addition & 1 deletion src/reflection/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function slots!(ci::CodeInfo)
end
if VERSION >= v"1.6.0-DEV.272"
ci.code[i] = MacroTools.prewalk(ci.code[i]) do x
x isa Core.ReturnNode ? Core.ReturnNode(f(x.val)) :
x isa Core.ReturnNode ? (isdefined(x,:val) ? Core.ReturnNode(f(x.val)) : x) :
x isa Core.GotoIfNot ? Core.GotoIfNot(f(x.cond), x.dest) :
f(x)
end
Expand Down
19 changes: 18 additions & 1 deletion test/compiler.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using IRTools, MacroTools, InteractiveUtils, Test
using IRTools: @dynamo, IR, meta, isexpr, xcall, self, insertafter!, recurse!,
argument!, return!, func, var, functional
argument!, return!, block!, branch!, func, var, functional
using InteractiveUtils: code_typed

@dynamo roundtrip(a...) = IR(a...)

Expand Down Expand Up @@ -194,3 +195,19 @@ function f94(x)
end
@test f94(1) == 1
@test passthrough(f94, 1) == 1

@testset "unreachable" begin
# 1: (%1)
# return nothing
# 2:
# unreachable
ir = IR()
argument!(ir)
return!(ir, nothing)
block!(ir)
branch!(ir, 0)

func_ir = IRTools.func(ir)
@test (code_typed(func_ir, Tuple{typeof(func_ir)}) |> only
isa Pair{Core.CodeInfo,DataType})
end