|
| 1 | +# Seamless C support |
| 2 | + |
| 3 | +| Metadata | | |
| 4 | +| --- | --- | |
| 5 | +| Owner(s) | *Github usernames or other identifying info for goal owners* | |
| 6 | +| Teams | *Names of teams being asked to commit to the goal* | |
| 7 | +| Status | WIP | |
| 8 | + |
| 9 | +## Motivation |
| 10 | + |
| 11 | +Using C from Rust should be as easy as using C from C++: completely seamless, |
| 12 | +as though it's just another module of code. You should be able to drop Rust |
| 13 | +code into a C project and start compiling and using it in minutes. |
| 14 | + |
| 15 | +### The status quo |
| 16 | + |
| 17 | +Today, people who want to use C and Rust together in a project have to put |
| 18 | +substantial work into infrastructure or manual bindings. Whether by creating |
| 19 | +build system infrastructure to invoke bindgen/cbindgen (and requiring the |
| 20 | +installation of those tools), or manually writing C bindings in Rust, projects |
| 21 | +cannot simply drop Rust code into a C program or C code into a Rust program. |
| 22 | +This creates a high bar for adopting or experimenting with Rust, and makes it |
| 23 | +more difficult to provide Rust bindings for a C library. |
| 24 | + |
| 25 | +By contrast, dropping C++ code into a C project or C code into a C++ project is |
| 26 | +trivial. The same compiler understands both C and C++, and allows compiling |
| 27 | +both together or separately. The developer does not need to duplicate |
| 28 | +declarations for the two languages, and can freely call between functions in |
| 29 | +both languages. |
| 30 | + |
| 31 | +C and C++ are still not the same language. They have different idioms and |
| 32 | +common types, and a C interface may not be the most ergonomic to use from C++. |
| 33 | +Using C++ from C involves treating the C as C++, such that it no longer works |
| 34 | +with a C compiler that has no C++ support. But nonetheless, C++ and C integrate |
| 35 | +extremely well, and C++ is currently the easiest language to integrate into an |
| 36 | +established C project. |
| 37 | + |
| 38 | +This is the level of integration we should aspire to for Rust and C. |
| 39 | + |
| 40 | +### The next few steps |
| 41 | + |
| 42 | +To provide seamless integration between Rust and C, we need a single compiler |
| 43 | +to understand both Rust and C. Thus, the first step will be to integrate a C |
| 44 | +preprocessor and compiler frontend into the Rust compiler. For at least the |
| 45 | +initial experimentation, we could integrate components from LLVM, taking |
| 46 | +inspiration from `zig cc`. (In the future, we can consider other alternatives, |
| 47 | +including a native Rust implementation. We could also consider components from |
| 48 | +c2rust or similar.) |
| 49 | + |
| 50 | +We can either generate MIR directly from C (which would be experimental and |
| 51 | +incomplete but integrate better with the compiler), or bypass MIR and generate |
| 52 | +LLVM bytecode (which would be simpler but less well integrated). |
| 53 | + |
| 54 | +This first step would provide substantial benefits already: a C compiler that's |
| 55 | +always available on any system with Rust installed, that generates code for any |
| 56 | +supported Rust target, and that always supports cross-language optimization. |
| 57 | + |
| 58 | +We can further improve support for calling C from Rust. We can support |
| 59 | +"importing" C header files, to permit using this support to call external |
| 60 | +libraries, and to support inline functions. |
| 61 | + |
| 62 | +### The "shiny future" we are working towards |
| 63 | + |
| 64 | +Once C support is integrated, we can generate type information for C functions |
| 65 | +as if they were unsafe Rust functions, and then support treating the C code as |
| 66 | +a Rust module, adding the ability to import and call C functions from Rust. |
| 67 | +This would not necessarily even require header files, making it even simpler to |
| 68 | +use C from Rust. The initial support can be incomplete, supporting the subset |
| 69 | +of C that has reasonable semantics in Rust. |
| 70 | + |
| 71 | +We will also want to add C features that are missing in Rust, to allow Rust to |
| 72 | +call any supported C code. |
| 73 | + |
| 74 | +Once we have a C compiler integrated into Rust, we can incrementally add C |
| 75 | +extensions to support using Rust from C. For instance: |
| 76 | +- Support importing Rust modules and calling `extern "C"` functions from |
| 77 | + them, without requiring a C header file. |
| 78 | +- Support using `::` for scoping names. |
| 79 | +- Support simple Rust types (e.g. `Option` and `Result`). |
| 80 | +- Support calling Rust methods on objects. |
| 81 | +- Allow annotating C functions with Rust-enhanced type signatures, such as |
| 82 | + marking them as safe, using Rust references for pointer parameters, or |
| 83 | + providing simple lifetime information. |
| 84 | + |
| 85 | +We can support mixing Rust and C in a source file, to simplify incremental |
| 86 | +porting even further. |
| 87 | + |
| 88 | +To provide simpler integration into C build systems, we can accept a |
| 89 | +C-compiler-compatible command line (`CFLAGS`), and apply that to the C code we |
| 90 | +process. |
| 91 | + |
| 92 | +We can also provide a CLI entry point that's sufficiently command-line |
| 93 | +compatible to allow using it as `CC` in a C project. |
| 94 | + |
| 95 | +## Design axioms |
| 96 | + |
| 97 | +- **C code should feel like just another Rust module.** Integrating C code into |
| 98 | + a Rust project, or Rust code into a C project, should be trivial; it should |
| 99 | + be just as easy as integrating C with C++. |
| 100 | + |
| 101 | +- **This is not primarily about providing *safe* bindings.** This project will |
| 102 | + primarily make it much easier to access C bindings as unsafe interfaces. |
| 103 | + There will still be value in wrapping these unsafe C interfaces with safer |
| 104 | + Rust interfaces. |
| 105 | + |
| 106 | +- **Calling C from Rust should not require writing duplicate information in Rust** |
| 107 | + that's already present in a C header or source file. |
| 108 | + |
| 109 | +- **Integrating C with Rust should not require third-party tools**. |
| 110 | + |
| 111 | +- **Compiling C code should not require substantially changing the information |
| 112 | + normally passed to a C compiler** (e.g. compiler arguments). |
| 113 | + |
| 114 | +## Ownership and other resources |
| 115 | + |
| 116 | +**Owner:** TODO |
| 117 | + |
| 118 | +### Support needed from the project |
| 119 | + |
| 120 | +* Lang team: |
| 121 | + * Design meetings to discuss design changes |
| 122 | + * RFC reviews |
| 123 | +* Compiler team: |
| 124 | + * RFC review |
| 125 | + |
| 126 | +## Outputs and milestones |
| 127 | + |
| 128 | +### Outputs |
| 129 | + |
| 130 | +The initial output will be a pair of RFCs: one for an experimental integration of a C compiler into rustc, and the other for minimal language features to take advantage of that. |
| 131 | + |
| 132 | +### Milestones |
| 133 | + |
| 134 | +- Compiler RFC: Integrated C compiler |
| 135 | +- Lang RFC: Rust language support for seamless C integration |
| 136 | + |
| 137 | +## Frequently asked questions |
0 commit comments