Repository navigation
Type @inline constants in the type checker, keep their attributes and allow annotations - #9467
Conversation
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.
AST modified (expanded tree):
Example:
/** The old limit. */
@deprecated("Use newLimit instead") @inline
let limit = 10
Before (expanded):
structure_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"
After (expanded): the binding as written
structure_item
Pstr_value Nonrec
[
<def>
attribute "res.doc"
…
attribute "deprecated"
…
attribute "inline"
[]
pattern
Ppat_var "limit"
expression
Pexp_constant PConst_int (10,None)
]
Typedtree modified (inline constant):
Before (typed):
structure_item
Tstr_primitive
value_description limit/1001
core_type
Ttyp_constr "int/1"
[]
<inline const>
After (typed):
structure_item
Tstr_primitive
value_description limit/1001
attribute "deprecated"
…
attribute "res.doc"
…
core_type
Ttyp_constr "int/1"
[]
<inline const>
Diagnostic added (deprecated inline constant):
Example:
// User.res
let a = Legacy.limit
Before (diagnostic): none
After (diagnostic):
Warning number 3
User.res:1:9-20
deprecated: Legacy.limit
Use newLimit instead
Diagnostic removed (reserved primitive):
Example:
external answer: int = "#rescript-inline"
Before (diagnostic):
"#rescript-inline" is reserved for @inline constants
After (diagnostic): none; 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.
Review: 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.
Review: 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.
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>
Changelog🐛 Bug fix
Semantic Diff5 commits · Full page Type
|
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.
JS modified (annotated constant):
Example:
// A.res
type id = int
@inline let typed: int = 3
@inline let aliased: id = 4
// U.res
let u = (A.typed, A.aliased)
Before (js): A.mjs
let typed = 3;
let aliased = 4;
export {
typed,
aliased,
}
After (js): A.mjs
/* This output is empty. Its source's type definitions, externals and/or unused code got optimized away. */
Before (js): U.mjs
import * as A from "./A.mjs";
let u = [
A.typed,
A.aliased
];
After (js): U.mjs
let u = [
3,
4
];
Diagnostic removed (annotated constant):
Before (diagnostic):
Warning number 53
A.res:2:1-26
the @inline attribute cannot appear in this context
After (diagnostic): none
Diagnostic added (annotation mismatch):
Example:
@inline let ratio: float = 3
Before (diagnostic): the ordinary `let` mismatch
This has type: int
But it's expected to have type: float
After (diagnostic):
This @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.
Review: Is `@inline let x: type a. int = 3` inlined? No. A `type a.`
annotation keeps the binding an ordinary `let`.
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>
🎉 Compiler benchmarksAllocated wordsIn total: 658,600,291 → 658,911,438 (+0.05%). Generated projects: 629,278,417 → 629,861,705 (+0.09%). 🎉 Improvement: 1 benchmark allocates more than 1% less.
84 more changes
InstructionsIn total: 9,227,223,941 → 9,224,795,389 (-0.03%).
6 more changes
Output sizeIn total: 26,829,064 → 26,830,802 (+0.01%). None of the 13 benchmarks changed by 0.1% or more. Pull request d46c901 compared with base 87db367 in this run. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #9467 +/- ##
==========================================
+ Coverage 82.34% 82.37% +0.02%
==========================================
Files 462 462
Lines 65817 65781 -36
==========================================
- Hits 54198 54186 -12
+ Misses 11619 11595 -24
🚀 New features to boost your workflow:
|
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
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. 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>
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 35d909ba6e
ℹ️ 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".
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.
Diagnostic removed (annotation with a type variable):
Example:
@inline let x: _ = 3
let y = x
Before (diagnostic):
This @inline constant has type int, but it is annotated with type 'a.
After (diagnostic): none; `x` is the inline constant 3 of type int
After (js):
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.
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>
|
@codex review |
|
Codex Review: Didn't find any major issues. 👍 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
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.
Typing modified (annotated @inline constant):
Before (text): Typemod, then Typedecl.transl_value_decl
translate the annotation, unify it with Predef's shared type_int ()
translate the annotation again, as a type scheme
After (text): Typedecl.transl_value_decl, inside the binding's warning scope
translate the annotation once, unify it with a fresh int, string, float, bool or bigint
on failure, undo the unification, then report it
Diagnostic modified (a dependent of a module with an alias constant):
Example:
// D2.res
module M = {
type id = int
@inline let al: id = 4
}
let one = 1
// U.res
let a = D2.one + 1
Before (diagnostic):
This has type: id
But it's expected to have type: int
After (diagnostic): none
Diagnostic modified (char annotation):
Example:
@inline let x: char = 97
Before (diagnostic):
Fatal error: exception Stack overflow
After (diagnostic): none; `x` is the inline constant 97 of type char
Representation modified (an @inline constant's attributes):
Before (mapping):
.cmi ↦ every attribute but @inline, doc comments included
After (mapping):
.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.
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>
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
cristianoc
left a comment
There was a problem hiding this comment.
Are they still inlined across files?
|
Yes. An The last commit drops attributes from the From the tests:
|
|
Developer playground preview: https://rescript-lang.github.io/rescript/dev-playground/?version=pr-9467 |
The type checker now handles
@inlineconstants itself. Before, the frontend rewrote@inline let x = 1and@inline(1) let x: intintoexternal x: int = "#rescript-inline", and the type checker decoded that again.The rewrite dropped all other attributes. So
@deprecatedon an@inlineconstant never warned, and its doc comment was lost. Both are now kept. The reserved"#rescript-inline"primitive name is gone too.This is the
@inlineitem of #8624 and proposal 2 of #8733, done the way #8728 handled FFI attributes: the attribute stays as written, and the parsetree doesn't change.Annotated constants such as
@inline let x: int = 3are now inlined too. Before, they were ordinary values with warning 53. The annotation is checked as for an ordinarylet: an alias is kept,_and type variables take the literal's type, and anything else is an error: "This @inline constant has type int, but it is annotated with type float." Like other@inlineconstants, they're no longer exported to JavaScript or by genType. Otherwise no JavaScript output changes.As for a plain
let, the.cmikeeps only an@inlineconstant's@deprecatedattributes; the.cmt, which editors read, keeps all of them.Benchmarks:
Stdlib_Ordering.resallocates about 1.25% more. Its three@inlineconstants have long doc comments, which are now kept instead of thrown away. Everything else is flat or allocates less.New tests:
Cross_deprecated_inline_const(multi-file error test): a deprecated@inlineconstant from a module and from an interface warns at its use in another module, not inside its own module.inline_const_annotated.resandinline_const_annotated_use.res(JS output tests): annotated constants are inlined, including through an alias,char,_and'a. A module using it from outside still sees its plainintvalues asint.inline_const_annotation_mismatch.res(error test): an annotation that isn't the literal's type.inline_const_annotation_deprecated_type.res(error test): a deprecated type in an annotation warns once, and not under the binding's@warning("-3").inline_const_not_a_constant.res(error test):@inline let x: 'a. int = …and@inline let _ = …stay ordinary values with the usual warnings.InlineConstants.res(ast-mapping fixture):@inlineconstants round-trip through the v0 AST.The
external_reserved_inline_primitiveerror test is removed along with the error.🤖 Generated with Claude Code
https://claude.ai/code/session_01Kbf3gJwNt3jmNq5d8xKA9D