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

Line number for first statement(s) in short form of function definition is wrong #31458

Open
KristofferC opened this issue Mar 23, 2019 · 1 comment
Labels
parser Language parsing and surface syntax

Comments

@KristofferC
Copy link
Sponsor Member

KristofferC commented Mar 23, 2019

julia> f(x) = 
           (x = 2; x = 3;
            x = 1)
f (generic function with 1 method)

julia> m = @code_lowered f(2.0);

julia> m.code
5-element Array{Any,1}:
 :(_3 = _2) 
 :(_3 = 2)  
 :(_3 = 3)  
 :(_3 = 1)  
 :(return 1)

julia> m.linetable[m.codelocs]
5-element Array{Any,1}:
 Core.LineInfoNode(Main, :f, Symbol("REPL[54]"), 1, 0)
 Core.LineInfoNode(Main, :f, Symbol("REPL[54]"), 1, 0)
 Core.LineInfoNode(Main, :f, Symbol("REPL[54]"), 2, 0)
 Core.LineInfoNode(Main, :f, Symbol("REPL[54]"), 3, 0)
 Core.LineInfoNode(Main, :f, Symbol("REPL[54]"), 3, 0)

Why is the statement where x gets assigned to 2 said to be on line 1 and the one where it is assigned to 3 on line 2?

Compare to the long form version of defining functions:

julia> function g(x) 
           (x = 2; x = 3;
            x = 1)
       end
g (generic function with 1 method)

julia> m2 = @code_lowered g(2.0);

julia> m2.code
5-element Array{Any,1}:
 :(_3 = _2) 
 :(_3 = 2)  
 :(_3 = 3)  
 :(_3 = 1)  
 :(return 1)

julia> m2.linetable[m2.codelocs]
5-element Array{Any,1}:
 Core.LineInfoNode(Main, :g, Symbol("REPL[58]"), 2, 0)
 Core.LineInfoNode(Main, :g, Symbol("REPL[58]"), 2, 0)
 Core.LineInfoNode(Main, :g, Symbol("REPL[58]"), 2, 0)
 Core.LineInfoNode(Main, :g, Symbol("REPL[58]"), 3, 0)
 Core.LineInfoNode(Main, :g, Symbol("REPL[58]"), 3, 0)

The code is exactly the same, the statements are on the same line in the source code, but here the line numbers are correct for the first statement (line 2).

This makes it hard to print out where you are in the source code when stepping through it in e.g. a debugger.

@KristofferC KristofferC changed the title Line number for first statement in short form of function definition is wrong Line number for first statement(s) in short form of function definition is wrong Mar 23, 2019
@JeffBezanson JeffBezanson added the parser Language parsing and surface syntax label Mar 27, 2019
@c42f c42f mentioned this issue Mar 18, 2020
2 tasks
@c42f
Copy link
Member

c42f commented Mar 24, 2020

The problem is in the parser: to get the correct functionloc, the parser has a special case for short form functions which adds an Expr(:block) and LineNumberNode corresponding to the function name's location, not the location of the body:

julia> :(f(x) = 
           (x=1;))
:(f(x) = begin
          #= REPL[12]:1 =#
          x = 1
      end)

Essentially this is the exact opposite of #34820. So the fix is related, but not the same: we'd need some way to add an extra line number node for the right hand side of the assignment, but only if the assignment is a short form function definition. (I don't yet see quite how to fix this non-disruptively in the parser.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser Language parsing and surface syntax
Projects
None yet
Development

No branches or pull requests

3 participants