Skip to main content

Module consts

Module consts 

Source
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§

ResolvedConst
#3445: a constant an annotation argument names, resolved to the literal it denotes.
VisibleConst
A constant visible in a unit (visible_consts), with where it is declared.

Functions§

alias_literal
#3065: the string literal a String constant alias denotes, following each hop in the scope of the unit declaring the constant it was written in.
builtin_name_error
bynk.resolve.name_conflict for 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 reservation bynk.resolve.reserved_builtin_type makes for a type declaration).
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. next names the constant a reference points to, in whichever scope the caller resolves names; limit bounds the walk, since a longer chain is a cycle already reported as bynk.resolve.const_cycle.
compose_unit_consts
Every constant visible in unit name: its own, plus the public ones of each commons it uses (one level, the mixin rule crate::embeds::compose_unit_embeds applies to embeds). A local declaration of any kind wins over an imported constant of the same name; two used commons declaring the same name is bynk.uses.name_conflict, so which is kept here is never observable in a program that checks.
cycle_errors
bynk.resolve.const_cycle for each cycle through the constants local (this file’s, in declaration order) among consts (every constant in scope). Each cycle is reported once, at the member declared first here.
is_constant_name
Whether name is upper-camel case: the lexer’s constant_name class, /[A-Z][A-Za-z0-9_]*/.
is_literal_of
Whether kind is a plain literal of the base type base: 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_case for c, 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 is bynk.resolve.duplicate_const; an embed, an asset, a free function or a type is bynk.resolve.name_conflict (a constant is a value, in the same namespace as a free function). None when 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_commons for c.
referenced_consts
The constants e refers to, by name, in source order (with repeats).
resolve_const_literal
#3445: resolve the bare name name, written in unit at, to the literal of type base it denotes. Each hop of the chain is read in the scope of the unit declaring it, as alias_literal does.
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, and builtin_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 init outside the closed set of static initialisers (decision A of #2617, after ADR 0004), as bynk.resolve.bad_const_initialiser on that part. consts are the constants in scope (a reference to one is static); types let a variant constructor be told from a function call.
variant_conflict_error
bynk.resolve.name_conflict for a constant that shares its name with a variant of a sum type in scope, or with a predeclared HttpResult variant: a bare Name would read as either, so the constant may not take the name.
visible_consts
#2967: the constants compose_unit_consts composes, from a unit’s own table, its uses and a lookup of the used units’ tables, each with its owner. The project phases that run before UnitInfo is 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.