diff --git a/doc/changes/fixed/16180.md b/doc/changes/fixed/16180.md new file mode 100644 index 00000000000..03757bbe62f --- /dev/null +++ b/doc/changes/fixed/16180.md @@ -0,0 +1,2 @@ +- Prevent diagnostic source locations at compact-position field boundaries from + being truncated (#16180, @rgrinberg) diff --git a/otherlibs/stdune/src/compact_position.ml b/otherlibs/stdune/src/compact_position.ml index 8b0700ba824..772cf85808e 100644 --- a/otherlibs/stdune/src/compact_position.ml +++ b/otherlibs/stdune/src/compact_position.ml @@ -9,8 +9,7 @@ module Position = struct let shift_cnum = 2 * field_size let small_enough = - let max_size = 1 lsl field_size in - let test int = int <= max_size in + let test int = int >= 0 && int <= field_mask in fun[@inline] { Lexing.pos_bol; pos_cnum; pos_lnum; pos_fname = _ } -> test pos_bol && test pos_cnum && test pos_lnum ;; @@ -73,10 +72,7 @@ module Same_line_loc = struct create ~bol ~lnum ~start_cnum:stop_cnum ~stop_cnum ;; - let small_enough = - let max_size = 1 lsl field_size in - fun[@inline] int -> int <= max_size - ;; + let[@inline] small_enough int = int >= 0 && int <= field_mask let[@inline] to_loc t ~fname:pos_fname = let pos_lnum = lnum t in diff --git a/otherlibs/stdune/test/compact_position_tests.ml b/otherlibs/stdune/test/compact_position_tests.ml index fa65d6669c4..11869c5786f 100644 --- a/otherlibs/stdune/test/compact_position_tests.ml +++ b/otherlibs/stdune/test/compact_position_tests.ml @@ -27,23 +27,9 @@ let%expect_test "round trip tests" = test { base with pos_cnum = (1 lsl 21) - 1; pos_lnum = 2_200; pos_bol = 300 }; [%expect {| [PASS] |}]; test { base with pos_cnum = 1 lsl 21; pos_lnum = 2_200; pos_bol = 300 }; - [%expect - {| - [FAIL] - expected: - { pos_lnum = 2200; pos_bol = 300; pos_cnum = 2097152 } - received: - { pos_lnum = 2200; pos_bol = 300; pos_cnum = 0 } - |}]; + [%expect {| position too large |}]; test { base with pos_cnum = -1; pos_lnum = 2_200; pos_bol = 300 }; - [%expect - {| - [FAIL] - expected: - { pos_lnum = 2200; pos_bol = 300; pos_cnum = -1 } - received: - { pos_lnum = 2200; pos_bol = 300; pos_cnum = 2097151 } - |}]; + [%expect {| position too large |}]; test { base with pos_cnum = 1 lsl 32; pos_lnum = 2_200; pos_bol = 300 }; [%expect {| position too large |}] ;; @@ -55,5 +41,5 @@ let%expect_test "same-line locations at the field boundary" = | Same_line _ -> print_endline "same-line encoding overflowed" | Loc _ -> print_endline "used the wider encoding" | Loc_does_not_fit -> print_endline "location does not fit"); - [%expect {| same-line encoding overflowed |}] + [%expect {| used the wider encoding |}] ;;