Export @inline constants from their module
Why
An @inline constant was only a primitive, so its module had no field for it: the JS output did not export it, genType silently dropped @genType @inline let x = 42, and hiding one behind a plain let x: int in an interface rebuilt the constant in a coercion. Now the constant is also an ordinary field of its module (proposal 5 of #8733). Uses in ReScript still get the literal, read from the .cmi as before. Types.is_module_field decides which values are fields, and every place that counts field positions or skips primitives uses it.
modifiedJS a module with @inline constants
@inline let less = -1 module Nested = { @inline let label = "nested"} let pair = (less, Nested.label)
js
Before
After
let Nested = {}; let Nested = { label: "nested"}; let less = -1; let pair = [let pair = [ -1, -1, "nested" "nested"];]; export {export { less, Nested, Nested, pair, pair,}}
modifiedJS a constant in a recursive module
module rec A: { @inline(9) let k: int let f: unit => int} = { @inline let k = 9 let f = () => B.g()}and B: { let g: unit => int} = { let g = () => A.k + 1}
js
let A = Primitive_module.init([ "x.res", 1, 4], { TAG: "Module", _0: [ [ { TAG: "Value", _0: 9 }, "k" ], [ "Function", "f" ] ]});…
modifiedDiagnostic a constant takes its JavaScript name
@inline let \"a-b" = 1let a$b = 2
diagnostic
This value has the same JavaScript name a$bas the value at x.res:1:1-22.Rename one of them.
modifiedReanalyze an unused @inline constant
@inline let unusedConst = 2reanalyze Warning Dead Value
x.res:1:1-27unusedConst is never used
Review
- Does an interface still decide what other modules inline? Yes.
@inline(c) let x: tin a .resi still needs an @inline implementation with the same constant, and a plainlet x = cstill does not match it. A plainlet x: thides the constant: other modules read the field, which is now the implementation's own binding instead of a coercion that rebuilt it. - Why do Int.Constants and Ordering change in the runtime? Their constants are @inline, so
Stdlib_Int.Constants.minValueandmaxValueandStdlib_Ordering.less,equalandgreaterare now exported. The unreleased changelog entries that said constants have no export are updated, and the breaking entry forInt.Constantsis dropped: with the export back, nothing about it breaks. - Why does a module alias in a recursive module now get the tag
Valueinstead ofvalue? Constants and aliases share one shape tag, andValueis the name of the runtime constructor. The runtime only tests forModule, so both spellings behave the same.