Skip to content

Export @inline constants from their module - #9526

Open
cknitt wants to merge 1 commit into
masterfrom
export-inline-constants
Open

cknitt wants to merge 1 commit into
masterfrom
export-inline-constants

Conversation

@cknitt

@cknitt cknitt commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

@inline constants are now exported from their module, like any other let. Uses in ReScript still get the literal.

Until now an @inline constant was only a primitive, like an external, so its module had no field for it:

  • JavaScript code couldn't import it;
  • genType silently left @genType @inline let x = 42 out of the .gen.tsx;
  • since Type @inline constants in the type checker, keep their attributes and allow annotations #9467, annotated constants (@inline let x: int = 3) are @inline constants too, so they lost the export they had as ordinary values;
  • the module object didn't match its own interface. When a module with an @inline constant was used through an interface that declares a plain let, such as a functor argument or a first-class module, the compiler couldn't pass the module itself. It built a new object with the constant added. The module, as exported or as passed to JavaScript, lacked the field:
module type S = {
  let limit: int
  let name: string
}

module Impl = {
  @inline let limit = 10
  let name = "impl"
}

let packed = module(Impl: S)

Before:

let Impl = {
  name: name
};

let packed = {
  limit: 10,
  name: name
};

After:

let Impl = {
  limit: 10,
  name: name
};

let packed = Impl;

This is proposal 5 of #8733. It lands before the next release, so the exports #9467 removed never ship missing.

@inline let less = -1

module Nested = {
  @inline let label = "nested"
}

let pair = (less, Nested.label)

Before:

let Nested = {};

let pair = [
  -1,
  "nested"
];

export {
  Nested,
  pair,
}

After:

let Nested = {
  label: "nested"
};

let less = -1;

let pair = [
  -1,
  "nested"
];

export {
  less,
  Nested,
  pair,
}

The constant stays a primitive in the .cmi, so other modules still inline it. In addition it now counts as a field of its module. Types.is_module_field decides that, and every place that counts field positions or skips primitives uses it: module paths, signature inclusion and coercions, include, recursive modules, JS name clashes and genType.

What else changes:

  • Interfaces still decide what other modules inline. @inline(c) let x: t in a .resi still needs an @inline implementation with the same constant. A plain let x: t hides the constant: other modules read the field, which is now the implementation's own binding instead of a copy built by the coercion.
  • Runtime: Stdlib_Int.Constants.minValue/maxValue and Stdlib_Ordering.less/equal/greater are exported again.
  • genType emits constants, also ones hidden behind a plain let in a .resi.
  • Two constants with the same JavaScript name in one module are now an error, as for other values.
  • Reanalyze reports an unused @inline constant as a dead value, as it does for other values.

New tests:

  • inline_const_export_test checks the exports by importing them from the generated JavaScript: every literal kind, nested modules, interfaces that hide or reorder fields, functors, include, module aliases, first-class modules and module rec;
  • an error test for a JS name clash with a constant, and one showing that a plain let still doesn't satisfy @inline(1) in an interface;
  • genType fixtures for constants, with and without an interface;
  • a reanalyze fixture for an unused constant.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Changelog

🚀 New Feature

  • @inline constants are exported from their JavaScript module and by genType, while uses still get the literal. Their JavaScript names now also count in the check for clashing names.

🐛 Bug fix

  • Inline @inline let bigint constants like other literals, instead of warning that @inline cannot appear there.
  • Keep @deprecated and doc comments on @inline constants, so uses from other modules warn and editors show the doc.
  • Inline @inline constants with a type annotation, such as @inline let x: int = 3, instead of ignoring the attribute with warning 53.

Semantic Diff

1 commit · Full page

✏️ Export @inline constants from their module

0895c16 · cknitt + Claude · Dec 04, 2026, 13:04 UTC · edited

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.

JS modified (a module with @inline constants)

@inline let less = -1

