Skip to content

Deterministically box cyclic cddl-derive struct references#562

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-infinite-size-issue
Draft

Deterministically box cyclic cddl-derive struct references#562
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-infinite-size-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 21, 2026

cddl-derive generated direct struct-to-struct references for cyclic CDDL rules, which produces infinite-size Rust types (e.g. NodeChild). This change introduces deterministic cycle breaking by boxing recursive edges during code generation.

  • Recursive type graph analysis

    • Build a reference graph across generated type defs.
    • Compute strongly connected components (SCCs) to identify cyclic groups (including self-recursion).
  • Deterministic cycle breaking

    • For struct fields that reference types in the same cyclic SCC, mark selected edges as boxed using a stable lexical ordering rule.
    • Ensures one side of a mutual cycle is boxed consistently, avoiding nondeterministic output.
  • Codegen output updates

    • Add boxed field support in RustField (is_boxed).
    • Emit Box<T> / Option<Box<T>> for marked recursive fields while preserving existing optional/serde behavior.
  • Regression coverage

    • Add tests for:
      • mutual recursion (node/child)
      • self recursion (node containing node)
node = { ? "child": child }
child = { ? "parent": node }

Now generates finite Rust types (deterministically), e.g.:

pub struct Node {
    pub child: Option<Box<Child>>,
}

pub struct Child {
    pub parent: Option<Node>,
}

Copilot AI linked an issue May 21, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Fix infinite size issue in cddl-derive for recursive types Deterministically box cyclic cddl-derive struct references May 21, 2026
Copilot AI requested a review from anweiss May 21, 2026 18:02
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.

cddl-derive recursive types have infinite size

2 participants