Skip to content

Reject __proto__ as the JavaScript name of a field - #9513

Merged
cknitt merged 1 commit into
masterfrom
forbid-proto-field
Dec 3, 2026
Merged

cknitt merged 1 commit into
masterfrom
forbid-proto-field

Conversation

@cknitt

@cknitt cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member

Closes #9512. Fixes #9457.

In JavaScript, __proto__ is an accessor that every object inherits from Object.prototype, not a plain key. A field with that name is written through the setter and, when absent, reads the prototype. So each code path that builds or updates a record needed its own workaround.

The type checker now rejects __proto__ as the JavaScript name of:

  • a record or inline-record field, plain or renamed with @as (exception records included)
  • an object-type field, including those an @obj external generates
  • a @tag, on the type or on a constructor
A field's JavaScript name can't be __proto__: in JavaScript, it sets the object's prototype instead of a property. Use another name, or a dict.

Object literals without a type annotation and dict literals still compile, and still print ["__proto__"] as a computed key.

Removed: the Object.defineProperty branch for __proto__ in record spreads (#9416) and its helper E.define_own_property, with the tests for it. This makes #9471 unnecessary. In #9460, only its __proto__ part can go.

New tests: four error snapshots in tests/error_tests/single (plain field, @as field, object field, @tag).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gL4qcnMmoKbamLEyfN

Why:
    In JavaScript, __proto__ is an accessor every object inherits from
    Object.prototype, not a plain key. A record field with that name is
    written through the setter (#9457) and, when absent, reads the
    prototype, and every code path that builds a record needs its own
    workaround. The type checker now rejects the name (#9512).

Diagnostic added (__proto__ field name):
Example:
    type t = {name: string, @as("__proto__") parent?: int}
After (diagnostic):
    A field's JavaScript name can't be __proto__: in JavaScript, it sets the object's prototype instead of a property. Use another name, or a dict.

The check covers record and inline-record labels, plain or renamed
with @as, object-type fields (including those @obj externals
generate) and @tag. Object literals without a type annotation and
dict literals keep printing a "__proto__" key as a computed key.

Review: Why does the record spread lose its Object.defineProperty
branch? It only ran for a field named __proto__, which can't be
declared any more, so no program that compiles changes its output.

Breaking: A type declaring a field, object field or @tag with the
JavaScript name __proto__ no longer compiles. Rename the field with
@as, or use a dict for objects that have such a key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gL4qcnMmoKbamLEyfN
Signed-off-by: Christoph Knittel <ck@cca.io>
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

Changelog

💥 Breaking Change

  • A record field, object field or @tag can no longer have the JavaScript name __proto__, which sets an object's prototype instead of a property.

Semantic Diff

1 commit · Full page

Reject __proto__ as the JavaScript name of a field

9655a68 · cknitt + Claude · Dec 03, 2026, 06:57 UTC

Why

In JavaScript, __proto__ is an accessor every object inherits from Object.prototype, not a plain key. A record field with that name is written through the setter (#9457) and, when absent, reads the prototype, and every code path that builds a record needs its own workaround. The type checker now rejects the name (#9512).

Diagnostic added (__proto__ field name)

type t = {name: string, @as("__proto__") parent?: int}

diagnostic · After

A field's JavaScript name can't be __proto__: in JavaScript, it sets the object's prototype instead of a property. Use another name, or a dict.

The check covers record and inline-record labels, plain or renamed with @as, object-type fields (including those @obj externals generate) and @tag. Object literals without a type annotation and dict literals keep printing a "__proto__" key as a computed key.

⚠️ Breaking: A type declaring a field, object field or @tag with the JavaScript name __proto__ no longer compiles. Rename the field with @as, or use a dict for objects that have such a key.

Review

  • Why does the record spread lose its Object.defineProperty branch? It only ran for a field named __proto__, which can't be declared any more, so no program that compiles changes its output.

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

⚠️ Compiler benchmarks

The benchmarks couldn't run for pull request 9655a68; the run shows why.

@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.54%. Comparing base (0a686a9) to head (9655a68).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9513   +/-   ##
=======================================
  Coverage   82.54%   82.54%           
=======================================
  Files         469      469           
  Lines       66224    66227    +3     
=======================================
+ Hits        54666    54669    +3     
  Misses      11558    11558           
Files with missing lines Coverage Δ
compiler/backend/js_exp_make.ml 68.49% <ø> (-0.19%) ⬇️
compiler/backend/lam_compile.ml 92.29% <100.00%> (-0.02%) ⬇️
compiler/typing/ast_untagged_variants.ml 88.35% <100.00%> (+0.14%) ⬆️
compiler/typing/typedecl.ml 84.12% <100.00%> (+0.07%) ⬆️
compiler/typing/typetexp.ml 88.55% <ø> (+0.02%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9513

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9513

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9513

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9513

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9513

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9513

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9513

commit: 9655a68

@cknitt

cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown
Codex Review Summary: 🔄 Running

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review 🔄 Running since 2026-12-03T08:02:13.746638Z 9655a68 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 9655a683f0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cknitt
cknitt requested a review from fhammerschmidt December 3, 2026 09:43
@cknitt
cknitt marked this pull request as ready for review December 3, 2026 09:43
@cknitt
cknitt merged commit fe8c75b into master Dec 3, 2026
21 of 22 checks passed
@cknitt
cknitt deleted the forbid-proto-field branch December 3, 2026 11:14
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.

Forbid __proto__ as a record field name Writes to an @as("__proto__") record field run the inherited setter

2 participants