compiler/docs/rust-port/rust-port-notes.md MARKDOWN 104 lines View on github.com → Search inside
1## Input/Output Format: JSON AST and Scope Tree23* Define a Rust representation of the Babel AST format using serde with custom serialization/deserialization in order to ensure that we always produce the "type" field, even outside of enum positions. Include full information from Babel, including source locations.4* Define a Scope type that encodes the tree of scope information, mapping to the information that babel represents in its own scope tree56The main public API is roughly `compile(BabelAst, Scope) -> Option<BabelAst>` returning None if no changes, or Some with the updated ast.78## Arenas910Use arenas and Copy-able "id" values that index into the arenas in order to migrate "shared" mutable references.1112* `Identifier`:13    * Table on Environment, stores actual Identifier values14    * `Place.identifier` references indirectly via `IdentifierId`15* `ReactiveScope`:16    * Table on Environment, stores actual ReactiveScope values17    * `Identifier`, scope terminals, etc reference indirectly via `ScopeID`18* `Function`:19    * Table on Environment, stores the inner HirFunction values20    * `InstructionValue::FunctionExpression` and `::ObjectMethod` reference indirectly via `FunctionId`21* `Type`:22    * Table on Environment, stores actual types23    * `Identifier` types and other type values use `TypeId` to index into2425## Instructions Table2627Store instructions indirectly. This allows passes that need to cache or remember an instruction's location (to work around borrowing issues) to have a single id to use to reference that instruction. Do not use `(BlockId, usize)` or similar.2829* Rename `InstructionId` to `EvaluationOrder` - this type is actually about representing the evaluation order, and is not even instruction-specific: it is also present on terminals.30* `HirFunction` stores `instructions: Vec<InstructionId>`31* `BasicBlock.instructions` becomes `Vec<InstructionId>`, indexing into the `HirFunction.instructions` vec3233## AliasingEffect3435* `Place` values are cloned36* `Call` variant `args` array is cloned37* `CreateFunction` variant uses `FunctionId` referencing the function arena3839## Environment4041Pass a single mutable environment reference separately from the HIR.4243* Remove `HIRFunction.env`, pass the environment as `env: &mut Environment` instead44* Maintain the existing fields/types of `Environment` type (don't group them)45* Use direct field access of Environment properties, rather than via methods, to allow precise sliced borrows of portions of the environment4647## Error Handling4849In general there are two categories of errors:50- Anything that would have thrown, or would have short-circuited, should return an `Err(...)` with the single diagnosstic51- Otherwise, accumulate errors directly onto the environment.52- Error handling must preserve the full details of the errors: reason, description, location, details, suggestions, category, etc5354### Specific Error Patterns and Approaches5556* TypeScript non-null assertions:57    * Example: `!`58    * Approach: panic via `.unwrap()` or similar.59* Throwing expressions:60    * Example: `throw ...` (latent bugs, should have been `invariant`)61    * Example: `CompilerError.invariant()`62    * Example: `CompilerError.throwTodo()`63    * Example: `CompilerError.throw*` (other "throw-" methods)64    * Approach: Make the function return a `Result<_, CompilerDiagnostic>`, and return `Err(...)` with the appropriate compiler error value. 65* Non-throwing expressions (Invariant):66    * Example: local `error` object and `error.pushDiagnostic()` (where the error *is* an invariant)67    * Approach: Make the function return a `Result<_, CompilerDiagnostic>`, and change the `pushDiagnostic()` with `return Err(...)` to return with the invariant error. 68* Non-throwing expressions (excluding Invariant):69    * Example: local `error` object and `error.pushDiagnostic()` (where the error is *not* an invariant)70    * Example: `env.recordError()` (where the error is *not* an invariant)71    * Approach: keep as-is7273## Pass and Pipeline Structure 7475Structure the pipeline and passes along these lines to align with the above error handling guidelines:7677```78// pipeline.rs79fn compile(80    ast: Ast, 81    scope: Scope,82    env: &mut Environment,83) -> Result<CompileResult, CompilerDiagnostic>> {84    // "?" to handle cases that would have thrown or produced an invariant85    let mut hir = lower(ast, scope, env)?;86    some_compiler_pass(&mut hir, env)?;87    ...88    let ast = codegen(...)?;8990    if (env.has_errors()) {91        // result with errors92        Ok(CompileResult::Failure(env.take_errors()))93    } else {94        // result with 95        Ok(CompileResult::Success(ast))96    }97}9899// <compilerpasss>.rs100fn passname(101    func: &mut HirFunction,102    env: &mut Environment103) -> Result<_, CompilerDiagnostic>;104```

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.