JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.73k stars 5.49k forks source link

Suggestion to make an unhelpful error message more helpful #52674

Open DavidSagan opened 10 months ago

DavidSagan commented 10 months ago

Using: Julia 1.10.0 Consider the following test file:

function f1()
  if true
  endif
end

function f2()
end

There is a typo in f1 and endif should be end. Loading this file generates the error message:

julia> include("z.jl")
ERROR: LoadError: ParseError:
# Error @ /Users/dcs16/z.jl:7:5
function f2()
end
#  
└ ── Expected `end`
Stacktrace:
 [1] top-level scope
   @ ~/z.jl:7
 [2] include(fname::String)
   @ Base.MainInclude ./client.jl:489
 [3] top-level scope
   @ REPL[5]:1
in expression starting at /Users/dcs16/z.jl:7

The problem is that the line number given by the error message is the line at the end of the file. In a real life situation the file could be hundreds or more lines long so the error message is fairly useless in terms of localizing the error. Trying to find where the error can thus be a lengthy process of dividing the file into half, testing, and then further dividing, etc., until the error is localized.

As a feature request, if it were possible to modify the error message so that it gave where the unmatched block started (line 2 in this example) this would be very useful and substantially cut down on debugging time.

giordano commented 10 months ago

modify the error message so that it gave where the unmatched block started (line 2 in this example)

Technically, the if is closed by the end on line 4. What the error is saying is that the function f1 is missing its closing end, f2 is seen as defined inside the body of f1. How can the parser possibly figure out the user misspelled the closing end of the if?

DavidSagan commented 10 months ago

@giordano Yes you are correct. I should have said that it would be useful to give where the unmatched block started which is indeed the function f1() line (line 1). This does not alter the fact that it would be useful to know where the unmatched block begins.

Tortar commented 6 months ago

Indeed adding where the block started would make a lot easier to spot where you missed an end which is something happening a lot to me, a reminiscence of python :D