Semantic Diff

#9471 Define a __proto__ field of a recursive value as its own property

3 commits

6b9bc077dc2592dd6b21bc9239bdd70465244595..pr-9471

650d877aa · illusionalsagacity · Nov 28, 2026, 18:49 UTC

Define a __proto__ field of a recursive value as its own property

Why

A recursive value let rec x = {...} is made as an empty object whose fields are set afterwards: assigned one by one when they are variables or constants, or copied from the record with Object.assign. For a field renamed with @as("__proto__"), or a tag renamed with @tag("__proto__"), both run the __proto__ setter the object inherits from Object.prototype, where the record literal prints ["__proto__"]: v and makes an own property. An int was dropped, an object became the prototype, and the record itself threw "Cyclic __proto__ value". The field is now defined with Object.defineProperty, as a record spread's copy already does, and a record with such a key is copied with Object.defineProperties. An optional field set to None is left out, as it is of a record literal, so that it does not become an own property holding undefined.

modifiedJS recursive record with a __proto__ field

@inline(never) let id = x => xtype rec t = {next: t, @as("__proto__") p: int}let rec n = {next: n, p: 1}let rec m = {next: m, p: id(2)}
js
Before
After
let n = {};let n = {}; n.next = n;n.next = n; n.__proto__ = 1; Object.defineProperty(n, "__proto__", { value: 1, writable: true, enumerable: true, configurable: true}); let m = {};let m = {}; Primitive_object.updateDummy(m, {Object.defineProperties(m, Object.getOwnPropertyDescriptors({ next: m, next: m, ["__proto__"]: id(2) ["__proto__"]: id(2)});}));

modifiedJS recursive variant with a __proto__ tag

@tag("__proto__")type rec tagged = First({nextTagged: tagged}) | Second({value: int})let rec taggedFirst = First({nextTagged: taggedFirst})
js
Before
After
let taggedFirst = {};let taggedFirst = {}; Primitive_object.updateDummy(taggedFirst, {Object.defineProperties(taggedFirst, Object.getOwnPropertyDescriptors({ ["__proto__"]: "First", ["__proto__"]: "First", nextTagged: taggedFirst nextTagged: taggedFirst});}));

modifiedJS recursive record with optional fields set to None

type rec r = {next: r, @as("__proto__") p?: int, q?: int}let rec x = {next: x, p: ?None, q: ?None}
js
Before
After
let x = {};let x = {}; x.next = x;x.next = x; x.__proto__ = undefined; x.q = undefined;

Review

  • Does the output change for other recursive values? No: only a block whose keys, as Js_dump prints them, include __proto__ is copied with Object.defineProperties, and only a field named __proto__ is defined with Object.defineProperty. Other values still use updateDummy and assignments, and no checked-in output changes. An optional field set to None is no longer assigned undefined, whatever its name, so Object.keys of the value matches the literal's.
  • Is Object.defineProperties the same as Object.assign otherwise? For the copied literal, yes: its properties are writable, enumerable and configurable data properties, defined in the same order, and the empty object has no own properties they would replace.
  • Why not change updateDummy in the runtime? Recursive modules use it too, and every other recursive value keeps the call it has.
  • Why skip a None field in the caller and not in init_field? The record spread's copy calls init_field too, and there an override set to None must replace the field the copy took from the spread record.
4ea90e472 · illusionalsagacity · Nov 29, 2026, 04:13 UTC

Test __proto__ keys on recursive constructors, extensions and untagged records

Add cases to recursive_record_proto_field_test.res covering the remaining reachable branches of block_has_proto_key: a tuple-payload constructor with @tag("__proto__"), an extensible variant inline record with @as("__proto__"), and @unboxed untagged inline records with and without @as("__proto__").

db553ff10 · illusionalsagacity · Nov 29, 2026, 17:54 UTC

Preserve undefined slots in recursive array-backed records

Why

Codex review of 58df2f349 found that omitting optional fields set to None on the recursive-record field-assignment path also applied to array-backed records (Blk_record {array = true}), leaving trailing slots missing from the array's length and leading slots as sparse holes, unlike Js_dump which keeps every element in array literals. Only object-backed records (array = false) now omit optional None fields.

modifiedJS recursive array-backed record with optional None fields

type rec arrayOptional = {@as("0") nextArr: arrayOptional, @as("1") valueArr?: int}let rec arrayOptionalNone = {nextArr: arrayOptionalNone, valueArr: ?None}
js
Before
After
let arrayOptionalNone = [];let arrayOptionalNone = [];arrayOptionalNone[0] = arrayOptionalNone;arrayOptionalNone[0] = arrayOptionalNone; arrayOptionalNone[1] = undefined;

Review

  • Which checked-in outputs change? Only recursive_record_proto_field_test.mjs for the new array-record cases.