Skip to content

Provide useful backtraces #6

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

Merged
merged 1 commit into from
Aug 2, 2016
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
27 changes: 25 additions & 2 deletions src/SimpleTraits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,20 @@ function traitfn(tfn)
else
val = :(::Type{$trait})
end
# Get line info for better backtraces
ln = findline(tfn)
if isa(ln, Expr)
pushloc = Expr(:meta, :push_loc, ln.args[2], fname, ln.args[1])
poploc = Expr(:meta, :pop_loc)
else
pushloc = poploc = nothing
end
retsym = gensym()
if hasmac
fn = :(@dummy $fname{$(typs...)}($val, $(args...)) = $fbody)
fn = :(@dummy $fname{$(typs...)}($val, $(args...)) = ($pushloc; $retsym = $fbody; $poploc; $retsym))
fn.args[1] = mac # replace @dummy
else
fn = :($fname{$(typs...)}($val, $(args...)) = $fbody)
fn = :($fname{$(typs...)}($val, $(args...)) = ($pushloc; $retsym = $fbody; $poploc; $retsym))
end
quote
$fname{$(typs...)}($(args...)) = (Base.@_inline_meta(); $fname($curmod.trait($trait), $(strip_tpara(args)...)))
Expand Down Expand Up @@ -208,6 +217,20 @@ Base.next(::GenerateTypeVars{:upcase}, state) = (Symbol("X$state"), state+1) # X
Base.next(::GenerateTypeVars{:lcase}, state) = (Symbol("x$state"), state+1) # x1,...
Base.done(::GenerateTypeVars, state) = false

####
# Annotating the source location
####

function findline(ex::Expr)
ex.head == :line && return ex
for a in ex.args
ret = findline(a)
isa(ret, Expr) && return ret
end
nothing
end
findline(arg) = nothing

####
# Extras
####
Expand Down
30 changes: 30 additions & 0 deletions test/backtraces.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
###### BEGIN: LINE NUMBER SENSITIVITY ##########
@traitdef BacktraceTr{X}

@traitimpl BacktraceTr{Integer}

@traitfn foo{X; BacktraceTr{X}}(A::X) = backtrace()
@traitfn foo{X; !BacktraceTr{X}}(A::X) = backtrace()
###### END: LINE NUMBER SENSITIVITY ##########
# (the tests below depend on the particular line numbers in the code above)

function hasline(bt, n)
for b in bt
lkup = StackTraces.lookup(b)
if length(lkup) >= 2
l1, l2 = lkup[1], lkup[2]
if (contains(string(l1.file), "backtraces.jl") &&
l1.func == :foo &&
l1.line == n &&
contains(string(l2.file), "SimpleTraits.jl"))
return true
end
end
end
false
end

@test hasline(foo(1), 6)
@test hasline(foo(1.0), 7)

nothing
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ trait = SimpleTraits.trait
# Other tests
#####
include("base-traits.jl")

if VERSION >= v"0.5.0-dev"
include("backtraces.jl")
end