JuliaLang / JuliaSyntax.jl

The Julia compiler frontend
Other
274 stars 33 forks source link

Crash formatting errors arising from `parseatom` #314

Closed c42f closed 1 year ago

c42f commented 1 year ago

Printing errors arising from parseatom sometimes fails, but not parsestmt or parseall.

MWE:

julia> exc = try JuliaSyntax.parseatom(Expr, ")") ; catch e ; e end
JuliaSyntax.ParseError(SourceFile("", 0, nothing, 1, [1, 1]), JuliaSyntax.Diagnostic[JuliaSyntax.Diagnostic(1, 0, :error, "unexpected `)`"), JuliaSyntax.Diagnostic(1, 1, :error, "unexpected text after parsing atom")], :none)

julia> showerror(stdout, exc)
ParseError:
# Error @ line 1:1
ERROR: BoundsError: attempt to access empty SubString{String} at index [0]
Stacktrace:
 [1] checkbounds(s::SubString{String}, I::Int64)
   @ Base ./strings/basic.jl:216 [inlined]
 [2] getindex(s::SubString{String}, i::Int64)
   @ Base ./strings/substring.jl:91
 [3] getindex(source::SourceFile, i::Int64)
   @ JuliaSyntax ~/.julia/dev/JuliaSyntax/src/source_files.jl:120
 [4] highlight(io::Base.TTY, source::SourceFile, range::UnitRange{…}; color::Tuple{…}, context_lines_before::Int64, context_lines_inner::Int64, context_lines_after::Int64, note::String, notecolor::Symbol)
   @ JuliaSyntax ~/.julia/dev/JuliaSyntax/src/source_files.jl:222
 [5] kwcall(::NamedTuple, ::typeof(JuliaSyntax.highlight), io::IO, source::SourceFile, range::UnitRange)
   @ JuliaSyntax ~/.julia/dev/JuliaSyntax/src/source_files.jl:190 [inlined]
 [6] show_diagnostic(io::Base.TTY, diagnostic::JuliaSyntax.Diagnostic, source::SourceFile)
   @ JuliaSyntax ~/.julia/dev/JuliaSyntax/src/diagnostics.jl:76
 [7] showerror(io::Base.TTY, err::JuliaSyntax.ParseError; limit::Bool)
   @ JuliaSyntax ~/.julia/dev/JuliaSyntax/src/parser_api.jl:21 [inlined]
 [8] showerror(io::Base.TTY, err::JuliaSyntax.ParseError)
   @ JuliaSyntax ~/.julia/dev/JuliaSyntax/src/parser_api.jl:18
 [9] top-level scope
   @ REPL[108]:1
Some type information was truncated. Use `show(err)` to see complete types.

Found in https://github.com/JuliaLang/julia/issues/50245

c42f commented 1 year ago

Actually this can also happen with parsestmt if there's trailing text

julia> exc = try JuliaSyntax.parsestmt(Expr, "x\nz") ; catch e ; e end
JuliaSyntax.ParseError(SourceFile("x", 0, nothing, 1, [1, 2]), JuliaSyntax.Diagnostic[JuliaSyntax.Diagnostic(3, 3, :error, "unexpected text after parsing statement")], :none)

julia> showerror(stdout, exc)
ParseError:
ERROR: BoundsError: attempt to access 1-codeunit SubString{String} at index [2]
Stacktrace:
 [1] _nextind_str(s::SubString{String}, i::Int64)
   @ Base ./strings/string.jl:177
 [2] nextind(s::SubString{String}, i::Int64)
   @ Base ./strings/substring.jl:104 [inlined]
 [3] source_location(source::SourceFile, byte_index::Int64)
   @ JuliaSyntax ~/.julia/dev/JuliaSyntax/src/source_files.jl:64
 [4] show_diagnostic(io::Base.TTY, diagnostic::JuliaSyntax.Diagnostic, source::SourceFile)
   @ JuliaSyntax ~/.julia/dev/JuliaSyntax/src/diagnostics.jl:61
 [5] show_diagnostics(io::Base.TTY, diagnostics::Vector{JuliaSyntax.Diagnostic}, source::SourceFile)
   @ JuliaSyntax ~/.julia/dev/JuliaSyntax/src/diagnostics.jl:86
 [6] showerror(io::Base.TTY, err::JuliaSyntax.ParseError)
   @ JuliaSyntax ~/.julia/dev/JuliaSyntax/src/parser_api.jl:20
 [7] top-level scope
   @ REPL[122]:1