Skip to content

Commit

Permalink
fix #454: work around a weird miscompilation
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Sep 17, 2024
1 parent 6ab811f commit c959e39
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/core/CCVector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ let append a b =

let[@inline] get v i =
if i < 0 || i >= v.size then invalid_arg "CCVector.get";
Array.unsafe_get v.vec i
(* NOTE: over eager inlining seems to miscompile for int32 at least (#454) *)
Sys.opaque_identity (Array.unsafe_get v.vec i)

let[@inline] set v i x =
if i < 0 || i >= v.size then invalid_arg "CCVector.set";
Expand Down Expand Up @@ -282,7 +283,8 @@ let[@inline] top v =

let[@inline] top_exn v =
if v.size = 0 then raise Empty;
Array.unsafe_get v.vec (v.size - 1)
(* NOTE: over eager inlining seems to miscompile for int32 at least (#454) *)
Sys.opaque_identity (Array.unsafe_get v.vec (v.size - 1))

let[@inline] copy v = { size = v.size; vec = Array.sub v.vec 0 v.size }

Expand Down
16 changes: 15 additions & 1 deletion tests/core/t_vector.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module T = (val Containers_testlib.make ~__FILE__ ())
include T
open CCVector;;
open CCVector

let spf = Printf.sprintf;;

t @@ fun () -> create_with ~capacity:200 1 |> capacity >= 200;;
t @@ fun () -> return 42 |> to_list = [ 42 ];;
Expand Down Expand Up @@ -751,3 +753,15 @@ push v 0;
push v 0;
push v 0;
6 = foldi (fun i acc _ -> acc + i) 0 v
;;

t ~name:"reg454" @@ fun () ->
let arr : Int32.t vector = create () in
CCVector.push arr (Int32.of_int 123456);
let s = spf "%d\n" (Int32.to_int (CCVector.get arr 0)) in
Printf.eprintf "%d\n" (Int32.to_int (CCVector.get arr 0));
let x = CCVector.get arr 0 in
let s2 = spf "%d\n" (Int32.to_int x) in
Printf.eprintf "%d\n" (Int32.to_int x);
assert_equal ~printer:(spf "%S") s s2;
true

0 comments on commit c959e39

Please sign in to comment.