Skip to content

Add compile-time typestates and infallible accessors - #277

Merged
jasongraffius merged 4 commits into
google:rust_dev_typestatesfrom
yi-json:rust_dev_typestates
Aug 17, 2026
Merged

Add compile-time typestates and infallible accessors#277
jasongraffius merged 4 commits into
google:rust_dev_typestatesfrom
yi-json:rust_dev_typestates

Conversation

@yi-json

@yi-json yi-json commented Aug 3, 2026

Copy link
Copy Markdown

Foundation: Compile-time Type States and Checked Field Accessors

This PR adds optional compile-time typestate tracking (UncheckedState, CompleteState) to the experimental Rust runtime and compiler backend.

The main goal is to let callers read fields infallibly via .read() without .try_read().unwrap() or .expect() calls in consumer code once a view is validated with .check_complete(). Furthermore, it introduces a self-consuming Writer pattern (write_<field>(self, val) -> <Typename>Writer<S, NextState>) that safely tracks typestates on mutation and degrades layout-affecting fields back to UncheckedState.

All generated views default to ST: State = UncheckedState, so existing code continues to compile and work without breaking any changes

What's included in this stack:

  1. rust: Implement compile-time typestates and infallible read accessors:
  • Defines typestate markers (UncheckedState, CompleteState) and traits (State, IsComplete,
    CheckComplete, InfallibleRead) in typestate.rs.
  • Updates the Rust code generator (emboss_codegen_rust.py and templates) to parameterize generated
    struct views by typestate (<TypeName><S, ST = UncheckedState>) and implement CheckComplete.
  • Enables infallible .read() access on CompleteState views.
  1. rust: Implement self-consuming Typestate Writer for safe mutations:
  • Generates dedicated <TypeName>Writer<S, ST> types and .into_writer() conversions.
  • Emits infallible write_<field>(self, val) setters directly on generated Writer types.
  • Incorporates AST layout-dependency analysis so that writing to a field which affects downstream
    layout (e.g. dynamic offsets/sizes) degrades the returned Writer to UncheckedState, while writing to
    fixed non-layout fields preserves CompleteState.
  • Adds unit and integration test suites in dynamic_size_test.rs

Testing

  • cargo test in runtime/experimental/rust
  • bazel test //compiler/back_end/experimental/rust/testcode/...

@yi-json
yi-json marked this pull request as draft August 3, 2026 21:52
@yi-json
yi-json marked this pull request as ready for review August 3, 2026 21:53
Comment thread compiler/back_end/experimental/rust/generated_code_templates Outdated
Comment thread compiler/back_end/experimental/rust/generated_code_templates Outdated
Comment thread compiler/back_end/experimental/rust/generated_code_templates
Comment thread compiler/back_end/experimental/rust/generated_code_templates
Comment thread compiler/back_end/experimental/rust/generated_code_templates Outdated
Comment thread runtime/experimental/rust/src/lib.rs Outdated
Comment thread runtime/experimental/rust/src/lib.rs Outdated
Comment thread runtime/experimental/rust/src/lib.rs Outdated
Comment thread runtime/experimental/rust/src/typestate.rs Outdated
Comment thread runtime/experimental/rust/src/typestate.rs
@yi-json
yi-json force-pushed the rust_dev_typestates branch from b3952e7 to fc18c79 Compare August 5, 2026 15:06
Comment thread compiler/back_end/experimental/rust/generated_code_templates Outdated
Comment thread runtime/experimental/rust/src/prelude.rs Outdated
Comment thread runtime/experimental/rust/src/typestate.rs Outdated
Comment thread runtime/experimental/rust/src/typestate.rs Outdated
Comment thread runtime/experimental/rust/src/typestate.rs
Comment thread runtime/experimental/rust/src/typestate.rs
@yi-json
yi-json force-pushed the rust_dev_typestates branch 2 times, most recently from b412d57 to df0546c Compare August 8, 2026 18:42
Comment thread compiler/back_end/experimental/rust/generated_code_templates Outdated
Comment thread runtime/experimental/rust/src/lib.rs
Comment thread compiler/back_end/experimental/rust/generated_code_templates
Comment thread compiler/back_end/experimental/rust/generated_code_templates Outdated
@yi-json
yi-json force-pushed the rust_dev_typestates branch from df0546c to 01bb309 Compare August 11, 2026 21:09
Comment thread compiler/back_end/experimental/rust/generated_code_templates
Comment thread compiler/back_end/experimental/rust/generated_code_templates
Comment thread compiler/back_end/experimental/rust/generated_code_templates Outdated
Comment thread runtime/experimental/rust/src/lib.rs
@yi-json
yi-json force-pushed the rust_dev_typestates branch from 01bb309 to ee226fb Compare August 13, 2026 20:24
Comment thread compiler/back_end/experimental/rust/testcode/dynamic_size_test.rs Outdated
Comment thread compiler/back_end/experimental/rust/emboss_codegen_rust.py Outdated
Comment thread compiler/back_end/experimental/rust/emboss_codegen_rust.py Outdated
Comment thread runtime/experimental/rust/src/lib.rs
Introduce compile-time typestate tracking (UncheckedState, CompleteState)
and the CheckComplete trait to statically track struct layout validity.

Update the Rust code generator and runtime to parameterize generated
views by typestate, enabling infallible read() access on CompleteState views.
@yi-json
yi-json force-pushed the rust_dev_typestates branch from ee226fb to e08eea5 Compare August 14, 2026 18:29

@jasongraffius jasongraffius left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, looks like some of the verification checks are failing, try reformatting the python with black.

Introduce dedicated Writer types to support typestate-tracked,
infallible mutations.
@yi-json
yi-json force-pushed the rust_dev_typestates branch from e08eea5 to 2de1e0f Compare August 17, 2026 17:24
Comment thread testdata/nested_structure.emb Outdated
@jasongraffius
jasongraffius merged commit e98da5c into google:rust_dev_typestates Aug 17, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants