CO2: Extending C with Rust interop features

CO2 language is a backward compatible with C language that has Rust interop features. It uses cargo as its build system and many cargo commands like `cargo test`, `cargo doc`, `cargo miri` and ... are supported.

CO2 uses a rustc fork as its compiler and compiles code to MIR. It is useful if you want to gradually rewrite a C project in Rust, use Rust dependencies in your existing C project, or if you want to manipulate some raw pointers without writing unsafe.

You can try it online in godbolt: Compiler Explorer

Now this is an interesting new angle! It seems like it's similar to the zig build integration of C?

Further, for the foreseeable future C++ is out of scope, I'd expect, but do you have any ideas of how integrating that would work? Normally I'd think "create a C API" but then we're basically back to square one again...

Now this is an interesting new angle! It seems like it's similar to the zig build integration of C?

IIUC, Zig<->C interop consists of these parts:

  1. a C compiler zig cc
  2. a C to Zig translator zig translate-c
  3. a builtin function cInclude/cImport which let you import a C header

In Rust, for 2 we have c2rust, and for 3 we have bindgen +include! which is not as nice as Zig, but isn't too much different. While CO2 provides 1, it goes beyond what Zig provides for C interop by changing the C itself and allowing C code to call Rust ABI functions and use Rust generic items, which is not possible in neither Rust nor Zig without changing the C. The most close thing I found to CO2 in other languages is D's importC feature.

Further, for the foreseeable future C++ is out of scope, I'd expect, but do you have any ideas of how integrating that would work? Normally I'd think "create a C API" but then we're basically back to square one again...

If CO2 becomes successful, I have ideas about a CO2++ project, which is C++ extended with Rust interop feature. I documented some of my ideas here: co2/docs/vision/co2pp.md at main · hkalbasi/co2 · GitHub

Dang, that does point out some rough complexities. If the goal is "allow smoothly transitioning code from C++ to Rust at the expression level" there's just some unworkable issues that require (seemingly) one-shot Big Changes. More practically, allowing Rust and C++ to just coexist in the same compilation unit seems somewhat more workable, assuming you can implement the entirety of C++, of course!

Honestly I'm a bit sad I don't have anything serious I could give this a try on, the best idea I have is to port Doom this way :grinning_face_with_smiling_eyes:

Yes that's one of the big challanges. I think by having a working C compiler, I went about ~20% of the work to have a working C++11 compiler (More recent C++ versions have much more complexity) and adding Rust integration to that pile of complexity would take work.

But even before starting that, there are still things remaining on the CO2 itself. The compiler is slow, I want to have a little more features, diagnostics are very poor, I should make some of my work upstream so that CO2 becomes buildable with a normal nightly Rust, and I have an idea about a borrow checker that looks through raw pointers (which is what CO2 code tends to use) and emits warnings.