The React Compiler Rust Port is an experimental rewrite of the core React Compiler logic in Rust. This initiative aims to improve compilation performance and leverage Rust's memory safety and concurrency features. The port is organized within the compiler/crates/ workspace and integrates with the existing JavaScript-based ecosystem via a NAPI bridge.
The Rust implementation is housed within the compiler/ sub-monorepo. The Rust components are managed as a Cargo workspace defined in the root of the compiler directory. The workspace groups multiple crates and external dependencies and is declared in compiler/Cargo.toml:
"crates/*" for all Rust crates in the workspace.napi crate used for bridging Rust and Node.js.| Component | Location | Description |
|---|---|---|
| Cargo Workspace | compiler/Cargo.toml | Defines Rust crates and workspace members including crates/* and their dependencies compiler/Cargo.toml1-5 |
| Compiler Core | compiler/crates/react_compiler | Main entrypoint orchestrating the compilation pipeline, a Rust port of the TypeScript Program.ts compiler/crates/react_compiler/src/entrypoint/program.rs6-16 |
| AST Definitions | compiler/crates/react_compiler_ast | Rust representation of the JavaScript/Babel AST compiler/crates/react_compiler_ast/Cargo.toml1-4 |
| Diagnostics | compiler/crates/react_compiler_diagnostics | Common error and logging infrastructure for Rust crates compiler/crates/react_compiler_ast/Cargo.toml7 |
This modular design enables clear separation of responsibilities between the parsing/AST model, compiler logic, diagnostics, and optimization passes, all coordinated by the core compilation entrypoint.
Sources: compiler/Cargo.toml1-5 compiler/crates/react_compiler/src/entrypoint/program.rs6-16 compiler/crates/react_compiler_ast/Cargo.toml1-7
The Rust port employs an arena-based data model to represent the High-level Intermediate Representation (HIR) used throughout compilation. Unlike the TypeScript compiler’s potentially more object/cyclic graph-based model, the Rust port uses indices (newtyped integer IDs) referencing arena-allocated entities. This design:
These integer-indexed newtypes serve as stable, type-safe handles for core code entities:
| Identifier | Meaning | Defined in |
|---|---|---|
IdentifierId | Unique variable/identifier index in HIR | react_compiler_hir::IdentifierId react_compiler_hir/src/lib.rs29 |
ScopeId | Unique reactive scope index | react_compiler_hir::ScopeId react_compiler_hir/src/lib.rs44 |
BlockId | Basic block index in Control Flow Graph | react_compiler_hir::BlockId react_compiler_hir/src/lib.rs26 |
FunctionId | Unique function index discovered during compilation | react_compiler_hir::FunctionId react_compiler_hir/src/lib.rs50 |
JsString | UTF-16 safe string type handling lone surrogates | In diagnostics crate, handles ill-formed UTF-16 for JS strings compiler/crates/react_compiler_diagnostics/src/js_string.rs1-34 |
This model aligns with the architecture described in the React Compiler design principles, representing nodes as indices for fast access in a flat arena rather than nested pointers.
Sources: compiler/crates/react_compiler_hir/src/lib.rs25-50 compiler/crates/react_compiler_hir/src/lib.rs158-174 compiler/crates/react_compiler_diagnostics/src/js_string.rs1-34
To integrate the Rust compiler with the Babel-based React Compiler ecosystem, the Rust code exposes APIs using napi-rs, which generates Node.js native modules via N-API for seamless interoperability.
react_compiler_ast), runs compilation passes, and returns transformed AST back to Babel as JSON.napi and napi-derive crates compiler/Cargo.lock189-192The main entrypoint in Rust is program.rs within the react_compiler crate. It maps closely to the TypeScript Program.ts by orchestrating the compilation workflow:
HirBuilder and lowering passes (in react_compiler_lowering crate) [see build_hir.rs].memoize_fbt_and_macro_operands_in_same_scope identify macro operands and enforce memoization scoping [react_compiler_inference/src/memoize_fbt_and_macro_operands_in_same_scope.rs].react_compiler_optimization crate [constant_propagation.rs].CodegenFunction in the react_compiler_reactive_scopes crate [codegen_reactive_function.rs].This pipeline enables tight integration with the existing Babel tooling while benefiting from Rust’s performance and correctness guarantees.
Sources: compiler/crates/react_compiler/src/entrypoint/program.rs1-63 compiler/crates/react_compiler_lowering/src/build_hir.rs1-24 compiler/crates/react_compiler_reactive_scopes/src/codegen_reactive_function.rs6-11 compiler/crates/react_compiler_optimization/src/constant_propagation.rs1-42 compiler/Cargo.lock189-192
Testing the Rust port uses the established fixtures and snapshot harness shared with the TypeScript compiler implementation to ensure correctness and parity.
--rust Flag: When running the compiler test suite, this flag directs the test harness to invoke the Rust compiler instead of the TypeScript one, enabling direct comparison [test scripts, implied].gatherCapturedContext for consistent diagnostics and handling compiler/crates/react_compiler_lowering/src/identifier_loc_index.rs1-48JsString Normalization: The JsString type handles the representation of potentially ill-formed UTF-16 strings from JavaScript, including lone surrogates that are encoded as __SURROGATE_XXXX__ markers in JSON, ensuring snapshot parity by matching the original source compiler/crates/react_compiler_diagnostics/src/js_string.rs8-12react_compiler_hir::print::PrintFormatter module provides debug formatting utilities to print HIR and reactive functions uniformly compiler/crates/react_compiler_hir/src/print.rs1-15format_js_number function compiler/crates/react_compiler_hir/src/lib.rs113-151This shared infrastructure guarantees that both Rust and TypeScript implementations produce equivalent diagnostics and transformed code snapshots.
Sources: compiler/crates/react_compiler_lowering/src/identifier_loc_index.rs1-48 compiler/crates/react_compiler_diagnostics/src/js_string.rs8-12 compiler/crates/react_compiler_hir/src/print.rs1-15 compiler/crates/react_compiler_hir/src/lib.rs113-151
The Rust port of the React Compiler represents a major engineering effort to rewrite the compiler's core pipeline in Rust while preserving the existing semantics, interfaces, and developer experience from the TypeScript version. Key aspects include:
compiler/crates dividing the compiler into AST, core logic, diagnostics, and optimization crates.--rust flag to verify strict parity with the TypeScript compiler outputs.The Rust port lays the groundwork for potential improvements in performance, memory safety, and future extensibility of the React Compiler.
Workspace and core orchestration:
compiler/Cargo.toml:1-5
compiler/crates/react_compiler/src/entrypoint/program.rs:6-16
Core arena data types:
compiler/crates/react_compiler_hir/src/lib.rs:25-50
compiler/crates/react_compiler_hir/src/lib.rs:158-174
Diagnostics and string handling:
compiler/crates/react_compiler_diagnostics/src/js_string.rs:1-34
Lowering AST to HIR and identifier indexing:
compiler/crates/react_compiler_lowering/src/build_hir.rs:1-24
compiler/crates/react_compiler_lowering/src/identifier_loc_index.rs:1-48
Optimization passes:
compiler/crates/react_compiler_optimization/src/constant_propagation.rs:1-42
Code generation:
compiler/crates/react_compiler_reactive_scopes/src/codegen_reactive_function.rs:6-11
Inference passes (e.g., memoize FBT operands):
compiler/crates/react_compiler_inference/src/memoize_fbt_and_macro_operands_in_same_scope.rs:98-101
HIR debug printing and number formatting:
compiler/crates/react_compiler_hir/src/print.rs:1-15
compiler/crates/react_compiler_hir/src/lib.rs:113-151
Sources: All files referenced above
Refresh this wiki