From 515c2edf6658fdce8d7d2beef43d6cceb2accc7b Mon Sep 17 00:00:00 2001 From: Jon Sterling Date: Sat, 2 May 2026 15:49:35 +0100 Subject: [PATCH] Make the import_graph field immutable (explanation follows) This is related to 7ba54ad1da28456aa7d35ab95c011bc95480ae57. The issue that the mentioned commit has fixed is a misunderstanding of the mutable value semantics of OCaml records. A simplified version of this bug is, I think, the following: type t = { foo : string; mutable bar : int} (* will crash on assertion *) let _ = let r1 = { foo = "hello"; bar = 0 } in let r2 = { r1 with foo = "hello world" } in r1.bar <- 2; assert (r2.bar = 2) When you replace a record by doing a functional update, the mutable fields are *new* rather than aliased. To put it another way, the following type `t2` has very different semantics from `t`: type t2 = { foo : string; bar : int ref } Anyway, this kind of thing is pretty subtle in OCaml (in contrast to something like Swift, where mutable fields of let-bound records cannot be mutated). For that reason, I think it's best to remove the mutability entirely. --- lib/compiler/State.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compiler/State.ml b/lib/compiler/State.ml index a205c42..67b5c08 100644 --- a/lib/compiler/State.ml +++ b/lib/compiler/State.ml @@ -21,7 +21,7 @@ type t = { index: Tree.t URI.Tbl.t; diagnostics: Error.t list URI.Tbl.t; graphs: (module Forest_graphs.S); - mutable import_graph: Forest_graph.t; + import_graph: Forest_graph.t; search_index: Forester_search.Index.t; history: Action.t list; hosts: (string, unit) Hashtbl.t; -- 2.51.2