RFC: Implementing Rust's borrow checker as static analysis on top of merlin
I wonder if it's possible to implement a tool to do a static analysis of OCaml program in a similar way to Rust's borrow checker. It seems that merlin has both the AST and the typing information for the whole program (even with cross-references between files). One could augment types with some attributes that will describe the desired semantics with regards to linearity checking and implement a static analysis using merlin library to enforce the constraints in code.
Does that sound like a feasible approach?
Why would this not be better implemented in the OCaml compiler itself? (Apart from the argument that the compiler team may not necessarily want to include linearity checking in its code base).
The linearity annotations outside a module would/could be made available via mli files.
Of course it would be better to have opt-in linearity checking in OCaml type system and compiler. But compiler team is quite busy and won't have cycles to do this as this does not seem to be a priority.
Maybe prototyping it in "userland" would help get the benefits from linearity checking earlier and if external linearity checking tool gets popular, that might be a trigger to consider this feature for the OCaml compiler.