Skip to content

Forbid __proto__ as a record field name #9512

Done

Description

@cknitt

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 @as with 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 dropped

Forbidding the name costs little:

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):

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_name rejecting __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.

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

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions