feat: reserve $ in the Form-A lexer (closes #44)

Enforces the `$`-in-authored-names reservation that fresh_binder
(ailang-core::desugar) silently relied on. `fresh_binder` mints
shadow-rename binders as <base>$<n>; its collision probe could not see
an authored binder literally named <base>$<n> that binds later under
the same (def, name) uniqueness key — the exact collapse class #43
closed. The `$`-for-synthetic convention (`$mp_N`, <hint>$lr_N,
<base>$<n>) was held by discipline only.

This makes it a lexer-enforced invariant: no Form-A identifier token
may contain `$`. A one-line guard in tokenize rejects any token run
containing `$` before int/float/ident classification, raising the new
LexError::ReservedDollar { token, start }. It surfaces through the
existing ParseError::Lex -> W::SurfaceParse -> surface-parse-error
channel with zero new wiring — no new CheckError, no AST walker, no
entry-point threading.

Placement rationale (the load-bearing call): the threat vector is
Form-A source only. The client LLM author is forbidden from emitting
canonical .ail.json directly — it writes Form A exclusively; only the
orchestrator hand-authors JSON in rare exceptions. So every authored
identifier a hallucinating client could produce flows through the
lexer. `$` is a reserved character (like parens) with no legitimate
authored use in any position — unlike `.`/`/`, which DO have
legitimate uses (std_list.map) and therefore live in the check layer
(InvalidDefName) with AST-aware positional logic. No positional split
for `$` means no walker; the lexer is the right boundary. An earlier
draft put the reject in pre_desugar_validation justified by "a client
could build .ail.json directly" — false under the authoring contract,
so the simpler lexer reservation replaced it.

Exemptions by scan order, not special-case: `$` inside string literals
and comments stays legal because both are consumed by earlier branches
of the scan loop, before a run is ever sliced. The six checked-in
example files mentioning loop$lr_0 in comments keep parsing.

Documented non-goals (honesty rule): the .ail.json deserialization
path stays unguarded (the orchestrator's self-responsible channel);
the fresh_binder probe-body simplification (issue Q4) is not bundled —
only its now-false doc-comment is corrected to state the enforced
invariant, scoped to Form-A authored identifiers.

Verification (all run, all green):
- 3 in-source lexer tests: reject on x$1, exemption for $ in string,
  exemption for $ in comment. RED confirmed pre-guard (tokenize("x$1")
  returned Ok([Ident]), expect_err panicked); GREEN after.
- 2 integration tests (reserved_dollar_pin.rs): the reject reaches the
  public parse entry as ParseError::Lex(ReservedDollar); string
  exemption survives at parse level.
- cargo test --workspace: green, 0 failed.
- CLI: check on the comment-$ example (exit 0) and on the #43 shadow
  idiom raw_buf_int.ail (exit 0, buf->buf$1 rename minted post-parse,
  never re-lexed); a fresh $-binder source now rejected with the
  surface-parse-error diagnostic naming the token and byte offset.

Spec: docs/specs/0057-reserved-dollar-in-names.md (grounding-check
PASS). Plan: docs/plans/0112-reserved-dollar-in-names.md.
This commit is contained in:
2026-05-30 15:00:17 +02:00
parent 559531806d
commit 11e4c4624d
3 changed files with 72 additions and 9 deletions
+10 -9
View File
@@ -525,15 +525,16 @@ impl Desugarer {
/// prior mints). Records the minted name in `used` so a later mint
/// is distinct.
///
/// `$` is the project's convention for synthetic names, but it is
/// NOT lexer-enforced an authored binder may legally contain `$`.
/// The probe rejects a `<base>$<n>` that is already in `used` or
/// currently in `scope`, but it does NOT see an authored
/// `<base>$<n>` that is out of scope at mint time yet later binds
/// under the same `(def, name)` key. No fixture exercises an
/// authored `$` binder; closing that latent gap (enforce the
/// reservation, or seed `used` with every authored binder name in
/// the def) is tracked as a follow-up.
/// `$` is reserved for the compiler's synthetic namespace and is
/// rejected in any authored Form-A identifier by the lexer
/// (`ailang_surface::lex::tokenize` → `LexError::ReservedDollar`).
/// So a `<base>$<n>` candidate minted here cannot alias any
/// authored name — no authored name can contain `$`. The probe
/// against `used` and `scope` therefore guards only against prior
/// synthetic mints and currently in-scope renamed binders. (The
/// reservation is on *Form-A authored* identifiers; the `.ail.json`
/// deserialization path is intentionally not guarded — it is the
/// orchestrator's self-responsible channel, not client-reachable.)
fn fresh_binder(&mut self, base: &str, scope: &Scope) -> String {
let mut n = 1u64;
loop {