Skip to content

Treat a record spread that copies mutable fields as expansive - #9158

Merged
cknitt merged 1 commit into
masterfrom
fix-spread-generalize
Nov 1, 2026
Merged

cknitt merged 1 commit into
masterfrom
fix-spread-generalize

Conversation

@cknitt

@cknitt cknitt commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

Fixes #9156.

The value restriction (Typecore.is_nonexpansive) checked only the fields a spread literal writes, and required the spread source to be non-expansive. Fields copied from the source didn't count, so {...base, tag: 1} was generalized even when it copied a mutable field. The literal allocates a fresh record with that field, so the value could be written at one type and read at another:

type cell<'a> = {tag: int, mutable value: option<'a>}
@val external base: cell<'a> = "base"

let copy = {...base, tag: 1}
copy.value = Some(1)
let s: option<string> = copy.value // compiled; `s` held the number 1

A record literal allocates every field of its type: written, copied from the spread, or left out. The check now treats spread literals like the others (#9149, #9152): all labels must be immutable, the written expressions non-expansive, and the spread source non-expansive. This drops the spread special case from the check.

What newly fails: only a spread that copies a mutable field from a polymorphic, non-expansive source, used at more than one type, or left open at the top level. A literal of a record type with a mutable field is itself expansive, so in practice the source is an external, e.g. {...defaults, x: 1} with @val external defaults: options<'a>. Spreads of immutable records still generalize. The new super_errors fixture record_spread_mutable_does_not_generalize.res checks both.

Changelog: a one-sentence fix entry.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe

@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Stop generalizing the type of a record spread {...base, field: value} that copies a mutable field from base, which let a value be stored at one type and read at another.

Semantic Diff

1 commit · Full page

✏️ Treat a record spread that copies mutable fields as expansive

fa279ba · cknitt + Claude · Nov 01, 2026, 07:05 UTC · edited

Why

The value restriction checked only the fields a spread literal writes, so {...base, tag: 1} was non-expansive even when it copied a mutable field from base, and its type was generalized (#9156). A record literal allocates every field of its type, whether written, copied from the spread or left out, so the check now requires all of them to be immutable, with or without a spread.

Diagnostic added (record spread copying a mutable field)

type cell<'a> = {tag: int, mutable value: option<'a>}
@val external base: cell<'a> = "base"
let copy = {...base, tag: 1}
copy.value = Some(1)
let s: option<string> = copy.value

diagnostic

-none; `s` holds the number 1 at run time
+This has type: option<int>
+But it's expected to have type: option<string>
+
+The incompatible parts:
+  int vs string

Review

  • Does a spread of an immutable record still generalize? Yes; the super_errors fixture uses one at both option<int> and option<string> first.

@cknitt

cknitt commented Nov 1, 2026

Copy link
Copy Markdown
Member Author

@codex review

@cknitt
cknitt marked this pull request as ready for review November 1, 2026 06:46
@cknitt
cknitt enabled auto-merge (squash) November 1, 2026 06:46
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-01T06:48:13.715321Z f81267d 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.

@cknitt
cknitt requested a review from cristianoc November 1, 2026 06:46
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: f81267d17c

ℹ️ 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".

@codecov

codecov Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 81.74%. Comparing base (8e26a9c) to head (fa279ba).

Files with missing lines Patch % Lines
compiler/ml/typecore.ml 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9158      +/-   ##
==========================================
- Coverage   81.74%   81.74%   -0.01%     
==========================================
  Files         460      460              
  Lines       64944    64942       -2     
==========================================
- Hits        53091    53089       -2     
  Misses      11853    11853              
Files with missing lines Coverage Δ
compiler/ml/typecore.ml 89.18% <90.00%> (-0.01%) ⬇️
🚀 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 Nov 1, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: fa279ba

Why:
    The value restriction checked only the fields a spread literal writes,
    so `{...base, tag: 1}` was non-expansive even when it copied a mutable
    field from `base`, and its type was generalized (#9156). A record
    literal allocates every field of its type, whether written, copied
    from the spread or left out, so the check now requires all of them to
    be immutable, with or without a spread.

Diagnostic added (record spread copying a mutable field):
Example:
    type cell<'a> = {tag: int, mutable value: option<'a>}
    @Val external base: cell<'a> = "base"
    let copy = {...base, tag: 1}
    copy.value = Some(1)
    let s: option<string> = copy.value
Before (diagnostic): none; `s` holds the number 1 at run time
After (diagnostic):
    This has type: option<int>
    But it's expected to have type: option<string>

    The incompatible parts:
      int vs string

Review: Does a spread of an immutable record still generalize? Yes; the
super_errors fixture uses one at both option<int> and option<string>
first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gKyiYMf54wPRxFdUWe
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the fix-spread-generalize branch from e1dbac7 to fa279ba Compare November 1, 2026 07:06
@github-actions

github-actions Bot commented Nov 1, 2026

Copy link
Copy Markdown

@cknitt
cknitt merged commit bf75af4 into master Nov 1, 2026
26 checks passed
@cknitt
cknitt deleted the fix-spread-generalize branch November 1, 2026 13:31
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.

Record spread that copies a mutable field is generalized (unsound)

2 participants