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

overhaul to allow for different labels #8

Merged
merged 9 commits into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
51 changes: 36 additions & 15 deletions src/OptionalArgChecks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ module OptionalArgChecks
using IRTools: @dynamo, IR, recurse!, block, branches, branch!
using MacroTools: postwalk

simeonschaub marked this conversation as resolved.
Show resolved Hide resolved
export @argcheck, @skipargcheck
export @mark, @elide, @skipargcheck

"""
@argcheck ex
@mark label ex

Marks `ex` as an optional argument check, so when a function is called via
[`@skipargcheck`](@ref), `ex` will be omitted.

```jldoctest
julia> function half(x::Integer)
@argcheck iseven(x) || throw(DomainError(x, "x has to be an even number"))
@mark argcheck iseven(x) || throw(DomainError(x, "x has to be an even number"))
return x ÷ 2
end
half (generic function with 1 method)
Expand All @@ -30,22 +30,38 @@ julia> @skipargcheck half(3)
1
```
"""
macro argcheck(ex)
return Expr(:block, Expr(:meta, :begin_argcheck), esc(ex), Expr(:meta, :end_argcheck))
macro mark(label, ex)
label isa Symbol || error("label has to be a Symbol")
return Expr(
:block,
Expr(:meta, :begin_optional, label),
esc(ex),
Expr(:meta, :end_optional, label),
)
end

@dynamo function skipargcheck(x...)
struct ElideCheck{label}
ElideCheck(label::Symbol) = new{label}()
Copy link
Contributor

Choose a reason for hiding this comment

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

Any particular reason, why label is not a field, but a type parameter?

Copy link
Owner Author

Choose a reason for hiding this comment

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

I need to do some benchmarks, whether there is actually any difference

Copy link
Contributor

Choose a reason for hiding this comment

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

Just for my understanding. You are talking about compile time performance, not run time right? Because as I understand it at run time no ElideCheck objects exist anymore.

Copy link
Owner Author

Choose a reason for hiding this comment

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

Ok, now that I've looked into this, a type parameter is definitely the way to do this. A @dynamo cannot access any fields of ElideCheck, so the only way to access the label would be to insert a statement into the IR, which wouldn't even work in this case. This makes sense, if you think about it, because otherwise IRTools would have to recompile f every time, since label is not a compile time constant.

end

@dynamo function (::ElideCheck{label})(x...) where {label}
ir = IR(x...)
ir === nothing && return
next = iterate(ir)
while next !== nothing
(x, st), state = next
if Meta.isexpr(st.expr, :meta) && st.expr.args[1] === :begin_argcheck
if Meta.isexpr(st.expr, :meta) &&
st.expr.args[1] === :begin_optional &&
st.expr.args[2] === label

orig = block(ir, x)
delete!(ir, x)

(x, st), state = iterate(ir, state)
while !(Meta.isexpr(st.expr, :meta) && st.expr.args[1] === :end_argcheck)
while !(Meta.isexpr(st.expr, :meta) &&
st.expr.args[1] === :end_optional &&
st.expr.args[2] === label)

delete!(ir, x)
(x, st), state = iterate(ir, state)
end
Expand All @@ -63,20 +79,25 @@ end
return ir
end

macro elide(label, ex)
label isa Symbol || error("label has to be a Symbol")
ex = postwalk(ex) do x
if Meta.isexpr(x, :call)
pushfirst!(x.args, Expr(:call, GlobalRef(@__MODULE__, :ElideCheck), Expr(:quote, label)))
end
return x
end
return esc(ex)
end

"""
@skipargcheck ex

For every function call in `ex`, expressions wrapped in [`@argcheck`](@ref) get omitted
recursively.
"""
macro skipargcheck(ex)
ex = postwalk(ex) do x
if Meta.isexpr(x, :call)
pushfirst!(x.args, GlobalRef(@__MODULE__, :skipargcheck))
end
return x
end
return esc(ex)
return :(@elide argcheck $(esc(ex)))
end

end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Test
using OptionalArgChecks

function f(x)
@argcheck x<2 && if x == 1
@mark argcheck x<2 && if x == 1
return "foo"
else
error("Test")
Expand Down