Rule 4.6
Chapter 4, Types Test
boolean written as a type must mean bool, and number written as a type must mean that the type is inferred from its uses (OD-7); where no use decides, the type must be f32.
Where TypeScript lets an annotation be omitted (a local, a result, a field, a list’s element, a parameter), number must read as if it were omitted, and an unannotated parameter, with or without a default, must mean the same as one written number. f32, i32, u32 and f64 must keep fixing a width.
A parameter’s width must come from the places that call the function:
- One call site, or call sites of one width: the parameter has the width of the values the calls pass.
- Call sites of different widths: the function is compiled once for each set of widths the calls use, as a generic function is (Rule 8.9, Rule 3.9); no call is refused for its width.
- No call site: the parameter is
f32. - An exported function: a call of it from any file of the program (Rule 3.9) is a call site and rules 1 to 3 apply; a call from outside the program is not read. A parameter of an entry point or a kernel function is inferred from the uses in its body only, and is
f32where none decides. - A recursive function: the width of a parameter of a call cycle is the width the calls from outside the cycle pass, and the calls inside the cycle must agree with it or the cycle is compiled once for each such width (recursion itself stays under Rule 8.4).
- A value whose uses in one function demand two widths, and a
numberthat one use makes an integer and that/divides, must be refused as Rule 5.1 refuses them.
A struct field, a class field and a field of a uniform or storage struct that is written number is decided from the places that assign or read it, and is f32 where none decides.
The rule text and the parts under it are the compiler's own English, as the design document writes them.
Rationale
TypeScript accepts number and boolean (OD-12), and OD-7 removed the argument that number names no width: the compiler infers the width and chooses f32 where no use decides.
Derives from
Integer Types, Floating Point Types, Boolean Type; changes/0159-number-and-boolean-written-as-types.md.
How it is verified
Checked by a test. A test, a gate script or a CI workflow names this rule, and the traceability check fails when a file listed below stops naming it.
Where the rule says the compiler enforces it:
src/compiler/ts/number-types.test.ts.
The files that verify it at commit af54ddad5, each at the first line that names the rule:
Explained in
The sections of the surface document that explain this rule, at commit af54ddad5:
See also
Source
The rule at commit af54ddad5:
docs/language-design.md:377(the design document)reqs/rules/RULE-0406.md(its traceability item)