module Nested = {
  @inline let label = "nested"
}

let pair = (less, Nested.label)

js

-let Nested = {};
+let Nested = {
+  label: "nested"
+};
 
+let less = -1;
+
 let pair = [
   -1,
   "nested"
 ];
 
 export {
+  less,
   Nested,
   pair,
 }

JS modified (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 · After

let A = Primitive_module.init([
  "x.res",
  1,
  4
], {
  TAG: "Module",
  _0: [
    [
      {
        TAG: "Value",
        _0: 9
      },
      "k"
    ],
    [
      "Function",
      "f"
    ]
  ]
});
…

Diagnostic modified (a constant takes its JavaScript name)

@inline let \"a-b" = 1
let a$b = 2

diagnostic · After

This value has the same JavaScript name a$b
as the value at x.res:1:1-22.
Rename one of them.

Reanalyze modified (an unused @inline constant)

@inline let unusedConst = 2

reanalyze · After

Warning Dead Value
x.res:1:1-27
unusedConst is never used

Review

  • Does an interface still decide what other modules inline? Yes. @inline(c) let x: t in a .resi still needs an @inline implementation with the same constant, and a plain let x = c still does not match it. A plain let x: t hides 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.minValue and maxValue and Stdlib_Ordering.less, equal and greater are now exported. The unreleased changelog entries that said constants have no export are updated, and the breaking entry for Int.Constants is dropped: with the export back, nothing about it breaks.
  • Why does a module alias in a recursive module now get the tag Value instead of value? Constants and aliases share one shape tag, and Value is the name of the runtime constructor. The runtime only tests for Module, so both spellings behave the same.

@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.68%. Comparing base (e489471) to head (0895c16).
⚠️ Report is 6 commits behind head on master.

Files with missing lines Patch % Lines
compiler/gentype/translate_signature.ml 66.66% 2 Missing ⚠️
compiler/typing/transl_recmodule.ml 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9526      +/-   ##
==========================================
+ Coverage   82.59%   82.68%   +0.09%     
==========================================
  Files         468      469       +1     
  Lines       66296    66597     +301     
==========================================
+ Hits        54754    55067     +313     
+ Misses      11542    11530      -12     
Files with missing lines Coverage Δ
analysis/reanalyze/src/dead_value.ml 85.77% <100.00%> (-0.13%) ⬇️
compiler/gentype/translate_structure.ml 74.66% <100.00%> (+0.51%) ⬆️
compiler/gentype/translate_type_expr_from_types.ml 89.96% <100.00%> (+0.07%) ⬆️
compiler/typing/env.ml 80.97% <100.00%> (-0.04%) ⬇️
compiler/typing/includecore.ml 85.24% <100.00%> (+0.04%) ⬆️
compiler/typing/includemod.ml 75.29% <100.00%> (-0.15%) ⬇️
compiler/typing/mtype.ml 69.02% <100.00%> (+0.20%) ⬆️
compiler/typing/translmod.ml 95.72% <100.00%> (+0.07%) ⬆️
compiler/typing/typemod.ml 78.21% <100.00%> (+0.43%) ⬆️
compiler/typing/types.ml 73.17% <ø> (+1.37%) ⬆️
... and 2 more

... and 37 files with indirect coverage changes

🚀 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.

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

⚠️ Compiler benchmarks

Allocated words

In total: 656,885,811 → 656,914,067 (±0.00%).

Generated projects: 629,112,563 → 629,113,063 (±0.00%).

⚠️ Regression: 1 benchmark allocates more than 1% more.

Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Ordering.res 152,661 155,677 +1.98%
packages/@rescript/runtime/Stdlib_Int.res 445,251 446,026 +0.17%

Instructions

In total: 8,976,933,908 → 8,977,999,362 (+0.01%).

Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Math.resi 21,254,630 21,297,098 +0.20%
packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res 24,107,389 24,148,812 +0.17%
packages/@rescript/runtime/Stdlib_Temporal_PlainDateTime.res 25,570,142 25,612,932 +0.17%
packages/@rescript/runtime/Stdlib_List.resi 24,530,881 24,570,206 +0.16%
packages/@rescript/runtime/Stdlib_String.resi 29,379,782 29,422,776 +0.15%
packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res 31,229,325 31,274,798 +0.15%
packages/@rescript/runtime/Stdlib_Date.resi 34,183,528 34,228,981 +0.13%
parse/RedBlackTree.res 14,911,369 14,930,927 +0.13%
parse/RedBlackTreeNoComments.res 14,499,103 14,517,581 +0.13%
packages/@rescript/belt/src/Belt_Array.resi 33,713,161 33,755,473 +0.13%
3 more changes
Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_TypedArray.res 41,320,511 41,368,976 +0.12%
packages/@rescript/runtime/Stdlib_Array.resi 39,968,098 40,013,033 +0.11%
packages/@rescript/belt/src/Belt_List.resi 38,958,272 38,998,343 +0.10%

Output size

In total: 27,594,641 → 27,595,022 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request 0895c16 compared with base 4375d6e in this run.

@pkg-pr-new

pkg-pr-new Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 0895c16

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.

JS modified (a module with @inline constants):
Example:
    @inline let less = -1

    module Nested = {
      @inline let label = "nested"
    }

    let pair = (less, Nested.label)
Before (js):
    let Nested = {};

    let pair = [
      -1,
      "nested"
    ];

    export {
      Nested,
      pair,
    }
After (js):
    let Nested = {
      label: "nested"
    };

    let less = -1;

    let pair = [
      -1,
      "nested"
    ];

    export {
      less,
      Nested,
      pair,
    }

JS modified (a constant in a recursive module):
Example:
    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
    }
