Skip to content

Check @inline constants in signatures and their payloads - #9483

Merged
cknitt merged 1 commit into
remove-cross-module-optfrom
inline-const-diagnostics
Nov 30, 2026
Merged

cknitt merged 1 commit into
remove-cross-module-optfrom
inline-const-diagnostics

Conversation

@cknitt

@cknitt cknitt commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

Diagnostics for @inline constants. This is PR 6 of the plan in #8733 (problem 2.11), and it fixes #9468.

Signature constants are checked against their type (#9468). @inline("x") let x: int was accepted, and code using the signature compiled M.x + 1 to "x" + 1 | 0. The declared type may be:

  • the constant's own type, or an alias of it, private or not;
  • an abstract type, which an implementation with type t = int provides.

Another built-in type, a type variable, or a concrete type such as a variant is now an error: "This @inline constant has type string, but it is declared with type int."

An @inline in a signature without a constant (@inline let x: int) did nothing. It now gets warning 47. Stdlib_Int.resi had two, which are plain values again; the runtime's JavaScript doesn't change.

Payloads on constants in a structure:

  • @inline(never) leaves an ordinary value, as on a function, with warning 53.
  • A payload that is neither a constant nor always gets warning 47 and leaves an ordinary value.
  • A constant payload that repeats the value stays a constant, as packages such as rescript-nodejs write it: @inline("ascii") let ascii = "ascii".
  • One that differs from the value (@inline(3) let x = 4) stays a constant and gets warning 47.

New tests:

  • tests/tests/src/inline_const_signature.res: the accepted declared types, and rescript-nodejs's pattern;
  • error tests: a different built-in type, a type variable and a variant in a signature, a signature value without a constant, and the payload cases.

Part of stack #9486, after #9480.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D

@cknitt
cknitt changed the base branch from master to remove-cross-module-opt November 29, 2026 09:27
@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Report an @inline constant in a signature whose declared type can't hold it, such as @inline("x") let x: int. Code using such a signature compiled to wrong JavaScript. The constant's own type, an alias of it (private or not) and an abstract type are still accepted.
  • Warn about an @inline value in a signature without a constant it can inline, such as @inline let x: int, which had no effect.
  • Treat @inline(never) on a constant like on a function: the value is no longer inlined, and the attribute is reported as misplaced. A payload that is neither a constant nor always is reported as an illegal payload, and so is a constant payload that differs from the value, as in @inline(3) let x = 4.

Semantic Diff

1 commit · Full page

Check @inline constants in signatures and their payloads

2bb598f · cknitt + Claude · Nov 29, 2026, 08:05 UTC

Why

A signature's @inline(constant) declaration was never checked against its type, so @inline("x") let x: int was accepted and M.x + 1 compiled to "x" + 1 | 0 until some implementation was matched against it (#9468). An @inline without a constant in a signature did nothing and said nothing; Stdlib_Int.resi had two of them. And in a structure, any payload on a constant was ignored: @inline(never) let y = 5 was inlined, and @inline(3) let x = 4 was the constant 4 without a word. A payload that repeats the value, as packages such as rescript-nodejs write it, stays a constant. This is PR 6 of the plan in #8733 (problem 2.11).

Typing modified (a signature's @inline constant)

mapping · Before: Typedecl.transl_value_decl · After: Typedecl.check_signature_inline_const

-any declared type ↦ accepted
+the literal's type, or an alias of it, private or not ↦ accepted
+an abstract type ↦ accepted
+another built-in type, a type variable, a concrete type ↦ error

Diagnostic added (signature constant with the wrong type)

module type S = {
  @inline("x") let x: int
}

diagnostic

-none
+This @inline constant has type string, but it is declared with type int.

Diagnostic added (signature value without a constant)

module type S = {
  @inline let r: int
}

diagnostic

-none
+Warning number 47
+
+illegal payload for attribute @inline.
+In a signature, it must be a string, int, float, bool or bigint literal, such as @inline(3)

Diagnostic added (payload on a constant)

@inline(3) let x = 4
@inline(never) let y = 5
@inline("ascii") let ascii = "ascii"

diagnostic · After: x and ascii stay constants, y is an ordinary value

-none; all three are inlined constants
+Warning number 47
+
+illegal payload for attribute @inline.
+It differs from the value, which is used instead
+
+Warning number 53
+
+the @inline attribute cannot appear in this context

Review

  • Why may an abstract type hold a constant? type t with @inline(1) let one: t hides the representation, which an implementation with type t = int provides; matching it against the signature checks the constant.
  • Does the runtime change? No. Stdlib_Int.resi had two @inline values without a constant, which did nothing and now warn; they are plain values again.

@cknitt
cknitt added this pull request to stack #9486 November 29, 2026 09:29
@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 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-11-29T10:09:48.065112Z a173165 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b8bdbe8467

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

Comment thread compiler/typing/ast_external_mk.ml Resolved
@codecov

codecov Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.41667% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.46%. Comparing base (7cc0465) to head (2bb598f).

Files with missing lines Patch % Lines
compiler/typing/ast_external_mk.ml 73.68% 5 Missing ⚠️
compiler/typing/typedecl.ml 93.10% 2 Missing ⚠️
Additional details and impacted files
@@                   Coverage Diff                    @@
##           remove-cross-module-opt    #9483   +/-   ##
========================================================
  Coverage                    82.46%   82.46%           
========================================================
  Files                          466      466           
  Lines                        66029    66070   +41     
========================================================
+ Hits                         54448    54486   +38     
- Misses                       11581    11584    +3     
Files with missing lines Coverage Δ
compiler/typing/typedecl.ml 84.04% <93.10%> (+0.16%) ⬆️
compiler/typing/ast_external_mk.ml 91.04% <73.68%> (-2.84%) ⬇️

... and 1 file 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 Nov 29, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 674,610,681 → 674,608,294 (±0.00%).

Generated projects: 630,566,002 → 630,566,091 (±0.00%).

Benchmark Base Head Change
packages/@rescript/runtime/Stdlib_Int.resi (inputs changed) 204,922 204,566 -0.17%

(inputs changed): this pull request changes runtime or Belt sources, so these benchmarks compile different code than on the base, and their changes don't count as regressions.

Instructions

In total: 9,225,535,250 → 9,225,173,296 (±0.00%).

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

Output size

In total: 27,350,702 → 27,351,980 (±0.00%).

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

Pull request 2bb598f compared with base 4e9a23b in this run.

@pkg-pr-new

pkg-pr-new Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 2bb598f

@cknitt
cknitt force-pushed the inline-const-diagnostics branch from c5e8794 to c085466 Compare November 29, 2026 09:57
@cknitt
cknitt force-pushed the inline-const-diagnostics branch from c085466 to a173165 Compare November 29, 2026 10:07
@cknitt

cknitt commented Nov 29, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: a1731655e8

ℹ️ 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 force-pushed the inline-const-diagnostics branch from a173165 to c5bf043 Compare November 29, 2026 10:35
@cknitt
cknitt force-pushed the inline-const-diagnostics branch from c5bf043 to 2aca9bb Compare November 29, 2026 10:59
@cknitt
cknitt force-pushed the inline-const-diagnostics branch 2 times, most recently from 45949b3 to 705d4e3 Compare November 29, 2026 11:30
@cknitt
cknitt force-pushed the inline-const-diagnostics branch from 705d4e3 to 1b603ff Compare November 29, 2026 11:56
@cknitt
cknitt marked this pull request as ready for review November 29, 2026 12:08
@cknitt
cknitt requested a review from cristianoc November 29, 2026 12:12
Why:
    A signature's `@inline(constant)` declaration was never checked against
    its type, so `@inline("x") let x: int` was accepted and `M.x + 1`
    compiled to `"x" + 1 | 0` until some implementation was matched against
    it (#9468). An `@inline` without a constant in a signature did nothing
    and said nothing; `Stdlib_Int.resi` had two of them. And in a structure,
    any payload on a constant was ignored: `@inline(never) let y = 5` was
    inlined, and `@inline(3) let x = 4` was the constant 4 without a word.
    A payload that repeats the value, as packages such as rescript-nodejs
    write it, stays a constant. This is PR 6 of the plan in #8733 (problem 2.11).

Typing modified (a signature's @inline constant):
Before (mapping): Typedecl.transl_value_decl
    any declared type ↦ accepted
After (mapping): Typedecl.check_signature_inline_const
    the literal's type, or an alias of it, private or not ↦ accepted
    an abstract type ↦ accepted
    another built-in type, a type variable, a concrete type ↦ error

Diagnostic added (signature constant with the wrong type):
Example:
    module type S = {
      @inline("x") let x: int
    }
Before (diagnostic): none
After (diagnostic):
    This @inline constant has type string, but it is declared with type int.

Diagnostic added (signature value without a constant):
Example:
    module type S = {
      @inline let r: int
    }
Before (diagnostic): none
After (diagnostic):
    Warning number 47

    illegal payload for attribute @inline.
    In a signature, it must be a string, int, float, bool or bigint literal, such as @inline(3)

Diagnostic added (payload on a constant):
Example:
    @inline(3) let x = 4
    @inline(never) let y = 5
    @inline("ascii") let ascii = "ascii"
Before (diagnostic): none; all three are inlined constants
After (diagnostic): x and ascii stay constants, y is an ordinary value
    Warning number 47

    illegal payload for attribute @inline.
    It differs from the value, which is used instead

    Warning number 53

    the @inline attribute cannot appear in this context

Review: Why may an abstract type hold a constant? `type t` with
`@inline(1) let one: t` hides the representation, which an implementation
with `type t = int` provides; matching it against the signature checks
the constant.

Review: Does the runtime change? No. `Stdlib_Int.resi` had two `@inline`
values without a constant, which did nothing and now warn; they are
plain values again.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the inline-const-diagnostics branch from 1b603ff to 2bb598f Compare November 29, 2026 14:05
@cknitt
cknitt merged commit 51c0dae into master Nov 30, 2026
52 of 69 checks passed
@cknitt
cknitt deleted the inline-const-diagnostics branch November 30, 2026 07:53
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.

@inline constants in signatures are not checked against their type

2 participants