You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
julia>f(x) =
(x =2; x =3;
x =1)
f (generic function with 1 method)
julia> m =@code_loweredf(2.0);
julia> m.code
5-element Array{Any,1}:
:(_3 = _2)
:(_3 =2)
:(_3 =3)
:(_3 =1)
:(return1)
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>functiong(x)
(x =2; x =3;
x =1)
end
g (generic function with 1 method)
julia> m2 =@code_loweredg(2.0);
julia> m2.code
5-element Array{Any,1}:
:(_3 = _2)
:(_3 =2)
:(_3 =3)
:(_3 =1)
:(return1)
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.
The text was updated successfully, but these errors were encountered:
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
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.)
Why is the statement where
x
gets assigned to 2 said to be on line1
and the one where it is assigned to 3 on line2
?Compare to the long form version of defining functions:
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.
The text was updated successfully, but these errors were encountered: