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

hide internally created methods in stacktraces that occur as a result of argument forwarding #49102

Merged
merged 4 commits into from
Mar 23, 2023

Conversation

KristofferC
Copy link
Member

@KristofferC KristofferC commented Mar 22, 2023

Inspired by #49044, I tried to make a small incremental improvement. This hides some frames that are automatically generated in kw... forwarding.

Running

julia> sum([])

and looking at the stacktrace we have before:

[12] #_sum#810
@ ./reducedim.jl:999 [inlined]
[13] _sum
@ ./reducedim.jl:999 [inlined]
[14] #_sum#809
@ ./reducedim.jl:998 [inlined]
[15] _sum
@ ./reducedim.jl:998 [inlined]
[16] #sum#807
@ ./reducedim.jl:994 [inlined]
[17] sum(a::Vector{Any})
@ Base ./reducedim.jl:994
[18] top-level scope
@ REPL[1]:1

and after:

 [11] _sum
    @ ./reducedim.jl:999 [inlined]
 [12] _sum
    @ ./reducedim.jl:998 [inlined]
 [13] sum(a::Vector{Any})
    @ Base ./reducedim.jl:994
 [14] top-level scope
    @ REPL[5]:1

The "MWE" for this is

f(g, a; kw...) = error();
@inline f(a; kw...) = f(identity, a; kw...);
f(1)

which before shows as

julia> f(1)
ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] f(g::Function, a::Int64; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Main ./REPL[1]:1
 [3] f(g::Function, a::Int64)
   @ Main ./REPL[1]:1
 [4] #f#4
   @ ./REPL[2]:1 [inlined]
 [5] f(a::Int64)
   @ Main ./REPL[2]:1
 [6] top-level scope
   @ REPL[3]:1

and after (frame 4 above removed).

julia> f(1)
ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] f(g::Function, a::Int64; kw::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
   @ Main ./REPL[7]:1
 [3] f(g::Function, a::Int64)
   @ Main ./REPL[7]:1
 [4] f(a::Int64)
   @ Main ./REPL[6]:1
 [5] top-level scope
   @ REPL[8]:1

There are more cases to detect, for example:

julia> g(x, y=1, z=2) = error()
g (generic function with 3 methods)

julia> g(1)
ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] g(x::Int64, y::Int64, z::Int64)
   @ Main ./REPL[5]:1
 [3] g(x::Int64)
   @ Main ./REPL[5]:1
 [4] top-level scope
   @ REPL[6]:1

could maybe hide [3] (or [2]??) but it is also a bit hard to not also filter out "real" calls.

@KristofferC KristofferC added the error messages Better, more actionable error messages label Mar 22, 2023
Copy link
Member

@vtjnash vtjnash left a comment

Choose a reason for hiding this comment

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

Can we move the kwcall hiding into the same place (instead of the process_backtrace function below), using this same approach of looking at the parent too?

@KristofferC
Copy link
Member Author

What would the kwcall check in the parent?

Looking at

julia> f(; a, b) = error()

julia> f(a=3)
ERROR: UndefKeywordError: keyword argument `b` not assigned
Stacktrace:
 [1] kwcall(::NamedTuple{(:a,), Tuple{Int64}}, ::typeof(f))
   @ Main ./REPL[2]:1
 [2] top-level scope
   @ REPL[4]:1

there doesn't even really seem to be a parent to the kwcall?

Copy link
Member

@timholy timholy left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this!

base/errorshow.jl Outdated Show resolved Hide resolved
@KristofferC
Copy link
Member Author

KristofferC commented Mar 22, 2023

Looking at another case of non ideal stacktraces:

f(g, a; kw...) = error();
f(a; kw...) = f(identity, a; kw...);
f(1; kw=3)

giving

ERROR: 
Stacktrace:
 [1] error()
   @ Base ./error.jl:44
 [2] f(g::Function, a::Int64; kw::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:kw,), Tuple{Int64}}})
   @ Main ./REPL[1]:1
 [3] f(a::Int64; kw::Base.Pairs{Symbol, Int64, Tuple{Symbol}, NamedTuple{(:kw,), Tuple{Int64}}})
   @ Main ./REPL[2]:1
 [4] top-level scope
   @ REPL[4]:1
 [5] top-level scope
   @ ~/julia/usr/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1410

seems like the printing of the kw arg could be improved. But that doesn't really have anything to do with collapsing frames.

@KristofferC KristofferC changed the title hide internally created methods in stacktraces that sometimes occur in kw... forwarding methods hide internally created methods in stacktraces that occur as a result of argument forwarding Mar 22, 2023
@timholy
Copy link
Member

timholy commented Mar 22, 2023

The new code LGTM. Merge at will.

@KristofferC KristofferC merged commit 703b3f8 into master Mar 23, 2023
@KristofferC KristofferC deleted the kc/stack_collapse branch March 23, 2023 13:25
params, last_params = Base.unwrap_unionall(m.sig).parameters, Base.unwrap_unionall(last_m.sig).parameters

if last_m.nkw != 0
pos_sig_params = Base.rewrap_unionall(Tuple{last_params[(last_m.nkw+2):end]...}, last_m.sig).parameters
Copy link
Member

Choose a reason for hiding this comment

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

Seems like you meant just last_params[(last_m.nkw+2):end] here, since the rest doesn't generally make sense

Copy link
Member Author

Choose a reason for hiding this comment

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

I took it from here:

pos_sig = Base.rewrap_unionall(Tuple{uw.parameters[(def.nkw+2):end]...}, sig)

Copy link
Member

Choose a reason for hiding this comment

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

That is useful if you want to do subtyping queries, but it doesn't seem like you wanted to?

Copy link
Member

Choose a reason for hiding this comment

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

You potentially could phrase it as a subtyping query though, by adding Vararg to the shorter one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
error messages Better, more actionable error messages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants