Repository navigation
Description
Proposal
Make __proto__ an invalid JS name for record fields, inline-record fields, object-type fields and @tag(...). This covers a plain label ({__proto__: int}, which is a valid identifier) and a renamed one (@as("__proto__")). The check runs once, at the type declaration, with a message like:
__proto__can't be a field name: in JavaScript it sets the object's prototype. Use@aswith a different name, or a dict.
Rollout: a deprecation warning in 12.x, an error in v13.
Why
In JavaScript, __proto__ is an inherited accessor on Object.prototype, not an ordinary key. Each code path that builds or touches a record needs its own workaround, and some paths can't be fixed at a reasonable cost:
| Construct | Today |
|---|---|
| Record or object literal | Works: printed as ["__proto__"]: v (#8747) |
| Record spread | Works since #9416, through Object.defineProperty |
let rec value |
Broken; #9471 adds another defineProperty path |
Field write x.p = v |
Broken (#9457): runs the setter, drops an int or replaces the prototype |
| Read of an absent optional field | Broken: returns Object.prototype instead of undefined; a fix needs an own-property check on every read |
type r = {mutable __proto__?: int, q: int}
let get = (x: r) => x.__proto__ // x.__proto__ -> Object.prototype when absent
let set = (x: r) => x.__proto__ = Some(3) // x.__proto__ = 3 -> setter, value droppedForbidding the name costs little:
- An own
__proto__property is almost never a real interop need. Its main source isJSON.parse, and that is the usual prototype-pollution vector. Code that does see such keys can usedict<_>. - Code that uses such a field today is already broken for writes and for reads of absent optional fields.
- The workarounds can be removed: the
definePropertypaths from Copy a record spread's base before overrides that need statements #9416, and Define a __proto__ field of a recursive value as its own property #9471, [12.x] Evaluate a record spread's overrides in order #9460 (12.x backport) and Writes to an @as("__proto__") record field run the inherited setter #9457 would no longer be needed.
Not covered
Dicts keep the computed-key printing from #8747, since their keys are runtime data. Reading a runtime "__proto__" key from a dict is a separate question.
Cleanup
Remove (code as of 0a686a9):
compiler/backend/lam_compile.ml: the"__proto__"branch and its comment in the record-spread copy (Copy a record spread's base before overrides that need statements #9416). All overrides go throughJs_of_lam_block.set_fieldagain.compiler/backend/js_exp_make.ml/.mli:define_own_property, whose only caller is that branch.tests/compiler_tests/src/record_spread_order_test.res: theproto/mutablePrototypes and the two__proto__tests. A super_errors test for the new check replaces them.- Define a __proto__ field of a recursive value as its own property #9471 is no longer needed. In [12.x] Evaluate a record spread's overrides in order #9460 (12.x backport of Copy a record spread's base before overrides that need statements #9416), only the
__proto__part can go; the field-order fix stays.
Keep, because dicts need them:
compiler/backend/js_dump_property.ml: printing a"__proto__"key as["__proto__"]. Dict literals use it (Stdlib_DictTests.res).compiler/backend/js_dump.ml:is_jsx_attribute_namerejecting__proto__. A dict spread into JSX props can carry that key (jsx_preserve_test.res,jsx_preserve_semantics/cases/Dict.res).
Related
#7343, #8747, #9416, #9457, #9460, #9471
This issue was filed by Claude.
Activity
Reject __proto__ as the JavaScript name of a field (#9513)