Skip to content

Commit 17c67d3

Browse files
authored
Improve error handling in WrappedUnions.jl (#25)
1 parent 656df03 commit 17c67d3

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

src/WrappedUnions.jl

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,34 @@ call will be type-stable.
110110
end
111111
final_kw_args = [Expr(:kw, name, val) for (name, val) in final_kw_args_map]
112112
func = iswrappedunion(F) ? :(unwrap(f)) : :f
113-
body = :($func($(final_pos_args...); $(final_kw_args...)))
113+
body = :(return $func($(final_pos_args...); $(final_kw_args...)))
114+
unwrapped_tup = []
114115
for (source, id, T) in reverse(wrappedunion_args)
115116
unwrapped_var = source == :pos ? Symbol("v_pos_", id) : Symbol("v_kw_", id)
116117
original_arg = source == :pos ? :(args[$id]) : :(kwargs.$id)
118+
push!(unwrapped_tup, (unwrapped_var, original_arg))
117119
wrapped_types = Base.uniontypes(fieldtype(T, 1))
118-
branch_expr = :(error("THIS_SHOULD_BE_UNREACHABLE"))
120+
branch_expr = nothing
119121
for V_type in reverse(wrapped_types)
120122
condition = :($unwrapped_var isa $V_type)
121-
branch_expr = Expr(:elseif, condition, body, branch_expr)
123+
branch_expr = isnothing(branch_expr) ?
124+
Expr(:elseif, condition, body) : Expr(:elseif, condition, body, branch_expr)
122125
end
123126
branch_expr = Expr(:if, branch_expr.args...)
124127
body = quote
125-
$unwrapped_var = unwrap($original_arg)
126128
$branch_expr
127129
end
128130
end
129-
return body
131+
for t in unwrapped_tup
132+
body = quote
133+
$(t[1]) = unwrap($(t[2]))
134+
$body
135+
end
136+
end
137+
return quote
138+
$body
139+
error("UNREACHABLE_REACHED")
140+
end
130141
end
131142

132143
"""

0 commit comments

Comments
 (0)