Adding Refinement Types to Rust: A Feasibility Study

2024-12-24

This article explores the feasibility of adding refinement types to the Rust programming language. Drawing on experience with YAIOUOM, a static analyzer that used refinement types to check units of measure, the author examines approaches to implementing refinement types within Rust's type system. Several options are proposed, including modifications to trait resolution, type variable unification, and the introduction of a pluggable keyword mechanism for post-compilation type checking. An optimistic approach—ignoring unit information early in compilation and checking later—is deemed more practical. API design and error message handling are discussed. Future work involves gathering feedback, writing a rustc driver supporting plugins, and implementing several refinement types, potentially including a new version of YAIOUOM and subsets of Flux or Liquid Haskell.