Check @inline constants in signatures and their payloads
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).
modifiedTyping 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 ↦ acceptedan abstract type ↦ acceptedanother built-in type, a type variable, a concrete type ↦ error
addedDiagnostic signature constant with the wrong type
module type S = { @inline("x") let x: int}
diagnostic
Before
After
noneThis @inline constant has type string, but it is declared with type int.
addedDiagnostic signature value without a constant
module type S = { @inline let r: int}
diagnostic
Before
After
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)
addedDiagnostic payload on a constant
@inline(3) let x = 4@inline(never) let y = 5@inline("ascii") let ascii = "ascii"
diagnostic
Before
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 twith@inline(1) let one: thides the representation, which an implementation withtype t = intprovides; matching it against the signature checks the constant. - Does the runtime change? No.
Stdlib_Int.resihad two@inlinevalues without a constant, which did nothing and now warn; they are plain values again.