johnyob / grace

A fancy diagnostics library that allows your compilers to exit with grace
MIT License
72 stars 2 forks source link

πŸ› Bug: Empty ranges #1

Open johnyob opened 9 months ago

johnyob commented 9 months ago

Context

Operating System: macOS

When printing a diagnostic containing a label with an empty range, the renderer incorrectly panics.

Current behaviour

open! Grace

let source : Source.t = 
  `String { name = Some "hello.txt"; content = "Hello world!\nBye world!\n   " }
;;

let range start stop = Range.create ~source (Byte_index.of_int start) (Byte_index.of_int stop)

let diagnostic = 
  Diagnostic.(createf ~labels:[ Label.primaryf ~range:(range 6 6) "middle" ] Note "empty range")
;;

let () =
  Format.printf
    "%a@."
    Grace_rendering.(Ansi.pp_diagnostic ~config:Config.default)
    diagnostic
;;
Uncaught exception:                  

  "Assert_failure lib/rendering/snippet.ml:218:4"

Raised at Grace_rendering__Snippet.Of_diagnostic.segments_of_labels in file "lib/rendering/snippet.ml", line 218, characters 4-40
Called from Grace_rendering__Snippet.Of_diagnostic.line_of_labels in file "lib/rendering/snippet.ml", line 225, characters 19-54
Called from Grace_rendering__Snippet.Of_diagnostic.lines_of_labels.(fun) in file "lib/rendering/snippet.ml", line 362, characters 12-148
Called from Iter.fold.(fun) in file "src/Iter.ml", line 77, characters 23-31
Called from Grace_rendering__Source_reader.lines_in_range in file "lib/rendering/source_reader.ml", line 279, characters 4-59
Called from Iter.fold in file "src/Iter.ml", line 77, characters 2-32
Called from Grace_rendering__Snippet.Of_diagnostic.lines_of_labels in file "lib/rendering/snippet.ml", line 320, characters 6-1023
Called from Grace_rendering__Snippet.Of_diagnostic.block_of_labels in file "lib/rendering/snippet.ml" (inlined), line 373, characters 4-33
Called from Grace_rendering__Snippet.Of_diagnostic.of_diagnostic.(fun) in file "lib/rendering/snippet.ml", line 389, characters 35-61
Called from Base__List.count_map in file "src/list.ml", line 479, characters 13-17
Called from Grace_rendering__Snippet.Of_diagnostic.of_diagnostic in file "lib/rendering/snippet.ml", line 378, characters 6-507
Called from Grace_rendering__Ansi.pp_diagnostic.(fun) in file "lib/rendering/ansi.ml", line 712, characters 16-48
Called from Stdlib__Fun.protect in file "fun.ml", line 33, characters 8-15
Re-raised at Stdlib__Fun.protect in file "fun.ml", line 38, characters 6-52
Called from Stdlib__Format.output_acc in file "format.ml", line 1295, characters 4-20
Called from Stdlib__Format.kfprintf.(fun) in file "format.ml", line 1356, characters 16-34
Called from Dune__exe__Main in file "examples/main.ml", line 59, characters 2-105

Expected behaviour

    note: empty range
        β”Œβ”€ hello:1:7
      1 β”‚  Hello world!
        β”‚        ^ middle