Coccinelle for Rust Progress Report

https://news.ycombinator.com/rss Hits: 4
Summary

In collaboration with Inria (the French Institute for Research in Computer Science and Automation), Tathagata Roy shares the progress made over the past year on the CoccinelleForRust project, co-sponsored by Collabora. Coccinelle is a tool for automatic program matching and transformation that was originally developed for making large-scale changes to the Linux kernel source code (i.e., C code). Matches and transformations are driven by user-specific transformation rules in the form of abstracted patches, referred to as semantic patches. As the Linux kernel—and systems software more generally—is starting to adopt Rust, we are developing Coccinelle for Rust to make the power of Coccinelle available to Rust codebases. Example usage This diff illustrates a patch in which the type_of function was being called before confirming that the target item’s trait was implemented. A straightforward CfR-based fix is to find every expression of the form expression.type_of(impl_id) and replace it with expression.type_of(impl_id).subst_identity() There are roughly fifty occurrences of this pattern in the diff, so updating them all by hand would be quite tedious. The accompanying Semantic Patch to perform this transformation automatically is: @change_ty_of@ expression exp1, impl_id; @@ -exp1.type_of(impl_id) +exp1.type_of(impl_id).subst_identity() While the above example could be achieved with a complicated and unreadable regex pattern, things can quickly become more complex. The following patch changes a function signature and all the related calls. @change_sig@ expression x; identifier fname, sname; @@ impl sname { ... pub(crate) fn fname(&self, - guard: &RevocableGuard<'_> ) -> Result { ... } ... } @modify_calls@ expression x, guard; identifier change_sig.fname; @@ x.fname( ... - guard ) Rule change_sig finds all the occurrences of functions which take a guard of type &RevocableGuard<'_> and removes that parameter. The rule modify_calls updates the calls to that method. This seman...

First seen: 2025-06-25 20:20

Last seen: 2025-06-25 23:20