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

Allow empty return statements #446

Merged
merged 2 commits into from
Jan 9, 2024
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
12 changes: 9 additions & 3 deletions src/macros.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import MacroTools: splitdef, combinedef, isexpr, postwalk

function find_return(stmt)
# Check whether there are `return` statements that return something else but `nothing`
function contains_return_something(stmt)
result = false
postwalk(stmt) do expr
result |= @capture(expr, return x_)
if @capture(expr, return x_)
# `nothing` indicates a stand-alone return statement, `:nothing` indicates a `return nothing`
result |= x !== nothing && x !== :nothing
end
expr
end
result
Expand All @@ -14,7 +18,9 @@ function __kernel(expr, generate_cpu=true, force_inbounds=false)
def = splitdef(expr)
name = def[:name]
args = def[:args]
find_return(expr) && error("Return statement not permitted in a kernel function $name")
if contains_return_something(expr)
error("Return statement (except `return` or `return nothing`) not permitted in kernel function $name")
end

constargs = Array{Bool}(undef, length(args))
for (i, arg) in enumerate(args)
Expand Down
55 changes: 53 additions & 2 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,64 @@ end

@conditional_testset "return statement" skip_tests begin
try
@eval @kernel function kernel_return()
@eval @kernel function kernel_return1()
return 1
end
@test false
catch e
@test e.error ==
ErrorException("Return statement (except `return` or `return nothing`) not permitted in kernel function kernel_return1")
end
try
@eval @kernel function kernel_return2()
x = nothing
return x
end
@test false
catch e
@test e.error ==
ErrorException("Return statement (except `return` or `return nothing`) not permitted in kernel function kernel_return2")
end
try
@eval @kernel function kernel_return3()
return
end
@test true
catch e
@test false
end
try
@eval @kernel function kernel_return4()
return nothing
end
@test true
catch e
@test false
end
try
@eval @kernel function kernel_return5()
if something()
return something2()
end
something3()
return
end
@test false
catch e
@test e.error ==
ErrorException("Return statement not permitted in a kernel function kernel_return")
ErrorException("Return statement (except `return` or `return nothing`) not permitted in kernel function kernel_return5")
end
try
@eval @kernel function kernel_return6()
if something()
return
end
something3()
return
end
@test true
catch e
@test false
end
end

Expand Down
Loading