Semantic Diff

#9493 Fix inlining copying a record literal to each use

4 commits

69e11bfe7acda5984c1bf6bb2e7b10c320cd7df6..pr-9493

5482bf54e · cknitt + Claude · Dec 01, 2026, 19:41 UTC

Fix inlining copying a record literal to each use

Why

When Lam_beta_reduce inlines a call, it substituted a constant argument for each use of its parameter. A record, tuple or option literal compiles to a new object at each copy, so b === b on the parameter was false and each use allocated.

Beta reduction now substitutes only constants that allocate nothing. Other constants are bound to a variable, like any other argument.

A switch on the copied literal used to fold. To keep that, Lam_pass_remove_alias now folds a switch on a variable bound to a constant, as it already did for a string switch. This also folds switches on top-level variant constants in several existing tests.

modifiedJS record literal argument

type xy = {x: int, y: int} @inlinelet same = (b: xy) => b === b Console.log(same({x: 1, y: 2}))
js
Before
After
console.log(({ x: 1, y: 2}) === ({let b = { x: 1, x: 1, y: 2 y: 2})); }; console.log(b === b);

Fixes #9490

bf8b52d6f · cknitt + Claude · Dec 01, 2026, 19:41 UTC

Repeat a nested none argument of an inlined function

Why

The previous commit bound every allocating constant argument to a variable. That includes a nested none, such as Some(None), which compiles to a Primitive_option.some(undefined) call. But every evaluation of Some(None) already gives a different object, so copying it can't change what === returns, and the binding protected nothing. Its lets only made the output longer: in option_repr_test, a let inside a list element forced the elements before it into temporaries.

Beta reduction now binds only constants that compile to an object literal: a block, or Some around one. The new Lambda.const_has_identity says which those are. option_repr_test's output is the same as on master again.

d4edf3fbc · cknitt + Claude · Dec 01, 2026, 20:22 UTC

Add Lambda unit tests for constant arguments and switches

The earlier commits were tested only through generated JavaScript. These unit tests check the two Lambda invariants directly. Beta reduction binds a block argument once and repeats a nested none. The alias pass takes the case of a switch on a variable bound to a constant, and keeps the variable when no case is taken.

0682c690c · cknitt + Claude · Dec 02, 2026, 06:13 UTC

Give the right reason why repeating a nested none is safe

The comment on Lambda.const_has_identity said that every evaluation of Some(None) gives a different object. It is the other way round: Primitive_option.some returns one frozen marker object for each nesting depth, so every copy is the same object and === still holds. That is why repeating the constant is safe.