Skip to content

A record in a recursive group can be read before it exists #9466

Backlog

Description

@cknitt

In a let rec … and … group of records, a record that is declared directly can be read by an earlier record built into a dummy, before it is declared.

type rec b = {next: a, v: int}
and a = {f: unit => b, w: int}

let rec b = {next: a, v: 1}
and a = {f: () => b, w: 2}

Output:

let b = {};

Primitive_object.updateDummy(b, {
  next: a,
  v: 1
});

function a_f() {
  return b;
}

let a = {
  f: a_f,
  w: 2
};

Loading the module throws ReferenceError: Cannot access 'a' before initialization.

b reads a, so b becomes a dummy. But a reads nothing of the group eagerly (its field is a function), so it is declared directly, after b's updateDummy already read it. A binding should also become a dummy (or be declared first) when an earlier binding in the group reads it. functions_before_values doesn't help here because a is a record, not a function.

Found while reviewing #9427, which doesn't change this. This issue was filed by Claude.

Activity

  1. added theissue type on Nov 28, 2026
  2. illusionalsagacity commented on Nov 29, 2026

    @illusionalsagacity
    Contributor

    #9427 fixes this, in 7b2e4eb. A binding that an earlier binding of the group reads is now built into a dummy too, so it is declared before that read. Your example compiles to:

    let b = {};
    
    let a = {};
    
    Primitive_object.updateDummy(b, {
      next: a,
      v: 1
    });
    
    Primitive_object.updateDummy(a, {
      f: () => b,
      w: 2
    });

    It's a test in recursive_records_test.res on that branch now.

    Investigated by Claude.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions