Semantic Diff

#9467 Type @inline constants in the type checker, keep their attributes and allow annotations

5 commits

e51ee460f8c8190e9c0edec81ae11284233d22b6..pr-9467

0a953dc8f · cknitt + Claude · Nov 28, 2026, 19:46 UTC

Type @inline constants in the type checker and keep their attributes

Why

The frontend rewrote @inline let x = 1 and @inline(1) let x: int into external x: int = "#rescript-inline", whose only attribute was the @inline payload, and the type checker decoded that again. The rewrite dropped every other attribute, so a deprecated @inline constant warned nowhere and its doc comment was lost. The type checker now reads @inline itself, as it does FFI attributes since #8728, and the reserved primitive name is gone. This is the @inline item of #8624 and proposal 2 of #8733.

modifiedAST expanded tree

/** The old limit. */@deprecated("Use newLimit instead") @inlinelet limit = 10
expanded
Before
After the binding as written
structure_itemstructure_item├─ Pstr_primitive└─ value_description "limit"   ├─ attribute "inline"   │     └─ structure_item   │        ├─ Pstr_eval   │        └─ expression   │           └─ Pexp_constant PConst_int (10,None)   ├─ core_type   │  ├─ Ptyp_constr "int"   │  └─ []   └─ "#rescript-inline" └─ Pstr_value Nonrec   └─ <def>      │  ├─ attribute "res.doc"      │  │  └─ …      │  ├─ attribute "deprecated"      │  │  └─ …      │  └─ attribute "inline"      │     └─ []      ├─ pattern      │  └─ Ppat_var "limit"      └─ expression         └─ Pexp_constant PConst_int (10,None)

modifiedTypedtree inline constant

typed
Before
After
structure_itemstructure_item├─ Tstr_primitive├─ Tstr_primitive└─ value_description limit/1001└─ value_description limit/1001    ├─ attribute "deprecated"   │  └─ …   ├─ attribute "res.doc"   │  └─ …   ├─ core_type   ├─ core_type   │  ├─ Ttyp_constr "int/1"   │  ├─ Ttyp_constr "int/1"   │  └─ []   │  └─ []   └─ <inline const>   └─ <inline const>

addedDiagnostic deprecated inline constant

// User.reslet a = Legacy.limit
diagnostic
Before
After
none Warning number 3User.res:1:9-20 deprecated: Legacy.limitUse newLimit instead

removedDiagnostic reserved primitive

external answer: int = "#rescript-inline"
diagnostic
Before
After
"#rescript-inline" is reserved for @inline constantsnone; it is an unknown primitive like any other `#` name

Review

  • Does the generated JavaScript change? No. The typed tree is the same Tstr_primitive with Kind_inline_const, and every tests/tests output is unchanged.
  • Why does Ast_checks call inline_const_of_binding? The general literal check runs before type checking and would add warning 107 to the out-of-range @inline int error. Calling it first reports only the error, as the frontend rewrite did.
  • Is an annotated binding such as @inline let x: int = 3 inlined now? No, it stays an ordinary exported value with warning 53. Inlining it would remove its JavaScript export, so that is left for a separate decision.
ffb8d0962 · cknitt + Claude · Nov 28, 2026, 19:55 UTC

Inline annotated @inline constants

Why

@inline let x: int = 3 was an ordinary value with warning 53, because only a binding without a type annotation was turned into a constant. The author asked for inlining, and the annotation says no more than the literal does. The annotation must be the literal's type, possibly through an alias, since the constant is inlined with the literal's representation.

modifiedJS annotated constant

// A.restype id = int@inline let typed: int = 3@inline let aliased: id = 4 // U.reslet u = (A.typed, A.aliased)
js
Before A.mjs
After A.mjs
let typed = 3; let aliased = 4; export { typed, aliased,}/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
js
Before U.mjs
After U.mjs
import * as A from "./A.mjs"; let u = [let u = [ A.typed, A.aliased 3, 4];];

removedDiagnostic annotated constant

diagnostic
Before
After
Warning number 53A.res:2:1-26 the @inline attribute cannot appear in this contextnone

addedDiagnostic annotation mismatch

@inline let ratio: float = 3
diagnostic
Before the ordinary let mismatch
After
This has type: intBut it's expected to have type: floatThis @inline constant has type int, but it is annotated with type float.

Review

  • Can an annotation hide the literal's type, as in type t and @inline let x: t = 3? No. The annotation must equal the literal's type after expanding aliases, so an abstract type, a type variable or option<int> is an error. An interface can still give an @inline constant an abstract type.
  • Is @inline let x: type a. int = 3 inlined? No. A type a. annotation keeps the binding an ordinary let.
35d909ba6 · cknitt + Claude · Nov 28, 2026, 20:31 UTC

Do less work for each @inline constant

The allocation benchmark flagged Stdlib_Ordering.res (+1.43% allocated words): its three @inline constants keep their doc comments now, and each one was also built twice and type-compared. The early range check now only reads the literal, the type comparison runs only for an annotated binding, and attributes are copied only when there is a @deprecated. The file now allocates 0.79% more than on master, which is the doc comments.

ed5b1e84c · cknitt + Claude · Nov 29, 2026, 06:51 UTC

Let the literal fill in type variables in an @inline annotation

Why

An annotated @inline constant was compared with the literal's type for equality, so @inline let x: _ = 3 and @inline let x: 'a = 3 were rejected, although an ordinary let accepts them and they compiled before this pull request. The annotation is now unified with the literal's type, as for a let. A Codex review found this.

removedDiagnostic annotation with a type variable

@inline let x: _ = 3let y = x
diagnostic
Before
After
This @inline constant has type int, but it is annotated with type 'a.none; `x` is the inline constant 3 of type int
js
After
let y = 3;

Review

  • Which type does the constant get? The annotation when it has no type variables, so an alias such as id is kept, and otherwise the literal's type. float, an abstract type or option<'a> are still errors.
d46c90172 · cknitt + Claude · Nov 29, 2026, 07:08 UTC

Check an @inline annotation once, against a fresh literal type

Why

A review by Claude (Fable model) found that the annotation check could corrupt the compilation unit. It unified the annotation with Predef's shared int node, so after @inline let x: id = 4 every later int in the file, and in its .cmi, became id, and @inline let x: char = 97 overflowed the stack, since char is defined as that node. It also translated the annotation twice, the first time outside the binding's @warning scope, which doubled or unsilenced warnings. And 'a. int annotations and @inline let _ = … were treated like constants.

modifiedTyping annotated @inline constant

text
Before Typemod, then Typedecl.transl_value_decl
After Typedecl.transl_value_decl, inside the binding's warning scope
translate the annotation, unify it with Predef's shared type_int ()translate the annotation again, as a type schemetranslate the annotation once, unify it with a fresh int, string, float, bool or biginton failure, undo the unification, then report it

modifiedDiagnostic a dependent of a module with an alias constant

// D2.resmodule M = {  type id = int  @inline let al: id = 4}let one = 1// U.reslet a = D2.one + 1
diagnostic
Before
After
This has type: idBut it's expected to have type: intnone

modifiedDiagnostic char annotation

@inline let x: char = 97
diagnostic
Before
After
Fatal error: exception Stack overflownone; `x` is the inline constant 97 of type char

modifiedRepresentation an @inline constant's attributes

mapping
Before
After
.cmi ↦ every attribute but @inline, doc comments included .cmi ↦ only @deprecated, as for a plain let.cmt ↦ every attribute but @inline, in source order, for editors

Review

  • Which bindings count as constants? Ast_checks and the type checker now share one test, Ast_external_mk.inline_binding: a single non-recursive let of a name, with no annotation or one that is not type a. or 'a.. @inline let _ = … and @inline let y: 'a. int = 3 are ordinary values with warning 53, as on master.