Expand description
#2617 (named constants): the checker-side rules for const Name: Type = <static expression> declarations — the name rules, the closed set of
static initialisers (ADR 0004, plus references to other constants), the
cycle rule and the composition of a unit’s visible constants through
uses.
A constant is a value in its unit’s single value namespace (spec §5.1),
like a function, an asset or an embed: a unit that uses a commons sees
its constants, and a sibling file of the same unit imports them as it
imports a function. Its type is its annotation, resolved where it is used.
It emits as a hoisted, cached function (spec §7.3.1a), so neither the
order of a file’s constants nor the order in which a unit’s file modules
evaluate matters.
Structs§
- Resolved
Const - #3445: a constant an annotation argument names, resolved to the literal it denotes.
- Visible
Const - A constant visible in a unit (
visible_consts), with where it is declared.
Functions§
- alias_
literal - #3065: the string literal a
Stringconstant alias denotes, following each hop in the scope of the unit declaring the constant it was written in. - builtin_
name_ error bynk.resolve.name_conflictfor a constant named like a built-in type (List,Map): the parser reads such a name as the type in every later reference, so the constant could never be used (the same reservationbynk.resolve.reserved_builtin_typemakes for atypedeclaration).- chain_
literal - #3445: the literal a constant denotes, following constant-to-constant
references. The one resolution loop every literal-only position shares: the
text of a SQL call (#3065, ADR 0762) and an annotation argument (ADR 0678 C,
as amended). Every constant on the chain must be declared exactly
base(a refined or opaque constant does not count) and the chain must end at a literal of it.nextnames the constant a reference points to, in whichever scope the caller resolves names;limitbounds the walk, since a longer chain is a cycle already reported asbynk.resolve.const_cycle. - compose_
unit_ consts - Every constant visible in unit
name: its own, plus the public ones of each commons ituses(one level, the mixin rulecrate::embeds::compose_unit_embedsapplies to embeds). A local declaration of any kind wins over an imported constant of the same name; two used commons declaring the same name isbynk.uses.name_conflict, so which is kept here is never observable in a program that checks. - cycle_
errors bynk.resolve.const_cyclefor each cycle through the constantslocal(this file’s, in declaration order) amongconsts(every constant in scope). Each cycle is reported once, at the member declared first here.- is_
constant_ name - Whether
nameis upper-camel case: the lexer’sconstant_nameclass,/[A-Z][A-Za-z0-9_]*/. - is_
literal_ of - Whether
kindis a plain literal of the base typebase: the closed set of values a constant can be bound to directly (a constant’s initialiser has no operators, calls or interpolation,static_error). - name_
case_ error bynk.resolve.const_name_caseforc, when its name is not upper-camel.- name_
error - The name rule for constant
c, given what its unit already declares under that name: an earlier constant isbynk.resolve.duplicate_const; an embed, an asset, a free function or a type isbynk.resolve.name_conflict(a constant is a value, in the same namespace as a free function).Nonewhen the name is free. One rule for the project pipeline (symbols::build_unit_table) and the single-file compile. - placement_
error bynk.resolve.const_outside_context_or_commonsforc.- referenced_
consts - The constants
erefers to, by name, in source order (with repeats). - resolve_
const_ literal - #3445: resolve the bare name
name, written in unitat, to the literal of typebaseit denotes. Each hop of the chain is read in the scope of the unit declaring it, asalias_literaldoes. - single_
file_ const_ errors - The single-file compile’s declaration diagnostics for constants, in
declaration order: each constant’s name rules (
name_error, against the file’s own embeds, assets, functions, types and earlier constants, andbuiltin_name_error). Placement needs no check here: a single file is always a commons. The case, static-ness, type and cycle rules are the checker’s, on every path. - static_
error - The first part of
initoutside the closed set of static initialisers (decision A of #2617, after ADR 0004), asbynk.resolve.bad_const_initialiseron that part.constsare the constants in scope (a reference to one is static);typeslet a variant constructor be told from a function call. - variant_
conflict_ error bynk.resolve.name_conflictfor a constant that shares its name with a variant of a sum type in scope, or with a predeclaredHttpResultvariant: a bareNamewould read as either, so the constant may not take the name.- visible_
consts - #2967: the constants
compose_unit_constscomposes, from a unit’s own table, itsusesand a lookup of the used units’ tables, each with its owner. The project phases that run beforeUnitInfois assembled (project_model::phase_workers_ws_open_key) and the Workers emitter read the same set through this, so the checker and the emitter agree on which names are constants.