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

Support try/catch on the happy (nothrow) path #1474

Merged
merged 9 commits into from
Sep 24, 2024
39 changes: 37 additions & 2 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ end
@test only(g) ∈ (1., 2.)
end

function f_try_catch(x,y)
function throws_and_catches_if_x_negative(x,y)
z = x + y
try
if x < 0.
Expand All @@ -259,14 +259,49 @@ function f_try_catch(x,y)
return 3z
end

function try_catch_finally(cond, x)

try
x = 2x
cond && throw(DomainError())
catch
x = 2x
finally
x = 3x
end

x
end

if VERSION >= v"1.8"
# try/catch/else is invalid syntax prior to v1.8
eval(Meta.parse("""
function try_catch_else(cond, x)
end
"""))
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code seems unused and incomplete


@testset "try/catch" begin
@testset "happy path (nothrow)" begin
res, (dx,dy) = withgradient(f_try_catch, 1., 2.)
res, (dx,dy) = withgradient(throws_and_catches_if_x_negative, 1., 2.)
@test res == 3 * (2 * (1. + 2.) + 1. + 2.)
@test dx == 3. * (2. + 1.)
@test dy == 3. * (2. + 1.)
end

@testset "try/catch/finally" begin
res, (_, dx,) = withgradient(try_catch_finally, false, 1.)
@test res == 6.
@test dx == 6.

res, pull = pullback(try_catch_finally, true, 1.)
@test res == 12.
@test_throws ErrorException pull(1.)
err = try pull(1.) catch ex; ex end
@test occursin("Can't differentiate function execution in catch block",
string(err))
end

function foo_try(f)
y = 1
try
Expand Down
Loading