Incorrect location of an error when the line is terminated by semicolon

Possible bug?
In the code,

a = 1
b ::Dict{String => String} = Dict();

Julia reports an error in line 1. More precisely, in the previously executed line. Without the semicolon after Dict(), the error is correctly reported in line 2.

More details

➜ echo -e "a = 1\n b ::Dict{String => String} = Dict();\n" > a.jl &&  julia a.jl
ERROR: LoadError: TypeError: in Type, in parameter, expected Type, got a value of type Pair{DataType, DataType}
Stacktrace:
 [1] top-level scope
   @ ~/..../a.jl:1
in expression starting at /home/..../a.jl:1

➜ echo -e "a = 1\n b ::Dict{String => String} = Dict()\n" > a.jl &&  julia a.jl
ERROR: LoadError: TypeError: in Type, in parameter, expected Type, got a value of type Pair{DataType, DataType}
Stacktrace:
 [1] top-level scope
   @ ~/..../a.jl:2
in expression starting at /home/..../a.jl:2

With syntax errors, the error is pinpointed correctly regardless of whether the semicolon is present or not.

2 Likes
julia> Meta.parseall("a = 1\n b ::Dict{String => String} = Dict();\n")
:($(Expr(:toplevel, :(#= none:1 =#), :(a = 1), :(#= none:1 =#), :($(Expr(:toplevel, :(b::Dict{String => String} = Dict())))))))

julia> Meta.parseall("a = 1\n b ::Dict{String => String} = Dict()\n")
:($(Expr(:toplevel, :(#= none:1 =#), :(a = 1), :(#= none:2 =#), :(b::Dict{String => String} = Dict()))))

Note the duplicated internal line number :(#= none:1 =#) preceding the ; chain; that is showing up in the stacktrace. Good news is the expected behavior can happen a couple versions later; bad news it seems there’s some hurdles in the way of backports to released versions: parser gives wrong line number of codes with semi-colon · Issue #57645 · JuliaLang/julia. At least semicolons aren’t necessary for delimiting lines in a multiline string and don’t suppress output in this context, so you could do without them for now.

Oh, snap. I quickly searched and found nothing, so I wasn’t sure what to think about this. It’s good to know that it’s known.