After (js):
    let A = Primitive_module.init([
      "x.res",
      1,
      4
    ], {
      TAG: "Module",
      _0: [
        [
          {
            TAG: "Value",
            _0: 9
          },
          "k"
        ],
        [
          "Function",
          "f"
        ]
      ]
    });
    …

Diagnostic modified (a constant takes its JavaScript name):
Example:
    @inline let \"a-b" = 1
    let a$b = 2
After (diagnostic):
    This value has the same JavaScript name a$b
    as the value at x.res:1:1-22.
    Rename one of them.

Reanalyze modified (an unused @inline constant):
Example:
    @inline let unusedConst = 2
After (reanalyze):
    Warning Dead Value
    x.res:1:1-27
    unusedConst is never used

Review: Does an interface still decide what other modules inline? Yes.
`@inline(c) let x: t` in a .resi still needs an @inline implementation with
the same constant, and a plain `let x = c` still does not match it. A plain
`let x: t` hides the constant: other modules read the field, which is now
the implementation's own binding instead of a coercion that rebuilt it.

Review: Why do Int.Constants and Ordering change in the runtime? Their
constants are @inline, so `Stdlib_Int.Constants.minValue` and `maxValue`
and `Stdlib_Ordering.less`, `equal` and `greater` are now exported. The
unreleased changelog entries that said constants have no export are
updated, and the breaking entry for `Int.Constants` is dropped: with the
export back, nothing about it breaks.

Review: Why does a module alias in a recursive module now get the tag
`Value` instead of `value`? Constants and aliases share one shape tag, and
`Value` is the name of the runtime constructor. The runtime only tests for
`Module`, so both spellings behave the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the export-inline-constants branch from 0c59094 to 0895c16 Compare December 4, 2026 22:18
@cknitt

cknitt commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T07:20:44.220581Z 0895c16 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. Can't wait for the next one!

Reviewed commit: 0895c161f7

ℹ️ 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 cristianoc December 5, 2026 11:47
@cknitt
cknitt marked this pull request as ready for review December 5, 2026 11:47

This branch has not been deployed

No deployments
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.

1 participant