Compare commits

...

203 Commits

Author SHA1 Message Date
agra
792ed55068 Merge branch 'flow/distribution/fix-0101'
Some checks failed
Build / build-linux (push) Has been cancelled
Build / build-windows (push) Has been cancelled
2026-06-06 07:47:23 +03:00
agra
6f2a1dc3dc fix(types): type force-unwrap so opt!.field chains resolve [0101]
ExprTyper.inferType had no `.force_unwrap` arm, so `mk()!` typed as
`.unresolved`. The bind-first form (`v := mk()!; v.field`) worked because
lowerForceUnwrap produces a correctly typed value stored in a slot, but the
chained `mk()!.field` re-derives the receiver type via inferExprType and got
`.unresolved` — the struct-field lookup failed, the field read emitted as
`undef` (garbage), and `mk()!.method()` failed to resolve the method.

Add a `.force_unwrap` arm resolving the operand's optional child type. One
arm fixes every chained form — field, nested `opt!.a.b`, `opt!.method()`
(pointer + value receiver), and `opt![i]` all route receiver typing through
inferExprType.

Regression: examples/0905-optionals-unwrap-field-chain.sx — garbage / compile
error pre-fix, all correct after.
2026-06-06 07:42:17 +03:00
agra
52310b6df1 Merge branch 'flow/distribution/fix-0100' 2026-06-06 03:24:56 +03:00
agra
d11f4c84b6 fix(lower): null-FuncId path restores full caller state [0100 F2]
lazyLowerFunction's three exit paths (non-null branch, already-promoted
early return, null-FuncId `ns.fn` qualified-alias branch) each duplicated
the caller-state restore, and the null branch's copy had drifted: it
restored every saved field EXCEPT `block_terminated`. A qualified alias
whose body terminates (e.g. a constant-folded `if true { return ... }`)
leaves `block_terminated = true` after lowerFunction; the null path
returned without resetting it, so the flag leaked into the CALLER's body
lowering and the caller's own trailing statements / `return` were rejected
as dead-after-terminator ("function ... body produces no value").

Fix: collapse the three restores into a single `defer` registered right
after the state is saved, so every exit path restores the identical full
set and the class cannot diverge again. Fields restored on all paths:
current_source_file (F1), scope, func_defer_base, block_terminated (F2),
force_block_value, builder.func/current_block/inst_counter. The
foreign-class / jni-env / pack-mono / inline-return fields already had
their own defers and are unchanged.

Regression: examples/0721-modules-qualified-terminating-callee.sx — a
qualified alias `m.foo` folds `if true { return helper(); }` (helper from
m.sx's own import) and is followed by caller statements + the caller's own
`return 0`. Reports "body produces no value" pre-fix; prints
"terminating-callee: ok" / "after" and exits 0 after. 0719 (collision) and
0720 (F1 own-import visibility) stay green. issues/0100 RESOLVED banner
extended with the F2 follow-up.
2026-06-06 03:15:29 +03:00
agra
9274d47adf fix(lower): qualified alias lowers in its own source context [0100 F1]
The 0100 identity fix registers a namespaced import's own functions under a
module-qualified name (ns.fn) in fn_ast_map WITHOUT an eager declareFunction,
so the alias is lowered through lazyLowerFunction's null-FuncId lowerFunction
path. That path had no Function.source_file to restore (the non-null path does
setCurrentSourceFile(func.source_file)), so the alias lowered in the CALLER's
visibility context. A qualified function that called a helper from its OWN
module's flat import was then rejected "not visible".

Fix:
- ProgramIndex.qualified_fn_source maps each ns.fn alias to its declaring
  source file, populated in registerQualifiedFn (current_source_file is
  pinned to the decl's source by registerNamespaceQualifiedFns).
- lazyLowerFunction's null-FuncId branch restores that source before
  lowerFunction, so ns.fn's body lowers in its own module's context and its
  intra-module / own-import callees resolve.
- lowerFunction records Function.source_file = current_source_file on the
  freshly-begun function (matching declareFunction), so the lowered alias
  carries its own module for diagnostics/emit.

Regression: examples/0720-modules-qualified-own-import.sx — calc.compute (a
qualified alias) calls triple/base from calc.sx's own flat import; reports
"'triple' is not visible" on the attempt-1 code, passes after. 0719's
cross-module dual-parse assertion stays green. issues/0100 RESOLVED banner
extended with the F1 follow-up.
2026-06-06 02:51:09 +03:00
agra
3edc67521b fix(lower): resolve cross-module same-name functions by identity [0100]
Two modules each exporting a top-level function with the same short name
(std.cli.parse 3-param, std.json.parse 2-param) collided in IR lowering's
bare-name function table. fn_ast_map (name -> AST) was last-wins while
module.functions / resolveFuncByName are first-wins, so importing both and
calling one bound one function's AST against the other's FuncId and tripped
lazyLowerFunction's param-count assert (lower.zig:1606) — reached
unreachable code.

Fix:
- Register a namespaced import's OWN plain functions under their qualified
  name (ns.fn) in fn_ast_map, giving cli.parse / json.parse independent
  identities. The qualified resolution paths in CallResolver.plan /
  lowerCall already prefer ns.fn. NamespaceDecl now carries own_decls
  (populated in imports.addNamespace). Generic/comptime/pack/foreign
  functions are excluded (they dispatch by monomorphization off the bare
  template name); no eager declareFunction (it would resolve types before
  the forward-alias fixpoint).
- Make scanDecls' bare fn_ast_map registration first-wins so a later
  namespace recursion cannot clobber an earlier (flat) entry, aligning it
  with mergeFlat / resolveFuncByName.

Regression: examples/0719-modules-cli-and-json.sx imports both std.cli and
std.json under distinct namespaces and calls both parses; panics pre-fix,
passes after. issues/0100 marked RESOLVED.
2026-06-06 02:30:19 +03:00
agra
fb3fdaf454 Merge branch 'flow/distribution/lsp-corpus-sweep' 2026-06-05 23:57:55 +03:00
agra
503dfd8344 test(lsp): permanent corpus-sweep over the editor analyzer [dist B]
Adds `src/lsp/corpus_sweep.test.zig`: a permanent test that drives the
editor analyzer (`DocumentStore.analyzeDocument` — the exact path the
server's `textDocument/didOpen` uses) over EVERY `.sx` file in the
example + issue corpora, in process. The contract: analysis must
complete without panic/abort for any file. A panic aborts the test
binary — the loud CI signal that some new AST node shape crashes the
analyzer (the bug class issue 0099 fixed at sema.zig:397).

- Corpus dirs are injected as absolute paths at configure time
  (build.zig `corpus_paths` options module) so the sweep is
  CWD-independent; the FILE LIST is still read from disk at test time,
  so new examples are covered automatically with no test edit.
- Imports resolve against the shipped `library/` (root_path + stdlib
  path set), so the analyzer runs over real, fully-resolved code —
  maximum crash surface, mirroring an editor session opened on the repo.
- Wired into `zig build test` via the `src/root.zig` lsp barrel, same
  mechanism document.test.zig uses (refAllDecls reaches one struct deep,
  so the file is referenced directly).
- `SX_LSP_SWEEP_VERBOSE` prints each file before analysis; on a crash the
  last printed line names the offending file.

Coverage: 470 examples + 1 issue repro analyze with zero crashes.
Regression-guard proven: temporarily reverting A's sema.zig:397 fix
(`@intCast(ate.length.data.int_literal.value)`) makes the sweep abort
with `access of union field 'int_literal' while field 'identifier' is
active`; restoring it turns the sweep green.
2026-06-05 23:52:22 +03:00
agra
bef2c66be2 Merge branch 'flow/distribution/fix-0099-lsp' 2026-06-05 23:41:26 +03:00
agra
d515696e61 fix(lsp): identifier array dimension no longer panics the analyzer [0099]
`Analyzer.resolveTypeNode` read the array `.length` node's `.int_literal`
union field unconditionally. For a named-const dimension (`MAX :: 4;
[MAX]u8`) that node is an `identifier`, so the access tripped Zig's
checked-union panic and `sx lsp` aborted on didOpen. The main compiler
was unaffected (it folds the dim through the IR).

- New `arrayDimLength` helper switches on the dimension node tag:
  int_literal → value; identifier → a recorded module-const int value;
  anything else / out-of-u32-range → unknown. Never assumes a node shape.
- `Type.ArrayTypeInfo.length` is now `?u32`; null is an explicit "editor
  couldn't fold this dimension" marker (rendered `[_]T`), never a
  fabricated concrete length.
- New `const_int_values` registry records integer-literal consts at
  registration time for the identifier path.

Regression: first `src/lsp/*.test.zig` (the minimal LSP harness), wired
into the test graph via `src/root.zig`. Drives `analyzeDocument` over
`[MAX]u8` (folds to 4, no panic), `[64]u8` (happy-path guard), and
`[N]u8` (explicit unknown). Fail-before/pass-after verified.

Sibling audit of the resolveTypeNode/fieldType family: the array dim was
the only unchecked union-field access; all other arms recurse or
tag-check first. Noted a non-crashing display gap in server.zig hover
rendering for step B.
2026-06-05 23:33:22 +03:00
agra
8eb514a804 Merge branch 'flow/distribution/fix-0097' 2026-06-05 22:53:55 +03:00
agra
1151d77e96 fix(ir): value-failable return — narrow target per-form + inline path [0097 F1/F2]
Attempt-1 narrowed lowerReturn's target to failableSuccessType(ret_ty) for
every value-carrying failable. That fixed the bare-enum success slot but
introduced two defects (attempt-2 review):

F1 — explicit full failable tuple `return (.v, error.X)` panicked. With the
target narrowed to the value type, the trailing error element no longer
resolved against the error set, leaving an `.unresolved` tuple field that
tripped "unresolved type reached LLVM emission" in backend/llvm/types.zig.

F2 — a `-> (Enum, !E)` body with a comptime parameter is inlined
(lowerComptimeCall), so its success `return .red` took the inline-return path,
which the first cut skipped: it stored `{value, undef}` (error slot undef) into
the inline slot, so the success error slot read garbage at runtime.

Fix: choose the return-expr target via failableReturnTarget(ret_ty, value_node)
— a BARE value resolves against failableSuccessType (real enum ordinal), while
an EXPLICIT full failable tuple literal (arity == full-tuple field count) keeps
the full-tuple target and is forwarded as-is. This applies on the inline path
too, and the inline value-failable return now routes through
lowerFailableSuccessReturn (whose emitTupleRet stores `{value, 0}` into the
inline slot + branches), so the success error slot is 0 there as well.

Regression: examples/1056-errors-enum-value-failable-tuple-and-comptime.sx —
F1 explicit-tuple error return + bare-value success in one fn (no panic, slot 0
on success, tag 1 on error); F2 comptime-param enum value-failable read at
runtime on the success path (cast, bare if, == error.X) + error path. Reads the
slot at runtime so an undef is caught, not masked by the `if !e` proof.
examples/1055 + the original 0097 repro still pass. Gate: zig build 0,
zig build test 0, run_examples.sh 453 ok / 0 failed / 0 timed out.
2026-06-05 22:42:12 +03:00
agra
82366a93df fix(ir): value-failable returning an enum zeroes the success error slot [0097]
A `-> (Enum, !E)` `return .variant` lowered the enum literal with
`target_type` set to the full failable tuple `(Enum, !E)` instead of the
success value type. The bare literal resolves its tag against `target_type`;
against a tuple it matched no variant (silent tag 0) and was stamped with the
tuple type, so `lowerFailableSuccessReturn` saw `val_ty == ret_ty` and took the
forwarding branch — returning the half-built `{value, undef}` aggregate and
never appending the `0` error slot. Every runtime read of the slot on the
success path (`cast(s64) e`, bare `if e`, `e == error.X`) saw garbage nonzero;
only the compile-time `if !e` proof masked it. The s32 case was already correct
because integer literals don't resolve variants against `target_type`.

Fix: in lowerReturn, narrow `target_type` to `failableSuccessType(ret_ty)` for
a value-carrying failable before lowering the returned expression. The enum
literal then resolves to its real ordinal and is typed as the value type, so
the success path correctly appends `0`. Forwarding (`return call()` / explicit
`(v, e)`) is unaffected — those still yield a value typed equal to the tuple.

Regression: examples/1055-errors-enum-value-failable-error-slot.sx reads the
error slot at runtime on the success path (cast, bare if, == error.X), checks a
non-zero ordinal (.blue=2, also corrupted to 0 pre-fix), and asserts the error
path still carries the right tag + error_tag_name. Fails pre-fix, passes after.
2026-06-05 22:10:14 +03:00
agra
e466bd5ddf Merge branch 'dist-foundation' 2026-06-05 21:32:34 +03:00
agra
40e5ea1bfb Merge branch 'flow/sx-foundation/FINAL-rem' into dist-foundation 2026-06-05 21:19:24 +03:00
agra
035ef6c613 fix(examples): 0712 sha256 streaming writes scratch to .sx-tmp not /tmp [FINAL-rem F2]
Scratch file path violated the project rule 'scratch files go in
.sx-tmp/, never /tmp'. Route the temp .bin through the repo-local,
gitignored .sx-tmp/ dir, creating it at runtime via create_dir_all
(mirroring 0713) so the test is self-contained on a clean checkout.
Digest assertions and output are unchanged.
2026-06-05 21:18:08 +03:00
agra
60e5939fdf Merge branch 'flow/sx-foundation/F0.12' into dist-foundation 2026-06-05 20:55:00 +03:00
agra
b0d85a858c fix(ir): comptime print of an Any-held Type no longer silently stops [F0.12]
`any_to_string` runs `type := type_of(val)`; for an `.any` operand
`type_of` lowers to `struct_get(val, 0)` to read the Any's tag. At
runtime a first-class Type value is the aggregate `{ tag=.any, value=tid }`
so the read succeeds, but the comptime interpreter stores a Type as a bare
`.type_tag(tid)` and the comptime `struct_get` arm had no case for it — it
raised `CannotEvalComptime`, which `runComptimeSideEffects` swallowed into
`void_val`, truncating the `#run` while still building with exit 0.

- interp.zig: comptime `struct_get` handles a `.type_tag(tid)` base by
  mirroring the runtime Any-Type layout (field 0 -> `.any` tag, field 1 ->
  the type id), so `type_of` of an Any-held Type evaluates as it does at
  runtime and execution continues.
- emit_llvm.zig: `runComptimeSideEffects` no longer swallows a side-effect
  bail; it prints a loud diagnostic and sets `comptime_failed`
  (-> error.ComptimeError, non-zero exit), matching the const-init path.
  A truncated `#run` can no longer ship a successful build.

Regression: examples/0613-comptime-print-any-type.sx (all five lines print,
exit 0). Resolves issue 0096.
2026-06-05 20:48:49 +03:00
agra
794f2ef94a Merge branch 'flow/sx-foundation/F0.11' into dist-foundation 2026-06-05 20:07:52 +03:00
agra
95adc52609 fix(ir): const evaluators' field-access arm is raw value-shadow aware [F0.11]
A backtick raw value-shadow receiver (`` `f64 := … `` then `` `f64.epsilon ``,
`` `s8.max ``) was misclassified as the builtin numeric-limit accessor by the
shared compile-time evaluators. The sibling `isFloatValuedExpr` already guards
this with an `is_raw` check, but `evalConstFloatExpr` / `evalConstIntExpr` did
not — so once a raw value-shadow's field read flowed into the unified float→int
narrowing rule or an array-dim count, the float folder returned the BUILTIN
`f64.epsilon` (2.22e-16) and wrongly errored, and the integer folder turned
`` `s8.max `` into the builtin `127` (a fabricated 127-element array).

Both evaluators' field-access arms now mirror `isFloatValuedExpr`'s `is_raw`
guard: a raw receiver yields `obj_name = null`, so it is never a
numeric-limit/pack leaf and falls through to the ordinary runtime field read. A
raw value-shadow is a mutable-local field (an observable later reassignment),
so it is genuinely runtime and must not be const-folded — it now behaves exactly
like a plainly-named field read: `` `f64.epsilon `` narrowing into `s64`
truncates its field value (11.5 → 11, identical to `b.epsilon`), and `` `s8.max ``
as an array dimension is rejected as a non-constant count (identical to `b.max`).
The bare builtin path is unchanged.

Regression (issue 0095 / F0.11-7):
- examples/0169-types-value-shadow-field-narrowing.sx (positive — raw float-field
  read narrows/truncates, mutation proves runtime, bare limit still folds)
- examples/1148-diagnostics-value-shadow-field-dim-not-const.sx (negative — raw
  int-field dim rejected as non-const)
- program_index.test.zig "a backtick raw-shadow receiver is a field read, not a
  numeric-limit fold (F0.11-7)"

specs.md + readme.md note the value-shadow rule extends into the narrowing/count
contexts.
2026-06-05 20:02:11 +03:00
agra
e442cdf5e7 fix(ir): float / folds as FLOAT division under the unified narrowing rule — int folder refuses a float-operand / [F0.11]
The shared compile-time integer folder (`evalConstIntExpr`) accepts an
integral float literal/const as an integer leaf (`[4.0]` → 4) and then
applied INTEGER arithmetic to the whole expression — so `5.0 / 2.0` folded
as `divTrunc(5,2)` = 2 instead of float division (`2.5`). The bug fired at
all FIVE unified-rule sites (typed local, field default, param default,
typed const, array dimension), because the typed sites evaluate through
`evalConstFloatExpr` (which delegates the node to the int folder) and the
count sites through `foldCountI64` (int folder first).

Fix at the single root: `evalConstIntExpr`'s `.div` arm refuses to fold a
division whose lhs/rhs is float-valued (`isFloatValuedExpr`), so the value
surfaces through `evalConstFloatExpr` + the unified rule — an integral
quotient (`6.0 / 2.0` → 3) folds, a non-integral one (`5.0 / 2.0` = 2.5,
mixed `5 / 2.0`, float-const `F / G`) errors. Genuine integer `/` (`5 / 2`
→ 2) is unchanged; `*`/`+`/`-` need no guard (they agree between int and
float for the integral operands the int folder ever sees).

`isFloatValuedExpr` judges a const-leaf by VALUE (`moduleConstIsFloatTyped`
recurses into the const's value with the existing cycle-guard frame), so an
untyped float-EXPRESSION const (`ME :: 4.0 + 1.0`, placeholder type s64) is
caught at both the count path and — via `foldComptimeFloatInit`'s guard —
the typed-binding path. A backtick RAW receiver (`` `f64.epsilon ``) is a
field read, not a float limit (is_raw check, issues 0092/0093).

Regression: examples/1147 (negative — `5.0 / 2.0` errors at all five sites
plus untyped float-EXPR const div); 0168 extended (positive — `6.0 / 2.0`,
`12.0 / 4.0`, `[6.0/2.0]`, `xx (5.0/2.0)` → 2); unit tests "the int folder
refuses a FLOAT division" and "moduleConstIsFloatTyped judges a const by
VALUE". specs.md + readme.md state the float-`/` rule.
2026-06-05 19:26:22 +03:00
agra
74f675ac0b fix(ir): evalConstFloatExpr reaches parity with evalConstIntExpr — numeric-limit float leaves + float % fold under the unified rule [F0.11]
The compile-time float evaluator lagged the integer one: it had no
numeric-limit field-access arm, so `y : s64 = f64.true_min + 0.5` (=0.5)
silently truncated to 0 even though the direct `f64.true_min` already
errored; the arm-by-arm audit also found a missing `%` arm, so
`y : s64 = 5.5 % 2.0` (=1.5) silently truncated to 1.

Bring evalConstFloatExpr to PARITY with evalConstIntExpr:
- Add a `.field_access` arm resolving a builtin FLOAT numeric-limit
  accessor (`f64.max`, `f32.epsilon`, `f64.true_min`, …) via the SAME
  `type_resolver.floatLimitFor` that `lowerNumericLimit` uses — the float
  twin of the int evaluator's `integerLimitFor` arm.
- Add a `.mod` arm via `@rem` (matching evalConstIntExpr and codegen's
  `frem`): `6.0 % 4.0` folds to 2 (via int delegation), `5.5 % 2.0` = 1.5
  is rejected.

The two evaluators now share every leaf/operator shape, so no
compile-time-const float form escapes the unified float→int rule at one
site while folding at another. All five sites (local/field/param/const/
array-dim) stay consistent.

Regression: 0168 (positive) adds `f64.max - f64.max` → 0, `6.0 % 4.0` → 2,
integer-limit `s8.max`/`[u8.max]` unregressed, `xx` escapes for both new
forms; 1146 (negative) adds `f64.true_min + 0.5` and `5.5 % 2.0` erroring
at a binding site; program_index.test.zig covers the floatLimitFor arm and
the `%` arm. specs.md + readme.md state the parity. issues/0095 RESOLVED
banner gains the attempt-5 entry.
2026-06-05 18:15:17 +03:00
agra
b73363ca4c fix(ir): array-dim/count path joins the unified float→int rule — all 5 sites consistent [F0.11]
The compile-time count fold (array dimension / Vector lane / value-param) was
integer-only: it folded a DIRECT integral float literal (`[4.0]`, `[N]` with
`N : f64 : 4.0`) but rejected an INTEGRAL expression built from a non-integral
float-const leaf (`[F + 1.5]` = 4.0, `F : f64 : 2.5`) — and a const folded from
one (`[K]` with `K : s64 : F + 1.5`) — as "must be a compile-time integer
constant". This was the last of issue 0095's five narrowing sites (local /
field / param / const / array-dim) still diverging.

Route the count fold through the SAME compile-time float evaluation the other
four sites use:

- New `program_index.foldCountI64` — the single int-or-integral-float count
  fold: `evalConstIntExpr` first, then (only on failure) `evalConstFloatExpr` +
  `floatToIntExact`. `foldDimU32` (dim/lane/u32 value-param), the non-u32
  value-param gate, and `emitModuleConst`'s integer-const materialization all
  delegate to it, so a const's emitted value and its use as a count come from
  one fold (no parallel integral check, no two-resolver divergence — issue 0083).
- New `DimU32.non_integral_float` variant carries a non-integral float dim to a
  distinct, accurate diagnostic ("array dimension must be an integer, but '2.75'
  is a non-integral float") — the cast-escape advice the binding sites give does
  not apply in a count position, so the dim wording omits it. `reportDimError`,
  the Vector-lane resolver, and the top-level array-alias diagnostic all handle
  the new variant, so the DIRECT and type-ALIAS forms emit the identical message.
- `type_bridge.StatelessInner.lookupFloatName` (via `moduleConstFloat`) is the
  float twin of its `lookupDimName`, so the registration-time alias path folds a
  float-const-leaf dimension to the SAME count as the stateful direct path.

`inline for` range bounds are spec endpoints, not counts (specs.md §2), so they
keep the int-only fold deliberately (no silent-truncation bug there).

Relaxes the F0.4 `examples/1132` wording: a non-integral float const dim now
reports the precise "non-integral float" message (it still errors).

Regression: 0168 (positive — `[F + 1.5]s64`, `[KF]s64`, alias `ArrFE` all fold
to len 4), 1146 (negative — `[F + 0.25]s64` errors), 1132 (precise wording), and
a `foldCountI64`/`foldDimU32` unit test. issues/0095 marked RESOLVED (attempt 4).
specs.md + readme.md state the unified rule across all five sites.
2026-06-05 17:43:45 +03:00
agra
b6d66d9c56 fix(ir): complete const-float evaluator — resolve float-const leaves too [F0.11]
Completes issue 0095: a non-integral float→int narrowing via a FLOAT-const
leaf (`F : f64 : 2.5; y : s64 = F + 0.25` = 2.75) silently truncated to 2.
`evalConstFloatExpr` delegated only INTEGER leaves to `evalConstIntExpr` and
had no float-const leaf arm, so the unified rule never saw the value.

- program_index.zig: add `moduleConstFloat`/`moduleConstFloatFramed` — the f64
  twin of `moduleConstInt` (same `isCountableConstType` gate, same cyclic-
  definition frame), recovering a numeric module const's value via
  `evalConstFloatExpr`. Add `lookupFloatName` to `ModuleConstCtx` and the
  `.identifier`/`.type_expr` leaf arms to `evalConstFloatExpr` that call it.
  Integer / integral-float leaves keep resolving through the existing
  `evalConstIntExpr` delegation, so the unified rule now applies to ANY
  compile-time-constant float expression — literal, int-const leaf, float-const
  leaf, and combinations — at every binding site.
- lower.zig: add `Lowering.lookupFloatName` delegating to `moduleConstFloat`.
  Route `typedConstInitFits`' integral-fold check through `evalConstFloatExpr` +
  `floatToIntExact` (the SAME facility `foldComptimeFloatInit` uses) instead of
  the int-only `evalComptimeInt`, which folded leaf-by-leaf in i64 and so
  rejected an integral SUM built from a non-integral float leaf
  (`K : s64 : F + 1.5` = 4.0 now folds; `K : s64 : F + 0.25` errors).

A LOCAL `::` const leaf is a scope ref (not in the const tables) so neither
the int nor float evaluator folds it — float now matches int exactly there.

Regression: examples/1146 (negative) + 0168 (positive) extended with
float-const-leaf cases at local/field/param/const; unit test in
program_index.test.zig covers the leaf resolution (F→2.5, F+0.25→2.75,
F+1.5→4.0). specs.md + readme.md state the rule covers any compile-time-const
float expression incl. float-typed const leaves. issues/0095 banner updated.

Gate: zig build + zig build test green; 447 examples pass, 0 failed.
2026-06-05 17:00:12 +03:00
agra
43d44fff75 fix(ir): narrow non-integral const-float EXPRESSIONS at typed local/field/param; align const message [F0.11]
Completes issue 0095 (attempt 2). The attempt-1 coerce arm only caught a direct
`const_float` literal, so a non-integral const-folded float EXPRESSION still
truncated silently at a typed local / field default / param default:

  M :: 2;
  local : s64 = M + 0.5;   // → 2  (silent truncation — BUG; now ERRORS)
  fld   : s64 = M + 0.5;   // field default — same
  take(x : s64 = M + 0.5)  // param default — same

while the typed-CONST site already errored. The integral expression
(`M + 2.0` → 4) folded but the runtime/explicit-cast paths must stay untouched.

Fix:
- New `program_index.evalConstFloatExpr` — the f64 counterpart to
  `evalConstIntExpr`, delegating every integer subtree back to it (no parallel
  integer logic) and adding only the float literal / unary-negate / `+ - * /`
  arms. Pure (no diagnostics, no resolution side effects).
- `Lowering.foldComptimeFloatInit` applies the unified rule to a typed-binding
  initializer EXPRESSION: an integral comptime float folds to its `constInt`, a
  non-integral one errors, a genuine runtime float / `xx`-cast falls through to
  the normal path. It runs `evalConstFloatExpr` FIRST (pure) so a `$pack[i]`
  argument is never spuriously type-resolved outside an active binding, then
  gates on `isFloat(inferExprType)` so a plain comptime int is left alone.
  Wired into the typed-local path, the three struct field-default sites (via a
  shared `lowerCoercedDefault`), and the call-argument loop (covers expanded
  param defaults).
- One `Lowering.diagNonIntegralNarrow` now emits the narrowing wording at all
  five sites (coerce arm, global init, const-expr value, the typed-binding
  sites, and the typed-const path). The typed-CONST non-integral diagnostic
  therefore reads "cannot implicitly narrow non-integral float …" instead of
  the stale "initializer is a float literal / floating-point expression".

Tests: examples/1146 (negative) extended with non-integral const-EXPRESSION
cases at local/field/param; examples/0168 (positive) extended with integral
const-EXPRESSION folds and `xx (M + 0.5)` truncation; examples/1143 reconciled
to the aligned const message (G/BAD/BAD2 stay errors); unit test
`evalConstFloatExpr folds comptime float expressions`. Full gate green (447).
2026-06-05 16:28:12 +03:00
agra
4c12e1de38 fix(ir): unify float→int narrowing — integral folds, non-integral errors [F0.11]
Issue 0095: a typed local/param/field silently TRUNCATED a float initializer
to an integer annotation (`y : s64 = 1.5` → 1) with no diagnostic. Agra ruled
the UNIFIED rule (Option B): an implicit float→int in a typed binding behaves
like the array-dimension rule —

  - an INTEGRAL compile-time float FOLDS to its int (`4.0` → 4, `-2.0` → -2);
  - a NON-integral float is a COMPILE ERROR (`1.5`, `4.5`);
  - explicit `xx` / `cast(T)` ALWAYS truncates (the escape hatch).

Applied consistently to typed local / param-default / field-default, typed
module CONST, and array dim — all reusing the single
`program_index.floatToIntExact` / `evalConstIntExpr` facility (no second
integral check).

- `Builder.constFloatInfo` reads a compile-time `const_float` back from its
  Ref (value + span).
- `coerceToType` is now the IMPLICIT path: its `.float_to_int` arm folds an
  integral const-float to `constInt`, else emits the narrowing diagnostic.
  `coerceExplicit` is the raw truncating path; `xx` (lowerXX) and `cast(T)`
  route through it so the escape still truncates.
- Field-default lowering (struct-literal pad, named-field default,
  buildDefaultValue) now coerces the default to the field type at the IR level
  (was silently bit-coerced by emitStructInit).
- Const path: `typedConstInitFits` accepts an integral float (literal or a
  `M + 2.0`-style expression folding via `evalComptimeInt`); `emitModuleConst`
  / `constExprValue` / `globalInitValue` fold an integral float to its int and
  reject a non-integral one — relaxing F0.7's blanket float rejection.

Tests: examples/0168 (positive: local/field/param/const fold, xx/cast
truncate), examples/1146 (negative: local/param/field error), integral-float
const cases added to examples/0162; non-integral const cases in 1143 stay
errors. specs.md + readme.md document the unified rule, cross-referencing the
array-dim rule. issues/0095 marked RESOLVED.
2026-06-05 15:34:33 +03:00
agra
341b62c197 Merge branch 'flow/sx-foundation/F0.10' into dist-foundation 2026-06-05 14:49:54 +03:00
agra
ed7665f8ae fix(ir): single-assign field store delegates to fieldLvaluePtr, completing the lvalue consolidation [F0.10]
Migrate lowerAssignment's `.field_access` target onto the shared
`fieldLvaluePtr` resolver, deleting its duplicated union / promoted /
tuple / vector / struct walk. All three lvalue field-store sites —
single-assign, address-of (lowerExprAsPtr), and multi-assign
(lowerMultiAssign) — now resolve through the one resolver, removing the
issue-0083 two-resolver divergence.

Fold vector-lane resolution into `fieldLvaluePtr` (reusing
vectorLaneIndex) so the single resolver covers struct fields, union
direct fields, promoted anonymous-struct union members, tuple elements,
and vector lanes — null only on a genuine miss, which every caller turns
into the read path's `emitFieldError` diagnostic.

`fieldLvaluePtr` now types every field GEP `*field_ty` (the convention
the single-assign path always used), not the bare field value type:
emitStore unwraps one pointer level to find the stored value's type.
The earlier lowerExprAsPtr / lowerMultiAssign walks typed the GEP with
the bare field type, so a field whose own type is a pointer-to-aggregate
(`*Pair`, a two-pointer struct) made emitStore unwrap to the aggregate
and coerceArg's closure auto-promotion store a 16-byte `{ptr,null}`
struct over the 8-byte slot, clobbering the neighbouring field.
Consolidating onto the one `*field_ty` resolver preserves single-assign
and fixes that pre-existing multi-assign / address-of clobber.

The types.zig `.unresolved` tripwire is untouched; no `.s64` / `.void` /
`.unresolved` default remains.

Regression: examples/0167-types-ptr-to-aggregate-field-store.sx (a
`*Pair` field stored via all three lvalue sites leaves the neighbour
intact) + a lowering unit test asserting the `*field_ty` GEP convention.
2026-06-05 14:40:06 +03:00
agra
c98bebc4e3 fix(ir): missing-field multi-assign + promoted-union-member lvalue [F0.10]
Completes the issue-0094 fix. attempt-1 made single-assign and address-of
diagnose a missing struct field; the stress-review found two remaining defects
in that change:

1. lowerMultiAssign's `.field_access` target kept the pre-fix shape — a
   struct-only loop that defaulted `field_idx 0` / `field_ty .unresolved` on a
   miss, then built the GEP and stored unconditionally. A missing field
   (`p.q, y = 2, 3`) silently wrote field 0 (printed `x=2 y=3`, no diagnostic),
   and a valid promoted-union / tuple member at a non-zero offset corrupted
   field 0 instead of its own slot.

2. attempt-1's new union branch in lowerExprAsPtr resolved only DIRECT union
   field names, so `@v.x` on a promoted anonymous-struct member reported
   "field 'x' not found on type 'Vec2'" even though `v.x = 41` worked.

Both lvalue-pointer sites and the multi-assign store now route through one
shared resolver, `fieldLvaluePtr`, that handles struct fields, union direct
fields, promoted anonymous-struct union members, and tuple elements, and
returns null (no field-0 / `.unresolved` default) on a genuine miss. Each
caller emits the read path's `emitFieldError` on null. This collapses the
three previously-divergent field-lvalue walks into one, fixing the
multi-assign missing-field corruption, the promoted-member over-rejection,
and (as a side effect of correct resolution) non-zero-offset promoted-union
and tuple multi-assign stores. The types.zig tripwire is untouched.

Regression tests:
- examples/1145 extended: multi-assign missing field (`p.r, y`) errors, exit 1.
- examples/0166 (new): promoted union member written and address-of'd,
  including a non-zero-offset member (`@v.y`), compiles and runs.
- src/ir/lower.test.zig: multi-assign missing-field field-not-found unit test.
2026-06-05 14:00:24 +03:00
agra
e13518e8aa fix(ir): missing struct field assignment errors cleanly, no LLVM panic [F0.10]
Assigning to a nonexistent struct field (`p.q = 2` where Point has no `q`)
aborted the compiler with the `.unresolved` LLVM tripwire instead of a source
diagnostic (issue 0094). The lvalue field lookup never diagnosed a miss:

- `lowerAssignment`'s `.field_access` target left `field_ty = .unresolved` when
  no struct field matched, then built `ptrTo(field_ty)` and stored — so a
  pointer-to-`.unresolved` reached LLVM emission and tripped the panic.
- `lowerExprAsPtr`'s `.field_access` fallback returned
  `structGepTyped(obj_ptr, 0, .s64, obj_ty)` on a miss — a silent field-0/`.s64`
  default that mislowered the lvalue.

Both sites now reuse the read path's `emitFieldError` (the exact facility
`lowerFieldAccessOnType` uses), so read and write reject identically with
`field 'q' not found on type 'Point'`. `lowerExprAsPtr` also resolves
union/tagged-union fields via `union_gep` (the old `.s64` fallback was silently
standing in for union field access — e.g. `u.a[0] = v`), so that path is fixed,
not just made loud. The `types.zig` tripwire is untouched: the fix is to never
produce `.unresolved` for a missing-field store.

Regression tests:
- examples/1145-diagnostics-missing-struct-field-assign.sx — negative, both
  sites error, exit 1.
- examples/0165-types-nested-struct-field-assign.sx — positive, nested struct
  field write + address-of a matched field still work.
- src/ir/lower.test.zig — lowering unit test asserting the field-not-found
  diagnostic for a missing-field assignment.
2026-06-05 13:24:15 +03:00
agra
22c2e60efc Merge branch 'flow/sx-foundation/F0.8' into dist-foundation 2026-06-05 12:58:44 +03:00
agra
136484f72f docs(specs,readme): narrow runtime Type-value support to type_name/type_is_unsigned [F0.8]
The 7 type-only builtins doc claimed all of them accept a runtime Type
value, but only type_name and type_is_unsigned do. The other five
(size_of, align_of, field_count, type_eq, is_flags) are compile-time-only
— a runtime Type value (type_of(x)) yields 'unresolved type' since
runtime reflection is deferred. Reword both docs to the accurate scope.

Verified: type_name(type_of(x))=u32, type_is_unsigned(type_of(x))=true;
size_of(type_of(x)) / align_of(type_of(x)) -> error: unresolved type.
2026-06-05 12:31:21 +03:00
agra
7d566d2d07 docs(specs): sync case int: formatting example with signedness-aware std.sx [F0.8]
The Type Category Matching example showed the old signed-only arm
(case int: result = int_to_string(xx val);), which would reproduce the
pre-fix unsigned mis-rendering (u64 -> -1) if followed. Update it to mirror
library/modules/std.sx:370 — branch on type_is_unsigned(type) so unsigned
types route to uint_to_string, with a one-line clause explaining the split.
2026-06-05 12:22:36 +03:00
agra
5f64ee4426 fix(ir): reflection builtins on an Any read its runtime tag, not payload [F0.8]
`type_name` / `type_is_unsigned` on an `Any` argument unconditionally read
the Any's payload as a TypeId index. That is correct only when the Any holds
a Type value (`{ .any, tid }`); for an Any holding a runtime *value*
(`av : Any = 6`, tag s64, payload 6) it returned `types[6]` — `type_name(av)`
gave "u8" and `type_is_unsigned(av)` gave true.

Both backends now branch on the Any's runtime type-tag: tag == `.any` → the
box is a Type value, use the payload as the TypeId; otherwise the tag IS the
held value's type. So `type_name(av)` → "s64", `type_is_unsigned(av)` → false,
while `type_name(type_of(x))` still names the held type. The `{}` formatter is
unchanged (it already passed `type_of(val)`, a proper Type value).

- src/ir/interp.zig: shared `Value.reflectTypeId` tag-branching resolver; the
  `type_name` / `type_is_unsigned` interp arms route through it.
- src/backend/llvm/ops.zig: shared `Ops.reflectArgTypeId` emits
  extractvalue-tag / icmp-eq-.any / select for the runtime path; both
  reflection arms route through it. The two backends agree.
- examples/0164-types-reflection-any-tag.sx: regression pinning type_name /
  type_is_unsigned / print on an Any holding a value vs a Type.
- src/ir/interp.test.zig: unit test for `reflectTypeId`.
- 22 .ir snapshots: the new select appears in every std-importing program's
  IR (any_to_string embeds these builtins) — benign, verified structurally
  identical apart from the three new instructions.
- issues/0090, specs.md: documented the Any-tag rule.
2026-06-05 12:09:52 +03:00
agra
b053c64149 fix(ir): reject non-type args to the 7 type-introspection builtins [F0.8]
size_of, align_of, field_count, type_name, type_eq, type_is_unsigned,
and is_flags silently reinterpreted a value argument as a type:
type_is_unsigned(6) read 6 as a TypeId index (types[6] = u8 -> true),
size_of(6)/size_of(true) sized its typeof (8), type_name(6) returned
types[6]'s name. Per Agra's ruling, all 7 now strictly require a type
(compile-time): a value argument is a compile error.

One shared guard (Lowering.reflectionTypeArgGuard, run at the top of
tryLowerReflectionCall) classifies each arg via reflectionArgIsType: a
spelled / compile-time type or generic type parameter (isStaticTypeArg),
or a runtime Type value (static type .any -- type_of(x), a []Type
element list[i], a Type-typed local/field/param) is accepted; anything
else is rejected with "<builtin> expects a type, got '<type>'". The
runtime path for type_name / type_is_unsigned is preserved (the {}
formatter calls type_is_unsigned(type_of(val)) at runtime). The 5
comptime-only builtins stay comptime-only (runtime reflection deferred).

Regression: examples/1144-diagnostics-reflection-builtin-needs-type.sx
(reject cases across all 7, exit 1). Unit test: reflectionArgIsType in
lower.test.zig. specs.md / readme.md document the strict type
requirement (and add the previously-undocumented align_of, type_eq,
type_is_unsigned). issues/0090 RESOLVED banner updated.
2026-06-05 11:22:59 +03:00
agra
64f77e9779 fix(std): render integer formatter extremes — i64::MIN and unsigned all-ones [F0.8]
Resolves issue 0090. The `{}` integer formatter mis-rendered both ends of
the 64-bit range:

- `int_to_string` computed the magnitude as `0 - n`, which overflows for
  `s64::MIN` (its magnitude is unrepresentable as a positive s64) — the
  value stayed negative, the digit loop ran zero times, so only `-`
  printed. It now extracts digits straight from `n` (per-digit
  `|n % 10|`, `n` truncating toward zero), never negating MIN.

- `any_to_string`'s `case int:` formatted every integer as s64, so a u64
  all-ones value printed as `-1`. There was no `uint` type-category to
  distinguish signedness. Added an additive `type_is_unsigned(T)`
  reflection builtin (static fold + dynamic interp/LLVM paths, mirroring
  `type_name`), backed by the new `TypeTable.isUnsignedInt` predicate, and
  a `uint_to_string` formatter (unsigned decimal via long-division over
  four 16-bit limbs). `case int:` routes through `type_is_unsigned(type)`.

The 16-bit-limb split is factored into a shared `decompose_u16x4`, now
reused by `int_to_hex_string` (no second unsigned-math routine).

Regression: examples/0046-basic-int-formatter-extremes pins both extremes
plus a width spread; unit tests cover `isUnsignedInt`. Docs (specs.md
representation note, readme std API) updated for unsigned/extreme `{}`
behavior. IR snapshots refreshed for the two new std functions.
2026-06-05 09:05:37 +03:00
agra
5ef74a15f3 Merge branch 'flow/sx-foundation/F0.7' into dist-foundation 2026-06-05 08:32:10 +03:00
agra
b69ec43ba3 fix(ir): infer mixed int+float arithmetic as the promoted float [F0.7]
`ExprTyper.inferType`'s binary-op arm inferred every non-comparison op
from the LHS alone, so `M + 0.5` (s64 + f64) statically typed as s64
while `0.5 + M` typed as f64 — operand-order-dependent. The value path
(`lowerBinaryOp`) already promoted int×float → float, so static
inference disagreed with the value: `M + 0.5` formatted as a truncated
int and a typed const `BAD : s64 : M + 0.5` was accepted+truncated
(issue 0088 mixed-numeric escape).

Extract the value path's inline promotion into a shared
`Lowering.arithResultType(lhs, rhs)` and reuse it at both sites, so
arithmetic / bitwise / shift inference reports exactly the type the
lowered value carries — int LHS × float RHS → the float, order-
independent. The value-path behavior is unchanged (the block is moved
verbatim into the helper), so no IR shifts; the suite stays green. The
typed-const validation reuses `inferExprType`, so this auto-closes the
escape with no change to the validation logic.

- examples/1143: BAD/BAD2 (`s64 : M + 0.5`, `s64 : 0.5 + M`) rejected
  in both operand orders.
- examples/0162: MF/MFR (`f64 : M + 0.5`, `f64 : 0.5 + M`) fold to 2.5.
- examples/0163 (new): pins the inference fix in a value context
  (`print("{}", n + 0.5)` formats the float, both orders, +-*/, f32).
- expr_typer.test.zig: arithResultType + mixed-arithmetic inference.
- specs.md / readme.md: document the numeric-promotion rule.
- issues/0088: RESOLVED banner notes the inferExprType root fix.
2026-06-05 08:23:59 +03:00
agra
454ea06bd4 fix(ir): validate const-expression typed module-const initializers [F0.7]
Attempt 1 rejected only LITERAL initializers that mismatch a typed module
const's annotation; a const-EXPRESSION initializer escaped, so the same
issue-0088 root remained for `M :: 2; N : string : M + 2` — accepted at exit 0,
folding `[N]s64` to 4 and printing N as an integer.

Root cause: `registerTypedModuleConst` validated only the enumerated literal
node kinds; any other kind fell through to `else => {}`, and pass 0
pre-registers binary_op/unary_op consts as a `.s64` placeholder that was never
reconciled with the annotation.

Fix — validate by TYPE, not by node kind:
- lower.zig: `registerTypedModuleConst` now covers literals AND const-expressions
  (binary_op/unary_op) through one path. `typedConstInitFits` keeps the literal
  arms and routes any non-literal through the new `constExprInitFits`, which
  compares the initializer's INFERRED type (`inferExprType`, the existing
  type-inference facility — no second const evaluator) to the annotation with the
  same integer/float compatibility. A mismatch emits the `type mismatch` diagnostic
  (a const-expression is described by its inferred type, e.g. "an integer
  expression") and evicts the pass-0 placeholder; a match registers the const at
  its resolved annotation type (the same `put` the literal path always did), so a
  const-expression folds and emits at its declared type.
- `literalKindName` → `initializerDescription` (+ `constExprDescription`) so the
  message is accurate for both a literal and a const-expression initializer.

Regression:
- examples/1143: extended with `E : string : M + 2` and `V : string : -M`
  (const-expr mismatches → exit 1, pinned diagnostics).
- examples/0162: extended with `KE : s64 : M + 2` (used as a count + printed) and
  `WE : f32 : M + 2` (over-rejection guard — valid const-exprs still work).
- program_index.test.zig: count-gate test extended with a binary_op value node
  declared `string` (must not fold as a count).

Docs: specs.md §3 + readme.md generalized from "initializer literal" to cover
constant expressions; issues/0088 RESOLVED banner updated.
2026-06-05 07:51:16 +03:00
agra
156edf8e28 fix(ir): reject typed module const whose initializer mismatches annotation [F0.7]
A typed module-level constant whose initializer did not match its
annotation was silently accepted: `N : string : 4` compiled, then
`print(N)` segfaulted (an integer emitted as a `string` const → a bogus
pointer) and `[N]s64` folded `N` to 4 as an integer count. Issue 0088.

Root cause: `registerTypedModuleConst` stored the annotation type but never
validated the initializer literal against it, and
`program_index.moduleConstInt` folded a const into a count by inspecting
the initializer node alone, ignoring `ModuleConstInfo.ty`.

Fix at the declaration (kills both symptoms):
- lower.zig: `registerTypedModuleConst` now validates the initializer via
  `typedConstInitFits` (arms mirror `emitModuleConst`'s faithful-emit
  precondition: int→int/float, float→float, bool→bool, string→string,
  null→pointer/optional, `---`→any). A mismatch emits a `type mismatch`
  diagnostic at the initializer span and does not register the const (also
  evicting the pass-0 placeholder). Not routed through
  `coercionResolver().classify`: that runtime-coercion planner is unsound
  here (null's natural type is void → false-rejects `*T`; bool is 1 bit →
  false-accepts s64).
- program_index.zig: `moduleConstInt` now takes the `TypeTable` and gates
  the fold on `isCountableConstType(ci.ty)` (integer of any width, or a
  float), so a non-numeric typed const can never fold into a count off its
  initializer node. Callers in lower.zig and type_bridge.zig updated.

Regression:
- examples/1143-diagnostics-typed-module-const-mismatch.sx (negative, exit 1)
- examples/0162-types-typed-module-const-roundtrip.sx (positive)
- program_index.test.zig: gate-on-declared-type unit test

Docs: specs.md §3 Constant Binding + readme.md note the compatibility rule.
2026-06-05 07:17:20 +03:00
agra
3edb60762d Merge branch 'flow/sx-foundation/F0.5' into dist-foundation 2026-06-05 06:53:22 +03:00
agra
a7ee179577 fix(ir): vector lane store resolves lane element type [F0.5]
Writing a Vector lane (`v.x = …`, `.y/.z/.w` + colour aliases) panicked
with "unresolved type reached LLVM emission". The store path had no
vector branch: a `.field_access` target on a Vector fell through to
struct-field lookup, matched nothing, left `field_ty = .unresolved`, and
built a `ptrTo(.unresolved)` that tripped the LLVM emission guard. The
read path resolved the lane fine — the two had diverged (issue-0083
two-resolver class).

Extract a shared `Lowering.vectorLaneIndex` resolver and route BOTH paths
through it. The read path (`lowerFieldAccessOnType`) delegates to it,
dropping its silent `else 0` fallback. A new vector branch in
`lowerAssignment` GEPs a typed pointer to the lane (`structGepTyped`) and
stores via `storeOrCompound` (plain + compound). `emitStructGep` now
addresses a vector base type with a `[0, lane]` GEP. A non-lane field now
reports field-not-found on both paths instead of silent-lane-0 / panic.

Regression: examples/1506-vectors-lane-store.sx (panicked pre-fix, now
reads back written values) + a vectorLaneIndex unit test. Resolves issue
0086; spec documents element assignment.
2026-06-05 01:32:35 +03:00
agra
3f279dea6b Merge branch 'flow/sx-foundation/F3.3' into dist-foundation 2026-06-05 01:11:26 +03:00
agra
0fc7a72cbc feat(lang): std.cli exit-code + --json contract helpers [F3.3]
Foundation milestone close — the minimal exit-code / --json contract
`dist` relies on, in pure sx (no compiler change).

- EX_OK (0) / EX_USAGE (64, sysexits.h) / EX_UNAVAILABLE (70) named
  constants in std.cli.
- exit_ok() / exit_usage() terminators routing through the canonical
  process.exit(code: u8) — removes the hand-rolled cli_bail_exit `_exit`
  binding; the unsupported-platform path now uses proc.exit(EX_UNAVAILABLE).
- --json read is parsed.json (already parsed by F3.2); documented as the
  detection point with a stdout-pure / stderr-human convention.
- examples/0718-modules-cli-exit-json.sx exercises the contract: json true
  with --json / false without, EX_USAGE == 64, and a usage path that exits
  64 via exit_usage() (expected .exit = 64).
- readme.md gains a std.cli command-line-interface subsection.
2026-06-05 01:01:25 +03:00
agra
9db26e9780 Merge branch 'flow/sx-foundation/NL.2' into dist-foundation 2026-06-05 00:45:38 +03:00
agra
bdf6433e72 test(lang): pin f64.min via signed union bit view in example 0159 [NL.2] 2026-06-05 00:35:52 +03:00
agra
6478ccbe3c fix(lang): numeric-limit shadow guard covers all 3 value sources [NL.2]
The issue-0092 fix guarded the numeric-limit accessor intercept against
raw value shadowing using only lexical Scope.lookup. The ordinary
identifier field-access path resolves a value through THREE sources
(scope / program_index.global_names / program_index.module_const_map),
so a backtick raw identifier bound at module scope — a global
`` `f64 := Box.{…} `` or a module constant `` `f64 :: Box.{…} `` — still
folded `` `f64.epsilon `` to the numeric limit instead of reading the
value's field (issue 0093, plus the module-const variant: same root
cause, same fix).

Fix: a single shared helper Lowering.identifierBindsValue(name) that
returns true when the name resolves through scope OR global_names OR
module_const_map. Used in BOTH lowerNumericLimit (lower.zig) and the
numeric-limit inference arm (expr_typer.zig) so the two resolvers can't
desync (issue-0083 class). A bare `f64.epsilon` / `s32.max` (a
.type_expr receiver) still folds even when a raw value of the same
spelling is bound — the bare receiver is never value-shadowed.

- examples/0161: extended to exercise all three binding kinds — a
  GLOBAL `` `f32 ``, a MODULE-CONST `` `s16 ``, and LOCAL
  `` `f64 ``/`` `s32 ``/`` `u8 `` — each reading its field while the
  bare spelling still folds.
- src/ir/expr_typer.test.zig: unit test pinning the global +
  module-const sources of the shared guard.
- issues/0093: RESOLVED banner (3-source root cause + fix, module-const
  variant folded in).
- specs.md / readme.md: numeric-limit shadow note now source-agnostic
  (local / global / module-const).
2026-06-05 00:21:32 +03:00
agra
b0cc22a8c0 fix(lang): numeric-limit intercept no longer shadows raw value bindings [NL.2]
The numeric-limit accessor intercept (NL.1 integer `.min`/`.max`, NL.2 float
`.epsilon`/`.min_positive`/`.true_min`/`.inf`/`.nan`) treated ANY receiver
whose text matched a builtin numeric type name as a TYPE receiver, without
first checking for an in-scope VALUE binding. An F0.6 backtick raw identifier
(`` `f64 := … ``) binds a local under the stripped name `f64`; field access on
it (`` `f64.epsilon ``) parses as an `.identifier` receiver, which the intercept
silently folded to the type's numeric limit — a silent-wrong-value bug
(issue 0092).

Fix: for `.identifier` receivers, prefer an in-scope value binding
(`Scope.lookup`) over the fold — defer to ordinary field lowering when the
identifier resolves to a value. `.type_expr` receivers are unambiguous types
and are never shadowed, so a bare `f64.epsilon`/`s32.max` still folds even in a
scope where `` `f64 `` is bound (the parser classifies a bare builtin name as a
`.type_expr`). Mirrored in expr_typer.zig so inference matches lowering
(avoids the issue-0083 two-resolver desync). Float-only-on-int and
non-numeric-receiver errors are unchanged.

- src/ir/lower.zig: value-binding guard in lowerNumericLimit.
- src/ir/expr_typer.zig: same guard in the numeric-limit inference arm.
- src/ir/expr_typer.test.zig: unit test pinning the two-resolver agreement.
- examples/0161-types-numeric-limit-value-shadow.sx: regression — raw
  `` `f64 ``/`` `s32 ``/`` `u8 `` value reads coexisting with bare folds.
- issues/0092: RESOLVED banner.
- specs.md / readme.md: receiver-vs-shadowing-value-binding note.
2026-06-04 23:59:11 +03:00
agra
463557990f feat(lang): float numeric-limit accessors — examples, unit tests, docs [NL.2]
Finish NL.2 on top of the WIP compiler impl (2e9e4fe): f32/f64 expose
.min/.max plus the float-only .epsilon/.min_positive/.true_min/.inf/.nan,
folded via the shared lowerNumericLimit intercept + builder.constFloat.

- examples/0159: pins every f32/f64 accessor by untagged-union bit
  reinterpret against exact IEEE-754 hex (true_min read before any
  arithmetic — FTZ/DAZ), plus the defining-property checks
  ((1+eps)!=1 / (1+eps/2)==1, inf>max, min==-max, true_min<min_positive,
  true_min>0, nan!=nan).
- examples/0160: float-only accessor on an int (s32.epsilon/u8.inf/
  s64.true_min) and any accessor on a non-numeric type compile-error
  cleanly (exit 1, pinned stderr).
- type_resolver.test.zig: floatLimitFor bit-pattern + property tests for
  f32/f64, isLimitField coverage, null for non-float/non-limit fields.
- specs.md Numeric Limits: float accessors + the min=-max / min_positive=
  smallest-normal / epsilon=ULP-of-1.0 / true_min=smallest-subnormal
  clarifications, with the mandatory FTZ/DAZ flush-to-zero caveat.
  readme.md overview updated.
2026-06-04 23:30:41 +03:00
agra
b7069801bd Merge branch 'dist-foundation' into flow/sx-foundation/NL.2 2026-06-04 23:13:50 +03:00
agra
2b7fe6828a Merge branch 'flow/sx-foundation/F0.6' into dist-foundation 2026-06-04 23:11:28 +03:00
agra
b9a29c39c5 docs(lang): fix invalid protocol method-signature snippets — (self) -> () -> s64 [F0.6]
A protocol method signature omits the receiver; a bare `self` has no type, so
`protocol { … :: (self) … }` fails at parse with 'expected :'. Correct the three
member-exemption doc snippets (readme.md, specs.md, issues/0089) to the valid
signature form, matching examples/0158's `Speaker :: protocol { s2 :: () -> s64; }`.
2026-06-04 23:02:09 +03:00
agra
166b42c308 docs(lang): sync issues/0089 member-name exemption with the f32/f64 keyword caveat [F0.6] 2026-06-04 22:52:29 +03:00
agra
685d3d122b docs(lang): keyword-spelled f32/f64 still need a backtick in member-name positions [F0.6]
The member-name exemption applies only to identifier-classified reserved
spellings (s1..s64, u1..u64, bool, string, void, usize, isize, Any). f32/f64
are lexer keywords (token.zig kw_f32/kw_f64) and member-name slots require an
identifier token, so a bare f32/f64 field/tag/method name is rejected at parse;
the backtick is required there too. specs.md + readme.md corrected.
2026-06-04 22:42:09 +03:00
agra
d14e29be02 docs(lang): precise reserved-name rule — member-name positions are EXEMPT [F0.6]
AGRA RULING (issue 0089, attempt 7): bare reserved-name MEMBER positions are
intentionally exempt from the reserved-type-name rule, and the implementation
already does the right thing — this is a docs + one-example change, no code.

The exempt member positions are struct FIELD names, union TAG names, and protocol
method-SIGNATURE names: they sit in a member slot, are reached via obj.name (or
dispatched by string), and are never type-classified, so they never mis-lower.
The backtick is optional there. The exemption stops at member DEFINITIONS: an
impl method is a real function (reached through the impl_block -> fn_decl arm), so
a reserved-spelled impl method still needs the backtick, exactly like a free
function (cf. examples/1122) — and every bare reserved-name value binding /
declaration name still errors (0076 preserved).

- specs.md / readme.md: replace the "every binding site" / "any binding site"
  overclaim with the precise rule — required positions (value bindings +
  declaration names + impl method definitions) vs the exempt member-name
  positions (field / tag / protocol signature; backtick optional).
- examples/0158-types-reserved-name-member-exempt.sx: pins the exempt behavior —
  bare reserved-name struct fields + union tag read & written bare AND via
  backtick, and a protocol with a bare reserved-name method dispatched through
  the protocol (impl definition takes the backtick).
- issues/0089: document the member-name exemption in the RESOLVED banner + add
  0158 to the regression list.

Gate: zig build, zig build test, bash tests/run_examples.sh — all green
(430 passed, 0 failed, 0 timed out).
2026-06-04 22:17:53 +03:00
agra
724a919fc1 feat(lang): raw provenance through ALL sema compound type metadata — finish universal raw identifier in the LSP classifier [F0.6]
The codegen-side resolver was already raw-aware for the universal model;
the sema/LSP editor index (the second classifier) only honored the DIRECT
raw type. A COMPOUND raw type (`*`s2`, `?`s2`, `[N]`s2`, `[]`s2`, `[*]`s2`)
stores its inner type-name as a bare string on the Type info struct, and
every resolution site re-read it with skip_builtin=false — so the index
reclassified a user type named `s2` as the builtin int, diverging from
codegen (issue-0083 class, LSP surface only; codegen unchanged).

Structural cure: every compound info struct (Pointer/Optional/Slice/
ManyPointer/Array) carries a REQUIRED is_raw bit (no default — a future
construction site cannot drop it). is_raw is set at every construction
site (resolveTypeNode arms, fieldType arms, variadic slice, .ptr/slice_expr
derivation, for-loop by-ref, substType) and passed as skip_builtin at every
resolution site (elementTypeOf, field-access pointer unwrap, index, deref,
optional unwrap/null-coalesce, if/while optional binding, match subject).
Optional-unwrap + deref sites converted from Type.fromName/pointerPointeeType
(builtin-only, divergent) to resolveTypeNameStr(name, is_raw); the now-dead
pointerPointeeType removed.

Tests: src/sema.test.zig gains pointer/optional/array raw-vs-bare
regressions (raw → user type, bare → builtin control) — each FAILS on
pre-fix sema, PASSES after — plus a parameterized-raw coverage test.
2026-06-04 21:46:31 +03:00
agra
ef8f021c01 feat(lang): universal raw identifier — parser exhaustiveness + raw type continuations + sema/LSP [F0.6]
Closes the remaining three F0.6 findings so the universal backtick raw
identifier holds in BOTH classifiers and at EVERY parser construction site.

1. Struct-body constants thread is_raw + name_span. The struct-body const
   forms (untyped `` `s2 :: 5 `` and typed `` `s2 : T : v ``) built the
   const_decl node without name_span/is_raw, so a backtick const was falsely
   rejected and a bare reserved-name const caretted at 1:1. They now capture
   both. Structural cure: `ast.ConstDecl`'s name_span + is_raw carry NO
   default, so the compiler rejects any construction site that omits them
   (mirrors checkBindingName's required `is_raw` arg). FnDecl keeps its
   defaults — every parser fn_decl routes through parseFnDecl whose
   `name_is_raw` is a required parameter (equivalent guarantee).

2. Raw identifier in TYPE position flows through the normal continuations.
   parseTypeExpr no longer returns a terminal type_expr for a raw atom; the
   raw flag rides the atom through the qualified-path / Closure / parameterized
   continuations, so `` `s2(s64) ``, `` *`s2 ``, `` ?`s2 `` all parse.
   ParameterizedTypeExpr carries is_raw; resolveParameterizedWithBindings
   skips the `Vector` intrinsic when raw.

3. sema/LSP (the second classifier) honors is_raw. Type.fromTypeExpr returns
   null for a raw type_expr; resolveTypeNode skips the builtin classifier when
   raw; resolveTypeNameStr takes a skip_builtin arg threaded from te/id.is_raw
   (compound inner names pass false). A backtick reserved-name annotation now
   resolves to the user type in the editor index, not the builtin.

Tests: examples/0156 (struct-body const), 0157 (parameterized raw type +
wrappers), 1142 (bare struct-body const errors, caret on name); src/sema.test.zig
pins the LSP raw-type resolution (fail-before verified). Gate: 365 unit tests,
429 examples, 0 failed.
2026-06-04 21:14:35 +03:00
agra
023971cae5 feat(lang): universal backtick raw identifier — valid in value, decl, AND type position [F0.6]
AGRA ruling (attempt 4): `` `name `` is THE LITERAL identifier `name`, usable in
EVERY position — the backtick only means "treat this token as a plain identifier,
never the reserved keyword/type", and is never part of the name's text.

- Raw in TYPE position is now VALID (reverses attempt-2 "raw is not a type"):
  `parseTypeExpr` emits a raw `type_expr`; `TypeResolver.resolveNamed` gains a
  `skip_builtin` flag (threaded from `te.is_raw` via lower.zig + type_bridge) so a
  `` `s2 `` reference resolves to a `` `s2 ``-declared type (struct/enum/union/alias),
  else a normal "unknown type 's2'" error (reportIfUnknownType skips the builtin
  exemption when raw). Bare `s2` in type position stays the builtin int.
- Every declaration-name site is is_raw-exemptible: `is_raw` added to TypeExpr +
  StructDecl/EnumDecl/UnionDecl/ErrorSetDecl/ProtocolDecl/ForeignClassDecl/UfcsAlias/
  NamespaceDecl/ImportDecl/CImportDecl/LibraryDecl; parser threads name_is_raw to
  every decl parse fn; namespace imports carry it through imports.addNamespace.
  Typed-const path (`` `s2 : s64 : 5 ``) now threads name_span+is_raw (fixes the
  1:1-caret bug).
- Check<->exemption made structurally symmetric: checkBindingName/checkDeclName take
  is_raw as a REQUIRED argument and skip inside the check, so no call site can
  validate a name without honoring the exemption (the desync cause of prior rounds).
- Bare reserved-name declarations of every kind still error (0076 preserved);
  `#import c` foreign names stay auto-raw + bare-callable.

specs.md + readme.md updated to the universal model. issue 0089 RESOLVED banner
rewritten. Examples: replace 1139 (raw-not-a-type) with 0154 (raw type reference);
add 0155 (typed const + union tag) and 1141 (bare type-decl negatives).
Gate: zig build + zig build test + run_examples (426 passed, 0 failed).
2026-06-04 20:27:53 +03:00
agra
c0e1a5db82 feat(lang): reserved-name check covers :: const/fn/type decls + scope call rewrite to raw provenance [F0.6]
A bare reserved-type-name `::` declaration was silently accepted, and the
attempt-2 lowerCall rewrite then made a bare `s2 :: (…) {…}` function callable —
bypassing the backtick rule for handwritten sx. The reserved-name binding check
covered `:=` / typed-local / param / captures but NOT the `::` declaration form.

- ast: `ConstDecl`/`FnDecl` carry `is_raw` + `name_span` threaded from the parser
  (parseConstBinding / parseFnDecl, all call sites incl. struct/impl methods).
- semantic_diagnostics: reject a bare reserved spelling at EVERY declaration-name
  site — const, function (incl. struct/impl methods), struct/enum/union/error-set,
  protocol, foreign-class, ufcs alias, namespaced/library/c-import name. Backtick
  (`is_raw`) and the compiler's `#builtin` definition (`string :: []u8 #builtin`)
  are the only exemptions; a value whose node is itself a named decl defers to
  that node's own check.
- c_import: synthesized foreign fn_decls are `is_raw = true`, so a C function
  whose own name collides with a reserved spelling (`int s2(int);`) imports and
  bare-calls unedited.
- lower: scope the `.type_expr`→`.identifier` call rewrite to a callee FnDecl of
  RAW provenance (`is_raw`) — only a backtick / `#import c` foreign fn can carry a
  reserved-name spelling, so a non-raw match never gets rewritten.
- examples: 0153 (positive — backtick `::` const + fn, bare + tick call), 1140
  (negative — bare `::` const + fn rejected).
- docs: specs.md + readme.md state the backtick is required at every binding site
  including `::` const / function / type declarations; issue 0089 banner updated.
2026-06-04 19:16:37 +03:00
agra
640f59dc54 feat(lang): backtick raw identifier in every binding form + raw-not-a-type + foreign reserved-name fn bare-call [F0.6]
Completes the issue-0089 backtick raw-identifier / `#import c` exemption
across all remaining identifier positions and closes three boundary gaps
the F0.6 review found.

1. Exhaustive raw-binding coverage. The `is_raw` bit now threads through
   `ast.Identifier` and EVERY binding/capture form — `IfExpr`/`WhileExpr`
   optional bindings, `ForExpr` capture + index, `MatchArm` capture,
   `CatchExpr`/`OnFailStmt` tag bindings, `DestructureDecl` per-name, and
   the protocol-default-body / foreign-class method param lists — not just
   `var_decl`/`param`. `UnknownTypeChecker` skips the reserved-name check at
   each arm when raw, so a backtick works in every identifier position while
   a bare reserved spelling still errors (issue 0076 preserved).

2. Raw identifier is never a type. `parseTypeExpr`'s atom rejects a raw
   identifier in type position (`x : `s2 = 1`, `List(`s2)`) with an accurate
   diagnostic instead of silently type-classifying it.

3. Reserved-name function bare-callable. A bare `s2(4)` parses its callee as
   a `.type_expr` (reserved spelling); `lowerCall` now rewrites a type_expr
   callee to an identifier when a function of that name is in scope, so a
   backtick-declared sx fn and a `#import c` foreign fn whose C name collides
   with a reserved type spelling both resolve by their bare name.
   (`TypeName(val)` is not a cast, so there is no ambiguity.)

Tests: examples/0152 (every control-flow/capture form + bare ref/call/member
access), examples/1054 (catch/onfail tag bindings), examples/1139 (raw in
type position rejected), examples/1220 extended (foreign reserved-name
function bare-call). 0076 negatives 1119/1121/1122/1123/1124/1125 stay green.
Gate: zig build + zig build test + 422 examples pass. specs.md + readme.md
updated; issues/0089 RESOLVED banner refreshed.
2026-06-04 18:31:08 +03:00
agra
0dbdc530ba feat(lang): backtick raw-identifier escape + #import c foreign-name exemption [F0.6]
Reserved type-name spellings (s1, s2, u8, …) can now be used as value
identifiers two ways, resolving issue 0089:

1. Backtick raw identifier: a leading backtick (`s2) lexes to an
   .identifier token carrying a new Token.is_raw flag, with the backtick
   excluded from the text. A raw identifier is never type-classified — the
   parser skips Type.fromName for it — so it is always a value identifier.
   The flag threads to VarDecl.is_raw / Param.is_raw at binding sites, and
   the reserved-type-name check (UnknownTypeChecker) skips raw bindings.
   Because the token tag stays .identifier, the escape works in every
   position (local, global, param, field, fn name, struct member, later
   reference) with no per-site parser change.

2. #import c exemption: c_import.zig synthesizes foreign decls with
   Param.is_raw = true, so generated C param names that collide with
   reserved type names (s1, s2) import unedited.

A bare reserved-name binding in sx still errors (issue 0076 preserved):
the is_raw-gated skip only fires for backtick / foreign names, and a raw
binding's address-of / autoref lowering stays correct because every
occurrence is an .identifier, never a .type_expr.

Tests: examples/0151 (backtick, every position),
examples/1220 (foreign exemption, compiled+run), lexer unit tests.
1119 (bare-binding rejection) stays green. specs.md + readme.md updated.
2026-06-04 17:40:42 +03:00
agra
7911494809 Merge branch 'flow/sx-foundation/F0.9' into dist-foundation 2026-06-04 17:17:43 +03:00
agra
5afbc65414 fix(backend): float != must be UNORDERED so nan != nan is true [F0.9]
emitCmpNe lowered float `!=` to `LLVMRealONE` (ordered not-equal), which
is false when either operand is NaN. That made `nan != nan` false in
native code — breaking the canonical `x != x` NaN test, making `!=`
non-complementary with `==` for NaN, and disagreeing with the interpreter.

Change the float predicate to `LLVMRealUNE` (unordered not-equal): true
if either operand is NaN OR they are unequal. For all non-NaN operands
`UNE` ≡ `ONE`, so only NaN-involving comparisons change (toward correct).
The integer predicate (`LLVMIntNE`) and `emitCmpEq` (`OEQ`) are unchanged,
so `nan == nan` stays false and `!=` is now the exact complement of `==`.

- Regression: examples/0150-types-float-ne-unordered-nan.sx (fails before,
  passes after; also pins #run/comptime == runtime agreement).
- specs.md: documents float comparison / NaN semantics (Operators).
- Resolves issue 0091 (issues/0091-float-ne-ordered-nan.md).
2026-06-04 17:04:41 +03:00
agra
2e9e4fe873 WIP: float numeric-limit accessors (NL.2) — blocked on 0091 (nan != nan); examples/docs pending 2026-06-04 16:48:34 +03:00
agra
b5a2535ab6 Merge branch 'flow/sx-foundation/NL.1' into dist-foundation 2026-06-04 16:30:29 +03:00
agra
04f46ef384 feat(lang): integer numeric-limit accessors (s64.max, u8.min, s3.max) [NL.1]
A field-like access on a builtin INTEGER type name folds to a compile-time
constant of the queried type, driven by (width, signedness) arithmetic:
  sN: min=-(2^(N-1)), max=2^(N-1)-1;  uN: min=0, max=2^N-1
for every width s1..s64 / u1..u64 (not just power-of-two), plus usize/isize.

- type_resolver.zig: extract the single width parser (parseWidthInt) reused by
  resolveNamed AND the new accessors (no second parser — issue-0083 class);
  add resolveBuiltinName / integerWidthSign / integerLimitBits / integerLimitFor.
- lower.zig: lowerNumericLimit intercept beside the error.X / Struct.CONST /
  pack-arity identifier-receiver intercepts; folds ints via constInt, emits a
  clean diagnostic for a non-numeric receiver (bool/string/void/Any/noreturn),
  falls through for floats (NL.2).
- expr_typer.zig: mirror the result type so inferExprType reports the queried type.
- program_index.zig: recognize the accessors in the comptime-int / array-dim path
  so [u8.max]T (255) / [s16.max]T (32767) work; [u64.max]T is rejected oversized.
- u64.max / usize.max stored as the all-ones bit pattern with TYPE u64 (i64 -1),
  asserted via union { u: u64; s: s64 } reinterpret.

Docs: specs.md numeric-limits subsection (formulas + result-type + u64 note);
readme.md language overview. Examples 0148 (positive) / 0149 (negative-receiver).
Unit tests for the value computation in type_resolver.test.zig.

Gate: zig build, zig build test (359/359), tests/run_examples.sh (416 ok, 0 failed).
2026-06-04 16:14:06 +03:00
agra
bc9777d81f docs: readme.md is now a maintained user-facing doc (lift freeze)
Per Agra ruling: user-facing sx changes must update all relevant docs
including readme.md. Replaces the prior 'Original syntax sketches. Do
not modify.' rule so the docs-track-changes review criterion is
enforceable from the next step onward.
2026-06-04 15:45:28 +03:00
agra
3c38808fd7 Merge branch 'flow/sx-foundation/F0.4' into dist-foundation 2026-06-04 15:42:43 +03:00
agra
c01ece5483 docs(spec): make the count zero-rule context-dependent per consumer (0083)
The count description claimed every count must be "positive integral",
which is wrong: zero is context-dependent. Verified at HEAD — an array
dimension (`[0]s64`) and a generic value-param count (`Box(0)`, $N:u32)
both accept zero as a length-0 instantiation, while a `Vector` lane
count stays strictly positive (`Vector(0,f32)` rejected). Negatives are
rejected for array dims and unsigned value-params, but a signed
value-param accepts a negative; only the integral requirement (folds
4.0, rejects 4.5) is common to all three.

Split the count paragraph into per-consumer bullets stating the exact
range each accepts. Range-bound paragraph unchanged. Pin the zero
contrast with examples 0147 (array-dim + value-param zero accepted) and
1505 (Vector zero-lane rejected). No compiler-code change.
2026-06-04 15:32:48 +03:00
agra
0d29f2c286 docs(spec): split range bounds from counts; pin inline-for range semantics (0083)
specs.md lumped `inline for` / `for` range bounds in with counts (array
dimension, Vector lane count, generic value-param count) under the
count negative-rejection rule. A range bound is a range ENDPOINT, not a
count: negative endpoints are valid and an empty/inverted range runs zero
iterations. The compiler already implements this correctly (Agra ruling:
spec-text bug, no code change).

- specs.md: counts and range bounds are now described separately. Counts
  reject negatives; bounds accept any compile-time integer (negatives
  valid, integral floats fold) but still reject a non-integral float
  because the loop cursor must be an integer.
- examples/0612-comptime-inline-for-range-bounds.sx: `inline for -2..1`
  and `for -2..1` both sum -3; `inline for 0..(-2.0)` runs zero
  iterations (empty range). Runtime/comptime parity asserted.
- examples/1138-diagnostics-inline-for-non-integral-bound.sx: a
  non-integral float bound `inline for 0..4.5` is a clean diagnostic,
  exit 1 (must-be-integer still applies to bounds).

Count consumers (1132/1133/1134/1135) unchanged and green.
2026-06-04 15:17:33 +03:00
agra
a7dcb23b70 fix(ir): poison type-fn binder on failed value-param bind (0083)
A failed value-param bind on a type-returning function (e.g.
`MakeC :: ($K: Count, $T: Type) -> Type { return [K]T; }` with
`a : MakeC(5_000_000_000, s64)`) emitted its correct range diagnostic
but then `instantiateTypeFunction` returned `null`, so
`resolveParameterizedWithBindings` fell through to an empty-struct
placeholder named after the function. The binding `a` got that
placeholder type, so a later `a.len` cascaded a bogus second error
`field 'len' not found on type 'MakeC'`.

The struct binder (`instantiateGenericStruct`) already returns
`.unresolved` here; the type-fn binder now matches it — a failed
value-param bind poisons to `.unresolved` instead of `null`, so the
caller propagates the diagnosed poison and the existing
`emitFieldError` suppression yields one clean diagnostic. Covers
every type-fn value-param failure mode: overflow via an aliased
constraint, a non-const arg, and an unknown type arg.

Regression: examples/1137-diagnostics-value-param-type-fn-no-cascade.sx
2026-06-04 14:38:18 +03:00
agra
a821323c3c fix(ir): converge the comptime-int count surface (0083)
Three adjacent cells of the shared count surface still diverged from the
rest; all now route through the same leaf+fold+narrow+diagnose path.

1. Aliased integer constraint bypassed the value-param range gate — only
   builtin constraint names matched intTypeRange, so Box(5_000_000_000)
   with `$K: Count` (Count :: u32) compiled and bound a truncated value.
   resolveValueParamArg (shared by both the struct AND type-fn binder) now
   resolves the constraint to its underlying builtin via
   canonicalIntConstraintName (Count -> u32, Small -> s8) before
   range-checking, so an aliased integer constraint behaves exactly like
   the builtin it names.

2. A named const with an expression RHS (M :: 2; N :: M + 1) did not fold
   as a count — moduleConstInt read only a literal RHS node. It now folds
   every const's RHS through the shared evalConstIntExpr, cycle-guarded
   (mutual / self cycles fold to null, not a stack overflow), and pass-0
   pre-registers expression-RHS consts. N :: M + 1 == 3 at every consumer:
   dim (direct + alias), Vector lane, value-param (struct + type-fn),
   inline for.

3. Stateful resolveArrayLen still fabricated length 0 after a failed fold;
   it now returns null -> the .unresolved sentinel (no fabrication). The
   binding's lowering never reaches sizeOf (alloca defers it; hasErrors
   aborts first) and a field access on an already-diagnosed .unresolved
   value is poison-suppressed (emitFieldError), so a failed-fold dim emits
   ONE clean diagnostic with no panic.

Regressions: examples/0146 (full positive matrix — every consumer x leaf
form), 1135 (aliased u32 + s8 overflow), 1136 (direct non-const dim halts
cleanly). The cascade cleanup also tightened 1502/1503 to one diagnostic.
Unit test added for moduleConstInt expression-folding + cycle detection.
2026-06-04 14:09:46 +03:00
agra
e03c087e5a fix(ir): integral-float counts + range-checked value-param binds (0083)
Item 2 (Agra ruling): a compile-time INTEGRAL float (`4.0`, `N : f64 :
4.0`, `N :: 4.0`) used as an array dimension / Vector lane / generic
value-param count / `inline for` bound now folds to its integer at the
shared leaf — `program_index.floatToIntExact`, used by both the
`.float_literal` arm of `evalConstIntExpr` and `moduleConstInt`. All four
consumers route through the one evaluator, so `[4.0]s64` lays out the same
`[4]s64` uniformly; a non-integral (`4.5`) or negative value stays
rejected by the downstream `foldDimU32` gate. Pass-0 now pre-registers
float-valued module consts for forward-alias parity with int consts.

Item 1: a generic value-param bind (`Box($K: u32)`) never range-checked
the folded arg, so `Box(5_000_000_000)` compiled and ran. The bind now
range-checks against the param's declared type — a `u32` count through the
shared `foldDimU32` gate (making program_index's "single u32 gate for
value-param counts" doc true), any other integer type through the new
`program_index.intTypeRange` — and emits a clean "value N does not fit in
u32 parameter K" otherwise. The declared type is threaded via a new
`TemplateParam.value_type`.

Regressions: examples 0145 (integral-float array dim), 1504 (Vector lane),
0611 (inline-for bound), 0209 (value-param integral-float), 1132
(non-integral float dim rejected), 1133 (negative float dim rejected),
1134 (oversized u32 value-param rejected) + program_index float-fold unit
tests. Gate: zig build, zig build test, 406/0 run_examples.
2026-06-04 13:16:39 +03:00
agra
e8cc9d03de fix(ir): precise oversized-dim diagnostic on the alias path (0083)
The stateless alias-registration array-dim path collapsed foldDimU32's
distinct .too_large / .below_min outcomes into null, so an oversized type
alias (Big :: [5000000000]s64) emitted the FALSE 'an array dimension is not
a compile-time integer constant' message while the direct form correctly
reported 'array dimension 5000000000 does not fit in u32'.

Add program_index.reportDimError as the single source of dim-error wording
(the stateful path now emits through it too) and type_bridge.foldArrayDim to
surface the DimU32 reason at the alias-registration site. An oversized/negative
alias dim now routes to reportDimError for the same precise message as the
direct form; a genuinely non-const alias dim keeps the alias-specific message.

Regression: examples/1131-diagnostics-array-dim-oversized-u32-alias.sx
2026-06-04 12:31:24 +03:00
agra
efc09699e8 fix(ir): value-param type functions + range-checked dim/lane fold (0083, 0087)
Two remaining siblings in F0.4's comptime-int path.

1. Type-returning function with a value param used as a TYPE annotation
   (`b : Make(N, s64)` where `Make :: ($K: u32, $T: Type) -> Type`):
   - `isValueParamPosition` (semantic_diagnostics) now also skips a value
     param of a `fn_ast_map` type-returning function, so `N` is not walked
     as the type name "N" ("unknown type 'N'").
   - `resolveParameterizedWithBindings` routes a type-returning-function
     name to `instantiateTypeFunction` (the `.call` path already did).
   - `instantiateTypeFunction` resolves a general return-type expression
     (`return [K]T`) with bindings active — not just struct/union returns.
   `Make(N, s64)`, `Make(M + 1, s64)`, `Make(3, s64)` all resolve to one
   `[3]s64`.

2. Oversized dim/lane fold panicked the compiler (0087): an array dim /
   Vector lane folded to a valid i64 (5e9) then narrowed to u32 with an
   unchecked `@intCast`. New single gate `program_index.foldDimU32` folds
   via `evalConstIntExpr` then range-checks `[min, maxInt(u32)]`; the three
   narrowing sites (resolveArrayLen stateful + stateless, resolveVectorLane)
   all route through it and emit a clean diagnostic + halt instead of
   panicking. Value-param args stay i64 until used as a dim/lane, where the
   same gate checks them.

Regressions: examples/0208 (value-param type function), examples/1130
(oversized array dim clean halt), examples/1503 (oversized Vector lane
clean halt). Marks issue 0087 RESOLVED.

Gate: zig build, zig build test, bash tests/run_examples.sh — 398 passed,
0 failed, 0 timed out.
2026-06-04 12:13:45 +03:00
agra
7238eea084 docs(issues): file 0086 — Vector lane store panics (discovered, pre-existing)
While fixing 0083 (attempt 5) noticed a distinct, pre-existing bug:
writing to a Vector component (`v.x = 1.0`) aborts with "unresolved type
reached LLVM emission" in emitStore. Reading a lane works; a literal lane
count triggers it, so it is NOT the lane-count class. Confirmed
reproducible on the pristine pre-attempt-5 compiler (not introduced by
the lane-count fix). The standard vector idiom (`.[…]` construction +
component reads / arithmetic, examples/1500) is unaffected. Filed for a
separate session; not worked around here.
2026-06-04 11:32:31 +03:00
agra
a491a1bf73 fix(ir): route every comptime-int through the shared evaluator (0083)
Attempts 1–4 fixed the array-dimension paths but the same length-0
fabrication class survived on every other site that resolves a
compile-time integer. Unify them all on the single shared
`program_index.evalConstIntExpr` so they cannot diverge:

- All three Vector lane resolvers (resolveTypeCallWithBindings,
  resolveParameterizedWithBindings, resolveArrayLiteralType) and both
  generic value-param binders (instantiateGenericStruct,
  instantiateTypeFunction) hand-rolled an `else => 0` switch. A
  module-const lane `Vector(N, f32)` fabricated a 0-lane `<0 x float>`
  (LLVM "huge alignment" abort); a value-param `Vec(N, f32)` fabricated
  a 0 binding / wrong mangled name. They now fold through the shared
  evaluator and emit a clean diagnostic + `.unresolved` on a non-const
  operand (resolveVectorLane / resolveValueParamArg) — never 0.
- evalComptimeInt (inline-for bounds) delegated to the shared evaluator,
  so `inline for 0..M` / `0..(M+1)` fold like array dims. The `<pack>.len`
  leaf moved into the shared folder via a new `ctx.lookupPackLen`.
- The unknown-type semantic checker no longer walks a value-param
  position (`Vector(N, …)` / `Vec(N, …)`) as a type name (was reporting
  "unknown type 'N'").
- The parameterized-type-arg parser and the function-body lookahead
  (hasFnBodyAfterArrow) accept a const-EXPRESSION in a value position, so
  `Vector(M + 1, f32)` and `[M + 1]T` parse as a return type too (the
  latter a pre-existing array-dim sibling that the same heuristic broke).

Regressions: examples/1501 (named-const + const-expr lane, direct +
alias, 3/4-lane reads), 1502 (runtime lane clean-halts, exit 1, no LLVM
crash), 0207 (Vec(N)/Vec(M+1) == Vec(3) instantiation), 0610 (inline-for
const bounds). Shared-evaluator unit test extended with the pack-len arm.

zig build && zig build test && bash tests/run_examples.sh: 395 passed,
0 failed.
2026-06-04 11:32:25 +03:00
agra
cd39316f5e fix(ir): evaluate constant-expression array dimensions (0083)
A constant-FOLDABLE expression array dimension (`[M + 1]`, `[M * N]`,
`[N - M]`, nested `[M + N - 1]`, parenthesised `[(M + 1) * 2]`, mixing
untyped and typed module consts) was wrongly rejected as "not a
compile-time integer constant" even though every operand is
compile-time-known. Attempts 1-3 resolved only a bare named-const dim or
a literal; an expression dim must be EVALUATED, not rejected.

Fix: the shared dim resolver now routes the dimension through a single
constant integer-expression evaluator (`program_index.evalConstIntExpr`)
that folds integer `+ - * / %` and unary negate over literals and
named/typed module consts, recursively (parentheses carry no AST node).
The leaf-name lookup is delegated via `ctx.lookupDimName`, so the
stateful body-lowering path (`Lowering`, which also sees comptime
constants and generic `$N` values) and the stateless registration path
(`type_bridge.StatelessInner`, module consts only) share the EXACT SAME
folding logic and cannot diverge — an expression dim via a type alias
resolves identically to the direct form.

No-fabrication discipline unchanged: a genuinely non-comptime dimension
(runtime local, non-comptime call, unbound name) or arithmetic that
overflows / divides by zero still yields null -> `.unresolved` -> the
same clean compile-halting diagnostic, never a fabricated length.

- examples/0144-types-const-expr-array-dim.sx: every expression form,
  direct vs alias, scalar / string / struct element types (fails on the
  pre-fix compiler, passes after).
- examples/1129 re-pointed at a genuinely non-const dimension
  (`[get()]s64`, a runtime call) so it still proves the stateless
  clean-halt (a foldable expression is no longer an error).
- program_index.test.zig: unit test for evalConstIntExpr folding and
  clean-halt-on-non-const.
2026-06-04 10:38:21 +03:00
agra
d2bf8f3f2d fix(ir): unify named-const array-dim resolution + kill length-0 fabrication (0083)
A type alias whose dimension is a named const (`Arr :: [N]T`) resolves its
dimension eagerly during scanDecls pass 1, on the stateless registration path,
which can only read `module_const_map`. Typed consts (`N : s64 : 16`) register
only in pass 2 and a forward-declared untyped const had not registered yet, so
the stateless resolver saw an empty table, printed a non-fatal warning,
fabricated length 0, and continued — yielding a 0-byte alloca, garbage reads,
and a segfault for slice/struct elements.

- scanDecls pass 0 pre-registers every integer-valued module const before any
  type alias resolves, so typed, untyped, and forward-referenced consts all
  resolve identically.
- Both dim resolvers now share `program_index.moduleConstInt`, so the stateful
  body-lowering path and the stateless registration path cannot diverge.
- `resolveArrayLen` returns `?u32`; `resolveCompound` yields `.unresolved` on
  null instead of a 0-length array. The stateful path emits a diagnostic; the
  alias-registration path surfaces an unresolved alias as a clean compile error
  that aborts the build. The Vector lane-count `else => 0` is fixed the same way.

Regressions: examples/0143 (typed-const dim direct + via alias for s64/string/
struct, forward-ref alias, nested) and examples/1129 (an unresolvable computed
dim halts with a clean diagnostic + non-zero exit). Both fail on the pre-fix
compiler (garbage/segfault; warning+exit0) and pass after.
2026-06-04 09:39:18 +03:00
agra
1f9f944ca1 fix(ir): exhaustive named-const array dims (0083) + nested slice-literal coercion (0085)
Makes the F0.4 fixes exhaustive across every resolution / nesting path.

0083 — named-const array dimension, stateless paths. Attempt 1 fixed the
stateful resolver (direct local decls, struct fields, params, returns) but the
binding-free registration-time resolver (`type_bridge`, used for type aliases
`Arr :: [N]T` and inline union/enum field types) still resolved a named dim with
a silent `else 0`, so `Arr :: [N]s64; a : Arr` and `union { a: [N]s64 }` were
still miscompiled (garbage / bus error). Thread the module-global const table
(`ProgramIndex.module_const_map`) into `type_bridge` alongside the alias map, so
`StatelessInner.resolveArrayLen` resolves a named module-const dim to the same
length everywhere. The remaining unresolvable case (a computed/comptime dim on
the binding-free path, which the stateful path hard-errors) now bails LOUDLY
instead of fabricating a 0 length.

0085 — nested slice-literal elements. `lowerArrayLiteral` lowered each element
with the element type as target but appended the raw value. A nested `.[...]`
element at a slice element type (`[][]s64`) still lowers to an aggregate array
`[N]T`, so the outer aggregate held raw arrays where slice {ptr,len} headers
were expected — indexing the inner slice read a garbage pointer and segfaulted.
After lowering each element, coerce a same-element array to the slice element
type via the existing `array_to_slice` op. The coercion recurses with the
nesting, so `[][]T` and deeper materialize at every level — local-bound AND
direct-call-argument forms.

Regressions (fail-before/pass-after demonstrated on the pre-fix compiler):
  examples/0140-types-named-const-array-dim.sx — extended with type-alias,
    nested [N][M]T, and union-field named dims (s64 / string / struct elems)
  examples/0142-types-nested-slice-literal-elements.sx — [][]s64 + [][]string,
    local-bound vs direct-arg
  src/ir/type_bridge.test.zig — named-const dim resolves to literal length

Gate: zig build, zig build test, bash tests/run_examples.sh (388 passed).
Issues 0083 and 0085 marked RESOLVED.
2026-06-04 09:06:08 +03:00
agra
12552e125d fix(ir): resolve named-const array dims (0083) + materialize literal slice args (0084)
Two silent-miscompile codegen fixes:

0083 — named-const array dimension. `TypeResolver.resolveCompound`'s array
arm resolved the dimension with `if int_literal ... else 0`, so a named const
(`N :: 16; [N]T`) hit the silent `else 0`: the array became 0-length / 0-byte
and element access ran out of bounds (garbage for scalars, bus error for
slice/pointer/struct elements). The arm now delegates the dimension to
`inner.resolveArrayLen` (symmetric with `inner.resolveInner` for the element).
The stateful `Lowering.resolveArrayLen` evaluates it as a compile-time integer
across the comptime-constant / generic-value / module-global const tables and
emits a diagnostic — no fabricated length — when it isn't one.

0084 — `.[...]` literal passed directly as a call arg. `lowerArrayLiteral`
always yields an aggregate array value; the array→slice conversion is the
caller's job. The local-bound var-decl path did it, but the call-arg coercion
path had no array→slice arm, so `classify([N]T, []T)` returned `.none` and the
raw array was passed where a slice was expected (callee read its {ptr,len}
header off the wrong bytes → 0 / garbage / segfault). `classify` now returns a
new `.array_to_slice` plan for same-element `[N]T → []T`, and `coerceToType`
emits the existing `array_to_slice` op — identical to the local-bound path.

Regressions (fail-before/pass-after demonstrated on the pre-fix compiler):
  examples/0140-types-named-const-array-dim.sx (s64 + string + struct elems)
  examples/0141-types-slice-literal-direct-call-arg.sx (string + []s64)

Gate: zig build, zig build test, bash tests/run_examples.sh (387 passed).
Issues 0083 and 0084 marked RESOLVED.
2026-06-04 08:22:45 +03:00
agra
3b36264e65 Merge branch 'flow/sx-foundation/F3.2' into dist-foundation 2026-06-04 08:03:43 +03:00
agra
9784ff8705 F3.2: assert Diag for the zero-arg and too-many-flags raise sites
Example 0717 now asserts the (token, index) Diag for ALL SIX raise sites
in cli.sx, closing the two the reviewer found still unasserted:

  - zero-arg UnknownCommand: parse([], ...) -> index -1, token ""
    (the args.len == 0 sub-branch of cli.sx:237, distinct from the
    one-arg too-few form already covered at index 0 / token args[0]).
  - TooManyFlags (cli.sx:256): a command declaring 17 flag specs (> the
    inline 16 cap) is rejected, not truncated -> index -1, token command.

The three index==-1 cases (zero-arg, too-many, missing-req) seed their
Diag with a sentinel before parse, so each assertion proves parse WROTE
the -1/"" rather than merely matching the `.{}` default. Verified
non-vacuous: flipping any expected value makes that line FAIL.

Test-only: cli.sx logic and src/ are untouched.
2026-06-04 07:54:20 +03:00
agra
d1e5f10039 F3.2: assert Diag (token,index) for all cli.parse error cases
Extend example 0717 to pin the offending token VIEW and its args index
for every failure the parser's Diag populates: unknown-command,
unknown-group, too-few-args, missing-value, value-eats-flag, and the
missing-required index. Closes the test-coverage gap flagged in review;
cli.sx parser logic unchanged.
2026-06-04 07:38:57 +03:00
agra
17b437ecfb F3.2: std.cli minimal subcommand + flag parser over explicit []string
Extend std/cli.sx with a zero-heap argument parser that the caller drives
over a logical argv ([]string), separate from the F3.1 os_args accessor.

Grammar: <group> <command> [--flag VALUE | --bool]... [--json] [-- rest...]
  - (group, command) dispatched against a caller-provided Command table;
    no match -> error.UnknownCommand.
  - value-taking vs boolean flags fixed by each command's FlagSpec list;
    --json is a reserved global boolean surfaced as parsed.json.
  - `--` or the first bare operand ends flag parsing; the remainder is
    parsed.rest (operand views).

Heap discipline (heap-discipline.md): zero heap, zero copy. group/command/
flag values/rest are all VIEWS into args. Parsed is a by-value stack struct;
flag presence/values live in a fixed [16]FlagValue inline array indexed by
spec position (no per-flag allocation, no context.allocator). The flag-spec
list and command table are caller storage passed as views.

Failure surfacing (no silent skip): unknown command, unknown flag, a
value-flag missing its value, and an absent required flag each raise a
specific CliError variant; a caller-owned Diag records the offending token
(index + view) before each raise, since error tags carry no data.

examples/0717 drives the parser over explicit []string vectors: a valid
group/command/--flag/--bool/--json case (asserting parsed values + that
values are views into argv), subcommand dispatch, `--`/bare-operand
separators, and the five failure variants each asserted via destructure +
Diag. zig build && zig build test && run_examples.sh green (385 passed).
2026-06-04 06:13:09 +03:00
agra
8c96290801 Merge branch 'flow/sx-foundation/F0.3' into dist-foundation 2026-06-04 05:38:18 +03:00
agra
d87bad2ec4 fix(ir): halt cleanly when a global initializer can't be serialized
The global-init constant serializers in emit_llvm.zig printed a diagnostic
on an unserializable value and then RETURNED an undef/null placeholder and
CONTINUED emitting. For a comptime `#run` global that yields a function
reference (`fp :: #run pick();` where pick returns a function), the build
fell through to the JIT and segfaulted calling through the undef pointer
(exit 134) — a silent miscompile dressed up as a printed error.

Route every genuine bail in the serialization family through a new
`failGlobalInit` helper: it sets `comptime_failed` (so core.generateCode
aborts with a non-zero exit after emit()) and returns an undef placeholder
that never ships, because the halt fires before object emission / JIT. This
covers the comptime func_ref leaf, the require_resolved aggregate func_ref
leaf, the top-level + vtable func_ref globals, the comptime-init catch, and
the remaining heap-walk / aggregate-shape bails. Unresolved-function
diagnostics now name the function instead of its (stdlib-unstable) IR index.

The require_resolved=false Pass-0 placeholder is unchanged (func_map is
empty until Pass 1; the aggregate is re-emitted with require_resolved=true).

Regression: examples/1128-diagnostics-comptime-global-funcref-rejected.sx —
a `#run` global returning a function ref now exits 1 with the diagnostic
(was: exit 134 segfault). Fail-before/pass-after verified.
2026-06-04 05:25:19 +03:00
agra
263333bd26 fix(ir): serialize enum-literal global initializers (issue 0082)
A module-global initialized with an enum literal silently zero-initialized
to the first tag (`chosen : Color = .green` read back as `.red`), and an
enum tag inside a global array/struct was rejected as non-constant. The
constant serializer had no enum-literal arm.

Add `Lowering.constEnumLiteral`: serialize an enum literal to a
`ConstantValue.int` holding the variant's tag value, resolved against the
destination enum type and respecting explicit variant values; the global's
type drives the backing width at emit time. Wired into `globalInitValue`
(scalar global) and `constExprValue` (array element / struct field / nested
aggregate). A non-enum destination or unknown variant is diagnosed loudly,
never silently zero-initialized. The compiler-injected OS/ARCH globals now
serialize to their real `.unknown` tag (6 / 4); runtime reads are unchanged
(they resolve through comptime_constants), so only the static initializer in
the pinned .ir snapshots changes.

Remove the silent `func_ref => orelse LLVMConstNull` fallbacks in the LLVM
constant emitters: aggregate func_ref leaves carry a `require_resolved` flag
(transient null in Pass 0, loud diagnostic if still unresolved in the
Pass-1.5 re-emit), a top-level func_ref global is resolved in
initVtableGlobals, and the comptime (#run) path bails loudly instead of
emitting a null function pointer.

Regression: examples/0139-types-global-enum-literal-init.sx (scalar, array,
struct field, explicit-value enum u16 stride, struct-array with enum field);
negative: examples/1127-diagnostics-global-enum-literal-bad-variant.sx.
Mark issue 0082 RESOLVED.
2026-06-04 04:52:42 +03:00
agra
d680b320f4 fix(ir): serialize null pointer fields in global aggregates (issue 0081)
A module-global aggregate initializer rejected a `null` literal in a
pointer (or optional-pointer) field as "must be initialized by a
compile-time constant". `Lowering.constExprValue` had no `.null_literal`
arm, so the null leaf returned no constant and the whole aggregate looked
non-constant — even though `null` is the compile-time zero pointer (a
top-level scalar `p : *s64 = null;` already serialized fine).

Add `.null_literal => .null_val` to constExprValue. While here, make the
two LLVM constant emitters exhaustive: emitConstAggregate and the
top-level init_val switch in emit_llvm.zig previously ended in a silent
`else => LLVMConstNull(...)` catch-all (the silent-arm class CLAUDE.md
mandates rooting out). They now handle every ConstantValue tag explicitly
(.null_val/.zeroinit -> all-zero constant, .undef -> LLVMGetUndef,
.func_ref resolved, nested .vtable is a hard @panic tripwire). The
reject-loud path for genuinely non-constant fields is preserved.

Regression: examples/0138 (array-of-struct null ptr fields, array of
all-null pointers, nested struct-in-struct null ptr) and the negative
examples/1126 (null ptr field beside a non-const field still errors).
Fail-before/pass-after verified.
2026-06-04 04:22:43 +03:00
agra
e93879816d fix(ir): materialize global aggregate struct-literal initializers (issue 0080)
A module-global array of struct literals (`pairs : [2]Pair = .[ .{...}, .{...} ]`)
was emitted as `zeroinitializer`, silently dropping every declared field — reads
returned 0 with no diagnostic. Global struct literals and struct-with-array
already worked; the gap was struct literals used as ARRAY elements.

Root cause: `Lowering.constExprValue` (the const-aggregate serializer for global
initializers) had no `.struct_literal` arm. `constArrayLiteral` serialized each
element through `constExprValue`, so a struct-literal element returned null,
collapsing the whole array initializer to null; `globalInitValue` then emitted no
payload and the LLVM backend zero-initialized the global — the same silent-zero
class as 0071/0072, one level inside an array literal.

Fix: make `constExprValue` type-aware — thread the destination element/field
TypeId so a struct-literal leaf routes through `constStructLiteral` and a nested
array-literal through `constArrayLiteral` with the correct element type.
`constArrayLiteral` derives its element type from the array TypeId;
`constStructLiteral` passes each field's type. A global aggregate initializer that
still does not fully reduce to a compile-time constant is now rejected loudly
(`diagnoseNonConstGlobal`) instead of silently zeroing. `emitConstAggregate`
already recurses over nested aggregates, so `sx run` (JIT) and `sx build` (AOT)
both materialize the declared values.

Regression: examples/0137-types-global-aggregate-literal-init.sx (global
[N]Struct literal, global struct literal, struct-with-array, nested
array-of-struct-with-array; values read back with no prior store, plus a store on
top). Fails on the pre-fix compiler (array-of-struct fields read 0), passes after.

Marks issues 0079 (already resolved) and 0080 RESOLVED.
2026-06-04 04:04:40 +03:00
agra
7306d37748 fix(ir): store to module-global array element targets live storage (issue 0079)
A store to a module-global array element (`g[i] = v`) was silently dropped:
a subsequent `g[i]` read the array's initializer, not `v`. Constant index,
variable index, and cross-function stores were all affected, in both `sx run`
and `sx build`. Global scalars and local arrays were fine.

Root cause: `Lowering.lowerExprAsPtr` (the lvalue/address path) handled only
local identifiers. A module-global identifier fell through to the value
fallback `lowerExpr`, which emits `global_get` — loading the whole array by
value. The LLVM backend's `emitIndexGep` then allocas a throwaway temp, copies
the value in, and GEPs into the temp, so the store wrote a discarded copy.

Fix: teach `lowerExprAsPtr`'s identifier arm about globals — emit `global_addr`
(a pointer into the global's live storage), or `global_get` for a pointer-typed
global (mirroring the local pointer case). Route the `address_of(index_expr)`
array base through `lowerExprAsPtr` too so `&g[i]` is likewise an lvalue into
the global. `index_gep` now GEPs directly into the global for const and variable
index, across functions. This also fixes global struct field stores, which
shared the same root cause.

Regression: examples/0136-types-global-array-element-store.sx (const-index,
var-index, cross-function store on a scalar global array; struct-element array
for stride; nested-array global for the recursive lvalue). Fails on the pre-fix
compiler, passes after.
2026-06-04 03:44:19 +03:00
agra
483b14015f Merge branch 'flow/sx-foundation/F3.1' into dist-foundation 2026-06-04 03:32:37 +03:00
agra
e7f5bd7aaa F3.1: std.cli os_args — real OS argv accessor via #foreign _NSGetArgv (examples/0716)
Add library/modules/std/cli.sx: a pure-sx command-line argument accessor
backed by the macOS C runtime (_NSGetArgv/_NSGetArgc), no compiler change.

  os_argc() -> s64
  os_args(buf: []string) -> []string

Zero heap, zero per-arg allocation: os_args fills a caller-provided buffer
(stack array) with string VIEWS over the process's own argv block, which
lives for the whole process. The returned slice header is a by-value stack
return; nothing touches context.allocator.

Documents the `sx run` reality: under `sx run <prog.sx> ...` the process
argv is the interpreter's argv (sx, run, prog.sx, ...), not a program's
logical args. This accessor reports the real process argv truthfully;
mapping to logical args is a later consumer concern (distribution P3.1).

Non-macOS platforms bail loudly (message + _exit) rather than returning a
silent empty.

examples/0716-modules-cli-argv.sx asserts only deterministic structural
invariants (argc >= 1, argv[0] non-empty, os_argc() == filled length).
2026-06-04 03:21:41 +03:00
agra
090bdd7cfa Merge branch 'flow/sx-foundation/F2.3' into dist-foundation 2026-06-04 03:04:16 +03:00
agra
1905d35507 F2.3: pin std.json round-trip + malformed-input suite (examples/0715)
Add 0715-modules-json-suite as the single comprehensive pinned suite for
std.json (mirrors 0711 for std.hash), alongside the focused 0713/0714 demos:

- ROUND-TRIP build->write->parse->write over a document covering EVERY value
  kind (a string with every escape form \" \\ \b \f \n \r \t plus a \u00XX
  control, integers 0 / negative / s64 MIN / s64 MAX, bool, null, array,
  nested object) with insertion-order assertions, exact writer bytes, and
  parse-then-rewrite idempotence.
- DECODE positives: \/, the full named-escape set, \uXXXX (BMP 1- and 2-byte)
  plus a surrogate pair, the escaped control forms, and raw multi-byte UTF-8
  round-tripping through writer + reader.
- MALFORMED matrix: one assertion per JsonParseError variant and its key
  edges (UnexpectedToken, UnexpectedEnd, BadEscape, BadNumber incl. leading
  zero / lone '-' / fraction / exponent / overflow, TrailingGarbage,
  BadControlChar), each asserted to raise.

Pure test work: src/ and library/ untouched, no json.sx change needed. Every
model is built through an explicit Arena allocator (heap discipline).
2026-06-04 02:57:32 +03:00
agra
dc2a6a0a87 Merge branch 'flow/sx-foundation/F2.2' into dist-foundation 2026-06-04 02:42:10 +03:00
agra
2871342c0a F2.2: reject raw control bytes (U+0000..U+001F) in JSON strings
parse_string scanned for `"` and `\` but accepted every other byte,
including raw control characters. RFC 8259 §7 requires those bytes to be
escaped inside a string; an unescaped one is invalid JSON and must surface
a parse error, not be silently accepted.

Add `BadControlChar` to JsonParseError and reject any unescaped byte < 0x20
in the string body scan (which gates the decode path too, so escaped forms
like \t/\n/	 still decode correctly; 0x20 and 0x7F are not over-rejected).

Regression test in examples/0714: raw 0x09/0x0A/0x00 each raise
BadControlChar via `?`/`!`; a positive case proves the escaped forms still
decode to the right bytes. All prior assertions kept.
2026-06-04 02:32:32 +03:00
agra
301e966bcf F2.2: un-workaround 0714 — combine string == under and/or (0078 fixed)
Issue 0078 (string == as an and/or operand emitting an invalid PHI) is
resolved on this branch, so the example no longer needs the split that
worked around it. Restore the natural combined assertion
  sub.items[0].key == "k" and sub.items[0].val.str == "v"
(one nested-pair report), and the in_range containment helper to
  return x >= lo and x < hi;
Drop the now-stale issues/0078 references. Re-captured expected stdout
(nested-key/nested-val -> nested-pair). json.sx and src/ untouched.
2026-06-04 02:17:22 +03:00
agra
0e7bdc7c11 Merge branch 'dist-foundation' into flow/sx-foundation/F2.2
# Conflicts:
#	issues/0078-string-eq-operand-of-short-circuit-and-invalid-phi.md
2026-06-04 02:10:42 +03:00
agra
1d92046b7c Merge branch 'flow/sx-foundation/F0.2' into dist-foundation 2026-06-04 02:09:45 +03:00
agra
7c1b90519f fix(emit): PHI predecessor for and/or operand that emits sub-CFG (issue 0078)
A string `==`/`!=` used as an operand of a short-circuit `and`/`or` emitted
invalid LLVM (`PHI node entries do not match predecessors!`). String compares
expand into their own memcmp sub-CFG during LLVM emission, so the operand
finishes in a later basic block (`str.merge`) than the one the IR block
started in. `fixupPhiNodes` wired the short-circuit merge PHI's incoming edge
to `block_map[ir_block]` (the block the IR block started as), recording a
stale predecessor (`%entry`/`%and.rhs.0`).

Fix: record the builder's actual insertion block after emitting each IR
block's instructions (`term_block_map`, via `LLVMGetInsertBlock`) and use it
as the PHI predecessor. General — corrects the incoming block for any operand
that emitted intermediate basic blocks (string `==`, value `match`, …), not
just string `==`.

Regression: examples/0045-basic-string-eq-short-circuit.sx (string `==` on
both sides of `and` and of `or`, plus a match-value + enum-payload `==` shape).
Fails (LLVM abort) pre-fix, passes after.
2026-06-04 02:00:13 +03:00
agra
88be541778 F2.2: std/json reader — explicit-alloc parse with error surfacing
Add the JSON reader (parser) to library/modules/std/json.sx, the inverse
of the F2.1 writer over the same value model: insertion-ordered objects,
arrays, strings (full unescaping incl. \uXXXX + surrogate pairs), s64
integers, bool, null.

Heap discipline (binding): exactly two allocation kinds, both through the
EXPLICIT `alloc` parameter, never the implicit context allocator —
composite backing stores (Array/Object.items via add/put) and decoded
escaped-string buffers (bounded by the raw span). Un-escaped string
values are zero-copy VIEWS into the input buffer (valid only while it
lives); scalars carry no heap.

Failure surfacing (hard contract): malformed input raises a meaningful
JsonParseError variant (UnexpectedToken / UnexpectedEnd / BadEscape /
BadNumber / TrailingGarbage) on the error channel, never a bogus value.
Trailing non-whitespace is TrailingGarbage; fractions/exponents,
out-of-s64 magnitudes, and leading zeros are BadNumber. Number
accumulation runs in negative space so s64 MIN parses exactly.

examples/0714-modules-json-reader.sx asserts the parsed structure
(insertion order, every kind), proves the view-vs-decoded heap split by
pointer containment, round-trips back through the writer byte-for-byte,
decodes a surrogate-pair into 4 UTF-8 bytes, and checks every malformed
variant.

Filed issues/0078: a string `==` (or any sub-CFG operand) used in a
short-circuit `and`/`or` emits invalid LLVM IR (stale PHI predecessor),
hit while writing the example's assertions and worked around there by not
combining comparisons with `and`/`or`. src/ untouched.
2026-06-04 01:41:33 +03:00
agra
295d95d51a Merge branch 'flow/sx-foundation/F2.1' into dist-foundation 2026-06-04 01:15:26 +03:00
agra
1d311b871e test(json): pin s64 MIN/MAX writer bytes; move scratch to .sx-tmp
Close the coverage gap from attempt 1: example 0713 now builds integer
fields holding s64 MIN (-9223372036854775808) and s64 MAX
(9223372036854775807) — plus zero, a small negative, and a small positive —
and asserts the EXACT emitted bytes. This permanently pins the edge that
write_int is specifically engineered for (folding positives into negative
space so MIN's non-representable-positive magnitude serializes correctly).

s64 MIN is expressed as (0 - 9223372036854775807 - 1) because its magnitude
is not a representable positive s64 literal.

Test hygiene: stream to a repo-local, gitignored .sx-tmp/ path (created if
missing) instead of a fixed /tmp name, and unlink it right after read-back
so nothing leaks. Writer/model logic and src/ are untouched.
2026-06-04 01:08:14 +03:00
agra
4552ed61f6 std/json: value model + zero-alloc writer with stable key order
Add library/modules/std/json.sx — the JSON value model and writer
(reader lands in a later step).

Value model: a tagged union over null/bool/integer(s64)/string/array/
object. Objects are an ORDERED list of (key,value) pairs preserving
INSERTION ORDER (no hash map, never sorted/deduped). Integers only — no
fraction/exponent this milestone.

Heap discipline:
  - Scalars carry no heap; string values are VIEWS into caller memory
    (never copied into the node).
  - Composite nodes (Array/Object) own growable child storage, allocated
    through an EXPLICIT allocator parameter on the builder methods
    (arr.add(v, alloc) / obj.put(key, val, alloc), mirroring List.append)
    — never the implicit context allocator.
  - The writer adds ZERO output allocations: it emits into a caller-
    provided Sink, either a fixed []u8 buffer (overflow raises, never
    truncates) or streaming straight to an fs.File through a small caller
    staging buffer (no whole-document string; peak memory O(staging)).
    Integer digits format in a stack [20]u8; s64 MIN is handled by
    formatting in negative space. Sink/IO/overflow surface on the !
    error channel.

examples/0713-modules-json-writer.sx builds a nested object + array +
string with every escape kind + negative int + bool + null, then asserts
the EXACT bytes (insertion order, escaping) from both the buffer sink and
the file-streaming sink, plus the overflow-raises path.
2026-06-04 00:47:30 +03:00
agra
9bf07e0c5f Merge branch 'flow/sx-foundation/F1.2' into dist-foundation 2026-06-04 00:20:31 +03:00
agra
f9bc593bb8 F1.2: std.hash zero-heap [64]u8 hex API + chunked file + pinned vectors
Make the SHA-256 digest path allocation-free (foundation heap-discipline):

- final() and sha256_hex() now return the 64-char lowercase hex digest as
  a [64]u8 by value on the stack; the cstring(64) heap allocation is gone.
- sha256_file() streams the file in fixed 64KB stack chunks via open_file/
  File.read/File.close (defer-closed on every path) instead of slurping it
  with read_file; peak memory is O(chunk), not O(filesize).

Tests (compare via a zero-copy string view over the [64]u8):
- 0710 updated to the by-value API (output unchanged).
- 0711 known-answer vectors: "", "abc", NIST-56/112, padding boundaries
  {0,55,56,57,63,64,65,119,120}, and 1000 / 1,000,000 'a' repeats, each
  pinned to its published digest (cross-checked with shasum -a 256).
- 0712 streaming equivalence (one-shot == byte-at-a-time == split-mid-block
  == split-on-boundary) plus sha256_file(temp) == in-memory digest.

src/ untouched. zig build && zig build test && tests/run_examples.sh green.
2026-06-04 00:08:46 +03:00
agra
ee1e097335 Merge branch 'flow/sx-foundation/F1.1' into dist-foundation 2026-06-03 22:47:40 +03:00
agra
8f9691c206 F1.1: std.hash — streaming SHA-256 in library/modules/std/hash.sx
Add a pure-sx streaming SHA-256 (FIPS 180-4) stdlib module, importable
as `#import "modules/std/hash.sx";`. All 32-bit word arithmetic is done
in s64 and masked back with `& MASK32`, so digests are deterministic and
platform-independent — no shelling out, no native crypto.

API:
- init() -> Sha256          (by-value *self pattern)
- update(*Sha256, string)   (multi-block + partial-block buffering)
- final(*Sha256) -> string  (32-byte digest as lowercase hex)
- sha256_hex(string) -> string             (one-shot)
- sha256_file([:0]u8) -> ?string           (digest of a file via fs.read_file)

Verified against FIPS/NIST known-answer vectors and `shasum -a 256`:
"" , "abc", the 56- and 112-byte multi-block vectors, 1000×'a', and the
64/65-byte block boundaries; chunked update() matches the one-shot call.

examples/0710-modules-sha256.sx pins the KAT vectors + the streaming
invariant; gate green (zig build, zig build test, run_examples 370/0/0/0).
2026-06-03 22:38:58 +03:00
agra
a89a5f8d18 Merge branch 'flow/sx-foundation/F0.1' into dist-foundation 2026-06-03 22:18:43 +03:00
agra
6433eb6155 fix(diagnostics): point reserved-type-name binding errors at the binding (issue 0076)
The reserved-type-name binding diagnostic fired correctly but underlined the
enclosing statement / if / while / for / match / protocol / #objc_class block
because every binding-name check reused the parent `node.span`.

Thread each binding name's own span through the AST and parser, and pass it to
`checkBindingNames`:

- ast: add name spans to VarDecl, DestructureDecl, If/WhileExpr, ForExpr
  (capture + index), MatchArm, Catch/OnFailStmt, Protocol/ForeignMethodDecl.
- parser: populate each span at the binding site from the name token's loc;
  destructure reuses each target identifier's own span.
- semantic_diagnostics: every checkBindingName call now passes the binding's
  own span — no site falls back to node.span. fn/lambda params already used
  Param.name_span.

Carets now land on the offending identifier itself. New regression
examples/1125 asserts the protocol default-body and sx-defined #objc_class
method param spans; 0125/1119-1124 expected updated to the precise carets.
2026-06-03 22:06:56 +03:00
agra
fcc76b9391 fix(diagnostics): make reserved-type-name binding check exhaustive (issue 0076)
The reserved/builtin-type-name binding diagnostic was a hand-walked subset
of binding-bearing AST nodes with a silent `else => {}`, so each review
found another syntactic binding form that bypassed it and hit the original
LLVM verifier abort: destructure names (`s2, x := …`), `impl` method
params/locals, and `if` / `while` / `for` / match-arm / `catch` / `onfail`
captures.

Rewrite `checkBindingNames` (src/ir/semantic_diagnostics.zig) as an
EXHAUSTIVE `switch` over every `Node.Data` tag with NO `else` arm — a future
binding-bearing node type now fails to compile until it is handled here, so
coverage is enforced by the compiler instead of a hand-maintained list. The
check stays in the pre-lowering semantic pass rather than moving to the
`Scope.put` scope-registration choke point: lowering is lazy, so an
uncalled function's bindings never reach `Scope.put`, yet they must still be
rejected at their declaration (e.g. the never-called `takes_u8` in 1119).
No lowering special-case; `lower.zig` unchanged.

Regression tests (fail-before: LLVM abort or silent accept → pass-after:
clean diagnostic, exit 1):
- 1121 control-flow: destructure, if/while bindings, for capture+index,
  match-arm capture
- 1122 impl-block method: reserved param AND reserved local
- 1123 catch + onfail tag bindings
- 1124 destructure name reserved in an imported module
Existing 0125 / 1119 / 0135 / 1120 tests kept; full suite 368 passed.
2026-06-03 20:09:46 +03:00
agra
df6e830bec fix(diagnostics): reject reserved type-name bindings in every module (issue 0077)
The issue-0076 reserved-type-name binding diagnostic only ran over main-file
decls, so an imported module (or the stdlib) could still declare `s2 := ...`
and reach lowering, where the address-of family loads the whole aggregate and
passes it by value to a `ptr` param — LLVM verifier abort.

Extend coverage to every compiled module: a dedicated `checkBindingNames` walk
(in semantic_diagnostics.zig) visits every var/`:=`/typed-local binding name and
function/lambda/struct-method parameter at any depth, with NO main-file filter,
descending the `namespace_decl` that a `mod :: #import` wraps so imported-module
decls are reached. It tracks each module's source_file (save/restore per node)
so the diagnostic renders against the imported module's text. Rejection still
defers to the parser's `Type.fromName` classifier; the unknown-type check (0064)
stays main-file-only. No lowering special-case; `.identifier`-only address-of
paths are unchanged.

Stdlib audit: the only reserved-name bindings under library/ were two `u1`
locals in ui/renderer.sx (UV coords) — renamed to u_min/u_max/v_min/v_max.

Regression test: examples/1120-diagnostics-imported-reserved-type-name.sx (+
companion mod.sx) — an imported `s2 := ...` now emits the clean diagnostic at
the import's declaration site (exit 1), not an LLVM abort.

Resolves issues 0076 (coverage extension) and 0077.
2026-06-03 19:32:49 +03:00
agra
f49a49cd07 fix(diagnostics): reject reserved/builtin type names used as identifiers (issue 0076)
A value binding (local/global `var` or a parameter) spelled as a
reserved/builtin type name parses as a `.type_expr` rather than an
`.identifier` (parser.zig, via `Type.fromName`), so the address-of
family in lower.zig never saw a scoped local and mis-lowered it —
loading the aggregate and passing it by value to a `ptr` parameter
(LLVM verifier abort, or a silent `*self`-mutation-losing copy).

Add a declaration-site diagnostic in semantic_diagnostics.zig
(`UnknownTypeChecker.checkBindingName`): reject any parameter name or
`var` binding name (`:=` / typed-local / global forms) whose spelling
collides with a reserved type name. `isReservedTypeName` defers to the
parser's own classifier (`types.Type.fromName`) so the rejected set
never drifts from the set that would parse as a type — the named
builtins (bool/string/void/f32/f64/usize/isize/Any) and `[su]N` over
sx's 1-64 range. Bare value names (`s`, `self`, `index`) are untouched.
No lowering special-case; the `.identifier`-only address-of paths are
correct once type-shaped names can never be bound. The rejected
attempt-1 `bareVarName` approach was never landed.

Tests:
- 0125-types-type-named-var-rejected: `:=` form (s2) rejected
  (repurposed from the old test that asserted the now-illegal behavior).
- 1119-diagnostics-reserved-type-name-as-identifier: parameter (u8),
  typed-local (s64, bool), `:=` (string) forms rejected.
- 0135-types-self-streaming-nonreserved: positive — `*self` streaming
  with non-reserved names accumulates correctly via both call styles.
- 0904-optionals: renamed incidental locals s1/s2 -> filled/empty.
2026-06-03 19:00:39 +03:00
agra
4ab3608f77 Merge branch 'docs/trace-output-repair' 2026-06-03 16:55:00 +03:00
agra
99a5c781a0 docs: fix stale error-trace output format + markers
The trace docs predated the current formatter. Corrected against the real
output (library/modules/trace.sx to_string + examples/expected/1025-errors-
trace-format.stderr):
- error-handling.md: replace the obsolete trace example ("error trace:" /
  "raised error.X" / "at func (file:line)") with the real format —
  "error return trace (most recent call last):" + per-frame "func at
  file:line:col" + source line + caret.
- debugger.md: drop the stale "(planned)" marker on the trace formatter
  (it is implemented); the tag-name table note now cites the failable-main
  reporter's "unhandled error reached main: error.X" line, not a
  nonexistent "raised error.X" trace line.
2026-06-03 16:54:36 +03:00
agra
973543ddf8 Merge branch 'arch-refactor' 2026-06-03 16:34:16 +03:00
agra
1148362353 Merge branch 'flow/sx-plan-arch/fix-0075' into arch-refactor 2026-06-03 16:12:39 +03:00
agra
aca077d720 fix(reflection): replace silent .s64 arg-type fallback with loud .unresolved (issue 0075)
The `type_name` / `type_eq` reflection builtins resolved their Type arg's IR
type via `getRefIRType(...) orelse TypeId.s64`, then gated `== .any`. A failed
must-succeed lookup silently became `.s64` (`!= .any`), classifying a boxed
`Any` arg as bare i64 and reading the wrong value with no diagnostic.

Add the sibling classifier `LLVMEmitter.reflectArgRepr`, which routes the
lookup through `argIRTypeOrFail` (the issue-0074 `.unresolved` resolver) and
returns `{ boxed, bare, unresolved }`. The three emit sites in ops.zig
(`type_name` + `type_eq` x2) now switch on it: `.boxed` extracts the Any value
field, `.bare` uses the value directly, `.unresolved` hits a hard `@panic`
tripwire — never silently treated as bare. Real args always resolve, so the
happy path is byte-identical (suite stays 361/0, zero snapshot churn).

Secondary `lower.zig` `null_literal`/`undef_literal => target_type orelse .void`
confirmed intentional (typeless-literal default deliberately handled by
emitConstNull/emitConstUndef as null-ptr / undef-i64) — left with an invariant
comment, not the `.unresolved` tripwire.

Regression test in emit_llvm.test.zig asserts the loud path: fail-before with
`orelse .s64` yields `.bare`; pass-after yields `.unresolved`.
2026-06-03 16:05:31 +03:00
agra
759e3caa5e Merge branch 'flow/sx-plan-arch/fix-0074' into arch-refactor 2026-06-03 15:55:39 +03:00
agra
633c0a2540 docs(issues): file 0075 — silent .s64 type fallback in reflection builtins
Discovered during the 0074 fix + a codebase-wide silent-type-fallback sweep.
getRefIRType(...) orelse TypeId.s64 at ops.zig:1023/1049/1055 (type_name/type_eq).
Blocker; to be resolved before the arch-refactor stream closes.
2026-06-03 15:55:32 +03:00
agra
4537538bb2 fix(ffi): replace silent .void arg-type fallback with loud .unresolved (issue 0074)
Four FFI call-arg lowering sites resolved an argument's IR type via
`getRefIRType(arg_ref) orelse .void` — a silent fallback to the load-bearing
real type `.void`. A failed lookup there is a codegen invariant violation, but
`.void` is treated by downstream `toLLVMType` → `abiCoerceParamType` →
`coerceArg` as a legitimate void-typed foreign argument, corrupting the call
ABI with no diagnostic.

Add one shared resolver `LLVMEmitter.argIRTypeOrFail` that returns the
dedicated `.unresolved` sentinel on a failed lookup — never `.void`/`.s64` — so
the failure cannot masquerade as a real type and trips `toLLVMType`'s existing
hard `@panic` tripwire at the call site. Route all four sites through it:
  - src/ir/emit_llvm.zig          JNI constructor (NewObject) arg loop
  - src/backend/llvm/ops.zig      objc_msgSend arg loop
  - src/backend/llvm/ops.zig      JNI non-virtual call arg loop
  - src/backend/llvm/ops.zig      JNI Call<Type>Method arg loop

Happy path is byte-identical (every real arg already has a resolved type); FFI
examples stay green with zero snapshot churn.

Regression test (fail-before/pass-after) in src/ir/emit_llvm.test.zig asserts an
unresolvable FFI arg ref now yields `.unresolved`, not the old silent `.void`.
2026-06-03 15:43:27 +03:00
agra
6f4b872254 Merge branch 'flow/sx-plan-arch/A9.2' into arch-refactor 2026-06-03 15:19:02 +03:00
agra
a7ddbeb85b docs(error-handling): trace locations come from embedded Frame metadata, not DWARF (A9.2) 2026-06-03 15:02:09 +03:00
agra
e5d9d1fec1 docs(debugger): correct interp push-call model and span.start term (A9.2)
The interp's .trace_frame op only yields the packed value; the separate
sx_trace_push call op is executed by the interp as a foreign call via
host_ffi/dlsym, so the prior 'no sx_trace_push call runs' / 'never calls
sx_trace_push' phrasing was wrong. The packed low word is the op's
span.start (a source byte offset), not an IR instruction offset; renamed
every ir_offset/offset reference to span.start.
2026-06-03 14:49:23 +03:00
agra
0e5b79ddab docs(debugger): call getFrameStructType a literal (anonymous) struct type (A9.2) 2026-06-03 14:36:08 +03:00
agra
e907fc9e01 docs(debugger): describe Frame global build as LLVMConstNamedStruct over getFrameStructType (A9.2)
The compiled backend builds each trace Frame global as an LLVM named-struct
constant over the cached getFrameStructType() layout (file, line, col, func,
line_text) via LLVMConstNamedStruct -- a type-safe LLVM struct, not the sx
Frame TypeId / normal struct-emission path. Also correct the file field to
the source basename (full paths live in DWARF).
2026-06-03 14:28:28 +03:00
agra
e6c51359fe docs(debugger): align trace-push mechanism to one ground-truth model (A9.2)
The .trace_frame op is niladic: it carries no operand and no GlobalId.
The compiled backend yields the interned Frame global's address as the
op's value (reflection.emitTraceFrame); the interpreter yields a packed
(func_id, ir_offset) as the op's value and never calls sx_trace_push
(recovered later by .trace_resolve). The sx_trace_push call is a separate
call op emitted by lower.zig at each push site, consuming the op's value.

Reword every passage that stated the old/wrong model: the niladic
invariant is about the op (not the push site emitting only one
instruction); reflection yields the op's value rather than lowering a
push; the interp returns the packed value rather than calling the foreign
sx_trace_push via host_ffi dlsym.
2026-06-03 14:17:24 +03:00
agra
5cb1691265 docs(debugger): correct trace-frame op name and sx_trace_push attribution (A9.2)
Name the niladic op `.trace_frame` (no `.trace_frame_push` op exists) in
the trace-path roadmap, matching the rest of the doc and src/ir/inst.zig.
Describe the `.trace_frame` arm as building/interning the Frame global and
yielding its address as the op's value; the separate sx_trace_push call is
emitted by the lowerer via normal call lowering, not by the arm itself.
2026-06-03 14:03:44 +03:00
agra
badf2af298 docs(debugger): point DWARF/Frame wiring at backend/llvm helpers (A9.2)
Refresh the debugging architecture reference for the A7.2 relocation:
DWARF emission lives in src/backend/llvm/debug.zig (DebugInfo) and the
interned Frame / tag-name tables in src/backend/llvm/reflection.zig
(Reflection); emit_llvm.zig is the orchestrator that owns LLVMEmitter and
dispatches to them. Behavior is unchanged; only the file-and-function map,
the 'what's emitted' home, and the debugEnabled() owner are corrected.
2026-06-03 13:52:38 +03:00
agra
d319cef367 Merge branch 'flow/sx-plan-arch/A8.2' into arch-refactor 2026-06-03 13:30:25 +03:00
agra
e13dbfeb94 refactor(types): shrink src/types.zig to editor/parse metadata (A8.2)
Remove the legacy parallel type model's compiler-like surface. The
compiler pipeline resolves/lowers/lays out against canonical
src/ir/types.zig (TypeId/TypeTable); src/types.zig.Type is now strictly
editor-indexing + parse-time name metadata.

- src/types.zig: delete the type-resolution surface (widen, bitWidth,
  isImplicitlyConvertibleTo) and every helper left dead once it was gone
  (eql, isInt/isFloat/isSigned/isUnsigned, isTuple/isVector, and the
  already-unused classification predicates isEnum/isUnion/isString/
  isStringLike/isAny/optionalChild/sliceElementType/manyPointerElementType/
  vectorElementType/isFunctionType/isClosureType/isCallable). Keep the Type
  union plus the display/name-classification helpers sema/lsp/parser use
  (fromName, fromTypeExpr, toName, displayName, isStruct/isOptional/isSlice/
  isPointer/isManyPointer/isArray, pointerPointeeType). Seal the file with a
  doc comment.
- src/sema.zig: inferExprType no longer calls Type.widen for arithmetic;
  it approximates the display type as the left operand's (no second
  resolver in the editor index).
- src/ir/type_bridge.zig: delete the dead bridgeType (legacy Type -> TypeId)
  function + its sole sx_types import; resolveAstType and the AST->TypeId
  path are untouched.
- src/ir/ir.zig: drop the bridgeType re-export.
- src/ir/type_bridge.test.zig: drop the two bridgeType tests (function gone).

Gate: zig build, zig build test (exit 0), tests/run_examples.sh 361/0,
zero examples/expected churn.
2026-06-03 13:21:00 +03:00
agra
d998e2809e Merge branch 'flow/sx-plan-arch/A8.1' into arch-refactor 2026-06-03 13:05:02 +03:00
agra
f52a24a0fb refactor(sema): seal sema.zig as editor indexing only (A8.1)
Remove the last compiler dependency on sema as semantic truth and stop
publishing as-you-type sema diagnostics from the LSP.

- core.zig: drop dead `Compilation.analyze()`, the `sema_result` field,
  and the sema->diagnostics merge; drop the now-orphaned sema import.
  The CLI pipeline (parse -> resolveImports -> generateCode) never called
  analyze(), so this removes only dead code.
- lsp/server.zig: rename `analyzeAndPublish` -> `refreshEditorIndex` and
  delete its sema-diagnostic publish (and the now-unused `semaToLspDiags`).
  The editor index (doc.sema) is still refreshed for nav/refs/completion/
  tokens. On-save/on-open diagnostics still come solely from the canonical
  compiler pipeline in `runProjectCheck` (unchanged).
- Document sema as an editor-indexing API (doc.sema field comment).

Intended behavior change: as-you-type sema diagnostics no longer publish;
on-save canonical diagnostics are the sole source. CLI compile output and
the 361-example suite are unchanged (361/0, zero snapshot churn).
2026-06-03 12:56:28 +03:00
agra
b7fce30f42 Merge branch 'flow/sx-plan-arch/A7.4e' into arch-refactor 2026-06-03 12:47:40 +03:00
agra
0e7bae563a refactor(backend): drain remaining emitInst handlers into ops.zig (A7.4 slice e)
Move the final inline emitInst handler groups (terminators, box/unbox-Any,
reflection, switch-branch, closure-creation, vector, block-param, misc) into
the Ops facade in src/backend/llvm/ops.zig. emitInst is now pure dispatch:
every arm delegates to self.ops().*, leaving only setInstDebugLocation plus
one-line delegations.

Widen the shared infra the moved bodies reach (emitFailableMainRet, getBlock,
anyTag, isSignedTypeEx, coerceToI64/coerceToI64Signed/coerceFromI64,
emitFieldValueGet) to pub on LLVMEmitter; helper and ref-tracking sections
stay put. Pure relocation: emitted LLVM IR byte-identical, zero snapshot churn.
2026-06-03 12:41:39 +03:00
agra
3152abcb57 Merge branch 'flow/sx-plan-arch/A7.4d' into arch-refactor 2026-06-03 12:33:13 +03:00
agra
1be16511ec refactor(backend): move aggregate handlers into ops.zig (A7.4 slice d)
Relocate the struct, enum, union, array/slice, tuple, and optional
opcode handler bodies out of emitInst into the existing Ops facade.
Each moved arm now delegates via self.ops().emit<Op>(...); shared infra
stays on LLVMEmitter, with resolveAggregate/resolveGepStructType widened
to pub as the GEP handlers require. Pure relocation, behavior-preserving:
zero snapshot churn (361/0).
2026-06-03 12:03:45 +03:00
agra
e58d2a1eed Merge branch 'flow/sx-plan-arch/A7.4c' into arch-refactor 2026-06-03 11:53:10 +03:00
agra
5388895b3e refactor(backend): move call + call-extension handlers into ops.zig (A7.4 slice c)
Relocate the Calls (objc_msg_send / jni_msg_send / call / call_indirect)
and Call-extensions (call_builtin / compiler_call / call_closure) emitInst
handler groups out of emit_llvm.zig into the existing Ops facade. Each
emitInst arm now delegates via self.ops().emit<Op>(...). Behavior-preserving
pure relocation; emitted LLVM IR is byte-identical (361/0 examples, no
snapshot churn).

Shared call infra stays on LLVMEmitter, widened pub only as the moved
bodies require: extractSlicePtr, loadJniFn, getObjcMsgSendValue, the math
F32/F64 declarators + types, getOrDeclareWrite/getWriteType, ffiCtors,
materializeByvalArg, emitCStringGlobal, emitJniConstructor, and the Jni
slot-offset constants. emitJniConstructor remains in emit_llvm.zig (A7.3
decision); the moved jni arm calls it via self.e.emitJniConstructor(...).
2026-06-03 11:45:30 +03:00
agra
e1d86e0144 Merge branch 'flow/sx-plan-arch/A7.4b' into arch-refactor 2026-06-03 11:33:06 +03:00
agra
b4faefa607 refactor(backend): move memory/globals/conversion/pointer handlers into ops.zig (A7.4 slice b)
Relocate the `// ── Memory ──`, `// ── Globals ──`, `// ── Conversions ──`,
and `// ── Pointer ops ──` opcode handler bodies out of `emitInst` in
src/ir/emit_llvm.zig into the existing `Ops` facade in
src/backend/llvm/ops.zig. Each `emitInst` arm now delegates via
`self.ops().emit<Op>(...)`. Widen `emitConversion`, `coerceArg`, and
`getRefIRType` to `pub` (the only helpers the moved bodies call).

Pure relocation: zero snapshot churn.
2026-06-03 11:26:31 +03:00
agra
fb19cf9e83 Merge branch 'flow/sx-plan-arch/A7.4a' into arch-refactor 2026-06-03 11:20:45 +03:00
agra
312d2e90ed refactor(backend): extract scalar instruction handlers into ops.zig (A7.4 slice a)
Move the Constants/Arithmetic/Bitwise/Comparisons/Logical opcode handler
bodies out of emitInst into a new Ops facade in src/backend/llvm/ops.zig.
emitInst's scalar arms now delegate via self.ops().*; the shared infra they
call (mapRef/resolveRef/matchBinOpTypes/emitCmp/emitCmpOrdered/emitStrCmp/
emitStringConstant/reflection + isFloatOrVecFloat/isSignedType) stays on
LLVMEmitter, widened to pub as needed. Pure relocation: zero snapshot churn.
2026-06-03 11:11:10 +03:00
agra
2f7c99fd11 refactor(backend): extract JNI slot cache into ffi_ctors.zig (A7.3 slice 2a)
Move getOrCreateJniSlots (the cls/methodid slot-cache builder) out of
emit_llvm.zig into the FfiCtors backend *LLVMEmitter facade. Behavior-preserving
— self.* -> self.e.* only.

- FfiCtors gains getOrCreateJniSlots (pub). The jni_slots cache + mangleJniKey
  stay on LLVMEmitter; mangleJniKey is widened to pub (the facade calls it back,
  like lazyDeclareCRuntime/emitPrivateCString), and JniSlotPair is widened to pub
  (the facade returns it; the call site consumes it). 1 call site routed via
  ffiCtors().
- emitJniConstructor intentionally NOT moved in this slice: it is emission-heavy
  (resolveRef/mapRef/coerceArg/getRefIRType/extractSlicePtr/loadJniFn/
  emitCStringGlobal — 100+ internal callers for the first two), so relocating it
  would pub-expose the emitter's core value-emission machinery. Consistent with
  A7.2 keeping emitFieldValueGet in emit_llvm.zig. Pending an explicit decision.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0
(JNI anchors 1402/1408/1418/1425 green, no churn).
2026-06-03 10:42:58 +03:00
agra
e8c33bfc00 refactor(backend): extract Obj-C runtime constructors into src/backend/llvm/ffi_ctors.zig (A7.3 slice 1)
Move the Obj-C module-init constructor emission out of emit_llvm.zig into a
FfiCtors backend *LLVMEmitter facade (field `e`). Behavior-preserving relocation
— self.* -> self.e.* only.

- src/backend/llvm/ffi_ctors.zig (FfiCtors): emitObjcSelectorInit (cached SEL
  init), emitObjcClassInit (objc_getClass class-object cache), and
  emitObjcDefinedClassInit (class-pair registration: ivars, method IMP table,
  +alloc/-dealloc IMPs, #implements protocol conformances). Emit-time caches
  (ir_mod.objc_*_cache) + global_map + cached LLVM handles read via self.e.*.
- 3 call sites in LLVMEmitter.emit routed via a new ffiCtors() accessor.
- Shared infra stays in emit_llvm.zig, widened to pub (the facade calls back):
  lazyDeclareCRuntime (11 callers), emitPrivateCString (11 callers),
  injectCtorIntoMain (the moved defined-class ctor's callee). No @llvm.global_ctors
  shape / IMP-table / ivar / protocol-conformance change.

Pins: 1309 (class-method lowering), 1319 (property getter/setter IMPs), 1314
(alloc/dealloc IMPs), 1332 (sret + addMethod) all green.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0 (no churn).
2026-06-03 10:35:15 +03:00
agra
836583d7f7 test(backend): pin JNI constructor (FindClass/<init>/NewObject) before A7.3 extraction (A7.3 scaffolding coverage fix)
Codex review of 91651e3 noted no .ir snapshot pinned emitJniConstructor's distinct
FindClass -> GetMethodID("<init>") -> NewObject shape; 1402/1418/1408 cover regular/
static GetMethodID slot caching, not constructor emission.

Add examples/expected/1425-ffi-jni-main-03-ctor.ir (FindClass x4 / GetMethodID x4
/ NewObject x2 / <init>), path-free + idempotent, trailing newline trimmed. Suite
count unchanged (snapshot on an existing example).

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0
(git diff --check clean; only the intended ctor snapshot added).
2026-06-03 10:26:45 +03:00
agra
91651e3e56 test(backend): pin Obj-C property + alloc/dealloc IMP ctors before A7.3 extraction (A7.3 scaffolding)
Backend-FFI .ir inventory + scaffolding for the Obj-C/JNI runtime-constructor
extraction (Phase A7.3). No code moved.

Inventory (recorded in ARCH-SAFETY.md): the existing FFI .ir set already pins the
core constructor emission — emitObjcSelectorInit (sel_registerName via 1309/1329/
1332), emitObjcClassInit (objc_getClass), emitObjcDefinedClassInit class
registration + ivars + method IMP table (objc_allocateClassPair / class_addIvar /
class_addMethod / objc_registerClassPair via 1309/1332), and getOrCreateJniSlots /
emitJniConstructor (GetMethodID via 1402/1418/1408).

Gaps closed (2 new .ir snapshots) for the ARCH-SAFETY-named metadata not covered
by 1309:
- 1319-ffi-objc-property-sx-defined: property getter/setter IMPs (_get/_set/
  class_addMethod x8).
- 1314-ffi-objc-class-dealloc-roundtrip: alloc/dealloc IMPs.
Both path-free + idempotent (verified across two captures; trailing newline
trimmed). Suite count unchanged (snapshots on existing examples).

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0 (no churn
beyond the 2 new .ir).
2026-06-03 09:39:16 +03:00
agra
46b874074b refactor(backend): extract reflection metadata + trace frames into src/backend/llvm/reflection.zig (A7.2 reflection)
Move the type/field/tag reflection name-array builders and the error-trace Frame
builders out of emit_llvm.zig into a Reflection backend *LLVMEmitter facade
(field `e`). Behavior-preserving relocation — self.* -> self.e.* only.

- src/backend/llvm/reflection.zig (Reflection): getOrBuildTypeNameArray /
  getOrBuildFieldNameArray / getOrBuildTagNameArray (pub) + emitTraceFrame (pub)
  + buildStringConst (private trace helper). The memoized state
  (type_name_array(_len) / field_name_arrays / tag_name_array / frame_str_cache)
  stays on LLVMEmitter; the facade reads/writes via self.e.*.
- Routed the 5 call sites through a new reflection() accessor (type_name /
  field_name / error_tag_name builtins, emitFailableMainRet's tag-name lookup,
  and the .trace_frame push).
- Kept in emit_llvm.zig per the A6.1 "emission-heavy stays" precedent:
  getFrameStructType (composite-type getter, widened to pub — emitTraceFrame calls
  it back), emitFieldValueGet (field-value reflection EMISSION, not an array
  builder), emitFailableMainRet. getStringStructType/getAnyStructType already pub.
- No reflection-array layout, trace-Frame field order, or linkage change.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0 (reflection
anchors 0030/0118/0517/0520 + trace anchors 1024/1025/1026 all ok, no churn).
2026-06-03 09:29:27 +03:00
agra
f92a743c85 refactor(backend): extract DWARF debug info into src/backend/llvm/debug.zig (A7.2 debug)
Move the DWARF debug-info emission out of emit_llvm.zig into a DebugInfo backend
*LLVMEmitter facade (field `e`). Behavior-preserving relocation — self.* ->
self.e.* only.

- src/backend/llvm/debug.zig (DebugInfo): debugEnabled + diFileFor (private) +
  initDebugInfo / beginFunctionDebug / endFunctionDebug / setInstDebugLocation /
  finalizeDebugInfo (pub). The mutable DI state (di_builder/di_cu/di_files/
  di_scope/current_func_file) + the shared source map (import_sources/main_file)
  stay on LLVMEmitter; the facade reads/writes them via self.e.*.
- Routed the 5 pass-order call sites in LLVMEmitter.emit (init/finalize/
  begin/end/setInstDebugLocation) through a new debugInfo() accessor.
- setDebugContext stays on LLVMEmitter (shared-state setter; callers in main.zig/
  core.zig/test). sourceForFile stays on LLVMEmitter and is widened to pub — it is
  shared with reflection's trace-frame emission (emitTraceFrame), not debug-only.
- No DI logic / module-flag / DWARF-version / scope-line change.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0 (no churn).
2026-06-03 09:22:40 +03:00
agra
71f1cb2fb0 refactor(backend): extract LLVM type/ABI lowering into src/backend/llvm/ (A7.1 step 2)
Move the LLVM type-mapping and C-ABI coercion helpers out of emit_llvm.zig into
the first src/backend/llvm/ modules. Behavior-preserving relocation — the only
rewrites are module plumbing and self.* -> self.e.* facade access.

- src/backend/llvm/types.zig (TypeLowering): toLLVMType + toLLVMTypeInfo.
- src/backend/llvm/abi.zig (AbiLowering): abiCoerceParamType / abiCoerceParamTypeEx
  / needsByval / materializeByvalArg.
- Both are backend *LLVMEmitter facades (field `e`) — the backend analogue of the
  IR-side *Lowering facades, NOT a *Lowering facade. They reach the cached LLVM
  handles, IR type table, module data layout, builder, and the memoizing
  composite-type getters via self.e.*.
- LLVMEmitter stays the facade: toLLVMType (~97 callers) + abiCoerceParamType /
  abiCoerceParamTypeEx / needsByval / materializeByvalArg kept as thin wrappers
  delegating through new typeLowering()/abiLowering() accessors. Zero caller
  churn. toLLVMTypeInfo deleted (sole caller moved).
- Widened getStringStructType / getAnyStructType / getClosureStructType to pub
  (the moved toLLVMTypeInfo calls them back; their memoization stays on
  LLVMEmitter). verifySizes stays in emit_llvm.zig (size-assertion pass, not type/
  ABI lowering). No ABI/type logic, branch order, diagnostic text, or snapshot
  changed. Circular import (emit_llvm <-> backend/llvm) resolves via the pointer
  facade.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0
(1202 .ir + the 2 ABI unit tests unchanged, no churn).
2026-06-03 09:10:27 +03:00
agra
e50caa4628 test(backend): trim trailing blank lines in 1202 .ir snapshot (A7.1 scaffolding fix)
Codex review of d6078c2 flagged a blank line at EOF in the new
examples/expected/1202-ffi-cc-c-large-aggregate.ir. Collapse the trailing
newlines to a single one so `git diff --check` is clean. Test-safe: the runner
reads both expected and actual IR through $(...) command substitution, which
strips trailing newlines, so the comparison is unaffected (1202 still ok).

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0.
2026-06-03 08:59:26 +03:00
agra
d6078c2e6b test(backend): lock LLVM type/ABI shapes before A7.1 extraction (A7.1 scaffolding step 1)
Test-first scaffolding for LLVM backend modularization (Phase A7.1) before the
type/ABI helpers move into src/backend/llvm/{types,abi}.zig. Visibility-only
change to the targets — no behavior change. Closes the ARCH-SAFETY "no generic
ABI snapshot" gap.

- 2 new emit_llvm.test.zig tests:
  - abiCoerceParamType across every C-ABI size bucket: <=8 -> i64, 9-16 ->
    [2 x i64], >16 -> ptr, HFA (all-float/all-double, <=4 fields) -> unchanged,
    string -> ptr, slice -> ptr, scalar -> unchanged. Built via a local
    internStruct helper (field slice in the module arena -> no testing-allocator
    leak); asserts against emitter.cached_* + LLVMArrayType2.
  - needsByval: true only for >16-byte non-HFA struct; false for <=16 / HFA /
    string / slice / non-struct.
- 1 new .ir snapshot: 1202-ffi-cc-c-large-aggregate (the canonical callconv(.c)
  >16-byte byval example that directly documents abiCoerceParamType) — pins the
  byval param path end-to-end (5 byval + entry reload + 2 sret from Arena.init).
  Path-free + idempotent (verified across two captures). Suite count unchanged
  (snapshot added to an existing example).
- Widened abiCoerceParamType + needsByval to pub (visibility only;
  abiCoerceParamTypeEx/materializeByvalArg/verifySizes stay private — move with
  callers in sub-step 2). No logic touched.
- Recorded the A7.1 coverage inventory + residual gaps (wasm32 usize->i32 branch,
  fn-ptr large-aggregate 1203/1204) in ARCH-SAFETY.md.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0 (no churn
beyond the new 1202 .ir).
2026-06-03 08:53:51 +03:00
agra
91b300580b Merge Phase A6 (FFI domain extraction) into master
Extracts the pure FFI decision helpers out of lower.zig into domain modules,
behavior-preserving, test-first (scaffolding commit locks behavior, extraction
commit moves code), gated on zig build / zig build test / tests/run_examples.sh.

- A6.1 — Obj-C decision helpers → src/ir/ffi_objc.zig (ObjcLowering, a *Lowering
  facade): deriveObjcSelector, objcTypeEncodingFromSignature (+appendObjcEncoding/
  bailObjcEncoding/ObjcEncodingStack), objcPropertyKind (+ObjcPropertyKind enum),
  isObjcClassPointer, objcDefinedStateStructType (+objcStateAllocatorType).
- A6.2 — pure JNI helpers → src/ir/jni_descriptor.zig (plain pub free fns, no
  facade): jniMangleNativeName, isJniReturnTypeSupported. (Descriptor derivation
  was already there.)

Emission-heavy code (emitObjc*/lowerObjc*Call, synthesizeJniMainStub*/lowerJni*)
stays in lower.zig per PLAN A6 step 6; Java rendering stays in jni_java_emit.zig.

Regression anchors: 48 13xx Obj-C + 26 14xx JNI examples, 4 Obj-C + 9 JNI .ir
snapshots, ObjC/JNI helper unit tests. Gate at merge tip: zig build,
zig build test, tests/run_examples.sh -> 361/0.
2026-06-03 08:35:38 +03:00
agra
20c767e336 refactor(ir): move pure JNI helpers into jni_descriptor.zig (A6.2 step 2)
Relocate the two pure JNI decision helpers out of lower.zig into
jni_descriptor.zig (already the JNI helper module), alongside the descriptor
derivation. Behavior-preserving move — no facade, since neither takes *Lowering.

- jniMangleNativeName(allocator, foreign_path, method_name) and
  isJniReturnTypeSupported(table, ret_ty) moved verbatim as pub free fns; added a
  types import + TypeId alias to jni_descriptor.zig.
- Rerouted lower.zig's 2 call sites (synthesizeJniMainStub; the JNI return-type
  guard at lower.zig:6000) through jni_descriptor.* — lower.zig already imported
  the module.
- Moved the 2 unit tests lower.test.zig -> jni_descriptor.test.zig (re-pointed to
  desc.*; a standalone TypeTable.init replaces the Module setup). Dropped the
  now-unused lower_mod alias.
- Stayed in lower.zig per PLAN A6.2 step 5/6: jniMapParamType (trivial resolveType
  wrapper), synthesizeJniMainStub(s), lowerJniCall, lowerJniConstructor,
  lowerSuperCall, getJniEnvTlFids. Java rendering stays in jni_java_emit.zig.
  Phase A6 complete.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0
(9 JNI .ir snapshots + 26 14xx examples green, no churn).
2026-06-03 08:28:41 +03:00
agra
0a4a240e31 test(ir): lock pure JNI decision helpers before A6.2 extraction (A6.2 scaffolding step 1)
Test-first scaffolding for the JNI FFI domain (Phase A6.2) before the pure
helpers move out of lower.zig. Visibility-only change — no behavior change.

- 2 new lower.test.zig tests for the pure JNI helpers lacking unit coverage:
  - jniMangleNativeName: `/`->`_` separator, `_`->`_1` escape (path AND method),
    `Java_` prefix, `_sx_1` infix (2 cases lock all rules).
  - isJniReturnTypeSupported: void/bool/s32/s64/f32/f64 + pointer/many-pointer
    -> true; other widths (s8/s16/u8/u32/u64) + by-value struct -> false.
- JNI descriptor derivation (writeType/deriveMethod) is already extracted into
  jni_descriptor.zig (15 tests) — not part of A6.2.
- Widened jniMangleNativeName -> pub (file-scope free fn; isJniReturnTypeSupported
  already pub). Reached from the test via ir_mod.lower.*. No logic touched.
- Recorded the A6.2 coverage inventory + residual emission-bound gaps
  (synthesizeJniMainStub*/lowerJniCall/lowerJniConstructor/lowerSuperCall/
  getJniEnvTlFids stay in lower.zig; jniMapParamType is a trivial resolveType
  wrapper) in ARCH-SAFETY.md.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0
(no .ir churn; 9 JNI .ir snapshots green).
2026-06-03 08:14:46 +03:00
agra
9bde1dd590 refactor(ir): extract ObjcLowering (ffi_objc.zig) for pure Obj-C decision helpers (A6.1 step 2)
Move the pure Obj-C decision helpers out of lower.zig into src/ir/ffi_objc.zig
behind an ObjcLowering *Lowering facade (Principle 5, like the A4/A5 resolvers).
Behavior-preserving relocation — the only non-self.l rewrites are facade
plumbing.

Moved verbatim (self. -> self.l. for Lowering members):
- deriveObjcSelector (selector derivation)
- objcTypeEncodingFromSignature + appendObjcEncoding + bailObjcEncoding +
  the ObjcEncodingStack type
- objcPropertyKind + the ObjcPropertyKind enum
- isObjcClassPointer
- objcDefinedStateStructType + objcStateAllocatorType

Emission-heavy code stays in lower.zig per PLAN A6.1 step 6: emitObjc* IMP
builders, lowerObjc*Call, registerObjc*, declareObjc*, the lookupObjc* property/
state lookups, and the Self-substitution resolvers.

- Call sites rerouted through a new objc() accessor: 15 in lower.zig, 1 in
  expr_typer.zig, 39 in lower.test.zig (the A6.1 scaffolding tests now drive the
  facade). No Lowering wrappers kept. Barrel-wired ffi_objc + ObjcLowering.
- No new visibility widening beyond sub-step 1's two pubs — the facade reads
  self.l.{alloc,module,program_index,diagnostics} (fields) + the already-pub
  resolveType. lower.zig -478 (->16615); ffi_objc.zig 428.
- Doc-only re-home: the property-IMP getter/setter comment was attached (a
  pre-existing artifact) to the moving ObjcPropertyKind enum, two decls away from
  its real subject emitObjcDefinedClassPropertyImps (which had no doc). Re-homed
  it there so the move neither orphans a `///` block (Zig errors on a dangling doc
  comment) nor misattributes it to ensureArcRuntimeDecls.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0
(48 13xx Obj-C examples + 4 Obj-C .ir snapshots green, no churn).
2026-06-03 08:00:42 +03:00
agra
b5119e8587 test(ir): cover Obj-C protocol pointers in isObjcClassPointer/objcPropertyKind (A6.1 scaffolding review fix)
Codex review of 0012228 noted isObjcClassPointer's contract is
`fcd.runtime == .objc_class or fcd.runtime == .objc_protocol`, but the new tests
only exercised the class case. Test-only fix (no visibility/behavior change —
still exactly the two pub widenings from the parent commit):

- isObjcClassPointer: add a *NSCopying case where NSCopying is a registered
  .objc_protocol foreign class -> true (alongside the .objc_class *NSString case).
- objcPropertyKind: add a *NSCoding protocol-pointer field -> strong default
  assertion, since it uses the same class/protocol object-pointer predicate.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0.
2026-06-03 07:45:10 +03:00
agra
0012228796 test(ir): lock pure Obj-C decision helpers before A6.1 extraction (A6.1 scaffolding step 1)
Test-first scaffolding for the Obj-C FFI domain (Phase A6.1) before the pure
helpers move into src/ir/ffi_objc.zig. Visibility-only change to the targets —
no behavior change.

- 3 new lower.test.zig tests for the pure helpers the ARCH-SAFETY A6.1 row names
  that lacked direct unit coverage:
  - deriveObjcSelector: niladic (bare name) / single-keyword (name:) /
    multi-keyword (_ -> : + trailing) / #selector(...) override (verbatim,
    keyword_count = #colons).
  - objcPropertyKind: assign default (primitive), strong default (object ptr),
    explicit weak/copy/assign win over the default.
  - isObjcClassPointer: pointer-to-foreign-Obj-C-class true; plain-struct ptr /
    *void / builtin false.
- objcTypeEncodingFromSignature (x6) + objcDefinedStateStructType (x3) already
  covered — no new tests.
- Widened deriveObjcSelector + objcPropertyKind to pub (they become facade
  methods in step 2; the ObjcPropertyKind enum stays private — tests compare via
  enum-literal == .strong). No logic touched.
- Recorded the A6.1 coverage inventory + residual gaps (resolveObjcParentName,
  class-method metadata, property/state lookups — example-guarded) in
  ARCH-SAFETY.md.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0
(no .ir churn; Obj-C snapshots 1309/1329/1332/1347 green).
2026-06-03 07:15:56 +03:00
agra
d346bbb677 Merge arch-refactor through Phase A5 into master
Brings the compiler-architecture refactor from the Phase A2 merge (7cc8057)
up through Phase A5. Each phase converged a compiler question onto a single
canonical owner, behavior-preserving, test-first (scaffolding commit locks
behavior, extraction commit moves code), gated on
zig build / zig build test / tests/run_examples.sh at every step.

- A2.4 — unknown-type diagnostic pass → semantic_diagnostics.zig.
- Issues 0068–0073 — forward type aliases, global/module-const initializers,
  non-constant global diagnostics, closure-in-defer lowering segfault.
- A3.1 — non-call expression typing → ExprTyper (expr_typer.zig).
- A3.2 — call result typing + classification → CallResolver/CallPlan (calls.zig);
  lowering shares the namespace/value boundary.
- A4.1 — generic substitution + mono keys → GenericResolver (generics.zig).
- A4.2 — protocol/impl lookup + registration + planning → ProtocolResolver
  (protocols.zig).
- A4.3 — coercion/xx classification → CoercionResolver (conversions.zig).
- A5.1 — error-set convergence → ErrorAnalysis (error_analysis.zig).
- A5.2 — path-sensitive error-flow diagnostics → ErrorFlow (error_flow.zig).

Regression anchors incl. examples 1046–1053 for the error streams.
Gate at merge tip: zig build, zig build test, tests/run_examples.sh -> 361/0.
2026-06-03 07:01:19 +03:00
agra
1f354f6da0 refactor(ir): extract ErrorFlow (error_flow.zig) for path-sensitive error-flow diagnostics (A5.2 step 2)
Move the diagnostic-only Pass 1e (ERR E1.7 cleanup-absorption + E1.8 value-slot
liveness) out of lower.zig into src/ir/error_flow.zig behind an ErrorFlow
*Lowering facade (Principle 5, like ErrorAnalysis/CoercionResolver). Behavior
preserved exactly — pure relocation.

Moved verbatim (self. -> self.l. for Lowering members; sibling calls stay on the
facade; provenHas is a file-local free fn): checkErrorFlow, analyzeFnBody,
flowWalk, flowStmt, flowIf, flowMatch, flowExpr, applyRefinement,
provenAdd/provenClone/provenIntersect, registerFailableDestructure,
checkCleanupBody/checkCleanupNode/cleanupReject, plus the FlowCtx/ProvenSet types.

- lowerRoot routes the single call site through
  self.errorFlow().checkErrorFlow(decls); no Lowering wrapper kept (only the
  pipeline calls it, no unit-test caller). New errorFlow() accessor.
- The pass takes AST decls + ProgramIndex + diagnostics only — independent of IR
  Builder state (PLAN-ARCH A5.2 success criterion).
- New pub: exprIsFailable (only widening; inferExprType/errorChannelOf already
  pub). lower.zig -389 (->17030); error_flow.zig 407. Barrel-wired in ir.zig.
- No .test.zig: diagnostic-pass altitude (functions return only bool + emit
  diagnostics) — guarded by example anchors 1046-1053 (incl. scaffolding
  1051/1052/1053). Phase A5 complete.

Gate: zig build, zig build test, bash tests/run_examples.sh -> 361/0
(anchors 1046-1053 all ok, no .ir churn).
2026-06-03 06:54:13 +03:00
agra
2d2bfafa29 test(ir): pin both lambda arms of the error-flow pass (A5.2 scaffolding review fix)
Codex review of 95895a3 found 1051 reached neither lambda arm it claimed to
pin: the lambda arrived only as a var_decl initializer, which routes through
checkCleanupNode's `.var_decl` arm -> cleanupReject(lambda) -> early-return
(a lambda literal is not failable), so the `.lambda` stop never ran; and its
accepted-direction `if !err` guard would still pass with flowExpr's lambda
recursion removed.

Scaffolding-only fix (no compiler change):
- 1051: add a bare lambda STATEMENT `() -> !E { failing(); };` in the cleanup
  body so checkCleanupNode sees a `.lambda` node directly and stops (the bare
  failable inside is accepted; were the arm to recurse it would reject like
  1052). Output byte-identical — only the .sx gained the statement.
- 1053-errors-nested-lambda-liveness-reject (exit 1): an E1.8 value-slot read
  inside a never-called nested lambda, rejected only because flowExpr recurses
  via `.lambda => analyzeFnBody`. Remove that arm and the diagnostic vanishes
  -> suite fails. This is the discriminating negative 1051 lacked.

Gate: zig build test, bash tests/run_examples.sh -> 361/0.
2026-06-03 06:42:51 +03:00
agra
95895a3bb2 test(ir): lock error-flow diagnostics before A5.2 extraction (A5.2 scaffolding step 1)
Test-first scaffolding for the path-sensitive error-flow pass
(checkErrorFlow/analyzeFnBody/flowWalk/flowIf/checkCleanupBody) before it
moves into src/ir/error_flow.zig. No compiler change — both examples lock
current behavior.

- 1051-errors-cleanup-closure-boundary (accepted): a closure literal inside a
  `defer` body is its own function boundary — the E1.7 cleanup rule and the
  parser's try/raise ban both stop at the lambda, and E1.8 value-slot liveness
  runs per-boundary. Pins checkCleanupNode's `.lambda` stop + flowExpr's
  `.lambda` recursion. Constructible since issue 0073 (0310).
- 1052-errors-cleanup-transitive-reject (exit 1): the E1.7 cleanup check is
  transitive — bare failables nested in an `if` (both branches), a nested
  block, and a `while` body all reject. Pins checkCleanupNode's recursive arms,
  distinct from 1049's direct-body case.

No .test.zig/.ir: diagnostic-pass altitude (checkErrorFlow/A2.4 precedent) —
the pass returns no fact object and emits no IR.

Gate: zig build, zig build test, run_examples.sh -> 360/0.
2026-06-03 06:31:18 +03:00
agra
08f263c6e4 fix(ir): open a fresh defer window when lowering a lambda body (issue 0073)
A closure literal declared inside a `defer` body segfaulted the compiler.
Root cause: lowerLambda never opened its own `func_defer_base` window. Every
other function-lowering entry (lowerFunction / monomorphizeFunction /
monomorphizePackFn) saves func_defer_base, sets it to defer_stack.items.len, and
restores it — lowerLambda didn't. So a lambda's `return` drained the ENCLOSING
function's defers; when the defer body itself declared the lambda, draining
re-lowered the lambda, which returned, which drained again → infinite recursion
→ stack-overflow SIGSEGV (the failable variant surfaced one frame out, in
expandCallDefaults→lookupFn reading a clobbered scope).

Fix: lowerLambda now saves func_defer_base + the defer_stack length, sets the
base to the current length (a fresh window), and restores both on exit — so a
lambda's `return` drains only its own defers.

Regression: examples/0310-closures-closure-literal-in-defer.sx — a closure
declared and called inside a `defer`; verifies `body` then `defer closure: 42`
at scope exit (exit 0). Issue 0073 marked RESOLVED; repro promoted from
issues/0073-*.sx.

zig build, zig build test, tests/run_examples.sh (358/0) all green.
2026-06-02 23:29:49 +03:00
agra
eb20b2ddb5 docs(issues): file 0073 — closure literal inside a defer body segfaults lowering
Minimal repro (issues/0073-...sx): a non-failable, uncalled closure literal
declared inside a `defer` body crashes the compiler with a SIGSEGV in
lowerLambda (src/ir/lower.zig). Isolation shows the trigger is "a closure
literal lowered inside a defer body" — not failability, not whether it's called
(closures and failable closures lower fine outside a defer). Pre-existing
lowering bug, unrelated to the A5 error-analysis extraction; surfaced while
writing an A5.2 cleanup-absorption test example.

Filed per the IMPASSIBLE RULE: work paused pending a fix in another session.
2026-06-02 23:20:19 +03:00
agra
667192c718 refactor(ir): extract ErrorAnalysis (error_analysis.zig) for error-set convergence (A5.1 step 2)
Error-set convergence now lives in src/ir/error_analysis.zig behind a *Lowering
facade (ErrorAnalysis), mirroring the other domain extractions. Moved verbatim:
- convergeInferredErrorSets (whole-program inferred-`!` SCC fix-point),
- convergeClosureShapeSets,
- collectErrorSites / collectClosureShapes (the AST collectors).

Added ErrorFacts (the PLAN-ARCH shape: inferred_error_sets + shape_inferred_sets)
+ a facts() view over the maps, which stay on Lowering for now (consumers read
them via self.*). recordClosureShape and its deep type/shape helper web stay in
Lowering; it reaches the moved collectErrorSites via self.errorAnalysis().

Lowering keeps convergeInferredErrorSets / convergeClosureShapeSets as thin pub
wrappers (the lowering pipeline + the E1.4b unit test call them); collectErrorSites
/ collectClosureShapes are deleted (no fallback). New pub: isErrorTagLiteralNode /
callTargetName / astIsPureBareInferred / astPureNamedSet / containsTag /
namedSetTags / recordClosureShape (the moved collectors / facade reach them).
lower.zig net -216 lines.

The 2 convergence unit tests (transitive SCC across a try edge; closure-shape
union) moved from lower.test.zig to error_analysis.test.zig and now drive the
facade directly; the E1.4b test stays in lower.test.zig via the wrapper. Module
named error_analysis.zig, NOT errors.zig (src/errors.zig is the DiagnosticList).

zig build, zig build test, tests/run_examples.sh (357/0) all green — no .ir churn.
2026-06-02 23:11:18 +03:00
agra
9153f958ea test(ir): lock error-set convergence before A5.1 extraction (A5.1 scaffolding step 1)
Test-first scaffolding ahead of extracting src/ir/error_analysis.zig — no code
change to the convergence targets (convergeInferredErrorSets /
convergeClosureShapeSets / collectErrorSites / collectClosureShapes).

Adds 2 unit tests via the already-pub convergence functions (no new exposure):
- convergeInferredErrorSets transitive/SCC: a `caller :: () -> ! { try raiser(); }`
  with no direct raise converges to raiser's {Foo} across the try edge — the
  whole-program fixpoint A5.1 must preserve. (Today's E1.4b test only covered a
  direct raiser + the empty-set warning.)
- convergeClosureShapeSets: a bare-`!` closure literal `() -> ! { raise error.Bar }`
  inside a host fn unions {Bar} into one shape_inferred_sets entry.

Adds 2 .ir snapshots (first .ir for these error forms), vetted clean
(idempotent, path-free, no #run): 1006-errors-inferred-error-sets (inferred-set
error-channel shapes) and 1009-errors-catch (catch lowering). 1004-errors-try
was already pinned.

PLAN-ERR is complete/idle, so the A5 overlap risk is low (the target functions
are stable, not in-flight). The sub-step-2 module will be named
src/ir/error_analysis.zig, NOT errors.zig (src/errors.zig is the DiagnosticList).

zig build, zig build test, tests/run_examples.sh (357/0) all green.
2026-06-02 22:57:39 +03:00
agra
f3bda369f6 refactor(ir): extract CoercionResolver (conversions.zig) for coercion planning (A4.3 step 2)
Coercion classification now lives in src/ir/conversions.zig behind a *Lowering
facade (CoercionResolver), mirroring CallResolver / GenericResolver /
ProtocolResolver. Two pure classifiers:
- classify(src, dst) -> CoercionPlan (15 kinds: no_op / unbox_any / box_any /
  closure_to_fn_reject / tuple_elementwise / optional_unwrap / void_to_optional /
  optional_wrap / erase_protocol / int_to_float / float_to_int / ptr_int_bitcast /
  widen / narrow / none) — the built-in coercion ladder.
- classifyXX(src, dst) -> XXPlan (unbox_any / no_op / erase_protocol /
  protocol_to_pointer / coerce) — the xx-operator head.

coerceToType and lowerXX now `switch (classify…)` then emit; branch order
mirrors the originals exactly and every arm reproduces the prior lowering — the
f32/f64 Any match dispatch, buildProtocolErasure (lowerXX) vs buildProtocolValue
(coerceToType), tuple/optional recursion, and the user-Into fallback + pointer
materialization + recursion-guard/diagnostics (which stay in lowerXX /
tryUserConversion). IR emission stays entirely in Lowering; the classifiers are
pure. lowerXX keeps the operand's lowered Ref type as src_ty. `.none` means no
built-in applies (pass through; the Into fallback runs) — no silent default.

New pub: isFloat / isIntEx / typeBitsEx / resolveConcreteTypeName (the classifier
reads them); coercionResolver() accessor. lower.zig net -54 lines.

conversions.test.zig drives CoercionResolver directly: the full classify ladder
(no-op, Any box/unbox, widen/narrow, int<->float, ptr<->int, optional
wrap/unwrap, void->optional, tuple, closure-reject, .none for two unrelated
structs), erase_protocol for a concrete source, and classifyXX (all 5 kinds incl.
protocol-to-pointer vs coerce and pointer-materialization -> coerce).

zig build, zig build test, tests/run_examples.sh (357/0) all green — no .ir churn.
2026-06-02 22:45:56 +03:00
agra
50dd2cc3d8 test(ir): lock coercion forms before A4.3 extraction (A4.3 scaffolding step 1)
Test-first scaffolding ahead of extracting src/ir/conversions.zig — no code
change to the coercion targets (lowerXX / coerceToType / coerceOrErase /
buildProtocolErasure / tryUserConversion / failable-adapter selection).

Adds 4 .ir snapshots (first .ir for 01xx/09xx/10xx), each captured surgically
via `sx ir | normalize_ir`, path-free, idempotent, and print-free at IR-gen time
(0114-types-build-block-convert was rejected — it prints `--- void / 0 args ---`
+ sx source at IR-gen):
- 0107-types-int-cmp-in-float-ternary   numeric int<->float coercion
- 0903-optionals-optional-roundtrip     optional wrap/unwrap
- 0904-optionals-any-to-string-optional xx unbox_any + optional
- 1004-errors-try                       error-channel adapter/coercion

Protocol erasure + user Into are already pinned by the 04xx snapshots
(0400/0413/0414/0416); duplicate-conversion rejection by the 0410/0411/0412
anchors.

Adds 1 unit test via the public surface (no new exposure, mirroring A4.1/A4.2
sub-step 1): optionalOfFlattened — the optional wrap/flatten coercion rule
(T -> ?T; ?T -> ?T, never ??T; contrasted with the non-flattening optionalOf).
The lowerXX/coerceToType/coerceOrErase/buildProtocolErasure decisions are private
+ emission-bound, so their CoercionPlan unit tests land with the extracted module
in sub-step 2.

zig build, zig build test, tests/run_examples.sh (357/0) all green.
2026-06-02 22:32:01 +03:00
agra
137285f33d refactor(ir): factor protocol/impl planning into ProtocolResolver (A4.2 planning increment)
Factor the lookup/planning half of the protocol emission functions into
protocols.zig, keeping IR emission in Lowering (PLAN-ARCH A4.2 final increment):
- protocolMethodInfos(proto) — the dispatch method table = which methods
  getOrCreateThunks must thunk. getOrCreateThunks now does PLANNING via this +
  EMISSION (createProtocolThunk loop) in Lowering.
- findVisibleImpls(entries, out) — moved verbatim (pure BFS over the import
  graph; the cross-module visibility selection behind the 0410 path).
  tryUserConversion calls it via the resolver.
- matchPackImpl(src_ty, pack_key) -> ?PackImplMatch — the pure pack-impl
  matching loop (prefix + return match) + convert-method find, returning the
  matched entry + convert fd + src params/ret. tryPackImplMatch consumes it; the
  binding + monomorphise + call emission stays in Lowering.

Emission untouched: createProtocolThunk, buildProtocolValue, and the
monomorphise+call tails of tryUserConversion / tryPackImplMatch remain in
Lowering. The reentrancy guard, key-build, and the Into no-visible / duplicate /
recursive diagnostics stay in tryUserConversion (byte-for-byte). lower.zig net
-94 lines. No new pub exposure (uses the existing ParamImplEntry /
PackParamImplEntry / formatTypeName surface).

protocols.test.zig +3: protocolMethodInfos (method table + null-for-unknown, no
silent empty default); findVisibleImpls (falls open with no graph; filters to
here + transitive imports); matchPackImpl (selects on prefix+return; null for
non-closure source / unknown key).

zig build, zig build test, tests/run_examples.sh (357/0) all green — no .ir
churn; the 0410/0411/0412 diagnostics are byte-for-byte preserved.
2026-06-02 22:23:01 +03:00
agra
e6cbb60d8f refactor(ir): move protocol/impl registration into ProtocolResolver (A4.2 registration increment)
Move the registration functions behind the protocols.zig facade, per PLAN-ARCH
A4.2 ("then registration", keeping IR emission in Lowering):
- registerProtocolDecl (protocol struct + dispatch method table + vtable type),
- registerImplBlock (concrete impl -> <Target>.<method> in fn_ast_map + default-
  method synthesis),
- registerParamImpl (parameterised impl -> param_impl_map / param_impl_pack_map
  + the same-file duplicate diagnostic),
- synthesizeDefaultMethod (facade-private; its only caller moved too).

Moved verbatim with self. -> self.l. facade rewrites. Emission stays in
Lowering: the registry calls self.l.declareFunction (the extern-stub primitive)
but the thunk/value builders (createProtocolThunk / buildProtocolValue /
tryUserConversion / getOrCreateThunks) are NOT moved.

Lowering keeps registerProtocolDecl as a thin pub wrapper (scan pass + 7
unit-test callers); registerImplBlock / registerParamImpl /
synthesizeDefaultMethod deleted (no fallback), the 2 scan call sites routed
through protocolResolver(). New pub: declareFunction (8 callers, emission infra),
ParamImplEntry / PackParamImplEntry (the registry constructs them; stay as
Lowering nested types). State maps remain on Lowering; the facade reads/writes
self.l.* (migrate once planning lands).

protocols.test.zig +2: registerImplBlock records Circle.draw in fn_ast_map (and
packArgConformsTo then sees it); registerParamImpl flags a same-file duplicate
impl Into(s64) for IntCell (the 0412-class, unit level).

zig build, zig build test, tests/run_examples.sh (357/0) all green — no .ir
churn; the 0410/0411/0412 rejection diagnostics are byte-for-byte preserved.
2026-06-02 22:10:40 +03:00
agra
81d332dfb0 refactor(ir): extract protocol/impl lookup into protocols.zig (A4.2 step 2)
Move the pure protocol/impl conformance lookups into one module,
src/ir/protocols.zig, behind a *Lowering facade (ProtocolResolver), mirroring
GenericResolver / CallResolver. Per PLAN-ARCH A4.2 ("move pure lookup first;
keep emission in Lowering"), this increment moves only the read-only queries:
- getProtocolInfo (is a type a registered protocol + its method table),
- hasImplPlain (have the (protocol, type) thunks been materialized),
- packArgConformsTo (impl-declaration-level conformance for ..xs: P).

Registration (registerProtocolDecl / registerImplBlock / registerParamImpl) and
all IR emission (createProtocolThunk / buildProtocolValue / tryUserConversion /
getOrCreateThunks) stay in Lowering for the later increments. The state maps
(protocol_thunk_map / param_impl_map on Lowering, protocol_decl_map /
protocol_ast_map in ProgramIndex) stay put; the facade reads them via self.l.* —
no map migration.

Lowering keeps getProtocolInfo as a thin pub wrapper (~9 callers incl.
calls.zig); hasImplPlain + packArgConformsTo are deleted (no fallback), their 3
call sites (computeHasImpl x2, the pack-conformance check x1) routed through
self.protocolResolver(). formatTypeName widened to pub (the lookups use it);
protocolResolver() accessor added.

protocols.test.zig (wired into the barrel) drives ProtocolResolver directly:
getProtocolInfo (registered vs builtin/plain-struct + wrapper delegation),
hasImplPlain (thunk-map materialization), packArgConformsTo (non-parameterised
requires <ty>.<m> in fn_ast_map; trivially-true for an erased protocol value;
false for unknown protocol).

zig build, zig build test, tests/run_examples.sh (357/0) all green — no .ir
snapshot churn; the 0410/0411/0412 rejection anchors still pass.
2026-06-02 21:56:03 +03:00
agra
df386a422e test(ir): lock protocol/impl lookup before A4.2 extraction (A4.2 scaffolding step 1)
Test-first scaffolding ahead of extracting src/ir/protocols.zig — no code change
to the refactor targets (registerProtocolDecl / registerImplBlock /
registerParamImpl / hasImplPlain / tryUserConversion / tryPackImplMatch /
createProtocolThunk / buildProtocolValue).

Adds 4 .ir snapshots (only 0400 existed for 04xx), each captured surgically via
`sx ir | normalize_ir`, path-free, idempotent, and print-free at IR-gen time
(the 0524 contamination lesson):
- 0413-protocols-parameterized-protocol-value  parameterized protocol
                                               (registerParamImpl + tryUserConversion)
- 0414-protocols-generic-struct-protocol-erase generic-struct erasure
                                               (createProtocolThunk + buildProtocolValue)
- 0416-protocols-auto-type-erasure             auto erasure (buildProtocolValue + thunk)
- 0528-packs-protocol-pack-methods             pack-variadic impl (tryPackImplMatch)

With existing 0400 (impl-for-builtin) they pin erasure (auto/generic/builtin) +
parameterized + pack-variadic + dispatch; the 0410/0411/0412 runtime anchors
already pin cross-module visibility + duplicate-impl rejections.

Adds 1 unit test via the public surface (no new exposure, mirroring A4.1
sub-step 1): registerProtocolDecl -> getProtocolInfo builds the dispatch method
table (method names, param_types with self excluded, concrete vs Self return
with ret_is_self + *void encoding). The impl-lookup / conversion plan-object
tests (hasImplPlain, tryUserConversion, tryPackImplMatch — private today) land
with the registry in sub-step 2.

zig build, zig build test, tests/run_examples.sh (357/0) all green.
2026-06-02 21:44:01 +03:00
agra
7ab5d7bee9 test(ir): cover buildTypeBindings strategy-2 inference (A4.1 coverage closeout)
Adds the one deferred A4.1 coverage item: a focused unit test for
GenericResolver.buildTypeBindings inferring a type param from value args
(strategy 2) with widest-match — add(1,2) => T=s64, and add(1.0,2) / add(1,2.0)
=> T=f64 regardless of argument order.

Previously this inference path was guarded only by the 0200 .ir snapshot; the
unit test pins it directly against the new generics.zig API. Test-only.

zig build test and tests/run_examples.sh (357/0) green.
2026-06-02 21:34:58 +03:00
agra
3ca68189c0 refactor(ir): extract GenericResolver (generics.zig) for substitution + mono keys (A4.1 step 2)
Generic substitution and monomorphization-key construction now live in one
module, src/ir/generics.zig, behind a *Lowering facade (GenericResolver),
mirroring CallResolver / ExprTyper. Moved verbatim:
- mangleTypeName + mangleParamList (the mono-key fragment builder),
- mangleGenericName (generic mono key), appendComptimeValueMangle (comptime-value
  fragment),
- buildTypeBindings (call-site type-param inference), inferGenericReturnType
  (generic return resolution).

inferGenericReturnType now uses a scoped TypeBindingScope (enter/exit with defer)
instead of a manual type_bindings save/restore — the PLAN-ARCH A4.1 "scoped
substitution env" shape; a generics.test.zig assertion confirms the prior
bindings are restored (the issue-0048/0050 leak class, for this field).

Lowering keeps a thin pub mangleTypeName wrapper delegating to
genericResolver().mangleTypeName, because ~30 cross-cutting callers (impl-map
keys, conversion keys, shape keys) reach it well beyond generics. mangleParamList
(sole caller was mangleTypeName) moved fully. The other 4 originals are deleted
(no fallback); their 6 call sites now go through self.genericResolver()
(calls.zig via self.l.genericResolver()).

matchTypeParam / extractTypeParam / isTypeParamDecl widened to pub (the moved
substitution logic calls them); genericResolver() accessor added. The 2
mangleTypeName / inferGenericReturnType unit tests moved from lower.test.zig to
generics.test.zig (driving GenericResolver directly) and wired into the barrel.

monomorphizeFunction / monomorphizePackFn intentionally stay in lower.zig (they
save/restore three fields across nested mono and call emission helpers) — a
heavier scoped-env adoption deferred to an optional sub-step 3.

zig build, zig build test, and tests/run_examples.sh (357/0) all green — no .ir
snapshot churn, confirming the move preserved mono-key/substitution output.
2026-06-02 21:28:31 +03:00
agra
e1f167a1c3 test(ir): replace contaminated 0524 IR snapshot with clean 0513 (A4.1 scaffolding fix)
The 0524-packs-generic-fn-pack-state-leak example has a #run that prints at
IR-gen time, and tests/run_examples.sh captures `sx ir ... 2>&1`, so its .ir
snapshot was contaminated with #run stdout (`0: len=0` ...) instead of pure IR.

Remove 0524.ir — pack-state isolation (the issue-0048/0050 class) stays guarded
by 0524's existing runtime .stdout/.exit, where a leaked outer pack_arg_types
would corrupt the printed len= sequence.

Replace it with 0513-packs-pack-mixed-comptime.ir, which is print-free at
IR-gen time (clean, idempotent, path-free) and additionally locks the
comptime-value mono-key path (appendComptimeValueMangle): the IR shows
tagged(7,..) vs tagged(9) producing distinct monos
@tagged__ct_7__pack_s64_s64_s64 / @tagged__ct_9__pack.

zig build, zig build test, tests/run_examples.sh (357/0) all green.
2026-06-02 21:13:50 +03:00
agra
91e99f80c7 test(ir): lock generic substitution + mono keys before A4.1 extraction (A4.1 scaffolding step 1)
Test-first scaffolding ahead of extracting src/ir/generics.zig — no code change
to the refactor targets (buildTypeBindings / mangleGenericName / monomorphize* /
inferGenericReturnType / mangleTypeName).

Adds the first non-FFI generic/pack .ir snapshots (closing the ARCH-SAFETY §3
gap for this phase), each captured surgically via `sx ir | normalize_ir`,
path-free and idempotent:
- 0200-generics-generic            generic fn, type-param inference + mono
- 0201-generics-generic-struct     generic struct instantiation
- 0507-packs-pack-mono-dedup       mono-key dedup (same shape => one mono)
- 0518-packs-pack-value-dispatch   pack value dispatch (monomorphizePackFn)
- 0524-packs-generic-fn-pack-state-leak  pack-state isolation (issue-0048/0050
                                         class; guards the future scoped-env change)

Adds 2 unit tests via the existing public surface (no new pub exposure,
mirroring the A3.2 sub-step-1 cadence):
- mangleTypeName: pins the mono-key fragment encoding per type shape
  (s64 / ptr_X / opt_X / SL_X / mptr_X / AR_n_X / vec_n_X / struct-name / tu_X_Y).
- inferGenericReturnType: explicit type-arg path binds $T and resolves the
  -> T return (pair(s64,..) => s64, pair(f64,..) => f64).

The internal substitution/mono-key unit tests (comptime-value mangle,
buildTypeBindings strategies, scoped-env isolation) land with the generics.zig
extraction in sub-step 2, as A3.2's plan-object tests landed with CallPlan.

zig build, zig build test, tests/run_examples.sh (357/0) all green.
2026-06-02 21:05:33 +03:00
agra
1007e23561 refactor(ir): source lowerCall's namespace/value boundary from CallResolver (A3.2 convergence step 3)
lowerCall re-derived the namespace-vs-value (receiver-prepend) decision with a
19-line block duplicating the exact identifier/type_expr + scope/global walk
that CallResolver already owns (objectIsValue, the negation of is_namespace).
This boundary determines whether the receiver is prepended, so it must agree
with the plan's free_fn_ufcs (prepends) vs namespace_fn (does not)
classification from fa59a9d.

Make CallResolver.objectIsValue pub and set
  is_namespace = !self.callResolver().objectIsValue(fa.object)
so plan and lowering share one boundary definition and can never drift.
`!objectIsValue` matches the old block case-for-case (non-identifier => value;
identifier/type_expr in scope/global => value; else => namespace), so this is a
behavior-identical substitution.

Deeper switch(plan.kind) routing of lowerCall is intentionally NOT done here: it
is not behavior-preserving as-is. `plan` is typing-only and coarser than
`lowerCall` — its method/namespace arms carry comptime / generic /
generic-template / #compiler / type-constructor dispatch `plan` does not model,
and its value-receiver kinds (struct_method/protocol_dispatch/foreign_instance)
do not gate on objectIsValue, so a type-name receiver (Point.make()) could be
mis-classified vs the namespace/static call lowerCall actually performs. Driving
prepend decisions off plan.kind would mis-prepend; objectIsValue is the correct
single source, hence routing the boundary specifically. PLAN-ARCH A3.2 success
criteria met (shared classifier; no duplicated return-type logic; plan tests;
stable .ir snapshots).

zig build, zig build test, tests/run_examples.sh (357/0) all green.
2026-06-02 20:53:13 +03:00
agra
fa59a9dc25 refactor(ir): distinguish free-function UFCS from namespace calls in CallPlan (A3.2 review fix)
CallPlan collapsed two different field-access dispatches onto namespace_fn:
a true namespace call (`pkg.fn()`, no receiver) and free-function UFCS
(`c.bump()`, receiver prepended + `*T` fixup). Return typing was preserved
either way, but sub-step 3 could not consume the plan — it would have had to
re-classify the AST to decide whether to prepend the receiver.

Add a distinct `free_fn_ufcs` kind and a plan(c) branch, inserted after the
struct-method block and gated on `objectIsValue` (the negation of lowerCall's
`is_namespace`: a non-identifier receiver is always a value; an
identifier/type_expr is a value iff it names a local or a global). The branch
sets prepends_receiver = true and reads prepends_ctx from the resolved FuncId
(best-effort, like direct_fn). namespace_fn now means strictly "receiver is a
namespace/type prefix".

New test `plan: free-function UFCS prepends receiver, distinct from
namespace_fn` covers a scope-bound `c.bump()` against a lowered free fn:
asserts free_fn_ufcs kind, func target, prepends_receiver, prepends_ctx, and
preserved s32 return type.

zig build, zig build test, tests/run_examples.sh (357/0) all green; return
typing unchanged.
2026-06-02 20:33:26 +03:00
agra
61f1f2368a refactor(ir): add CallPlan + CallResolver.plan(c); resultType delegates (A3.2 convergence step 2)
Introduce CallPlan — the single classification record for a call: kind (14
variants), return_type, a Target union (builtin/func/named/protocol_method/
foreign_method/constructed/none), variant tag, and the prepends_receiver /
prepends_ctx / expands_defaults properties the selected dispatch implies.

Move call recognition into CallResolver.plan(c) (branch order preserved
exactly) and reimplement resultType(c) as plan(c).return_type — the typing
consumer converges onto the plan first. lowerCall is untouched; routing it
through plan(c) is sub-step 3.

10 plan-object tests assert kind/target/variant + receiver/ctx/default
properties for every pinned call form: builtin/reflection, lazy + resolved
direct fn (incl. default-arg expansion + __sx_ctx prepend), closure /
default-conv vs C-conv fn-pointer, protocol dispatch, struct/UFCS #compiler
method, foreign instance vs static, qualified + dot-shorthand enum
construction, namespace fn, and the unresolved fallthrough.

Widen for the new collaborator only: resolveVariantIndex -> pub (plan resolves
the variant tag); Scope/Binding + init/deinit/put -> pub (so unit tests can
stand up a lexical scope for closure/fn-ptr callees without a full lowering).

zig build, zig build test, and tests/run_examples.sh (357/0) all green; no
behavior change.
2026-06-02 20:15:53 +03:00
agra
297f127821 test(ir): lock call lowering with .ir snapshots + classification tests (A3.2 convergence step 1)
Test-first scaffolding before the CallPlan convergence — no call-code
change. Locks current call behavior so the later lowerCall rewrite is
guarded.

- .ir snapshots for representative call forms: 0031 (direct local-fn +
  dot-shorthand enum ctor), 0032 (UFCS/struct method), 0301 (closure/
  fn-pointer slot), 0400 (protocol dispatch + static-through-impl).
- New focused example 0044-basic-default-arg-expansion + .ir snapshot,
  pinning call-site default expansion (scale(5)->scale(ctx,5,2),
  label(1)->label(ctx,1,"v","!")). Foreign-class instance+static is
  already pinned by the existing FFI .ir set.
- Broaden calls.test.zig (scope-free classification): remaining reflection
  builtins, sqrt->f64, cast->resolved type arg, enum_literal->target_type.

1033 (#caller_location) was rejected as a snapshot: it embeds the absolute
source path as a length-typed string that normalize_ir can't reconcile;
default-arg coverage uses the path-free 0044 instead.

Gate green: zig build, zig build test, tests/run_examples.sh -> 357/0.
2026-06-02 19:20:14 +03:00
agra
64cd3da5f5 docs(ir): correct stale ExprTyper comments re call result typing owner
The module doc and the `.call` arm comment still said call result typing
"stays in Lowering" and "converges in A3.2". As of 7f3a7b3 calls are routed to
CallResolver (calls.zig); update both comments to name the current owner. The
`.call` arm still delegates through Lowering.inferExprType — that's the routing
path to the owner, not a claim that Lowering owns the typing.

Comment-only. Gate: zig build, zig build test, run_examples.sh -> 356/0.
2026-06-02 19:02:44 +03:00
agra
7f3a7b35ef refactor(ir): extract CallResolver for call result typing (A3.2 relocation)
Move call-result-type discovery out of Lowering into a new src/ir/calls.zig
(CallResolver): the A3.1 Lowering.inferCallType body moves verbatim into
CallResolver.resultType. inferExprType's `.call` arm now delegates via
callResolver(); Lowering.inferCallType is gone.

CallResolver is a *Lowering facade (Principle 5, like ExprTyper/PackResolver):
call typing reads live lexical-scope / target-type state and the function /
foreign-class / protocol resolver helpers, so it borrows *Lowering. Transform
was `self.` -> `self.l.` plus the file-local static `resolveBuiltin(` ->
`Lowering.resolveBuiltin(`.

Widened to pub only what the facade actually consumes: resolveTypeArg,
inferGenericReturnType, resolveFuncByName, getProtocolInfo,
resolveForeignMethodReturnType, the static resolveBuiltin, and Scope.lookupFn.
resolveTypeArg widening is genuinely required here — the `cast` builtin's
result type calls it.

calls.test.zig adds focused tests (builtin/reflection classification, unknown
callee -> unresolved) for the scope-free paths. Barrel-wired in ir.zig.

This is the relocation half of PLAN-ARCH A3.2; call LOWERING (lowerCall) still
owns its own dispatch, and the CallPlan convergence (one plan shared by typing
and lowering, deleting the duplicated qualified/bare/lazy logic) remains.

Behavior-preserving. Gate: zig build, zig build test (incl. new CallResolver
tests), bash tests/run_examples.sh -> 356/0. lower.zig 18598 -> 18413.
2026-06-02 18:44:08 +03:00
agra
7d069107c8 refactor(ir): extract ExprTyper for non-call expression typing (A3.1)
Move the structural / non-call arms of Lowering.inferExprType into a new
src/ir/expr_typer.zig (ExprTyper): literals, unary/binary ops, try/catch, if,
block, field access, identifier/type-name, struct/tuple literals,
index/slice/deref, null-coalesce, caller_location, and the no-value statement
shapes. ExprTyper is a *Lowering facade (Principle 5, same as PackResolver) —
expression typing reads live lexical-scope / pack / target-type state and ~14
resolver helpers, so it borrows *Lowering rather than re-threading every field;
the plan's TypeResolver/ProgramIndex/ResolveEnv ideal is the later-phase target
as that state lifts into an explicit context (documented in the module doc).

Lowering.inferExprType is now a 2-arm dispatcher: `.call => inferCallType(c)`
(call result typing stays in Lowering until A3.2), else delegates to
ExprTyper.inferType. The call arm body moved verbatim into the new
Lowering.inferCallType (the by-value `|c|` capture became a `*const ast.Call`
param; the lone `&c` -> `c`).

14 Lowering helper methods consumed by the facade were widened to pub
(orIsFailableChain, orChainSuccessType, errorChannelOf, failableSuccessType,
isObjcClassPointer, lookupObjcPropertyOnPointer,
lookupObjcDefinedStateFieldOnPointer, getElementType, optionalOfFlattened,
getStructFields, isKnownTypeName, comptimeIndexOf, packArgNodeAt, resolveType)
plus Scope.lookup — the same pub-for-facade step PackResolver took. Fields need
no change (Zig fields are always cross-file accessible).

expr_typer.test.zig adds focused unit tests (literal shapes, comparison vs
arithmetic, unary not/negate, deref of non-pointer) for the scope-free
structural arms. Barrel-wired in ir.zig.

Behavior-preserving. Gate: zig build, zig build test (incl. new ExprTyper
tests), bash tests/run_examples.sh -> 356/0. lower.zig ~18774 -> 18598.
2026-06-02 18:14:34 +03:00
agra
b72d49073e fix(ir): diagnose non-constant global initializers loudly (issue 0072)
globalInitValue's issue-0071 .identifier arm closed the bare-identifier hole,
but .field_access (and every other non-literal expression shape) still fell
through to `else => null`, so a global like `g : s32 = K.x;` was emitted with
no payload and silently zero-initialized (g=0).

Make the `else` emit a diagnostic — "global '<name>' must be initialized by a
compile-time constant" — instead of a null payload, so no unsupported shape can
silently zero. Two arms added alongside:

- `.null_literal => .null_val`: a `*void = null` global was previously a
  no-payload zero-init; this preserves the exact LLVMConstNull emission (fixes
  3 ffi examples that regressed on the first cut).
- explicit `.enum_literal => null` carve-out: the stdlib's
  `OS : OperatingSystem = .unknown;` zero-init is load-bearing for compile-time
  `inline if OS == .X`; documented, not folded into a silent fallthrough.

Field-access constant *evaluation* (materializing K.x -> 9) is intentionally
not implemented: a typed struct const like K is not registered in
module_const_map, so it would require new plumbing whose writes are read at
runtime — out of scope. The diagnostic is the issue-sanctioned outcome.

Regression: examples/1118-diagnostics-global-non-const-initializer-rejected.sx
(exit 1). Gate: zig build, zig build test, run_examples.sh -> 356/0.
2026-06-02 17:57:17 +03:00
agra
ad7200c196 fix(ir): materialize global initialized from module const (issue 0071)
registerTopLevelGlobal's init_val switch serialized only literal / array-
literal / struct-literal initializers. An identifier initializer
(`K : A : 42; g : A = K;`) fell through to `else => null`, so the global was
emitted with no payload and silently zero-initialized (printed g=0).

Extract the initializer serialization into globalInitValue and add an
.identifier arm that materializes the global's static value from
ProgramIndex.module_const_map (typed module consts are registered in the same
scanDecls pass-2 just before, via registerTypedModuleConst). An identifier
that names no usable constant now emits a diagnostic instead of silently
zeroing — a global has no run site for a dynamic initializer.

Other initializer shapes (enum-literal shorthand, etc.) keep their established
static-lowering behavior; enum-literal globals' zero-init is load-bearing for
`inline if OS == ...` in the stdlib, so it stays out of scope here. This pass
only closes the identifier/module-const hole.

Regression: examples/0134-types-global-init-from-module-const.sx (g=42, exit
42). Gate: zig build, zig build test, run_examples.sh -> 355/0.
2026-06-02 17:45:37 +03:00
agra
932cdfa2ec fix(ir): resolve forward alias in top-level global annotations (issue 0070)
Issue 0069's resolveForwardIdentifierAliases fixpoint runs at the END of
scanDecls, but top-level var_decl globals and typed module constants had
their annotations resolved via resolveType(ta) inside the SAME scan loop,
before the fixpoint. So a forward identifier alias (`A :: B; B :: s32;`)
used as a global's type (`g : A = 7;`) was still absent from
type_alias_map: resolveType fabricated an empty-struct stub, and the global
got a type mismatching its initializer at LLVM verification (the typed-const
path `K : A : 42;` silently mistyped the constant instead).

Split scanDecls into two passes: pass 1 registers function/type/alias facts,
then resolveForwardIdentifierAliases converges the aliases, then pass 2
registers var_decl globals (registerTopLevelGlobal) and typed module
constants (registerTypedModuleConst) against the converged alias map.
Globals/typed-consts can't be named in a type position, so deferring them
past type/alias registration is order-safe; the untyped module-const branch
(no annotation to resolve) stays in pass 1.

One incidental IR snapshot reorder (examples/1309: user globals now emit
after foreign-class globals — semantically identical, program still exits 0).

Regression: examples/0133-types-forward-alias-global.sx (forward-alias global
+ typed const). Gate: zig build, zig build test, run_examples.sh -> 354/0.
2026-06-02 17:20:31 +03:00
agra
49a383df6d fix(ir): resolve forward identifier type aliases in scanDecls (issue 0069)
scanDecls' `.identifier` alias branch registered `A :: B` into
ProgramIndex.type_alias_map only when `B` was already known (in
type_alias_map or the TypeTable). A forward target declared later
(`MyChain :: MyInt; MyInt :: s32;`) was never present during the single
forward scan, so the alias name went unregistered and the A2.4
unknown-type pass — which treats type_alias_map keys as declared types —
flagged its uses as `unknown type 'MyChain'`.

Add a fixpoint post-pass `resolveForwardIdentifierAliases` at the end of
scanDecls that re-resolves identifier-RHS aliases until no progress, after
every top-level name has been seen. A value const is never an `.identifier`
node, and an alias whose target is a value const still misses both lookups,
so issue 0068's value-const rejection is preserved.

Regression: examples/0132-types-forward-type-alias.sx (forward alias +
forward chain). Gate: zig build, zig build test, run_examples.sh -> 353/0.
2026-06-02 16:59:20 +03:00
agra
877014578e fix(ir): value const used as a type must not satisfy unknown-type check (issue 0068)
The A2.4 unknown-type pass (semantic_diagnostics) added EVERY const_decl name to
its declared-type-name set. A value const (`NotAType :: 123`) thus satisfied
reportIfUnknownType, so `v: NotAType` was not flagged; lowering then hit
TypeResolver.resolveNamed's empty-struct-stub fallback and fabricated
`NotAType{}` (the program ran, printing it).

Fix: collectDeclaredTypeNames and harvestScopeDecls now gate the const-name-add
on a new constValueIntroducesType — true only when the value introduces a type
(declarations: struct/enum/union/error; type-expression aliases: type_expr,
pointer/many-pointer/slice/optional/array/function/closure/tuple, parameterized).
`.identifier` / `.call` aliases are intentionally excluded: the scan registers
the type-valued ones into ProgramIndex.type_alias_map / the TypeTable (both
queried separately by the pass), so a value-RHS alias is correctly left out and
flagged, while a type-RHS alias stays covered by the canonical facts.

Regression: examples/1117-diagnostics-value-const-as-type-rejected.sx (exit 1).
Issue-0064 regressions 1111-1116 and the 0115 aliases stay green. Gate: zig
build, zig build test, run_examples 352/0.
2026-06-02 16:33:38 +03:00
agra
8ff24472c9 refactor(ir): extract unknown-type diagnostic pass into semantic_diagnostics (A2.4)
Moves the issue-0064 unknown-type pass (checkUnknownTypeNames + 11 helpers:
collectDeclaredTypeNames, harvestScopeDecls, checkStructFieldTypes,
checkFnSignatureTypes, checkScope, walkBodyTypes, checkCastTarget,
checkTypeNodeForUnknown, reportIfUnknownType, isBuiltinTypeName, isIdentLike)
out of Lowering into a new src/ir/semantic_diagnostics.zig (UnknownTypeChecker).

The checker holds borrowed references (alloc, *DiagnosticList, *TypeTable,
*ProgramIndex, main_file) — not *Lowering — and queries the canonical facts:
declared top-level names from ProgramIndex, primitives from
TypeResolver.resolvePrimitive, registered concrete types from the TypeTable.
The AST decl/scope walk stays (it collects LOCAL type decls, which ProgramIndex
doesn't track — a per-pass scope need, not a parallel authoritative list).

Lowering.lowerRoot builds the checker only when diagnostics are active and runs
it; the 12 functions are deleted from lower.zig. Barrel-wired in ir.zig.
Example snapshots (issue-0064 regressions 1111-1115) are the guard, matching the
checkErrorFlow precedent (no .test.zig).

Phase A2 complete. Gate: zig build, zig build test, run_examples 351/0.
2026-06-02 16:12:28 +03:00
agra
7cc8057374 Merge Phase A2 (canonical type resolution) into master
Brings the architecture stream's Phase A2 onto master (A2.1–A2.3b + issue 0067):
- A2.1 (9eb85cf): ResolveEnv + TypeResolver shell (primitives + compounds).
- A2.2 (dd16bab): generic-binding + alias-aware name resolution into TypeResolver.
- A2.3 (3ed1b3a): pack projections → PackResolver; retire the TypeTable.aliases
  borrow (alias map threaded explicitly).
- A2.3b (9b50aac): converge structural type-shape resolution onto the single
  TypeResolver.resolveCompound; type_bridge reduced to a thin adapter.
- 0067 (744decc): reject non-type tuple-literal-as-type elements with a
  diagnostic instead of fabricating .s64.

Gate: zig build, zig build test, run_examples 351/0. Codex-reviewed (round 2).
2026-06-02 16:02:59 +03:00
agra
744decc6a1 fix(ir): reject non-type elements in tuple-literal-as-type (issue 0067)
`size_of((s32, 1))` treated the tuple literal as a tuple TYPE: for the non-type
element `1` it emitted a `std.debug.print` and substituted `.s64` for that field,
then compiled and printed a bogus size — a silent fabricated type (the forbidden
silent-fallback pattern).

Fix:
- type_bridge.resolveTupleLiteralAsType: a non-type element now yields
  `.unresolved` (no `.s64`, no debug print) — it refuses to fabricate a tuple.
  type_bridge is stateless, so this is the binding-free backstop.
- New stateful Lowering.resolveTupleLiteralTypeArg validates each element via
  isTypeShapedAstNode, emits a user-facing diagnostic at the offending element's
  span, and returns `.unresolved`. Wired into resolveTypeArg (size_of/align_of/…)
  and the resolveTypeWithBindings name-fallback; type_bridge builds the tuple
  only after validation passes.

Regression: examples/1116-diagnostics-tuple-type-nontype-element-rejected.sx
(exit 1 + diagnostic). Valid `(s32, s32)` still works (0115). Gate: zig build,
zig build test, run_examples 351/0.
2026-06-02 15:51:04 +03:00
agra
9b50aacbe4 refactor(ir): converge structural type-shape resolution onto resolveCompound (A2.3b)
Codex corrective step before the A2 merge gate: A2.3 left type_bridge with a
parallel structural type-resolution algorithm and an inline tuple-literal-spread
shape in lower.zig with a `.void` fallback.

Finding 1 — single owner for structural shapes:
- TypeResolver.resolveCompound is now the sole structural type-shape
  constructor. Namespaced on `table` (so the stateless type_bridge can call it)
  and extended to own function types, plain `Closure(P...) -> R`, and plain
  positional/named tuples (it already owned *T/[*]T/[]T/?T/[N]T). It returns
  null only for the pack-shaped forms that need caller state (`Closure(..p)`,
  spread tuples); OOM yields `.unresolved`.
- type_bridge: deleted its 8 independent structural resolvers
  (resolveArray/Slice/Pointer/ManyPointer/Optional/Function/Closure/TupleType).
  resolveAstType delegates those node kinds to resolveCompound via a binding-free
  StatelessInner adapter. The only residual stateless shape code is two tiny
  fallbacks for the pack-shaped forms resolveCompound defers
  (resolveClosurePackShape — used by Into(Block) at registration time —
  and resolveTupleSpreadShape) plus resolveParameterizedType (kept:
  generic-instantiation convergence is A4.1 per PLAN-ARCH).
- lower.zig: stateful resolveTypeWithBindings uses resolveCompound; the
  `.function_type_expr` switch arm is gone. PackResolver.resolveFunctionTypeWithBindings
  deleted (subsumed). Plain closures/tuples now resolve via resolveCompound in
  both paths; only pack closures / spread tuples reach PackResolver.

Finding 2 — no `.void` failure fallback in lower.zig pack handling:
- the inline tuple_literal-with-spread type assembly moved into
  PackResolver.resolveTupleLiteralType (returns ?TypeId; OOM `catch return .void`
  became `catch return .unresolved`).

Alias result preserved: TypeTable.aliases stays gone; no table.aliases reads;
ProgramIndex.type_alias_map threaded explicitly.

type_resolver.test.zig: resolveCompound test rewritten (namespaced + new
function/closure/tuple/pack-shape arms, arena-backed). Gate green: zig build,
zig build test, run_examples 350/0.
2026-06-02 15:20:31 +03:00
agra
3ed1b3a7a0 refactor(ir): pack projections → PackResolver + retire the alias borrow (A2.3)
A2-merge gate: both parts in one commit, behavior-preserving (350/0).

Part 1 — retire the TypeTable.aliases borrow (build-enforced):
- type_bridge.zig: add `AliasMap` and thread it as an explicit param through
  every name-resolving fn (resolveAstType, bridgeType, resolveTypeName, the
  compound resolvers, resolveTupleLiteralAsType, resolveParameterizedType, the
  inline enum/struct/union + error resolvers). resolveTypeName now forwards the
  threaded map to TypeResolver.resolveNamed instead of reading table.aliases.
- lower.zig: all 31 resolveAstType callers pass
  &self.program_index.type_alias_map; drop the lowerRoot loan.
- types.zig: remove the now-unused TypeTable.aliases field.
- type_bridge.test.zig: alias test passes alias_map explicitly; other calls
  pass null.

Part 2 — pack projections get one owner + no .void failure sentinel:
- New packs.zig (PackResolver, a *Lowering facade): moves
  resolveClosure/Tuple/FunctionTypeWithBindings, packTypeElems, packTypeArgs,
  elementProtocolTypeArg out of Lowering. Call sites route through
  Lowering.packResolver(); barrel-wired in ir.zig.
- The missing-projection `orelse .void` in packTypeArgs now emits a diagnostic
  and fills the slot with .unresolved (the tripwire sentinel), never a real
  .void; OOM `catch return .void` in the moved fns became .unresolved too.
  Legitimate no-return-type `else .void` defaults are preserved.
- packs.test.zig: packTypeArgs bound/unbound/no-constraint/no-state cases +
  the missing-projection backstop (diagnostic + .unresolved slot).
2026-06-02 14:43:47 +03:00
agra
dd16bab2c2 refactor(ir): move generic-binding + alias-aware name resolution into TypeResolver (A2.2)
Architecture phase A2.2 -- behavior-preserving. TypeResolver gains the
generic-binding and bare-name resolution it now owns:

- resolveBinding(node, env): $T / bare return-type T lookup via an explicit
  ResolveEnv (no hidden Lowering state).
- resolveNamed(name, table, alias_map): the full bare-name algorithm (primitive
  -> arbitrary-width int -> string-prefix [*]/*/?/[:0]u8 -> already-registered
  -> alias(alias_map) -> empty-struct stub), MOVED from
  type_bridge.resolveTypeName so it is single-sourced.
- resolveName(self, name): resolves through the canonical alias source
  ProgramIndex.type_alias_map -- the compiler path no longer reads the
  TypeTable.aliases borrow.

Lowering.resolveTypeWithBindings: the `if (self.type_bindings)` block (the $T
lookup plus parameterized/call/closure/function arms that were redundant with
the unconditional handling below) collapses to one resolveBinding delegation via
a new resolveEnv() snapshot; the bare-name fallback routes type_expr/identifier
to resolveName (index-based alias), other node kinds still to resolveAstType.

type_bridge.resolveTypeName becomes a 1-line delegate to resolveNamed, passing
its TypeTable.aliases borrow as the alias source. Single algorithm; the alias
map stays single-sourced in ProgramIndex.

Deferred to A2.3: removing the TypeTable.aliases borrow (its ~30 resolveAstType
callers must converge onto TypeResolver first) and type_bridge's stateless
compound resolvers. A2.2 #3 (templates/protocols/type-fns via ProgramIndex) was
already satisfied by A1.1b.

Tests: resolveBinding ($T bound/unbound/no-env), resolveName (alias->primitive,
alias->pointer via ProgramIndex), resolveNamed (width-int, string-prefix,
unknown->stub).

No new fallback path; no duplicate truth. Gate green: zig build, zig build test,
bash tests/run_examples.sh (350 passed, 0 failed).
lower.zig 19372->19367; type_bridge.zig 647->592; type_resolver.zig 90->159.
2026-06-02 13:56:32 +03:00
agra
9eb85cf9e3 refactor(ir): add ResolveEnv + TypeResolver shell; own primitives + compounds (A2.1)
Architecture phase A2.1 -- behavior-preserving. Introduce src/ir/type_resolver.zig
as the canonical AST-type-node -> TypeId resolver (Principle 1), starting with:

- ResolveEnv: the explicit resolution-context shape (Principle 2) -- type/pack/
  comptime bindings + target_type. Defined now; consumed as A2.2/A2.3 move the
  cases that need it.
- TypeResolver.resolvePrimitive(name): the builtin keyword table, MOVED here from
  type_bridge.resolveTypePrimitive (now a re-export -> single source; its 7
  callers are unaffected; no import cycle).
- TypeResolver.resolveCompound(node, inner): the structural compound types
  *T / [*]T / []T / ?T / [N]T. Element types recurse via inner.resolveInner (an
  anytype callback) so generic structs / bindings in element position keep their
  full stateful resolution.

Lowering.resolveTypeWithBindings duplicated the 5 simple compounds across its
bindings and no-bindings blocks (10 arms). Both are replaced with a single
self.typeResolver().resolveCompound(node, self) delegation; adds
Lowering.resolveInner (recursion hook) + typeResolver() (by-value view).

Deliberately deferred: tuples, closures, and function types stay on the existing
pack-aware helpers (resolveClosure/Tuple/FunctionTypeWithBindings); A2.3 owns
their pack-projection logic.

Tests: src/ir/type_resolver.test.zig (resolvePrimitive keyword/null cases;
resolveCompound for all 5 + null for non-compound; ResolveEnv defaults), wired
into the ir.zig barrel.

No new fallback path; no duplicate truth. Gate green: zig build, zig build test,
bash tests/run_examples.sh (350 passed, 0 failed). lower.zig 19393 -> 19372.
2026-06-02 13:25:27 +03:00
agra
8fbaf9ca6a docs(ir): mark sema/types as editor-only, not compiler truth (A1.2)
Architecture phase A1.2 — documentation/comment only, no behavior change.

Resolve the ambiguity over which type model compiler decisions trust:

- src/sema.zig: file-level module doc stating it is the editor symbol/type
  index for the language server (navigation/completion), NOT a compiler
  semantic pass. Its Type values are editor metadata; the compiler uses the
  canonical TypeId/TypeTable model in src/ir/. sx requires no as-you-type type
  checking -- authoritative diagnostics are produced on save by the canonical
  pipeline. Added notes on SemaResult, Analyzer, resolveTypeNode, inferExprType.
  No public API renamed (would churn LSP call sites).
- src/types.zig: note that Type is editor metadata only, not compiler truth;
  do not expand for new compiler semantics (A8 deletes/reduces it).
- src/ir/types.zig: fix stale TypeTable.aliases comment -- it borrows
  Lowering.program_index.type_alias_map (post-A1.1b).

Deleting the LSP's parallel sema diagnostic stream is A8.1, not this step.

Gate green: zig build, zig build test, bash tests/run_examples.sh (350 passed).
2026-06-02 12:54:30 +03:00
agra
fb262e9e59 refactor(ir): move declaration maps into ProgramIndex (A1.1b)
Architecture phase A1.1b — mechanical storage relocation. Move the 9
declaration-fact maps out of the Lowering state bag into ProgramIndex:

  high-fanout:   fn_ast_map, foreign_class_map, global_names, type_alias_map
  medium-fanout: struct_template_map, protocol_decl_map, protocol_ast_map,
                 module_const_map, ufcs_alias_map

168 self.<map> sites in lower.zig repointed to self.program_index.<map>;
external readers repointed too (core.zig foreign_class_map iteration;
lower.test.zig fn_ast_map / foreign_class_map). No duplicate storage, no
fallback path; zig build enforces no missed reference.

The four maps whose value types were Lowering-private pull those types into
program_index.zig as pub (GlobalInfo, StructTemplate + TemplateParam,
ProtocolDeclInfo + ProtocolMethodInfo, ModuleConstInfo); lower.zig aliases
them at file scope so call sites are unchanged.

Behavior is preserved exactly:
- per-map allocator unchanged — import_flags/fn_ast_map/global_names use the
  lowering allocator (ProgramIndex.init), the other 7 keep their page_allocator
  inline defaults;
- ProgramIndex.deinit frees only the 10 owned maps, never the borrowed
  module_scopes / import_graph;
- TypeTable.aliases still borrows &self.program_index.type_alias_map, loaned at
  lowerRoot with the same late-binding lifetime.

Extends program_index.test.zig with declaration-map round-trips (fn AST, type
alias, global, module const, foreign class, protocol decl/AST, struct template,
ufcs alias).

Registration logic (registerStructDecl / registerProtocolDecl /
registerForeignClassDecl, ...) stays in Lowering, writing through the index.

Gate green: zig build, zig build test, bash tests/run_examples.sh
(350 passed, 0 failed). lower.zig 19433 -> 19393 lines.
2026-06-02 12:30:11 +03:00
agra
90520eefeb refactor(ir): extract ProgramIndex, move low-fanout decl facts (A1.1a)
Architecture phase A1.1a. Introduce src/ir/program_index.zig as the single
storage owner for declaration-name / import / visibility facts, and move the
three low-fanout maps out of the Lowering state bag:

- import_flags     (owned by ProgramIndex)
- module_scopes    (borrowed pointer into a core.zig-owned map)
- import_graph     (borrowed pointer into a core.zig-owned map)

Lowering embeds one ProgramIndex by value and reaches every moved fact through
self.program_index.<field>; later phases hand collaborator modules a
*ProgramIndex instead of *Lowering. 8 call sites in lower.zig + 2 setters in
core.zig repointed. No duplicate storage, no fallback path; zig build enforces
no missed reference.

Mutation-heavy registration (registerStructDecl etc.) stays in Lowering and
now writes import_flags through the index. High-fanout maps are deferred to
A1.1b.

Adds src/ir/program_index.test.zig (init-empty, import_flags round-trip,
borrowed-view ownership) wired into the ir.zig barrel.

Behavior-preserving: zig build, zig build test, and bash tests/run_examples.sh
(350 passed, 0 failed) all green.
2026-06-02 12:04:31 +03:00
agra
795ce3dc7d test(runner): make example suite checkout-location independent
Diagnostics embed the absolute source path, but normalize() only scrubbed
hex addresses, so expected snapshots baked in the canonical checkout path
(/Users/agra/projects/sx/...). The suite only passed when run from that exact
directory; from a git worktree all 44 path-printing diagnostics mismatched.

Collapse any absolute `.../examples/` or `.../issues/` prefix to the repo-
relative form. The rule runs through normalize(), which is applied identically
to both expected and actual output, so it can only reconcile path noise — it
cannot desync an otherwise-matching pair. No snapshots regenerated.

Suite now reports 350 passed / 0 failed from a worktree as well as the
canonical tree.
2026-06-02 11:18:12 +03:00
agra
bd01d2224d fix(types): check nested closure/function bodies and cast targets (issue 0064)
Closes the two residual silent holes in the unknown-type diagnostic:

- Nested closure / function bodies. The body walk stopped at closure and
  nested-fn boundaries, so a typo'd type in a closure's local annotation
  silently became a 0-field struct. `walkBodyTypes` now descends control
  flow and expressions to re-enter each closure / nested fn via `checkScope`,
  which accumulates that scope's generic + value-`Type` params onto the
  parent's — so an inner closure still sees the outer function's `$T` (no
  false positive) while a genuine unknown is flagged at any nesting depth.
  `harvestScopeDecls` collects type-decl names across the whole body
  (including nested scopes) up front so locals are never false-flagged.

- Cast targets. `cast(T)` where `T` is a value-`Type` param (no `$`) cast to
  a fabricated empty struct silently; it now gets the tailored `$T` hint. An
  unknown *literal* cast target already errors via value resolution, so it's
  left to that path — no double diagnostic.

Suite: 350 passed, 0 failed. Regressions: examples/1114 (nested-closure
annotation), 1115 (cast value param).
2026-06-02 10:57:17 +03:00
agra
63b512a182 fix(types): extend unknown-type check into function bodies (issue 0064)
The signature/field check missed body-level type positions: a local
annotation naming a non-existent type flowed through the empty-struct stub
untouched, so `v: Coordnate = 5` silently compiled and ran (the value
dropped) — an invalid program accepted with no diagnostic.

`checkUnknownTypeNames` now also walks each main-file function body
(`checkBodyTypes`): local var/const type annotations — including inside
if / loop / match / push / defer / onfail blocks and decl-value blocks — are
validated with the enclosing function's generic params in scope, and
body-local `T :: struct/enum/union` declarations are collected first
(`collectBodyDeclNames`) so legitimate locals aren't false-flagged. Nested
function/closure bodies are their own scope and are not descended (safe
under-coverage); explicit `cast(T)` already surfaces its own `unresolved`
diagnostic and is left to it.

Regression: examples/1113 (local annotation of a non-existent type, exit 1).
2026-06-02 10:41:29 +03:00
agra
c490ffcfe9 fix(types): reject unknown type names instead of silent empty struct (issue 0064)
An identifier used in a type position that resolved to nothing fell through
to `type_bridge.resolveTypeName`'s empty-struct-stub fallback, silently
interning a 0-field struct named after the identifier. A value parameter
mistakenly used as a type (`(T: Type, ...) -> T`, missing the `$`) or a
typo'd type name therefore compiled and ran, rendering as `T{}`.

New post-scan diagnostic pass `checkUnknownTypeNames` (lower.zig Pass 1f)
walks every main-file function signature and non-generic struct field type
and rejects any leaf name that is not a primitive, an in-scope generic param
(`$T` / `type_params`), a declared type, or a real (non-stub) registered
type. The load-bearing empty-struct stub is left intact — forward references
and foreign-class opaque types still depend on it during the scan — and the
pass runs before body lowering, so `hasErrors()` halts the build before any
stub reaches codegen.

A value param used as a type gets a tailored hint to write `$T: Type`; a
genuine unknown gets "unknown type 'X'". Imported concrete types are
recognized via the type table, and inline compound spellings (`[:0]u8`),
arbitrary-width ints (`u1`/`u2`), and `$`-introduced generics (`-> $R`) are
exempted to avoid false positives.

Regressions: examples/1111 (tailored hint) + 1112 (typo'd field type).
2026-06-02 10:24:30 +03:00
605 changed files with 115590 additions and 9630 deletions

View File

@@ -573,7 +573,7 @@ Wiring a new bundling step:
| `current/PLAN-ERR.md` | **Active** ERR implementation plan (`!` errors, `try` / `catch` / `or` / `onfail`, return traces). |
| `current/CHECKPOINT-ERR.md` | **Active** ERR progress tracker. Update after every step. |
| `implementation_plan.md` | Archive of completed work (closures, protocols, etc.). Do not pick up tasks from here. |
| `readme.md` | Original syntax sketches. Do not modify. |
| `readme.md` | User-facing language overview — **maintained**. Update it whenever a user-facing sx change lands (new/changed syntax, semantics, gating diagnostics, language behavior), per the docs-track-changes rule. |
| `CLAUDE.md` | This file. Session instructions. |
| `library/modules/platform/bundle.sx` | sx-side `.app` / `.apk` bundler. See "Bundling lives in sx" above. |
| `library/modules/fs.sx`, `library/modules/process.sx` | POSIX stdlib for the bundler + general consumer use. |

View File

@@ -193,6 +193,16 @@ pub fn build(b: *std.Build) void {
run_cmd.addArgs(args);
}
// Corpus paths for the LSP corpus-sweep test (src/lsp/corpus_sweep.test.zig).
// Inject absolute corpus dirs at configure time so the in-process analyzer
// sweep is CWD-independent; the test still ENUMERATES the directory
// contents at runtime (new examples are covered with no test edit).
const corpus_opts = b.addOptions();
corpus_opts.addOption([]const u8, "examples_dir", b.path("examples").getPath(b));
corpus_opts.addOption([]const u8, "issues_dir", b.path("issues").getPath(b));
corpus_opts.addOption([]const u8, "library_dir", b.path("library").getPath(b));
mod.addOptions("corpus_paths", corpus_opts);
const mod_tests = b.addTest(.{
.root_module = mod,
});

View File

@@ -40,7 +40,7 @@ to satisfy all three. "JIT" and "comptime" are **not** the same thing.
|---|---|---|
| **AOT** (`sx build`) | native machine code in an on-disk binary | pointer to an interned `Frame` |
| **JIT** (`sx run`) | ORC-JIT'd machine code in anonymous memory | pointer to an interned `Frame` |
| **Comptime** (`#run`) | the IR interpreter (`interp.zig`) — no machine code | packed `(func_id, ir_offset)` |
| **Comptime** (`#run`) | the IR interpreter (`interp.zig`) — no machine code | packed `(func_id, span.start)` |
The crucial constraint: **the same lowered IR runs in the compiled
backend *and* the interpreter.** So a value the IR produces (like a trace
@@ -92,39 +92,55 @@ so the location — *and the offending source line itself* (`line_text`, for the
formatter reads it directly. No PC capture, no DWARF, no symbolizer, no runtime
file read.
A comptime frame is instead a packed `(func_id: u32, ir_offset: u32)`,
resolved through the interpreter's in-memory IR/source tables. The
interpreter **never dereferences the compiled `Frame` pointer** — it uses
its own representation — so the compiled and interpreted memory models
never collide.
A comptime frame is instead a packed `(func_id: u32, span.start: u32)`
where `span.start` is the op's source byte offset — resolved through the
interpreter's in-memory IR/source tables. The interpreter **never
dereferences the compiled `Frame` pointer** — it uses its own
representation — so the compiled and interpreted memory models never
collide.
### The niladic trace-push op
Because the same IR runs in both machines, the push is a **dedicated,
niladic, span-stamped IR op** — the same pattern as `is_comptime` /
`interp_print_frames`. It carries **no operands and no global reference**;
each backend derives the frame from its own context:
Because the same IR runs in both machines, the frame value comes from a
**dedicated, niladic, span-stamped IR op** (`.trace_frame`) — the same
pattern as `is_comptime` / `interp_print_frames`. It carries **no operands
and no global reference**; each backend derives the frame from its own
context:
- **`emit_llvm`:** resolves the op's `span` + current function →
`{file, line, col, func}` (reusing the source map wired in for DWARF),
**interns and builds the `Frame` global in `emit_llvm`** (the same
mechanism as the tag-name table), then emits `call sx_trace_push(ptr)`.
- **`interp`:** pushes the packed `(func_id, ir_offset)` from its own
execution context.
- **`emit_llvm` (the `.trace_frame` arm):** resolves the op's `span` +
current function → `{file, line, col, func}` (reusing the source map
wired in for DWARF), **interns and builds the `Frame` global** in
[`src/backend/llvm/reflection.zig`](../src/backend/llvm/reflection.zig)
(the same mechanism, in the same file, as the tag-name table), and yields
its address as the op's value. The lowerer feeds that value to a separate
`sx_trace_push` call emitted through the normal call lowering.
- **`interp`:** yields the packed `(func_id, span.start)` from its own
execution context as the op's value. The separate `sx_trace_push` call
op consuming it is executed by the interp as a foreign call (via
`host_ffi`/dlsym, the same path as any extern), storing the packed value
in the buffer; the comptime `.trace_resolve` resolver later recovers
`file:line:col` from it.
This keeps the lowerer thin: at each push site it emits the op and nothing
else — no operand wiring, no global construction. The rejected
alternative — an op carrying a `GlobalId` to an IR-level `Frame` global —
would make the global visible to the interpreter (forcing comptime onto
the pointer-deref path) and fatten the lowerer; **do not do this.**
The op stays niladic by design: it carries no operand and no `GlobalId`,
so no IR-level `Frame` global is ever visible to the interpreter. The
rejected alternative — an op carrying a `GlobalId` to an IR-level `Frame`
global — would make the global visible to the interpreter (forcing
comptime onto the pointer-deref path) and fatten the lowerer; **do not do
this.**
`Frame` is defined **once** in sx (`trace.sx`/std); `emit_llvm` builds the
interned global off that `TypeId` through the normal struct-emission path,
never a bespoke byte layout (which would risk the "8-bytes-assumed"
clobber class of bug). `file`/`func` strings are interned into a shared
pool so a path shared by N push sites is stored once — the table stays
tiny. File paths are normalized to a stable relative form so trace output
is machine-independent and snapshot-testable.
`Frame` is defined **once** in sx (`trace.sx`/std), and its runtime layout —
`{ string file, i32 line, i32 col, string func, string line_text }` — is
mirrored by the cached LLVM **literal (anonymous) struct type** `getFrameStructType()`
(`src/ir/emit_llvm.zig`). The reflection builder
(`src/backend/llvm/reflection.zig`) assembles each push site's global as an
LLVM **named-struct constant** over that cached type via
`LLVMConstNamedStruct` — a type-safe LLVM struct, not hand-packed bytes
(which would risk the "8-bytes-assumed" clobber class of bug). It does
**not** derive the layout from the sx `Frame` `TypeId`, nor route through
the normal struct-emission path. `file`/`func`/`line_text` strings are
interned into a shared pool so a path shared by N push sites is stored once
— the table stays tiny. The `file` field is the source basename (full paths
live in DWARF), so trace output is machine-independent and snapshot-testable.
### Push and clear sites
@@ -193,8 +209,9 @@ stripped without affecting traces.
### What's emitted
In [`src/ir/emit_llvm.zig`](../src/ir/emit_llvm.zig), gated on the same
debug opt levels + a wired source map (`setDebugContext`):
In [`src/backend/llvm/debug.zig`](../src/backend/llvm/debug.zig) (the
`DebugInfo` helper, driven from `emit_llvm`'s `emit()` pipeline), gated on
the same debug opt levels + a wired source map (`setDebugContext`):
- one `DICompileUnit` + `DIFile` on the main file,
- a `DISubprogram` per emitted function (`LLVMSetSubprogram`),
@@ -237,10 +254,12 @@ both the trace path and the DWARF path. Items marked ✅ exist today;
|---|---|
| [`src/core.zig`](../src/core.zig) | `Compilation`: owns `import_sources` (file→source map), constructs the emitter, calls `setDebugContext` + `emit`; re-enters the interpreter for `#run`/post-link |
| [`src/ir/lower.zig`](../src/ir/lower.zig) | AST→IR. Stamps `Inst.span`; emits push/clear at failure/absorb sites; `tracesEnabled` gate; declares the `sx_trace_*` externs |
| [`src/ir/emit_llvm.zig`](../src/ir/emit_llvm.zig) | IR→LLVM. Builds the interned `Frame` table; lowers the push op to a pointer push; emits all DWARF metadata |
| [`src/ir/interp.zig`](../src/ir/interp.zig) | Comptime IR interpreter. Lowers the push op to a packed `(func_id, offset)`; resolves comptime frames |
| [`src/ir/emit_llvm.zig`](../src/ir/emit_llvm.zig) | IR→LLVM orchestrator. Owns `LLVMEmitter` + the source map (`setDebugContext`); dispatches the `.trace_frame` op and the DWARF passes to the helpers below |
| [`src/backend/llvm/reflection.zig`](../src/backend/llvm/reflection.zig) | `Reflection`: builds the interned `Frame` table + the tag-name / type-name tables; yields the `.trace_frame` op's value (the `Frame` global's address) — the `sx_trace_push` call itself is emitted by `lower.zig` |
| [`src/backend/llvm/debug.zig`](../src/backend/llvm/debug.zig) | `DebugInfo`: builds all DWARF metadata (compile unit, per-function subprograms, per-instruction `DILocation`) |
| [`src/ir/interp.zig`](../src/ir/interp.zig) | Comptime IR interpreter. The `.trace_frame` op yields a packed `(func_id, span.start)`; the separate `sx_trace_push` call op runs as a foreign call (dlsym); `.trace_resolve` recovers comptime frames |
| [`src/errors.zig`](../src/errors.zig) | `SourceLoc.compute(source, offset) → {line, col}`; the `import_sources` map type |
| [`src/ir/inst.zig`](../src/ir/inst.zig) | `Inst.span`, `Function.source_file`, the `Op` union (home of the trace-push op) |
| [`src/ir/inst.zig`](../src/ir/inst.zig) | `Inst.span`, `Function.source_file`, the `Op` union (home of the `.trace_frame` op) |
| [`library/vendors/sx_trace_runtime/sx_trace.c`](../library/vendors/sx_trace_runtime/sx_trace.c) | the thread-local ring buffer + `sx_trace_report_unhandled` |
| [`library/modules/trace.sx`](../library/modules/trace.sx) | the formatter (`to_string` / `print_current`) |
| [`src/llvm_api.zig`](../src/llvm_api.zig) | binds `llvm-c/Core.h` + `llvm-c/DebugInfo.h` |
@@ -261,7 +280,7 @@ traces and DWARF can never disagree:
which file a function's spans index.
-`errors.SourceLoc.compute(source, span.start)` turns an offset into
`{line, col}`. Used by the diagnostics renderer, `#caller_location`,
the DWARF emitter, and (planned) the trace formatter — one function,
the DWARF emitter, and the trace formatter — one function,
every consumer.
### Trace path: compile → run → format
@@ -270,17 +289,23 @@ traces and DWARF can never disagree:
1. `lower.zig` reaches a failure site — `lowerRaise`, `lowerTry`'s
propagation branch, `lowerFailableOr`, or `lowerDestructureDecl` — and
(when `tracesEnabled()`) emits the niladic `.trace_frame_push` op,
replacing today's `emitTracePush(placeholderTraceFrame())`. Absorbing
sites emit `emitTraceClear()``call sx_trace_clear()`.
2. **Compiled backend** (`emit_llvm.emitInst`, `.trace_frame_push` arm):
(when `tracesEnabled()`) emits the niladic `.trace_frame` op via
`placeholderTraceFrame()`, whose result feeds a separate `sx_trace_push`
call via `emitTracePush()`. Absorbing sites emit `emitTraceClear()`
`call sx_trace_clear()`.
2. **Compiled backend** (`emit_llvm.emitInst`, `.trace_frame` arm):
resolve the op's `span` + current function → `{file,line,col,func}`,
intern into the `Frame` table (built alongside `tag_name_array`), and
emit `call sx_trace_push(ptr_to_Frame)`. The `sx_trace_push` extern is
yield the `Frame` global's address as the op's value, which the separate
`sx_trace_push` call (step 1) consumes. The `sx_trace_push` extern is
declared lazily by `getTraceFids()` (which sets `needs_trace_runtime`).
3. **Interpreter** (`interp.zig`, same op): pack `(current_func_id,
ir_offset)` into a `u64` and call the foreign `sx_trace_push` (resolved
via `host_ffi` `dlsym` against the linked `sx_trace.c`).
span.start)` into a `u64` and return it as the op's value. The separate
`sx_trace_push` call op is then executed by the interp as a foreign call
(`callForeign` → `host_ffi.lookupSymbol`/dlsym, the same path as any
extern), storing the packed value in the buffer. The comptime
`.trace_resolve` resolver later turns each packed value back into
`file:line:col` via the IR/source tables.
**Buffer (run time) ✅** — `sx_trace.c` stores the `u64`s. Linked into the
compiler so the JIT resolves `sx_trace_*` via `dlsym`; auto-injected as a
@@ -288,10 +313,10 @@ compiler so the JIT resolves `sx_trace_*` via `dlsym`; auto-injected as a
**Formatter (run time) ✅ (compiled 3a, comptime 3b)** — `trace.sx` `to_string()` loops
`sx_trace_len()` / `sx_trace_frame_at(i)` and resolves each `u64` through
a **read-side context-split primitive** (the mirror of the push op):
a **read-side context-split primitive** (the mirror of the `.trace_frame` op):
- compiled: cast the `u64` → `*Frame`, load the fields.
- comptime: unpack `(func_id, offset)`, resolve via the interpreter's
- comptime: unpack `(func_id, span.start)`, resolve via the interpreter's
IR/source tables → a `Frame`.
The same `trace.sx` source works in both because it runs in the matching
@@ -330,8 +355,8 @@ the failable-`main` wrapper, whose `ret` path in `emit_llvm`
### The gate: one switch, two consumers
`Lowering.tracesEnabled()` (lower.zig) and `LLVMEmitter.debugEnabled()`
(emit_llvm) both reduce to `opt_level == .none or .less`. The `Frame`
`Lowering.tracesEnabled()` (lower.zig) and `DebugInfo.debugEnabled()`
(backend/llvm/debug.zig) both reduce to `opt_level == .none or .less`. The `Frame`
table + push/clear ride `tracesEnabled`; DWARF rides `debugEnabled`.
Release (`-O2`/`-O3`) emits neither. `sx run` defaults to `-O0` (both on);
`sx ir`/`sx asm` default to `-O2` (both off) — which is why the `.ir`
@@ -372,7 +397,7 @@ symbolization is delegated to the platform debugger — sx ships none.
| Artifact | Lookup | Size | Shipped in release? |
|---|---|---|---|
| **Tag-name table** | tag id → name string | tiny (per distinct tag) | **yes, always** — `{}` interpolation, the `main` wrapper, and the trace's "raised error.X" line need names even in release |
| **Tag-name table** | tag id → name string | tiny (per distinct tag) | **yes, always** — `{}` interpolation and the failable-`main` reporter's `error: unhandled error reached main: error.X` line need names even in release |
| **`Frame` location table** | push site → `{file,line,col,func}` | small (interned strings; per push site) | **debug / `--release-traces` only** — rides the trace-mode gate |
| **DWARF (`.debug_line` / `DISubprogram`)** | PC → file:line:col, for *debuggers* | larger (per source position) | **debug / `--release-traces` only**, strippable; consumed by `lldb`/`gdb`, never by the trace formatter |
@@ -455,7 +480,7 @@ a Mach-O debug map, never register JIT DWARF.
| IR instructions carry source spans | ✅ done — E3.0 slice 1 (`b44a5d0`) |
| DWARF emission (compile unit / subprogram / line table) | ✅ done — E3.0 slice 2 (`c32d694`) |
| Niladic trace-push op + interned `Frame` table (runtime) | ✅ done — E3.3 slice 3a (`1b6cbc1`) |
| Comptime resolver (`func_id, ir_offset` → location) | ✅ done — slice 3b |
| Comptime resolver (`func_id, span.start` → location) | ✅ done — slice 3b |
| Source snippet + `^` caret | ✅ done — slice 3c (line embedded in `Frame`) |
| `--emit-obj` artifact plumbing | ✅ done — slice 3d |
| Stepping verification: macOS lldb | ✅ done — 3e rung 1 (`tests/debug_stepping_smoke.sh`) |

View File

@@ -317,16 +317,23 @@ v := parse(s) catch e {
```
```
error trace:
raised error.BadDigit
at parse_digit (parse.sx:12:5)
at parse_int (parse.sx:34:13)
at handle_line (main.sx:21:8)
error return trace (most recent call last):
parse_digit at parse.sx:12:5
c := s[i] or raise error.BadDigit;
^
parse_int at parse.sx:34:13
try parse_digit(s);
^
handle_line at main.sx:21:8
try parse_int(line);
^
```
Traces are on by default in debug builds and compiled out in release
(re-enable with `--release-traces`). They cost nothing on the success
path. Frame locations resolve through the binary's debug info, so
path. Each frame's location comes from `Frame` metadata
(file/line/col/func) baked in at the trace point — the trace resolves
itself with no debug info. Separately, sx emits standard DWARF, so
`lldb` / `gdb` work on sx binaries too.
Interpolating a tag with `{}` prints its **name**, not a number — in

View File

@@ -0,0 +1,23 @@
// Call-site default-argument expansion (`appendDefaultArgs` / `expandCallDefaults`
// in lowerCall). A call that omits a trailing parameter with a default value
// has the default expression spliced in at the call site; an explicit argument
// overrides it. Two trailing defaults cover the "fill all remaining" path.
// Path-free IR (literal defaults) so the `.ir` snapshot is location-stable.
#import "modules/std.sx";
scale :: (n: s32, factor: s32 = 2) -> s32 { n * factor }
label :: (n: s32, prefix: string = "v", suffix: string = "!") -> s32 {
print("{}{}{}\n", prefix, n, suffix);
n
}
main :: () {
print("default: {}\n", scale(5));
print("explicit: {}\n", scale(5, 3));
_ = label(1); // both defaults filled
_ = label(2, "x"); // suffix default filled
_ = label(3, "y", "?"); // no defaults
}

View File

@@ -0,0 +1,53 @@
// String `==`/`!=` as an operand of a short-circuit `and`/`or`.
//
// A string compare lowers to its own multi-block memcmp sub-CFG, so the
// operand finishes in a later basic block than the one the short-circuit
// started in. The `and`/`or` merge PHI must take that actual block as the
// incoming predecessor.
//
// Regression (issue 0078): combining string equality with `and`/`or` used to
// emit invalid LLVM (`PHI node entries do not match predecessors!`).
#import "modules/std.sx";
Json :: enum {
str: string;
int_: s64;
null_;
}
main :: () {
a := "k";
b := "v";
// string == on both sides of `and`
and_tt := a == "k" and b == "v";
and_tf := a == "k" and b == "x";
and_ft := a == "z" and b == "v";
print("and: {} {} {}\n", and_tt, and_tf, and_ft);
// string == on both sides of `or`
or_ff := a == "z" or b == "x";
or_tf := a == "k" or b == "x";
or_ft := a == "z" or b == "v";
print("or: {} {} {}\n", or_ff, or_tf, or_ft);
// string == feeding both `and` and `or` in one expression
mixed := a == "k" and b == "v" or a == "z";
print("mixed: {}\n", mixed);
// string `!=` operands too
ne := a != "z" and b != "z";
print("ne: {}\n", ne);
// the larger shape: a match-expression value plus an enum-payload string
// == combined under `and`/`or`.
v : Json = .str("v");
kind := if v == {
case .str: 1;
case .int_: 2;
case .null_: 3;
}
ok := kind == 1 and v.str == "v";
bad := kind == 2 or v.str == "x";
print("payload: {} {}\n", ok, bad);
}

View File

@@ -0,0 +1,37 @@
// Integer `{}` formatting across the full signed/unsigned range.
//
// Regression (issue 0090): the `{}` formatter was s64-based — it negated
// the value to print the sign (so s64::MIN, whose magnitude is
// unrepresentable as a positive s64, rendered as a bare "-"), and it had
// no unsigned-aware path (so a u64 all-ones value printed as the s64
// reinterpretation, "-1"). Both extremes now render correctly: signed
// MIN prints all its digits, and unsigned integers print as unsigned
// decimal across all 64 bits.
#import "modules/std.sx";
main :: () {
// Signed extreme: magnitude is never negated, so MIN survives.
print("s64.min={}\n", s64.min);
print("s64.max={}\n", s64.max);
// Unsigned extreme: all 64 bits as unsigned decimal, not -1.
print("u64.max={}\n", u64.max);
// Spread across widths — signed.
print("s8.min={} s8.max={}\n", s8.min, s8.max);
print("s16.min={} s16.max={}\n", s16.min, s16.max);
print("s32.min={} s32.max={}\n", s32.min, s32.max);
// Spread across widths — unsigned (max is all-ones for that width).
print("u8.max={} u16.max={}\n", u8.max, u16.max);
print("u32.max={}\n", u32.max);
// Mins of unsigned widths and zero.
print("u8.min={} u64.min={} zero={}\n", u8.min, u64.min, 0);
// Ordinary signed/unsigned values still print correctly.
neg : s32 = -42;
pos : u32 = 4000000000;
print("neg={} pos={}\n", neg, pos);
}

View File

@@ -0,0 +1,13 @@
// A local declared with a reserved/builtin type-name spelling (`s2` is the
// arbitrary-width `sN` integer type) is rejected at the declaration site.
// Previously such a name parsed as a `.type_expr`, so address-of sites
// mis-lowered it (load-by-value to a `ptr` param → LLVM verifier abort, or a
// silent `*self`-mutation-losing copy). Regression (issue 0076). Expected:
// error at the declaration; exit 1.
#import "modules/std.sx";
main :: () -> s32 {
s2 := 42;
print("s2: {}\n", s2);
return 0;
}

View File

@@ -1,19 +0,0 @@
#import "modules/std.sx";
#import "modules/math/math.sx";
#import "modules/compiler.sx";
#import "modules/test.sx";
pkg :: #import "modules/testpkg";
main :: () {
// ========================================================
// 21. TYPE-NAMED VARIABLES (s2, u8, etc.)
// ========================================================
print("=== 21. Type-Named Vars ===\n");
{
s2 := 42;
print("s2: {}\n", s2);
s2 = s2 + 1;
print("s2+1: {}\n", s2);
}
}

View File

@@ -0,0 +1,24 @@
// Forward identifier type alias — an alias whose target is declared LATER
// in the file resolves the same as an ordered one. `MyChain :: MyInt;`
// appears before `MyInt :: s32;`, yet `MyChain` resolves to `s32` and a
// forward chain (`A :: B; B :: C; C :: u8;`) converges too.
// Regression (issue 0069): the scan only registered identifier aliases whose
// target was already known, so a forward alias was falsely flagged
// `unknown type`. Now a fixpoint pass over the scanned decls resolves them.
#import "modules/std.sx";
MyChain :: MyInt;
MyInt :: s32;
A :: B;
B :: C;
C :: u8;
main :: () -> s32 {
v: MyChain = 7;
n: A = 3;
print("chain s32: {}\n", size_of(MyChain));
print("forward u8: {}\n", size_of(A));
print("v + n: {}\n", v + cast(s32) n);
return v;
}

View File

@@ -0,0 +1,22 @@
// Forward identifier type alias as a TOP-LEVEL annotation — a global var
// and a typed module constant whose annotation is a forward alias
// (`A :: B; B :: s32;`) resolve to the alias target, the same as the
// ordered form, instead of a fabricated stub.
// Regression (issue 0070): top-level global / typed-const annotations were
// resolved inside the scan loop BEFORE the forward-alias fixpoint ran, so
// `g : A` got a stub type that mismatched its initializer at LLVM
// verification. Global/const annotation resolution now runs in scan pass 2,
// after the fixpoint.
#import "modules/std.sx";
A :: B;
B :: s32;
g : A = 7;
K : A : 35;
main :: () -> s32 {
print("global g: {}\n", g);
print("const K: {}\n", K);
return g + K;
}

View File

@@ -0,0 +1,18 @@
// Top-level global initialized from a module constant copies the constant's
// value (not a silent zero). `K : A : 42; g : A = K;` resolves the forward
// alias `A` to `s32` and materializes `g`'s static initializer from `K`.
// Regression (issue 0071): `registerTopLevelGlobal`'s init_val switch only
// handled literals/array/struct literals; an identifier initializer fell
// through to a null payload and the global silently zero-initialized.
#import "modules/std.sx";
A :: B;
B :: s32;
K : A : 42;
g : A = K;
main :: () -> s32 {
print("g={}\n", g);
return g;
}

View File

@@ -0,0 +1,30 @@
// A `*self`-mutating streaming pattern with NON-reserved binding names
// (`hasher`, `ctx`) compiles and accumulates state correctly through BOTH
// call styles — explicit address-of `update(@h, ...)` and autoref
// `h.update(...)` — across multiple mutating calls. Proves the
// `.identifier`-only address-of paths in lowering are correct as-is, with no
// type-shaped-name special-case (companion to the issue-0076 rejection of
// type-named identifiers).
#import "modules/std.sx";
Hasher :: struct { total: s64 = 0; count: s64 = 0; }
update :: (self: *Hasher, n: s64) {
self.total += n;
self.count += 1;
}
main :: () -> s32 {
hasher := Hasher.{ total = 0, count = 0 };
update(@hasher, 10); // explicit address-of receiver
hasher.update(20); // autoref receiver
update(@hasher, 30);
hasher.update(40);
print("hasher total={} count={}\n", hasher.total, hasher.count);
ctx := Hasher.{ total = 100, count = 0 };
ctx.update(5);
update(@ctx, 7);
print("ctx total={} count={}\n", ctx.total, ctx.count);
return 0;
}

View File

@@ -0,0 +1,57 @@
// A store to a module-global array element writes the global's live storage,
// so a subsequent read sees the stored value — not the array initializer.
// Covers constant index, variable index, and a cross-function store, on a
// scalar global array, a struct-element global array (element-stride), and a
// nested-array global (recursive lvalue).
// Regression (issue 0079): global-array element stores were silently dropped
// (read returned the initializer) because the indexed lvalue base loaded the
// global by value into a temp instead of addressing the global's storage.
#import "modules/std.sx";
g : [3]s64 = .[10, 20, 30];
Pair :: struct { a: s64; b: s64; }
gp : [2]Pair = .[ .{ a = 1, b = 2 }, .{ a = 3, b = 4 } ];
grid : [2][3]s64 = .[ .[0, 0, 0], .[0, 0, 0] ];
write_global :: (i: s64, v: s64) { g[i] = v; }
main :: () {
// Scalar global array — const index.
g[1] = 222;
print("g[1]={}\n", g[1]); // 222
// Scalar global array — variable index.
k := 2;
g[k] = 333;
print("g[k]={}\n", g[k]); // 333
// Scalar global array — store from another function.
write_global(0, 111);
print("g[0]={}\n", g[0]); // 111
// Struct-element global array (16-byte stride) — const and var index.
gp[0] = .{ a = 10, b = 20 };
j := 1;
gp[j] = .{ a = 30, b = 40 };
print("gp[0]={},{}\n", gp[0].a, gp[0].b); // 10,20
print("gp[j]={},{}\n", gp[j].a, gp[j].b); // 30,40
// Nested-array global — element is [3]s64, recursive indexed lvalue.
grid[1][2] = 7;
r := 0;
grid[r][0] = 5;
print("grid[1][2]={}\n", grid[1][2]); // 7
print("grid[0][0]={}\n", grid[r][0]); // 5
if g[1] == 222 and g[2] == 333 and g[0] == 111
and gp[0].a == 10 and gp[0].b == 20
and gp[1].a == 30 and gp[1].b == 40
and grid[1][2] == 7 and grid[0][0] == 5 {
print("PASS\n");
} else {
print("FAIL: global array element store dropped\n");
}
}

View File

@@ -0,0 +1,49 @@
// A module-global aggregate (array of struct literals, a struct literal, and
// nested array/struct shapes) materializes its DECLARED field values into the
// global's static initializer, so reading the fields without any prior store
// returns the literal values — not zero.
// Regression (issue 0080): a global `[N]Struct` initialized with struct literals
// was emitted as `zeroinitializer`, silently dropping every field, because the
// constant-aggregate serializer had no struct-literal arm and collapsed the
// whole initializer to null. The fix threads the element/field type so struct
// and nested-array leaves serialize correctly; a genuinely non-constant
// initializer is now rejected loudly instead of silently zeroed.
#import "modules/std.sx";
Pair :: struct { a: s64; b: s64; }
WithArr :: struct { id: s64; xs: [3]s64; }
// global array of struct literals
pairs : [2]Pair = .[ .{ a = 1, b = 2 }, .{ a = 3, b = 4 } ];
// global struct literal
solo : Pair = .{ a = 7, b = 9 };
// global struct containing a fixed array (struct-with-array)
wa : WithArr = .{ id = 5, xs = .[ 11, 22, 33 ] };
// nested: global array of structs each containing an array
nested : [2]WithArr = .[ .{ id = 1, xs = .[ 1, 2, 3 ] }, .{ id = 2, xs = .[ 4, 5, 6 ] } ];
main :: () {
// Read the declared initializer values back with NO prior store.
print("pairs={},{} {},{}\n", pairs[0].a, pairs[0].b, pairs[1].a, pairs[1].b);
print("solo={},{}\n", solo.a, solo.b);
print("wa={} xs={},{},{}\n", wa.id, wa.xs[0], wa.xs[1], wa.xs[2]);
print("nested0={} xs={},{},{}\n", nested[0].id, nested[0].xs[0], nested[0].xs[1], nested[0].xs[2]);
print("nested1={} xs={},{},{}\n", nested[1].id, nested[1].xs[0], nested[1].xs[1], nested[1].xs[2]);
// A store on top of the materialized initializer still works (live storage).
pairs[0].a = 100;
nested[1].xs[2] = 999;
print("after-store={} {}\n", pairs[0].a, nested[1].xs[2]);
if pairs[0].b == 2 and pairs[1].a == 3 and pairs[1].b == 4
and solo.a == 7 and solo.b == 9
and wa.id == 5 and wa.xs[0] == 11 and wa.xs[2] == 33
and nested[0].id == 1 and nested[0].xs[0] == 1 and nested[0].xs[2] == 3
and nested[1].id == 2 and nested[1].xs[0] == 4
and pairs[0].a == 100 and nested[1].xs[2] == 999 {
print("PASS\n");
} else {
print("FAIL: global aggregate literal initializer zeroed\n");
}
}

View File

@@ -0,0 +1,48 @@
// A module-global aggregate initializer may carry `null` in a pointer field:
// `null` is a compile-time constant (the zero pointer), so the field reads back
// as null with NO prior store, and its non-pointer neighbors keep their declared
// values. Covered shapes: an array-of-struct with a null pointer field, a global
// array of all-null pointers, and a nested struct-in-struct with a null pointer.
// Regression (issue 0081): the constant-aggregate serializer had no
// `.null_literal` arm, so a `null` in a pointer field made the whole aggregate
// look non-constant and the global was rejected with "must be initialized by a
// compile-time constant". The fix serializes a null literal to a constant zero
// pointer (the same way a top-level pointer global `p : *s64 = null;` does)
// while still rejecting genuinely non-constant fields (see diagnostics 1126).
#import "modules/std.sx";
Box :: struct { p: *s64; marker: s64; }
Inner :: struct { q: *s64; tag: s64; }
Outer :: struct { inner: Inner; label: s64; }
// array-of-struct with null pointer fields + scalar neighbors
boxes : [2]Box = .[ .{ p = null, marker = 11 }, .{ p = null, marker = 22 } ];
// global array of all-null pointers
ptrs : [3]*s64 = .[ null, null, null ];
// nested: struct containing a struct with a null pointer field
nested : [2]Outer = .[
.{ inner = .{ q = null, tag = 1 }, label = 100 },
.{ inner = .{ q = null, tag = 2 }, label = 200 },
];
main :: () {
print("boxes ptrs={},{} markers={},{}\n",
boxes[0].p == null, boxes[1].p == null, boxes[0].marker, boxes[1].marker);
print("ptr arr nulls={},{},{}\n", ptrs[0] == null, ptrs[1] == null, ptrs[2] == null);
print("nested q nulls={},{} tags={},{} labels={},{}\n",
nested[0].inner.q == null, nested[1].inner.q == null,
nested[0].inner.tag, nested[1].inner.tag,
nested[0].label, nested[1].label);
if boxes[0].p == null and boxes[1].p == null
and boxes[0].marker == 11 and boxes[1].marker == 22
and ptrs[0] == null and ptrs[1] == null and ptrs[2] == null
and nested[0].inner.q == null and nested[1].inner.q == null
and nested[0].inner.tag == 1 and nested[1].inner.tag == 2
and nested[0].label == 100 and nested[1].label == 200 {
print("PASS\n");
} else {
print("FAIL: global aggregate null pointer field mis-serialized\n");
}
}

View File

@@ -0,0 +1,51 @@
// A module-global initialized with an enum literal (`.Variant`) reads back the
// declared tag — scalar, inside a global array, and as a struct field, for both
// a plain enum (tag == declaration index) and an explicit-value enum (`enum u16
// { ok :: 200; ... }`, larger backing for element-stride coverage).
// Regression (issue 0082): `globalInitValue` had an `.enum_literal => null`
// carve-out (kept for the compiler-injected `OS`/`ARCH` globals) that silently
// zero-initialized EVERY enum global to the first tag — so `chosen : Color =
// .green` read back as `.red` — while a global array/struct of enums was
// rejected outright as non-constant. The fix serializes the enum literal to its
// tag value (respecting explicit variant values) against the destination enum
// type, for the scalar global, the array element, and the nested aggregate
// field. (Explicit-value enums print as `.` because the `{}` formatter indexes
// variants by position — a separate, pre-existing limitation — so those are
// asserted by equality, not by their printed name.)
#import "modules/std.sx";
Color :: enum u8 { red; green; blue; }
Code :: enum u16 { ok :: 200; not_found :: 404; teapot :: 418; }
Pair :: struct { a: Color; b: Color; }
Row :: struct { status: Code; pad: s64; }
// scalar enum global
chosen : Color = .green;
// global array of enum
palette : [3]Color = .[ .blue, .green, .red ];
// enum field(s) inside a global struct
pair : Pair = .{ a = .blue, b = .green };
// explicit-value enum: scalar, array (2-byte stride), and inside a struct array
status : Code = .teapot;
codes : [3]Code = .[ .ok, .not_found, .teapot ];
rows : [2]Row = .[ .{ status = .not_found, pad = 11 }, .{ status = .teapot, pad = 22 } ];
main :: () {
print("chosen={}\n", chosen);
print("palette={},{},{}\n", palette[0], palette[1], palette[2]);
print("pair.a={} pair.b={}\n", pair.a, pair.b);
if chosen == .green
and palette[0] == .blue and palette[1] == .green and palette[2] == .red
and pair.a == .blue and pair.b == .green
and status == .teapot
and codes[0] == .ok and codes[1] == .not_found and codes[2] == .teapot
and rows[0].status == .not_found and rows[0].pad == 11
and rows[1].status == .teapot and rows[1].pad == 22 {
print("PASS\n");
} else {
print("FAIL: global enum-literal initializer mis-serialized\n");
}
}

View File

@@ -0,0 +1,69 @@
// A fixed array whose dimension is a module-global named constant
// (`N :: 16; [N]T`) has the same layout as a literal-dimension array
// (`[16]T`): correct length and element stride for scalar, slice/pointer
// (string), and struct element types — on EVERY type-resolution path:
// direct local decls, type aliases (`Arr :: [N]T`), nested fixed arrays
// (`[N][M]T`), and inline union fields. The named dim must resolve to the
// same length whether it flows through the stateful body-lowering resolver
// or the stateless registration-time resolver (type_bridge).
// Regression (issue 0083): a named-const dim resolved to length 0, giving a
// 0-byte alloca — scalar reads returned garbage and string/struct elements
// bus-errored. The alias and union-field paths went through the stateless
// resolver, which had no const table and silently fabricated a 0 length.
#import "modules/std.sx";
N :: 4;
M :: 3;
P :: struct { x: s64; y: s64; }
// Type aliases whose dimension is the named const N (stateless registration).
Arr :: [N]s64;
SArr :: [N]string;
// Inline union field with a named-const dimension (stateless registration).
U :: union { a: [N]s64; tag: s64; }
main :: () {
// Scalar elements (direct local): store then read back.
a : [N]s64 = ---;
a[0] = 7;
a[3] = 42;
print("scalar a0={} a3={}\n", a[0], a[3]);
// Slice/pointer elements (string, direct local): used to bus-error.
s : [N]string = ---;
s[0] = "hi";
s[1] = "yo";
print("string s0={} s1={}\n", s[0], s[1]);
// Struct elements (direct local).
ps : [N]P = ---;
ps[0] = P.{ x = 1, y = 2 };
ps[2] = P.{ x = 5, y = 6 };
print("struct p0x={} p0y={} p2x={}\n", ps[0].x, ps[0].y, ps[2].x);
// Type-alias dimension (scalar): same layout as the direct `[N]s64`.
aa : Arr = ---;
aa[0] = 11;
aa[3] = 99;
print("alias a0={} a3={}\n", aa[0], aa[3]);
// Type-alias dimension (string): no bus error, correct reads.
sa : SArr = ---;
sa[0] = "al";
sa[2] = "ok";
print("alias s0={} s2={}\n", sa[0], sa[2]);
// Nested fixed array `[N][M]s64`: both dimensions are named consts.
grid : [N][M]s64 = ---;
grid[0][0] = 1;
grid[3][2] = 8;
print("nested g00={} g32={}\n", grid[0][0], grid[3][2]);
// Inline union field with a named-const dimension.
u : U = ---;
u.a[0] = 70;
u.a[3] = 7;
print("union u0={} u3={}\n", u.a[0], u.a[3]);
}

View File

@@ -0,0 +1,34 @@
// A `.[...]` array/slice literal passed DIRECTLY as a call argument behaves
// identically to binding it to a typed local first: the literal is
// materialized into addressable storage and a {ptr,len} slice header is built
// over it, so the callee reads the element CONTENTS correctly.
// Regression (issue 0084): a direct literal arg passed the raw array value
// where a slice was expected, so the callee read its header off the wrong
// bytes and returned garbage (0).
#import "modules/std.sx";
count_nope :: (xs: []string) -> s64 {
n := 0;
i := 0;
while i < xs.len { if xs[i] == "nope" { n += 1; } i += 1; }
return n;
}
sum :: (xs: []s64) -> s64 {
s := 0;
i := 0;
while i < xs.len { s += xs[i]; i += 1; }
return s;
}
main :: () {
// string slice: direct literal vs local-bound — both see 2 "nope"s.
print("str direct={}\n", count_nope(.["a", "nope", "b", "nope"]));
local : []string = .["a", "nope", "b", "nope"];
print("str local={}\n", count_nope(local));
// numeric slice: direct literal vs local-bound — both sum to 100.
print("num direct={}\n", sum(.[10, 20, 30, 40]));
nums : []s64 = .[10, 20, 30, 40];
print("num local={}\n", sum(nums));
}

View File

@@ -0,0 +1,44 @@
// A nested array/slice literal (`.[.[1, 2], .[3, 4]]`) at an expected slice-of-
// slices type (`[][]s64`) materializes each inner `[N]T` literal as a real `[]T`
// slice, so indexing the inner slice in the callee reads element contents
// correctly — for both the local-bound form and the direct-call-argument form.
// Regression (issue 0085): inner literals were appended as raw `[N]T` arrays
// under an element type of `[]T`, so the outer aggregate's elements were arrays
// where slice {ptr,len} headers were expected; indexing the inner slice read a
// garbage pointer and segfaulted. The per-element array->slice materialization
// recurses with the nesting, so every level coerces.
#import "modules/std.sx";
sum_nested :: (xss: [][]s64) -> s64 {
total := 0;
i := 0;
while i < xss.len {
j := 0;
while j < xss[i].len { total += xss[i][j]; j += 1; }
i += 1;
}
return total;
}
count_x :: (xss: [][]string) -> s64 {
n := 0;
i := 0;
while i < xss.len {
j := 0;
while j < xss[i].len { if xss[i][j] == "x" { n += 1; } j += 1; }
i += 1;
}
return n;
}
main :: () {
// numeric [][]s64 — local-bound vs direct-arg both sum to 10.
local : [][]s64 = .[.[1, 2], .[3, 4]];
print("num local={}\n", sum_nested(local));
print("num direct={}\n", sum_nested(.[.[1, 2], .[3, 4]]));
// string [][]string — local-bound vs direct-arg both count 4 "x"s.
slocal : [][]string = .[.["x", "a"], .["b", "x"], .["x", "x"]];
print("str local={}\n", count_x(slocal));
print("str direct={}\n", count_x(.[.["x", "a"], .["b", "x"], .["x", "x"]]));
}

View File

@@ -0,0 +1,65 @@
// A named-const array dimension lays out identically whether the const is
// TYPED (`N : s64 : 16`) or untyped (`N :: 16`), used DIRECTLY (`a : [N]T`) or
// through a type alias (`Arr :: [N]T`), and regardless of whether the const is
// declared before or after the alias that consumes it.
//
// Regression (issue 0083): the stateless registration-time resolver
// (type_bridge) only saw module consts that were already in `module_const_map`
// when a type alias resolved its dimension. Typed consts register in a later
// pass, and a forward-declared untyped const had not registered yet — so the
// alias dimension fabricated length 0 (a 0-byte alloca), and element access
// returned garbage (scalars) or bus-errored (slice/struct elements). Module
// consts are now pre-registered before any alias resolves, and both the
// stateful and stateless paths share one dimension resolver.
#import "modules/std.sx";
NT : s64 : 8; // typed const used as a dimension
P :: struct { x: s64; y: s64; }
// Type aliases whose dimension is the TYPED const NT (stateless registration).
TArr :: [NT]s64;
TSArr :: [NT]string;
TPArr :: [NT]P;
// Forward reference: this alias is declared BEFORE its dimension const NF.
FArr :: [NF]s64;
NF :: 5;
main :: () {
// Typed-const dimension, DIRECT local decl.
d : [NT]s64 = ---;
d[0] = 3;
d[7] = 21;
print("direct d0={} d7={} len={}\n", d[0], d[7], d.len);
// Typed-const dimension via ALIAS (scalar): same layout as the direct form.
a : TArr = ---;
a[0] = 7;
a[7] = 99;
print("alias a0={} a7={} len={}\n", a[0], a[7], a.len);
// Typed-const dimension via ALIAS (string elements): no bus error.
s : TSArr = ---;
s[0] = "hi";
s[7] = "yo";
print("alias s0={} s7={}\n", s[0], s[7]);
// Typed-const dimension via ALIAS (struct elements).
ps : TPArr = ---;
ps[0] = P.{ x = 1, y = 2 };
ps[7] = P.{ x = 5, y = 6 };
print("alias p0x={} p0y={} p7x={}\n", ps[0].x, ps[0].y, ps[7].x);
// Nested fixed array whose both dimensions are the typed const NT.
grid : [NT][NT]s64 = ---;
grid[0][0] = 1;
grid[7][7] = 10;
print("nested g00={} g77={}\n", grid[0][0], grid[7][7]);
// Forward-referenced alias dimension (untyped const declared after it).
f : FArr = ---;
f[0] = 4;
f[4] = 40;
print("fwd f0={} f4={} len={}\n", f[0], f[4], f.len);
}

View File

@@ -0,0 +1,77 @@
// A constant-FOLDABLE expression array dimension (`[M + 1]`, `[M * N]`,
// `[N - M]`, nested `[M + N - 1]`, parenthesised `[(M + 1) * 2]`, and an
// expression mixing an untyped and a typed module const) resolves to its
// evaluated length — IDENTICALLY whether used DIRECTLY (`a : [M + 1]T`) or
// through a type alias (`A :: [M + 1]T`), and for scalar, string (slice/pointer
// class), and struct element types.
//
// Regression (issue 0083): the shared array-dimension resolver only looked up a
// bare named const or a literal; any const-foldable EXPRESSION dimension was
// rejected as "not a compile-time integer constant". It now routes the
// dimension through the shared comptime integer-expression evaluator
// (`program_index.evalConstIntExpr`), so integer `+ - * /` and parenthesisation
// over literals and module consts fold on BOTH the stateful (direct) and
// stateless (alias) paths — they share the one evaluator and cannot diverge.
#import "modules/std.sx";
M :: 4;
N :: 6;
TK : s64 : 2; // typed const, used inside an expression dimension
P :: struct { x: s64; y: s64; }
AddAlias :: [M + 1]s64; // 5
MulAlias :: [M * N]s64; // 24
SubAlias :: [N - M]s64; // 2
NestAlias :: [M + N - 1]s64; // 9
ParenAlias :: [(M + 1) * 2]s64; // 10
TypedAlias :: [M + TK]s64; // 6
StrAlias :: [M + 1]string; // 5, slice/pointer elements
StructAlias :: [M + 1]P; // 5, struct elements
main :: () {
// const + literal: direct and via alias resolve to the same length.
add_d : [M + 1]s64 = ---;
add_a : AddAlias = ---;
add_d[4] = 7;
add_a[4] = 7;
print("add direct.len={} alias.len={} d4={} a4={}\n", add_d.len, add_a.len, add_d[4], add_a[4]);
// const * const.
mul_d : [M * N]s64 = ---;
mul_a : MulAlias = ---;
mul_d[23] = 230;
mul_a[23] = 230;
print("mul direct.len={} alias.len={} d23={} a23={}\n", mul_d.len, mul_a.len, mul_d[23], mul_a[23]);
// const - const.
sub_d : [N - M]s64 = ---;
sub_a : SubAlias = ---;
sub_d[1] = 9;
sub_a[1] = 9;
print("sub direct.len={} alias.len={} d1={} a1={}\n", sub_d.len, sub_a.len, sub_d[1], sub_a[1]);
// nested and parenthesised forms (direct vs alias).
nest_d : [M + N - 1]s64 = ---;
nest_a : NestAlias = ---;
paren_d : [(M + 1) * 2]s64 = ---;
paren_a : ParenAlias = ---;
print("nest direct.len={} alias.len={} paren direct.len={} alias.len={}\n", nest_d.len, nest_a.len, paren_d.len, paren_a.len);
// typed const inside the expression dimension.
typ_d : [M + TK]s64 = ---;
typ_a : TypedAlias = ---;
print("typed direct.len={} alias.len={}\n", typ_d.len, typ_a.len);
// string elements (slice/pointer class) — no bus error, correct reads.
str_a : StrAlias = ---;
str_a[0] = "hi";
str_a[4] = "yo";
print("str alias.len={} s0={} s4={}\n", str_a.len, str_a[0], str_a[4]);
// struct elements.
ps : StructAlias = ---;
ps[0] = P.{ x = 1, y = 2 };
ps[4] = P.{ x = 5, y = 6 };
print("struct alias.len={} p0x={} p4y={}\n", ps.len, ps[0].x, ps[4].y);
}

View File

@@ -0,0 +1,29 @@
// An array dimension accepts any compile-time numeric constant whose value is a
// positive INTEGRAL number — an integral float (`4.0`) folds to its integer just
// like `4`. A float-typed const (`N : f64 : 4.0`), an untyped-float const
// (`M :: 4.0`), and a direct float literal (`[4.0]s64`) all lay out the same
// `[4]s64` as the integer spelling, so element store/read is in bounds.
//
// Regression (issue 0083 / F0.4 attempt 8, Agra ruling): an integral float used
// as a dimension was wrongly rejected "must be a compile-time integer constant".
// The shared const-int evaluator now folds an integral float literal (and a
// float-typed module const) via `program_index.floatToIntExact`; a non-integral
// float (`4.5`) is still rejected (see 1132).
#import "modules/std.sx";
N : f64 : 4.0; // float-typed const
M :: 4.0; // untyped float const
main :: () {
a : [N]s64 = ---; // dim from a float-typed const
a[0] = 10; a[3] = 40;
print("a len={} a0={} a3={}\n", a.len, a[0], a[3]);
b : [M]s64 = ---; // dim from an untyped float const
b[1] = 21;
print("b len={} b1={}\n", b.len, b[1]);
c : [4.0]s64 = ---; // direct integral-float-literal dim
c[2] = 32;
print("c len={} c2={}\n", c.len, c[2]);
}

View File

@@ -0,0 +1,68 @@
// The comptime-int COUNT surface is uniform: every count consumer — array
// dimension (direct `[N]T` and via type alias), `Vector` lane, generic
// value-param (struct AND type-fn binder), and `inline for 0..N` — folds the
// SAME leaf forms to the SAME value through one shared evaluator
// (`program_index.evalConstIntExpr` / `moduleConstInt`). The leaf forms
// exercised here: untyped int const (`M`), a named const with an EXPRESSION RHS
// (`N :: M + 1`), a typed-int const (`S : s64 : 5`), an integral float const
// (`F :: 4.0` ≡ 4), and an ALIASED integer constraint (`Count :: u32`,
// `Small :: s8`) on a value-param.
//
// Regression (issue 0083): two cells of this surface diverged from the rest.
// (1) A named const whose RHS is an expression (`N :: M + 1`) did not fold as a
// count ("not a compile-time integer constant") — `moduleConstInt` read only a
// literal RHS; it now folds the RHS through the shared `evalConstIntExpr`. (2) An
// aliased integer constraint (`$K: Count`) bypassed the value-param range gate,
// which only matched builtin constraint names; the constraint now resolves to
// its underlying builtin before range-checking, so `$K: Count` behaves exactly
// like `$K: u32`.
#import "modules/std.sx";
M :: 2; // untyped int const
N :: M + 1; // named const, EXPRESSION RHS (== 3)
S : s64 : 5; // typed-int const
KU : u32 : 3; // typed-u32 const
F :: 4.0; // integral float const (== 4)
Count :: u32; // integer ALIAS — value-param constraint
Small :: s8; // integer ALIAS — value-param constraint
ArrN :: [N]s64; // array dim via alias: expression const (3)
ArrF :: [F]s64; // array dim via alias: integral float (4)
ArrS :: [S]s64; // array dim via alias: typed const (5)
Buf :: struct ($K: u32, $T: Type) { data: [K]T; }
BufC :: struct ($K: Count, $T: Type) { data: [K]T; } // ALIASED u32 constraint
BufS :: struct ($K: Small, $T: Type) { data: [K]T; } // ALIASED s8 constraint
Make :: ($K: u32, $T: Type) -> Type { return [K]T; } // type-fn value-param
main :: () {
// array dimension — DIRECT
a : [N]s64 = ---; a[0] = 7; a[2] = 9;
print("dim.direct.expr: len={} a0={} a2={}\n", a.len, a[0], a[2]);
f : [F]s64 = ---; f[3] = 40;
print("dim.direct.float: len={} f3={}\n", f.len, f[3]);
// array dimension — via type ALIAS
aa : ArrN = ---; aa[2] = 99; print("dim.alias.expr: len={} aa2={}\n", aa.len, aa[2]);
af : ArrF = ---; print("dim.alias.float: len={}\n", af.len);
az : ArrS = ---; print("dim.alias.typed: len={}\n", az.len);
// Vector lane — expression const (3) and integral float (4)
v3 : Vector(N, f32) = .[1.0, 2.0, 3.0];
print("lane.expr3: {} {} {}\n", v3.x, v3.y, v3.z);
v4 : Vector(F, f32) = .[1.0, 2.0, 3.0, 4.0];
print("lane.float4: {}\n", v4.w);
// generic value-param — struct binder: expr const, aliased u32, aliased s8
bn : Buf(N, s64) = ---; bn.data[2] = 30; print("vp.struct.expr: len={} v={}\n", bn.data.len, bn.data[2]);
bc : BufC(KU, s64) = ---; bc.data[2] = 31; print("vp.struct.alias.u32: len={} v={}\n", bc.data.len, bc.data[2]);
bs : BufS(4, s64) = ---; bs.data[3] = 32; print("vp.struct.alias.s8: len={} v={}\n", bs.data.len, bs.data[3]);
// generic value-param — type-fn binder: expr const
mk : Make(N, s64) = ---; mk[2] = 33; print("vp.typefn.expr: len={} v={}\n", mk.len, mk[2]);
// inline-for bound — expr const (3) and integral float (4)
s := 0; inline for 0..N: (i) { s += i; } print("for.expr: {}\n", s); // 0+1+2 = 3
t := 0; inline for 0..F: (i) { t += i; } print("for.float: {}\n", t); // 0+1+2+3 = 6
}

View File

@@ -0,0 +1,19 @@
// Zero is a context-dependent count. An array dimension and a generic
// value-param count both ACCEPT zero — `[0]T` is a valid empty (zero-length)
// array, and `Box(0)` is a length-0 instantiation. (A `Vector` lane count
// rejects zero — see 1505.) This pins the zero-accepting half of the
// context-dependent count rule documented in specs.md (Array Types).
//
// Regression (F0.4 attempt 12): the spec previously claimed every count must be
// "positive integral", which wrongly implied `[0]T` / `Box(0)` are illegal.
#import "modules/std.sx";
Box :: struct($N: u32) { items: [N]s64; }
main :: () {
a : [0]s64 = ---;
print("array_dim={}\n", a.len);
b : Box(0) = ---;
print("value_param={}\n", b.items.len);
}

View File

@@ -0,0 +1,65 @@
// Integer numeric-limit accessors: `<IntType>.min` / `.max` fold to a
// compile-time constant of the QUERIED integer type, driven by the
// (width, signedness) arithmetic (`sN`: min=-(2^(N-1)), max=2^(N-1)-1; `uN`:
// min=0, max=2^N-1) — every width 1..64, not just the power-of-two ones, plus
// `usize`/`isize` (target-width). Usable in expressions and in array-dimension
// position via the comptime-int path (`[u8.max]T`).
//
// The extreme values that the s64-based integer formatter cannot render
// directly — `s64.min` (i64::MIN) and the all-ones `u64.max`/`usize.max` — are
// asserted EXACTLY via comparison and untagged-union bit reinterpret, never via
// the formatter (which prints i64::MIN as a bare "-" and u64.max as "-1").
#import "modules/std.sx";
// Untagged union for the exact u64.max bit-reinterpret check.
UU :: union { u: u64; s: s64; }
main :: () -> s32 {
// Sub-byte widths — arbitrary bit-width arithmetic, not a per-name table.
print("s1.min={} s1.max={}\n", s1.min, s1.max); // -1 0
print("s2.min={} s2.max={}\n", s2.min, s2.max); // -2 1
print("s3.max={}\n", s3.max); // 3
print("u1.min={} u1.max={}\n", u1.min, u1.max); // 0 1
print("u2.max={}\n", u2.max); // 3
// Byte / word widths.
print("s8.min={} s8.max={}\n", s8.min, s8.max); // -128 127
print("u8.max={}\n", u8.max); // 255
print("s32.min={} s32.max={}\n", s32.min, s32.max); // -2147483648 2147483647
// s64 extremes: max prints; min (i64::MIN) is pinned by relation since the
// formatter cannot render it (this is independent of this feature).
print("s64.max={}\n", s64.max); // 9223372036854775807
print("s64.min+1 == -(s64.max): {}\n", s64.min + 1 == -9223372036854775807); // true
print("s64.min + s64.max == -1: {}\n", s64.min + s64.max == -1); // true
// u64.max / usize.max = all-ones (18446744073709551615); reinterpret to s64
// to confirm the bit pattern is -1 (and NOT a mangled value).
o : UU = ---;
o.u = u64.max;
print("u64.max as s64 == -1: {}\n", o.s == -1); // true
o.u = usize.max;
print("usize.max as s64 == -1: {}\n", o.s == -1); // true (host = u64)
print("usize.max == u64.max: {}\n", usize.max == u64.max); // true
print("isize.min == s64.min: {}\n", isize.min == s64.min); // true (host = s64)
// Result carries the QUERIED type: each binding is declared with the queried
// type and round-trips, so a mistyped fold (e.g. boxed as Any / widened)
// would not type-check here.
m3 : s3 = s3.max;
mu : u8 = u8.max;
ms : s8 = s8.min;
print("typed: m3={} mu={} ms={}\n", m3, mu, ms); // 3 255 -128
// Array-dimension / comptime-int path: `[u8.max]T` and `[s16.max]T` are
// valid counts (255 and 32767), usable end-to-end.
a : [u8.max]u8 = ---;
a[254] = 7;
print("[u8.max]u8 len={} a[254]={}\n", a.len, a[254]); // 255 7
b : [s16.max]u8 = ---;
b[32766] = 9;
print("[s16.max]u8 len={} b[32766]={}\n", b.len, b[32766]); // 32767 9
return 0;
}

View File

@@ -0,0 +1,18 @@
// Numeric-limit accessors apply only to numeric types. `.min`/`.max` on a
// NON-numeric receiver is a clean compile error (never a silent value, never
// the `.unresolved` sentinel reaching codegen):
// - a builtin non-numeric type (`bool`, `void`, `string`) → a dedicated
// "type 'X' has no '.min'/'.max'" diagnostic from the accessor intercept;
// - a user struct (`MyStruct`) → the type name is not a builtin, so the
// intercept stays out and the existing field-not-found path reports it.
// Each case is accurate and located at the access; the program exits non-zero.
#import "modules/std.sx";
MyStruct :: struct { a: s64; }
main :: () -> s32 {
b := bool.max;
s := MyStruct.min;
v := void.max;
return 0;
}

View File

@@ -0,0 +1,35 @@
// Float `!=` is UNORDERED not-equal: `nan != nan` is true (the canonical
// `x != x` NaN idiom), and `!=` is the exact complement of `==` for every
// float input — including NaN, where `nan == nan` is false (ordered `==`).
// For all non-NaN operands unordered `!=` matches ordered `!=`, so finite
// comparisons are unchanged. The native backend agrees with the interpreter.
//
// Regression (issue 0091): the LLVM backend lowered float `!=` to ordered
// not-equal (LLVMRealONE), so `nan != nan` was false in native code.
#import "modules/std.sx";
main :: () {
// Produce a genuine NaN without any numeric-limit accessor: 0.0 / 0.0.
z := 0.0;
nan := z / z;
// The fix: `!=` is unordered, `==` is ordered.
print("nan != nan: {}\n", nan != nan); // true
print("nan == nan: {}\n", nan == nan); // false
print("nan != 1.0: {}\n", nan != 1.0); // true
print("nan == 1.0: {}\n", nan == 1.0); // false
// Complementarity holds for finite operands too (unchanged behavior).
print("1.0 != 2.0: {}\n", 1.0 != 2.0); // true
print("1.0 != 1.0: {}\n", 1.0 != 1.0); // false
print("2.0 != 2.0: {}\n", 2.0 != 2.0); // false
// Native codegen converges with the comptime interpreter.
print("comptime nan != nan: {}\n", #run nan_ne_nan());
}
nan_ne_nan :: () -> bool {
z := 0.0;
n := z / z;
return n != n;
}

View File

@@ -0,0 +1,35 @@
// Backtick raw-identifier escape: a leading backtick makes the following
// identifier RAW — its text excludes the backtick and it is never the
// reserved/builtin keyword, so a reserved type-name spelling (`s2`, `u8`, …)
// can be used as an ordinary identifier. Exercised in every VALUE position:
// global, local, param, struct field + member access, function name + call,
// and a later reference. (A raw identifier in TYPE position references a
// backtick-declared type instead — see examples/0154.) A *bare* `s2` is still
// the reserved type name (see examples/1119), so the escape is the only way to
// spell these as values.
// Regression (issue 0089).
#import "modules/std.sx";
// Global named with a reserved type spelling.
`u8 := 100;
// Function whose name is a reserved type spelling, with a reserved-name param.
`s2 :: (`s1: s64) -> s64 { return `s1 * 2; }
Point :: struct {
`s2: f64; // field name is a reserved type spelling
`u16: s64;
}
main :: () {
// Local with a reserved type spelling; later reference resolves to it.
`s64 := 7;
`s64 = `s64 + 1;
print("local = {}\n", `s64);
print("global = {}\n", `u8);
print("fn = {}\n", `s2(21)); // calls the `s2 function
p := Point.{ `s2 = 2.5, `u16 = 9 };
print("field = {} {}\n", p.`s2, p.`u16);
}

View File

@@ -0,0 +1,57 @@
// Backtick raw identifier across every control-flow / capture / binding form,
// plus bare later uses. A reserved type-name spelling (`s2`, `u8`, …) works as a
// binding name in a destructure, an `if`/`while` optional binding, a `for`
// capture + index, and a match-arm capture; a backtick-named function is
// bare-callable; and a backtick struct field is bare- or backtick-accessible.
// The escape is needed only at the binding site — a later BARE reference / call
// / member access resolves to the binding. A *bare* binding name is still the
// reserved type (see examples/1121), so the escape is the only way to spell
// these as values.
// Regression (issue 0089 — attempt-2 completeness across binding forms).
#import "modules/std.sx";
pair :: () -> (s64, s64) { (1, 2) }
maybe :: () -> ?s64 { return 42; }
// Function named with a reserved spelling — bare-callable (no backtick at call).
`s2 :: (n: s64) -> s64 { return n + 1; }
Quad :: struct { `s1: s32; `s2: s32; }
main :: () -> s32 {
// destructure binding names
`u8, rest := pair();
print("dstr = {} {}\n", `u8, rest);
// if optional binding + bare-position reference inside the branch
if `s16 := maybe() {
print("if = {}\n", `s16);
}
// while optional binding (name only — the while binding isn't body-exposed)
while `s32 := maybe() {
break;
}
// for capture + index names
xs := [3]s64.{ 10, 20, 30 };
for xs: (`bool, `u16) {
print("for = {} @ {}\n", `bool, `u16);
}
// match-arm capture
opt: ?s64 = 5;
m := if opt == {
case .some: (`string) { `string * 2 }
case .none: { 0 }
};
print("match = {}\n", m);
// backtick function called BARE and via backtick — both resolve to the fn
print("call = {} {}\n", s2(10), `s2(10));
// struct field named with a reserved spelling: bare + backtick member access
q := Quad.{ `s1 = 7, `s2 = 9 };
print("field = {} {} | {} {}\n", q.s1, q.s2, q.`s1, q.`s2);
return 0;
}

View File

@@ -0,0 +1,23 @@
// Backtick raw-identifier escape at the `::` declaration sites: a leading
// backtick makes a CONSTANT name and a FUNCTION name raw, so a reserved type
// spelling (`s2`, `u8`) can be declared and used. Complements examples/0151
// (var / param / field / global). The backtick fn is callable both via the
// backtick (`` `u8(5) ``) and bare (`u8(5)`) — the bare reserved-name callee
// resolves to the raw fn because its declaration is raw (issue 0089). A *bare*
// `s2 :: …` / `u8 :: …` declaration is still the reserved-name error (see
// examples/1140).
// Regression (issue 0089).
#import "modules/std.sx";
// Constant whose name is a reserved type spelling.
`s2 :: 2.5;
// Function whose name is a reserved type spelling.
`u8 :: (n: s64) -> s64 { return n + 7; }
main :: () -> s32 {
print("const = {}\n", `s2);
print("fn tick = {}\n", `u8(5));
print("fn bare = {}\n", u8(5));
return 0;
}

View File

@@ -0,0 +1,42 @@
// Backtick raw identifier in TYPE position (the universal model, issue 0089):
// `` `name `` is the LITERAL identifier `name` used as a type reference, never
// the builtin/reserved spelling. A reserved type spelling (`s2`, `u8`, …) can
// therefore both DECLARE a type (struct / enum / union / error-set / alias) and
// be REFERENCED as that type via the backtick — while a BARE `s2` in type
// position remains the signed-int type (see `add` below) and a bare reserved-
// name declaration still errors (see examples/1141). The backtick is required
// to declare or reference these names; it is never part of the name's text.
// Regression (issue 0089 — attempt-4 universal raw identifier).
#import "modules/std.sx";
// Type-introducing decls whose NAME is a reserved spelling.
`s2 :: struct { x: s64; }
`s8 :: enum { A; B; }
`u16 :: union { i: s32; f: f32; }
`u32 :: error { Bad, Empty }
RawAlias :: `s2; // alias to a backtick-declared struct
// A bare `s2` in type position is still the 2-bit signed int.
add :: (a: s2, b: s2) -> s2 { return a + b; }
main :: () -> s32 {
// Reference the backtick struct as a type; field access works.
v : `s2 = ---;
v.x = 7;
// Reference via a normal alias too.
a : RawAlias = ---;
a.x = 11;
// Backtick enum / union type references.
e : `s8 = .A;
u : `u16 = ---;
u.i = 5;
print("struct = {}\n", v.x);
print("alias = {}\n", a.x);
print("enum = {}\n", e == .A);
print("union = {}\n", u.i);
print("bare = {}\n", add(1, 0)); // bare s2 = the 2-bit int type
return 0;
}

View File

@@ -0,0 +1,24 @@
// Backtick raw identifier at the two remaining binding positions (issue 0089,
// attempt-4): a TYPED constant (`` `s2 : s64 : 5 ``) and a union TAG / field
// (`` `s2: s32 ``). The typed-const form previously slipped past the decl check
// without a name span (caret at 1:1); a bare `s2 : s64 : 5` is still rejected
// with the caret ON the name (see examples/1141). A union tag spelled with a
// reserved name works and is accessible bare or backticked.
// Regression (issue 0089 — attempt-4 typed const + union tag).
#import "modules/std.sx";
// Typed constant whose name is a reserved type spelling.
`s2 : s64 : 5;
// Union whose tags are reserved type spellings.
Mix :: union { `s1: s32; `u8: f32; }
main :: () -> s32 {
print("typed const = {}\n", `s2);
m : Mix = ---;
m.`s1 = 42;
print("union tick = {}\n", m.`s1); // backtick member access
print("union bare = {}\n", m.s1); // bare member access — same field
return 0;
}

View File

@@ -0,0 +1,21 @@
// Backtick raw-identifier escape at a STRUCT-BODY constant — both the untyped
// `` `name :: value `` and the typed `` `name : T : value `` forms. A struct
// member constant is a binding site like any top-level const (examples/0153),
// so a reserved type spelling (`s2`, `u8`) needs the backtick to be used as the
// constant's name; the value is read back via `Holder.`name`. A *bare*
// reserved-name struct const still errors with the caret on the name (see
// examples/1142). The backtick is never part of the name's text.
// Regression (issue 0089 — attempt-5: struct-body const decls thread is_raw +
// the precise name_span, previously dropped to a false reject / 1:1 caret).
#import "modules/std.sx";
Holder :: struct {
`s2 :: 5; // untyped raw struct-body const
`u8 : s64 : 9; // typed raw struct-body const
}
main :: () -> s32 {
print("untyped = {}\n", Holder.`s2);
print("typed = {}\n", Holder.`u8);
return 0;
}

View File

@@ -0,0 +1,30 @@
// Backtick raw identifier in PARAMETERIZED type position. A raw type reference
// (`` `s2 ``) flows through the SAME type-expression continuations as a bare
// name, so a reserved-spelled GENERIC template can be instantiated
// (`` `s2(s64) ``) and the result composes under pointer/field wrappers
// (`` *`s2(s64) ``, a struct field typed `` `s2(s64) ``). A bare `s2` in type
// position is still the 2-bit signed int. Complements examples/0154 (nullary
// raw type references).
// Regression (issue 0089 — attempt-5: the raw type atom no longer parses as a
// terminal `type_expr`; it reaches the parameterized + wrapper continuations).
#import "modules/std.sx";
`s2 :: struct($T: Type) {
x: $T;
}
Wrapper :: struct {
inner: `s2(s64); // raw parameterized type as a struct field
}
main :: () -> s32 {
v : `s2(s64);
v.x = 7;
p : *`s2(s64) = @v; // pointer to a raw parameterized type
w : Wrapper = ---;
w.inner.x = 12;
print("val = {}\n", v.x);
print("ptr = {}\n", p.x);
print("fld = {}\n", w.inner.x);
return 0;
}

View File

@@ -0,0 +1,55 @@
// Reserved-name MEMBER positions are EXEMPT from the reserved-type-name rule:
// a bare reserved spelling (`s2`, `u8`, `s1`, …) is legal as a struct FIELD
// name, a union TAG name, and a protocol METHOD-SIGNATURE name. These are
// unambiguous — the name sits in a member slot and is reached via `obj.name`
// (or dispatched by string), so it is never type-classified and never
// mislowers. The backtick form is optional there and resolves to the same
// member. Backtick access (`obj.`s2`) and bare access (`obj.s2`) both work.
//
// The exemption stops at member SIGNATURES: an `impl` method DEFINITION is a
// real function, so its name is a declaration site (like a free function) and a
// reserved spelling still needs the backtick (`` `s2 :: (self) ``) — bare would
// be type-classified and mislower (the issue-0076 protection). A bare reserved
// VALUE binding / declaration name still errors (see examples/1119, 1141, 1142).
// Regression (issue 0089 — attempt-7: pins the Agra-ruled member-name exemption).
#import "modules/std.sx";
// Struct fields spelled with reserved type names — bare is legal.
Holder :: struct {
s2: s64;
u8: s64;
}
// Union tags spelled with reserved type names — bare is legal.
Tag :: union {
s1: s32;
u16: f64;
}
// Protocol method SIGNATURE spelled with a reserved type name — bare is legal.
Speaker :: protocol {
s2 :: () -> s64;
}
Dog :: struct { n: s64; }
impl Speaker for Dog {
`s2 :: (self: *Dog) -> s64 { self.n } // impl DEFINITION → backtick required
}
main :: () -> s32 {
h := Holder.{ s2 = 10, u8 = 20 };
print("fields bare = {} {}\n", h.s2, h.u8); // bare member access
print("fields tick = {} {}\n", h.`s2, h.`u8); // backtick member access
h.s2 = 11;
h.`u8 = 21; // backtick write
print("fields set = {} {}\n", h.s2, h.u8);
t : Tag = ---;
t.s1 = 5;
print("union = {} {}\n", t.s1, t.`s1); // bare + backtick — same tag
items : List(Speaker) = .{};
items.append(Dog.{ n = 7 });
print("dispatch = {}\n", items.items[0].s2()); // bare reserved-name method call
return 0;
}

View File

@@ -0,0 +1,94 @@
// Float numeric-limit accessors: `f32`/`f64` expose `.min` / `.max` (sibling of
// the integer `.min`/`.max`, NL.1) plus the float-only `.epsilon`,
// `.min_positive`, `.true_min`, `.inf`, and `.nan`. Each folds, at compile time,
// to a constant of the QUERIED float type via the same `lowerNumericLimit`
// intercept as the integer case (`builder.constFloat` + the `std.math`
// constants), driven by `TypeResolver.floatLimitFor`.
//
// The lexer has no exponent notation and the default float formatter is crude
// (issue 0090), so these limits can be pinned NEITHER by literal comparison NOR
// by printing. Every accessor is asserted instead by reinterpreting its bits
// through an untagged union and comparing against the exact IEEE-754 hex
// pattern — plus the defining-property checks that no other value could satisfy.
//
// Semantics (Agra-ruled, consistent with the integer accessors):
// .min = most-NEGATIVE finite (= -max), NOT C's DBL_MIN
// .max = largest finite
// .epsilon = ULP of 1.0 (next f after 1.0 minus 1.0), NOT C#'s denormal Epsilon
// .min_positive = smallest positive NORMAL (= C DBL_MIN / Rust MIN_POSITIVE)
// .true_min = smallest positive SUBNORMAL (next value above 0.0)
// .inf = +infinity
// .nan = a quiet NaN
//
// Regression (issue 0091): `f64.nan != f64.nan` is true — native float `!=`
// lowers UNORDERED, so a NaN compares unequal to everything including itself.
#import "modules/std.sx";
// `bits` mirrors each float's raw IEEE-754 storage. f64 needs 64 bits, f32 32.
// The f64 union's `bits` (u64) view reads the all-ones-ish positive patterns as
// their true magnitude; its `s` (s64) view pins the negative `f64.min` pattern
// (0xFFEF…), whose unsigned form overflows the u64 literal parser, by comparing
// the signed reinterpret to -4503599627370497.
Uf64 :: union { f: f64; bits: u64; s: s64; }
Uf32 :: union { f: f32; bits: u32; }
main :: () -> s32 {
o : Uf64 = ---;
// Read `.true_min` (a subnormal) FIRST and through the union only — never via
// arithmetic. Under flush-to-zero / denormals-are-zero CPU modes a subnormal
// can flush to 0.0 on the first arithmetic op, so the bit reinterpret is the
// only reliable channel for it.
o.f = f64.true_min;
print("f64.true_min {}\n", o.bits == 0x0000000000000001); // true
o.f = f64.max;
print("f64.max {}\n", o.bits == 0x7FEFFFFFFFFFFFFF); // true
// f64.min = -max; its bit pattern 0xFFEFFFFFFFFFFFFF overflows an unsigned u64
// literal, so it is pinned directly via the SIGNED s64 view: -4503599627370497.
o.f = f64.min;
print("f64.min {}\n", o.s == -4503599627370497); // true (bits 0xFFEFFFFFFFFFFFFF)
o.f = f64.epsilon;
print("f64.epsilon {}\n", o.bits == 0x3CB0000000000000); // true
o.f = f64.min_positive;
print("f64.min_positive {}\n", o.bits == 0x0010000000000000); // true
o.f = f64.inf;
print("f64.inf {}\n", o.bits == 0x7FF0000000000000); // true
p : Uf32 = ---;
p.f = f32.true_min;
print("f32.true_min {}\n", p.bits == 0x00000001); // true
p.f = f32.max;
print("f32.max {}\n", p.bits == 0x7F7FFFFF); // true
p.f = f32.min;
print("f32.min {}\n", p.bits == 0xFF7FFFFF); // true
p.f = f32.epsilon;
print("f32.epsilon {}\n", p.bits == 0x34000000); // true
p.f = f32.min_positive;
print("f32.min_positive {}\n", p.bits == 0x00800000); // true
p.f = f32.inf;
print("f32.inf {}\n", p.bits == 0x7F800000); // true
// Defining-property checks — true epsilon is the ULP of 1.0: adding it to 1.0
// changes the value, adding half of it does not (round-to-nearest-even).
print("(1+eps)!=1 {}\n", (1.0 + f64.epsilon) != 1.0); // true
print("(1+eps/2)==1 {}\n", (1.0 + f64.epsilon/2.0) == 1.0); // true
print("inf>max {}\n", f64.inf > f64.max); // true
// f64.min = -max (the 0xFFEF… bit pattern overflows the i64 literal parser).
print("min==-max {}\n", f64.min == -f64.max); // true
print("true_min<min_pos {}\n", f64.true_min < f64.min_positive); // true
print("true_min>0 {}\n", f64.true_min > 0.0); // true
// Quiet NaN: unequal to everything, itself included (mantissa bits not pinned).
print("nan!=nan {}\n", f64.nan != f64.nan); // true
// Result carries the QUERIED type: each binding is declared with the float
// type and round-trips, so a mistyped fold (boxed as Any / wrong width) would
// not type-check here.
e64 : f64 = f64.epsilon;
e32 : f32 = f32.epsilon;
q : Uf64 = ---; q.f = e64;
r : Uf32 = ---; r.f = e32;
print("typed eps bits {}\n", q.bits == 0x3CB0000000000000 and r.bits == 0x34000000); // true
return 0;
}

View File

@@ -0,0 +1,27 @@
// Cross-type rules for the numeric-limit accessors. `.min` / `.max` are valid on
// BOTH integer and float types, but `.epsilon` / `.min_positive` / `.true_min` /
// `.inf` / `.nan` are FLOAT-ONLY. Applying a float-only accessor to an INTEGER
// type, or ANY accessor to a non-numeric type, is a clean compile error — never
// a silent value, never the `.unresolved` sentinel reaching codegen.
//
// - float-only accessor on an integer (`s32.epsilon`, `u8.inf`,
// `s64.true_min`) → a dedicated "applies only to float types" diagnostic
// from the accessor intercept, located at the access;
// - any accessor on a non-numeric builtin (`bool.nan`, `string.max`) → the
// "numeric limits apply only to integer and float types" diagnostic;
// - a user struct (`MyStruct.epsilon`) → the type name is not a builtin, so the
// intercept stays out and the existing field-not-found path reports it.
// Each case is accurate and located at the access; the program exits non-zero.
#import "modules/std.sx";
MyStruct :: struct { a: s64; }
main :: () -> s32 {
a := s32.epsilon;
b := u8.inf;
c := s64.true_min;
d := bool.nan;
e := string.max;
f := MyStruct.epsilon;
return 0;
}

View File

@@ -0,0 +1,76 @@
// Numeric-limit accessor vs. a raw value binding that shadows a builtin type
// name. A backtick raw identifier (F0.6) can legitimately bind a value whose
// spelling is a reserved numeric type name (`` `f64 ``, `` `s32 ``, `` `u8 ``).
// Field access on such a value is an ORDINARY field read — the numeric-limit
// intercept (NL.1 integer `.min`/`.max`, NL.2 float `.epsilon`/… ) must NOT
// hijack it. An adjacent BARE `f64.epsilon` / `s32.max` / `u8.max` — which the
// parser classifies as a type receiver, not the raw value — STILL folds to the
// numeric limit. Both behaviors coexist: the raw receiver reads the value, the
// bare receiver folds the limit.
//
// A raw value binding can reach the intercept through THREE sources, exactly
// mirroring the ordinary identifier field-access path (scope / globals / module
// consts). This example exercises all three: a GLOBAL `` `f32 ``, a MODULE-CONST
// `` `s16 ``, and LOCAL `` `f64 ``/`` `s32 ``/`` `u8 `` — each reads its field,
// and the bare spelling of each STILL folds.
//
// Regression (issues 0092 local, 0093 global + module-const): the intercept
// previously treated any identifier whose text matched a builtin numeric type
// name as a TYPE receiver, silently shadowing the in-scope value binding
// (`` `f64.epsilon `` folded to 2^-52, `` `s32.max `` folded to 2147483647 —
// a silent wrong value). The attempt-3 fix guarded only lexical scope, so
// GLOBAL and MODULE-CONST raw bindings still folded (issue 0093).
#import "modules/std.sx";
FBox :: struct { epsilon: s64; max: s64; min_positive: s64; }
IBox :: struct { max: s64; min: s64; }
UBox :: struct { max: s64; }
// GLOBAL raw value binding whose spelling shadows the builtin `f32`. Reachable
// via `program_index.global_names`, not lexical scope (issue 0093).
`f32 := FBox.{ epsilon = 44, max = 55, min_positive = 66 };
// MODULE-CONST raw value binding whose spelling shadows the builtin `s16`.
// Reachable via `program_index.module_const_map` (issue 0093, const variant).
`s16 :: IBox.{ max = 99, min = -99 };
main :: () -> s32 {
// LOCAL raw value bindings whose spelling shadows a builtin numeric type name.
`f64 := FBox.{ epsilon = 11, max = 22, min_positive = 33 };
`s32 := IBox.{ max = 78, min = -78 };
`u8 := UBox.{ max = 7 };
// Raw receiver → ordinary field READ (the value), never the numeric limit.
print("local f64: epsilon={} max={} min_positive={}\n",
`f64.epsilon, `f64.max, `f64.min_positive); // 11 22 33
print("local s32: max={} min={}\n", `s32.max, `s32.min); // 78 -78
print("local u8: max={}\n", `u8.max); // 7
// GLOBAL raw receiver → ordinary field READ (issue 0093).
print("global f32: epsilon={} max={} min_positive={}\n",
`f32.epsilon, `f32.max, `f32.min_positive); // 44 55 66
// MODULE-CONST raw receiver → ordinary field READ (issue 0093).
print("const s16: max={} min={}\n", `s16.max, `s16.min); // 99 -99
// The value-field read carries the field type (s64 here): round-trips
// through a typed binding, so a mistyped/boxed read would not type-check.
e : s64 = `f64.epsilon;
print("typed val e={}\n", e); // 11
// Bare receiver (a type receiver, NOT the raw value) → STILL folds to the
// numeric limit, even though a LOCAL (`s32`/`u8`/`f64`), GLOBAL (`f32`), or
// MODULE-CONST (`s16`) value of the same spelling is bound. The bare receiver
// is never blocked by any of the three value sources.
print("lim s32.max={} s32.min={}\n", s32.max, s32.min); // 2147483647 -2147483648
print("lim u8.max={}\n", u8.max); // 255
print("lim s16.max={} s16.min={}\n", s16.max, s16.min); // 32767 -32768
// Bare float accessors still fold; the formatter is crude (issue 0090), so
// pin the values by their defining properties rather than by printing.
print("lim (1.0+f64.epsilon)!=1.0: {}\n", (1.0 + f64.epsilon) != 1.0); // true
print("lim f64.inf > f64.max: {}\n", f64.inf > f64.max); // true
print("lim f64.min == -f64.max: {}\n", f64.min == -f64.max); // true
print("lim f32.inf > f32.max: {}\n", f32.inf > f32.max); // true
return 0;
}

View File

@@ -0,0 +1,68 @@
// Valid typed module-level constants compile, fold, and print correctly across
// every initializer/annotation pairing the registrar accepts:
// - integer literal → integer (`K : s64 : 4`) — usable as an array count too
// - integer literal → float (`W : f32 : 800`)
// - float literal → float (`PI : f32 : 3.14159`)
// - string literal → string (`S : string : "hi"`)
// - null → pointer (`P : *void : null`)
// - integer EXPRESSION → integer (`KE : s64 : M + 2`) — usable as a count too
// - integer EXPRESSION → float (`WE : f32 : M + 2`)
// - MIXED int+float EXPRESSION → float (`MF : f64 : M + 0.5`, both operand orders)
// - INTEGRAL float literal → integer (`KF : s64 : 4.0` → 4) — folds under the
// unified narrowing rule (F0.11), usable as a count too
// - INTEGRAL float EXPRESSION → integer (`KFE : s64 : M + 2.0` → 4)
//
// Companion to the negative example 1143: the issue-0088 fix rejects a typed
// const whose initializer mismatches its annotation, and these correctly-typed
// consts must keep working (no over-rejection) — including const-EXPRESSION
// initializers, whose type-based validation (attempt 2) must accept a correctly
// typed expression even though it isn't a literal.
//
// `MF`/`MFR` pin the attempt-3 inferExprType promotion fix: a mixed int+float
// arithmetic expression infers as the float result regardless of operand order,
// so it matches an `f64` annotation (and folds to 2.5, not a truncated 2).
#import "modules/std.sx";
M :: 2;
K : s64 : 4;
W : f32 : 800;
PI : f32 : 3.14159;
S : string : "hi";
P : *void : null;
KE : s64 : M + 2;
WE : f32 : M + 2;
MF : f64 : M + 0.5;
MFR : f64 : 0.5 + M;
KF : s64 : 4.0; // integral float literal → folds to 4
KFE : s64 : M + 2.0; // integral float expression → folds to 4
main :: () {
// Integer const: prints AND drives an array dimension (len 4).
a : [K]s64 = ---;
a[0] = 10;
a[3] = 40;
print("K={} len={} a0={} a3={}\n", K, a.len, a[0], a[3]);
// Integer-into-float and float consts print as floats.
print("W={} PI={}\n", W, PI);
// String const prints its text.
print("S={}\n", S);
// Null pointer const is null.
print("P_is_null={}\n", P == null);
// Integer const-EXPRESSION: prints AND drives an array dimension (len 4).
b : [KE]s64 = ---;
print("KE={} len={} WE={}\n", KE, b.len, WE);
// Mixed int+float const-EXPRESSION folds to the promoted float (2.5),
// operand-order-independent.
print("MF={} MFR={}\n", MF, MFR);
// Integral float const (literal + expression): folds to its integer under
// the unified narrowing rule; `KF` also drives an array dimension (len 4).
cc : [KF]s64 = ---;
print("KF={} len={} KFE={}\n", KF, cc.len, KFE);
}

View File

@@ -0,0 +1,35 @@
// Mixed int+float arithmetic infers as the FLOAT result, operand-order-independent.
//
// `print("{}", expr)` selects integer- vs float-formatting from the STATIC type
// `inferExprType` reports for the argument (not the lowered value's type), so it
// exercises the binary-op inference arm directly — distinct from the typed-const
// validation path. Before the attempt-3 fix, binary-op inference was LHS-biased:
// `n + 0.5` (int LHS) inferred `s64` and printed a truncated `2`, while `0.5 + n`
// (float LHS) inferred `f64` and printed `2.5`. The fix routes both through the
// shared promotion rule (`Lowering.arithResultType`, the same one `lowerBinaryOp`
// applies for the value), so an int operand with a float operand promotes to the
// float in either order.
//
// Regression (issue 0088, attempt 3 — the inferExprType numeric-promotion root fix).
#import "modules/std.sx";
main :: () {
n := 2; // runtime s64
// Addition, both operand orders — both promote to f64 → 2.5.
print("add: {} {}\n", n + 0.5, 0.5 + n);
// Multiplication, both orders — both promote → 3.0.
print("mul: {} {}\n", n * 1.5, 1.5 * n);
// Subtraction / division with the int on the left.
print("sub: {} div: {}\n", n - 0.5, n / 4.0);
// f32 operand promotes too (int LHS, f32 RHS).
half : f32 = 0.5;
print("f32: {}\n", n + half);
// A pure-int expression is unaffected — stays s64, prints as an integer.
print("int: {}\n", n + 3);
}

View File

@@ -0,0 +1,43 @@
// Reflection builtins on an `Any` consult the Any's runtime TYPE-TAG,
// not its raw payload.
//
// An `Any` is both a type AND a value: it can hold a runtime *value*
// (whose runtime tag names the value's type) or a *Type value* (the
// `{ .any, tid }` shape `type_of` / a `Type` literal produce). A
// reflection builtin must branch on the tag:
// - holds a value → report the type OF that value (the tag).
// - holds a Type → name the held type (the payload).
//
// Regression (issue 0090, attempt 3): `type_name` / `type_is_unsigned`
// used to read an Any's payload as a TypeId index unconditionally, so
// `type_name(av)` for `av : Any = 6` returned `u8` (types[6]) and
// `type_is_unsigned(av)` returned true. Both now read the runtime tag.
#import "modules/std.sx";
main :: () {
// Any holding a VALUE — reflection names the value's type.
av : Any = 6; // 6 is s64
print("type_name(av)={}\n", type_name(av));
print("type_is_unsigned(av)={}\n", type_is_unsigned(av));
print("print(av)={}\n", av); // formatter already used the tag
u : u32 = 7;
au : Any = u;
print("type_name(au)={}\n", type_name(au));
print("type_is_unsigned(au)={}\n", type_is_unsigned(au));
print("print(au)={}\n", au);
sv : Any = "hi";
print("type_name(sv)={}\n", type_name(sv));
// Any holding a TYPE value — reflection names the held type.
x : u64 = 9;
at : Any = type_of(x);
print("type_name(at)={}\n", type_name(at));
print("type_is_unsigned(at)={}\n", type_is_unsigned(at));
print("print(at)={}\n", at);
// A direct runtime Type value is unchanged.
print("type_is_unsigned(type_of(x))={}\n", type_is_unsigned(type_of(x)));
}

View File

@@ -0,0 +1,21 @@
// Writing through a nested struct field lvalue (`outer.inner.x = v`) and taking
// the address of a valid field both resolve the field pointer correctly: the
// lvalue-pointer path (lowerExprAsPtr) GEPs the matched field, never a silent
// field-0 default. Positive companion to the missing-field diagnostic (1145).
#import "modules/std.sx";
Inner :: struct { a: s64; b: s64; }
Outer :: struct { inner: Inner; tag: s64; }
bump :: (p: *s64) { p.* = p.* + 100; }
main :: () {
o := Outer.{ inner = Inner.{ a = 1, b = 2 }, tag = 9 };
o.inner.a = 11; // nested struct field store via lowerExprAsPtr
o.inner.b = 22;
o.tag = 33; // direct struct field store
print("a={} b={} tag={}\n", o.inner.a, o.inner.b, o.tag);
bump(@o.inner.a); // address-of a matched nested field
print("a2={}\n", o.inner.a);
}

View File

@@ -0,0 +1,32 @@
// Taking the address of a promoted anonymous-struct union member yields a
// pointer to that member's slot, so mutating through the pointer is visible
// through the member. The write path (`v.x = 41`) and the read path already
// resolve promoted members; the lvalue-pointer path (`@v.x`) now resolves them
// too, via the shared field-lvalue resolver.
//
// Regression (issue 0094, attempt 2): lowerExprAsPtr's union branch handled
// only DIRECT union field names, so `@v.x` on a promoted member reported
// "field 'x' not found on type 'Vec2'" even though `v.x = 41` worked. The
// over-rejection is gone, and a member that is NOT at offset 0 (`v.y`) resolves
// to its own slot — not a default field 0.
#import "modules/std.sx";
Vec2 :: union {
data: [2]s64;
struct { x: s64; y: s64; };
}
bump :: (p: *s64) {
p.* = p.* + 1;
}
main :: () {
v : Vec2 = ---;
v.x = 41;
v.y = 100;
bump(@v.x); // promoted member at offset 0 → 42
bump(@v.y); // promoted member at offset 8 → 101 (its own slot)
print("x={}\n", v.x);
print("y={}\n", v.y);
}

View File

@@ -0,0 +1,42 @@
// Storing a struct field whose own type is a pointer to a two-pointer struct
// (`*Pair` — the shape `coerceArg` would closure-auto-promote into `{ptr,null}`)
// must store an 8-byte pointer, never a 16-byte struct that overruns the slot
// and clobbers the neighbouring field. The shared field-lvalue resolver types
// the GEP as `*field_ty`, so `emitStore` unwraps exactly one pointer level to
// the field's own type (`*Pair`, an opaque pointer) instead of the pointee
// aggregate (`Pair`).
//
// Regression (issue 0094, attempt-3 consolidation): the shared `fieldLvaluePtr`
// resolver used to type struct/tuple GEPs with the bare field value type, so a
// `*Pair` field stored via the multi-assign / address-of paths promoted the
// pointer to a 16-byte struct and clobbered the next field (`sentinel` read 0).
// The single-assign path already used the `*field_ty` convention; folding all
// three lvalue sites onto the one resolver applies it uniformly.
#import "modules/std.sx";
Pair :: struct { a: *s64; b: *s64; }
Holder :: struct { pr: *Pair; sentinel: s64; }
main :: () {
x : s64 = 7;
y : s64 = 9;
pair : Pair = .{ a = @x, b = @y };
other : Pair = .{ a = @y, b = @x };
h : Holder = .{ pr = @pair, sentinel = 99 };
// single-assign: store a *Pair into h.pr; sentinel must stay 99.
h.pr = @other;
print("single: a={} b={} sentinel={}\n", h.pr.a.*, h.pr.b.*, h.sentinel);
// multi-assign: store into h.pr alongside a plain local; sentinel untouched.
dummy : s64 = 0;
h.pr, dummy = @pair, 1;
print("multi: a={} b={} sentinel={}\n", h.pr.a.*, h.pr.b.*, h.sentinel);
// address-of the *Pair field, store through it; sentinel untouched.
ppr := @h.pr;
ppr.* = @other;
print("addr: a={} b={} sentinel={}\n", h.pr.a.*, h.pr.b.*, h.sentinel);
}

View File

@@ -0,0 +1,119 @@
// Unified float→int narrowing rule (F0.11), POSITIVE side: an INTEGRAL float
// flowing into an integer-typed binding FOLDS to its integer — the same
// `floatToIntExact` rule an array dimension / `$K: Count` already uses — across
// all FIVE sites: a typed LOCAL, a struct FIELD default, a typed module CONST, a
// function PARAM default, and an array DIMENSION. It folds whether written as a
// float LITERAL (`4.0`), an INT-const-EXPRESSION (`M + 2.0`, with `M :: 2`), a
// FLOAT-const-LEAF expression whose sum is integral (`F + 1.5`, with
// `F : f64 : 2.5`, = 4.0) — including such a float-const-leaf expression driving
// an array dimension directly, through a const, or via a type alias — a builtin
// FLOAT numeric-limit leaf in an integral expression (`f64.max - f64.max` = 0),
// and an integral float `%` (`6.0 % 4.0` = 2). The compile-time float evaluator
// is at parity with the integer one, so integer numeric-limit accessors (`s8.max`,
// `[u8.max]` count) keep folding through the shared int folder, unregressed.
// The escape hatch (`xx` / `cast`) still TRUNCATES any float, integral or not —
// including a non-integral const expression (`xx (M + 0.5)` / `xx (F + 0.25)`).
//
// Companion to the negative example 1146 (non-integral floats error).
// Regression (issue 0095): a typed local/param/field silently truncated a float
// initializer (`y : s64 = 1.5` → 1) with no diagnostic; a non-integral const
// EXPRESSION (`M + 0.5`) and a non-integral float-const-LEAF expression
// (`F + 0.25`) truncated even when written through an int binding; the rule now
// folds an integral float (literal, int-const expr, or float-const leaf) and
// rejects a non-integral one.
#import "modules/std.sx";
M :: 2; // int module const, for the INT-const-EXPRESSION cases
F : f64 : 2.5; // float module const, for the FLOAT-const-LEAF cases
Box :: struct {
n : s64 = 4.0; // integral float field default → folds to 4
ne : s64 = M + 2.0; // integral int-const-EXPR field default → folds to 4
nf : s64 = F + 1.5; // integral float-const-LEAF field default → folds to 4
nd : s64 = 8.0 / 2.0; // integral float-DIVISION field default → folds to 4
}
withDefault :: (x : s64 = 6.0) -> s64 { return x; } // param default → 6
withFlt :: (x : s64 = F + 1.5) -> s64 { return x; } // float-const-leaf param default → 4
K : s64 : 8.0; // integral float module const → folds to 8
KF : s64 : F + 1.5; // integral float-const-LEAF module const → folds to 4
KD : s64 : 12.0 / 4.0; // integral float-DIVISION module const → folds to 3
ArrFE :: [F + 1.5]s64; // array-dim type ALIAS over a float-const-leaf expr → [4]s64
// (the stateless registration path must agree with the
// direct form `a : [F + 1.5]s64` below — issue 0083).
main :: () {
// Typed local: integral float folds (literal + int-const expr + float-const leaf).
z : s64 = 4.0;
ze : s64 = M + 2.0;
zf : s64 = F + 1.5;
print("local={} localExpr={} localFlt={}\n", z, ze, zf);
// Negative integral float folds to its (negative) integer.
neg : s64 = -2.0;
print("neg={}\n", neg);
// Integral float DIVISION folds (the subtle case: integral operands, but the
// `/` is float division). `6.0 / 2.0` = 3.0 → 3; the int folder refuses the
// float `/` and the unified rule folds the integral result.
zd : s64 = 6.0 / 2.0;
print("localDiv={}\n", zd);
// Struct field defaults fold (literal + int-const expr + float-const leaf +
// float division).
b := Box.{};
print("field={} fieldExpr={} fieldFlt={} fieldDiv={}\n", b.n, b.ne, b.nf, b.nd);
// Param defaults fold.
print("param={} paramFlt={}\n", withDefault(), withFlt());
// Module consts fold (and an integral float const can drive an array dim: len 8).
a : [K]s64 = ---;
print("const={} constFlt={} len={}\n", K, KF, a.len);
// Integral float-DIVISION const folds, and drives an array dimension directly
// (`[6.0 / 2.0]` → len 3) through the SAME refuse-int-fold / fold-float rule.
ad2 : [6.0 / 2.0]s64 = ---;
print("constDiv={} dimDiv={}\n", KD, ad2.len);
// Array DIMENSION — the fifth site joins the unified rule: an integral
// float-const-leaf expression folds to a count whether written DIRECTLY
// (`[F + 1.5]` → 4), THROUGH a float-expr const (`[KF]`, KF = F + 1.5 = 4),
// or via a type ALIAS (`ArrFE`, the stateless path agreeing with the direct).
ad : [F + 1.5]s64 = ---;
ak : [KF]s64 = ---;
aa : ArrFE = ---;
print("dim.direct={} dim.const={} dim.alias={}\n", ad.len, ak.len, aa.len);
// Numeric-limit float leaf in an expression: an INTEGRAL result folds (the
// compile-time float evaluator is at parity with the integer one — a
// `f64`/`f32` `.max`/`.min`/`.epsilon`/… leaf is recognised inside an
// expression, not only as a direct value). `f64.max - f64.max` = 0.0 → 0.
lim : s64 = f64.max - f64.max;
// Integral float `%` (parity with int `%`): `6.0 % 4.0` = 2.0 → 2.
fm : s64 = 6.0 % 4.0;
print("limit={} fmod={}\n", lim, fm);
// Integer numeric-limit accessors (NL.1) are unregressed by the float-leaf
// parity work: they still fold at a binding (`s8.max` = 127) and as an array
// dimension count (`[u8.max]` = len 255), through the SAME int folder.
il : s64 = s8.max;
iarr : [u8.max]s64 = ---;
print("intlimit={} intcount={}\n", il, iarr.len);
// Explicit escape: `xx` / `cast` always truncate, integral or not —
// including a non-integral const EXPRESSION (`xx (M + 0.5)` → 2), a
// non-integral float-const-LEAF expression (`xx (F + 0.25)` → 2), a
// non-integral numeric-limit expr (`xx (f64.true_min + 0.5)` → 0), and a
// non-integral float `%` (`xx (5.5 % 2.0)` → 1).
e : s64 = xx 4.9;
c : s64 = cast(s64) 1.5;
xc : s64 = xx (M + 0.5);
xf : s64 = xx (F + 0.25);
xl : s64 = xx (f64.true_min + 0.5);
xm : s64 = xx (5.5 % 2.0);
xd : s64 = xx (5.0 / 2.0); // non-integral float DIVISION → truncates to 2
print("xx={} cast={} xxExpr={} xxFlt={} xxLimit={} xxMod={} xxDiv={}\n", e, c, xc, xf, xl, xm, xd);
}

View File

@@ -0,0 +1,58 @@
// A raw value binding whose spelling shadows a builtin FLOAT type name
// (`` `f64 ``) and whose FLOAT field is read into an INTEGER binding. Field
// access on such a value is an ORDINARY runtime field read — the unified
// float→int narrowing rule (F0.11) must treat it EXACTLY like a non-shadowed
// struct's field read, never as the builtin numeric-limit accessor. So
// `` `f64.epsilon `` reads the value's `epsilon` field (a runtime f64) and a
// float→int narrowing TRUNCATES it, identical to a plainly-named `b.epsilon` —
// it does NOT fold the builtin `f64.epsilon` (= 2.22e-16) into the binding.
//
// The receiver is a mutable `:=` local, so its field is a RUNTIME value, not a
// compile-time constant: reading it after a reassignment yields the new value,
// proving it can never be const-folded from the initializer literal.
//
// Companion to 0161 (value-shadow field reads in NON-narrowing, s64-field
// contexts). This file exercises the narrowing path 0161 does not: a FLOAT
// field flowing into an integer binding.
//
// Regression (issue 0095 / F0.11-7): the compile-time float evaluator's
// field-access arm misclassified a raw value-shadow receiver as the builtin
// numeric-limit accessor, so `` `f64.epsilon `` newly errored under the
// narrowing rule with the BUILTIN value (2.22e-16) instead of reading the
// field. The fix mirrors the `is_raw` guard the sibling `isFloatValuedExpr`
// already applies, so the const-folding cluster agrees: a raw receiver is a
// field read, only a bare type receiver folds a limit.
#import "modules/std.sx";
FBox :: struct { epsilon: f64; }
main :: () {
// Raw value-shadow of the builtin `f64`, FLOAT field → narrow into s64.
// Ordinary field read + runtime float→int truncation: 11.0 → 11.
`f64 := FBox.{ epsilon = 11.0 };
x : s64 = `f64.epsilon;
// A NON-integral field value truncates exactly the same way — a runtime
// f64 has no compile-time value to fold, so 11.5 → 11 (NOT a non-integral
// narrowing error, which would only fire on a compile-time-constant float).
`f64b := FBox.{ epsilon = 11.5 };
y : s64 = `f64b.epsilon;
// The value-shadowed read is identical to a plainly-named one: `b.epsilon`
// narrows the same way, so the backtick spelling changes nothing.
b := FBox.{ epsilon = 11.5 };
yb : s64 = b.epsilon;
print("x={} y={} yb={}\n", x, y, yb); // 11 11 11
// The field is a RUNTIME value: reassign, then read → the new value, not
// the initializer literal (so const-folding it would be unsound).
`f64.epsilon = 4.0;
xm : s64 = `f64.epsilon;
print("xm={}\n", xm); // 4
// The bare builtin receiver (not raw-escaped) is UNAFFECTED — it still
// folds the numeric limit. `f64.max - f64.max` = 0.0 is integral → 0.
lim : s64 = f64.max - f64.max;
print("lim={}\n", lim); // 0
}

View File

@@ -0,0 +1,29 @@
// A generic value parameter (`$K: u32`) bound from a named const or a
// constant-foldable expression resolves to the SAME monomorphised instantiation
// as the literal form: `Vec(N, f32)` (N a module const) and `Vec(M + 1, f32)`
// (a const expression) are both `Vec(3, f32)`. The struct-copy assignment is the
// proof — it type-checks only because the two spellings name one instantiation.
//
// Regression (issue 0083): the value-param binder hand-rolled an `else => 0`
// switch, so a named-const value arg either fabricated a 0 binding under a wrong
// mangled name or was rejected outright as "unknown type 'N'". It now folds
// through the shared const-int evaluator (`program_index.evalConstIntExpr`).
#import "modules/std.sx";
N :: 3;
M :: 2;
Vec :: struct ($K: u32, $T: Type) { data: [K]T; }
main :: () {
a : Vec(N, f32) = ---; // named-const value param
a.data[0] = 10.0; a.data[1] = 20.0; a.data[2] = 30.0;
print("named: len={} a0={} a2={}\n", a.data.len, a.data[0], a.data[2]);
e : Vec(M + 1, f32) = ---; // const-expr value param (M + 1 == 3)
e.data[0] = 1.0; e.data[2] = 9.0;
print("expr: len={} e2={}\n", e.data.len, e.data[2]);
b : Vec(3, f32) = a; // same instantiation → struct copy type-checks
print("copy: len={} b2={}\n", b.data.len, b.data[2]);
}

View File

@@ -0,0 +1,32 @@
// A type-RETURNING function with a value parameter (`$K: u32`) used as a TYPE
// annotation: `b : Make(N, s64)` where `Make :: ($K, $T) -> Type { return [K]T; }`.
// A named-const value arg (`Make(N, s64)`), a const-expression value arg
// (`Make(M + 1, s64)`), and the literal form (`Make(3, s64)`) all instantiate to
// the SAME type — the array copy `b : Make(3, s64) = a` type-checks only because
// the three spellings name one `[3]s64`.
//
// Regression (issue 0083 / F0.4 attempt 6): the unknown-type checker walked the
// value-param position as a type name ("unknown type 'N'"), and the
// parameterized-type-annotation path never routed to `instantiateTypeFunction`,
// nor did that binder resolve a non-struct/union return shape (`return [K]T`).
// The value arg now folds through the shared const-int evaluator and the type
// function resolves its general return-type expression with bindings active.
#import "modules/std.sx";
N :: 3;
M :: 2;
Make :: ($K: u32, $T: Type) -> Type { return [K]T; }
main :: () {
a : Make(N, s64) = ---; // named-const value param
a[0] = 10; a[1] = 20; a[2] = 30;
print("named: len={} a0={} a2={}\n", a.len, a[0], a[2]);
e : Make(M + 1, s64) = ---; // const-expr value param (M + 1 == 3)
e[0] = 1; e[2] = 9;
print("expr: len={} e2={}\n", e.len, e[2]);
b : Make(3, s64) = a; // same instantiation → array copy type-checks
print("copy: len={} b2={}\n", b.len, b[2]);
}

View File

@@ -0,0 +1,19 @@
// A generic value parameter (`$K: u32`) binds a literal (`Vec(3, s64)`) and an
// integral-float named const (`Vec(L, s64)` with `L : f64 : 4.0`) to the same
// integer a plain `4` would — the value-param arg folds through the shared
// const-int evaluator, so the integral-float rule (F0.4 attempt 8, Agra ruling)
// reaches value params too. The folded value is the array length `[K]s64`.
//
// The bind is range-checked against the declared `u32` (an out-of-range arg is a
// clean compile error — see 1134); a valid in-range value binds normally.
#import "modules/std.sx";
Vec :: struct ($K: u32, $T: Type) { data: [K]T; }
L : f64 : 4.0;
main :: () {
a : Vec(3, s64) = ---; // literal value param
b : Vec(L, s64) = ---; // integral-float named-const value param → 4
print("a.len={} b.len={}\n", a.data.len, b.data.len); // 3 and 4
}

View File

@@ -0,0 +1,22 @@
// Closure literal declared (and used) inside a `defer` body.
//
// Regression (issue 0073): this used to segfault lowering. A lambda inherited
// the enclosing function's `func_defer_base`, so the lambda's `return` re-drained
// the enclosing function's defers — and when the defer body itself declared the
// lambda, that re-lowered the lambda forever (infinite recursion). A lambda now
// opens its own defer window (like every other function-lowering entry).
#import "modules/std.sx";
run :: () {
defer {
cb := (n: s32) -> s32 { return n * 2; };
print("defer closure: {}\n", cb(21)); // 42, at scope exit
}
print("body\n");
}
main :: () -> s32 {
run();
return 0;
}

View File

@@ -0,0 +1,23 @@
// `inline for 0..K` with a named-const or constant-foldable bound unrolls at
// compile time, just like a literal bound.
//
// Regression (issue 0083): the inline-for bound folder (`evalComptimeInt`) only
// handled literals, comptime cursors, and `<pack>.len`, so `inline for 0..M`
// (M a module const) and `inline for 0..(M + 1)` (a const expression) both
// failed with "range end is not a compile-time integer". `evalComptimeInt` now
// delegates to the single shared const-int evaluator
// (`program_index.evalConstIntExpr`), so the inline-for bound and an array
// dimension fold the same shapes to the same value.
#import "modules/std.sx";
M :: 3;
main :: () {
s := 0;
inline for 0..M: (i) { s += i; }
print("sum 0..M = {}\n", s); // 0 + 1 + 2 = 3
t := 0;
inline for 0..(M + 1): (i) { t += i; }
print("sum 0..(M+1) = {}\n", t); // 0 + 1 + 2 + 3 = 6
}

View File

@@ -0,0 +1,14 @@
// An `inline for 0..M` bound accepts an integral float constant — `M :: 3.0`
// unrolls the same three iterations as `M :: 3`. The inline-for bound folder
// (`evalComptimeInt`) delegates to the shared const-int evaluator, so the
// integral-float rule (issue 0083 / F0.4 attempt 8, Agra ruling) applies here
// too.
#import "modules/std.sx";
M :: 3.0;
main :: () {
s := 0;
inline for 0..M: (i) { s += i; }
print("sum 0..M = {}\n", s); // 0 + 1 + 2 = 3
}

View File

@@ -0,0 +1,24 @@
// An `inline for` / `for` range bound is a range ENDPOINT, not a count, so the
// count negative-rejection rule does NOT apply to it: negative endpoints are
// valid and an empty/inverted range simply runs zero iterations.
//
// Regression (F0.4 attempt 11, Agra ruling): the spec wrongly lumped inline-for
// bounds with counts (array dim / Vector lane / value-param), which reject
// negatives. Bounds are exempt — `inline for -2..1` iterates -2,-1,0 and an
// integral-float empty range `0..(-2.0)` runs zero iterations. Comptime and
// runtime loops must agree.
#import "modules/std.sx";
main :: () {
s := 0;
inline for -2..1: (i) { s += i; }
print("inline for -2..1 sum = {}\n", s); // -2 + -1 + 0 = -3
r := 0;
for -2..1: (i) { r += i; }
print("for -2..1 sum = {}\n", r); // -2 + -1 + 0 = -3 (runtime parity)
e := 0;
inline for 0..(-2.0): (i) { e += i; }
print("inline for 0..(-2.0) sum = {}\n", e); // empty range -> 0 iterations
}

View File

@@ -0,0 +1,32 @@
// Comptime `#run` formatting of an `Any` that holds a `Type`.
//
// `print("{}", at)` where `at: Any` holds a `Type` value routes through
// `format` → `any_to_string`, whose `type := type_of(val)` lowers (for an
// `.any` operand) to `struct_get(val, 0)` — reading the Any-Type's tag.
// At runtime a `Type` value is the aggregate `{ tag=.any, value=tid }`,
// so the read works and the type's name prints. The comptime interpreter
// stores a first-class `Type` as a bare `.type_tag`, so the struct_get
// must mirror that same `{ .any, tid }` layout — otherwise it bails and
// the `#run` truncates. Reflection over the same `Any` (`type_name`,
// `type_is_unsigned`) already works; the value-print must match.
//
// Regression (issue 0096): a comptime `#run` print of an `Any`-held
// `Type` silently stopped (omitted `value=` + every later line) yet still
// built with exit 0.
#import "modules/std.sx";
ct_probe :: () {
print("before\n");
x : u64 = 1;
t : Type = type_of(x);
at : Any = t;
print("name={}\n", type_name(at));
print("unsigned={}\n", type_is_unsigned(at));
print("value={}\n", at);
print("after\n");
}
#run ct_probe();
main :: () {}

View File

@@ -0,0 +1,40 @@
// Streaming SHA-256 (FIPS 180-4) from `modules/std/hash.sx`.
//
// Known-answer vectors (empty, "abc", the 112-byte NIST multi-block
// vector) plus the streaming invariant: feeding the same bytes in
// several `update` chunks yields the same digest as the one-shot call.
//
// The digest is a zero-heap `[64]u8` returned by value; tests build a
// `string` view over it (no copy) to compare against the pinned hex.
#import "modules/std.sx";
#import "modules/std/hash.sx";
check :: (label: string, got: [64]u8, want: string) {
view := string.{ ptr = @got[0], len = 64 };
if view == want {
print("{}: ok\n", label);
} else {
print("{}: FAIL got {} want {}\n", label, view, want);
}
}
main :: () {
check("empty", sha256_hex(""),
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
check("abc", sha256_hex("abc"),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
// 112-byte input — spans more than one 64-byte block.
multi := "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
check("multi", sha256_hex(multi),
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1");
// Streaming must equal one-shot regardless of chunk boundaries.
h := init();
h.update("abcdefghbcdefghicdefghijdefghijke"); // 33 bytes
h.update("fghijklfghijklmghijklmnhijklmno"); // crosses block edge
h.update("ijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu");
check("stream-eq-oneshot", h.final(),
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1");
}

View File

@@ -0,0 +1,82 @@
// SHA-256 known-answer vectors for `modules/std/hash.sx`.
//
// Pins published digests for: the empty input, "abc", the two NIST
// multi-block sample vectors, the padding/length boundaries around the
// 56-byte (one-block-with-length) and 64-byte (block) edges, and the
// classic large repeats (1000 and 1,000,000 'a'). Each expected hex is
// hard-coded from FIPS 180-4 / NIST CAVP and cross-checked with
// `shasum -a 256`.
//
// The digest is a zero-heap `[64]u8`; we compare it via a `string` view
// (no copy). Repeat vectors are built by streaming an 'a'-filled stack
// buffer, so even the 1,000,000 case allocates nothing on the heap.
#import "modules/std.sx";
#import "modules/std/hash.sx";
check :: (label: string, got: [64]u8, want: string) {
view := string.{ ptr = @got[0], len = 64 };
if view == want {
print("{}: ok\n", label);
} else {
print("{}: FAIL got {} want {}\n", label, view, want);
}
}
// Digest of `total` bytes of 'a', streamed in 1000-byte chunks so peak
// memory stays O(chunk) for the 1,000,000 case. `total == 0` yields the
// empty-input digest (the loop body never runs).
hash_a :: (total: s64) -> [64]u8 {
chunk : [1000]u8 = ---;
k := 0;
while k < 1000 { chunk[k] = 97; k += 1; } // 97 = 'a'
h := init();
remaining := total;
while remaining > 0 {
take := if remaining < 1000 then remaining else 1000;
h.update(string.{ ptr = @chunk[0], len = take });
remaining -= take;
}
h.final()
}
main :: () {
check("empty", sha256_hex(""),
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
check("abc", sha256_hex("abc"),
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
// NIST CAVP sample vectors (56-byte and 112-byte multi-block).
check("nist-56", sha256_hex("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"),
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
check("nist-112", sha256_hex("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"),
"cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1");
// Padding/length boundaries around the 56- and 64-byte edges, using
// 'a' repeats so the boundary is exercised independently of content.
check("len-0", hash_a(0),
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
check("len-55", hash_a(55),
"9f4390f8d30c2dd92ec9f095b65e2b9ae9b0a925a5258e241c9f1e910f734318");
check("len-56", hash_a(56),
"b35439a4ac6f0948b6d6f9e3c6af0f5f590ce20f1bde7090ef7970686ec6738a");
check("len-57", hash_a(57),
"f13b2d724659eb3bf47f2dd6af1accc87b81f09f59f2b75e5c0bed6589dfe8c6");
check("len-63", hash_a(63),
"7d3e74a05d7db15bce4ad9ec0658ea98e3f06eeecf16b4c6fff2da457ddc2f34");
check("len-64", hash_a(64),
"ffe054fe7ae0cb6dc65c3af9b61d5209f439851db43d0ba5997337df154668eb");
check("len-65", hash_a(65),
"635361c48bb9eab14198e76ea8ab7f1a41685d6ad62aa9146d301d4f17eb0ae0");
check("len-119", hash_a(119),
"31eba51c313a5c08226adf18d4a359cfdfd8d2e816b13f4af952f7ea6584dcfb");
check("len-120", hash_a(120),
"2f3d335432c70b580af0e8e1b3674a7c020d683aa5f73aaaedfdc55af904c21c");
// Large repeats (FIPS 180-4 / classic vectors).
check("a-1000", hash_a(1000),
"41edece42d63e8d9bf515a9ba6932e1c20cbc9f5a5d134645adb5db1b9737ea3");
check("a-1000000", hash_a(1000000),
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
}

View File

@@ -0,0 +1,77 @@
// SHA-256 streaming-equivalence + file hashing for `modules/std/hash.sx`.
//
// The chunk boundary must not affect the result: feeding the same bytes
// one-shot, one byte at a time, split mid-block, and split exactly on a
// 64-byte block boundary all yield the same digest, anchored to a pinned
// value. Then `sha256_file` of a written temp file must equal the
// in-memory digest of the same bytes — the streaming file path agrees
// with the buffered path.
//
// All comparisons go through `string` views over the zero-heap `[64]u8`
// digests; the byte/split updates view directly into the input buffer
// (no `substr`, no copies).
#import "modules/std.sx";
#import "modules/std/hash.sx";
#import "modules/fs.sx";
// 112-byte NIST multi-block vector — long enough that a 64-byte split is
// a genuine block boundary and a 30-byte split lands mid-block.
MSG :: "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
PIN :: "cf5b16a778af8380036ce59e7b0492370b249b11e8f07a51afac45037afee9d1";
check :: (label: string, got: [64]u8, want: string) {
view := string.{ ptr = @got[0], len = 64 };
if view == want {
print("{}: ok\n", label);
} else {
print("{}: FAIL got {} want {}\n", label, view, want);
}
}
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
// Absorb `data` one byte at a time (views of length 1 into the buffer).
stream_by_byte :: (data: string) -> [64]u8 {
h := init();
i := 0;
while i < data.len {
h.update(string.{ ptr = @data[i], len = 1 });
i += 1;
}
h.final()
}
// Absorb `data` as two updates split at `at` (views into the buffer).
stream_split :: (data: string, at: s64) -> [64]u8 {
h := init();
h.update(string.{ ptr = @data[0], len = at });
h.update(string.{ ptr = @data[at], len = data.len - at });
h.final()
}
main :: () {
check("oneshot-pinned", sha256_hex(MSG), PIN);
check("byte-at-a-time", stream_by_byte(MSG), PIN);
check("split-mid-block", stream_split(MSG, 30), PIN); // 30: mid first block
check("split-on-boundary", stream_split(MSG, 64), PIN); // 64: exact block edge
// sha256_file (streaming) must equal the in-memory digest.
if !create_dir_all(".sx-tmp") { print("mkdir: FAIL\n"); return; }
path := ".sx-tmp/sx_0712_stream.bin";
if !write_file(path, MSG) { print("file-write: FAIL\n"); return; }
maybe := sha256_file(path);
if maybe == null { print("file-eq-memory: FAIL (open)\n"); return; }
file_digest := maybe!;
mem_digest := sha256_hex(MSG);
fv := string.{ ptr = @file_digest[0], len = 64 };
mv := string.{ ptr = @mem_digest[0], len = 64 };
report("file-eq-memory", fv == mv);
check("file-pinned", file_digest, PIN);
delete_file(path);
}

View File

@@ -0,0 +1,106 @@
// JSON value model + writer from `modules/std/json.sx`.
//
// Builds a representative value — a nested object holding a string with
// every escape kind (quote, newline, tab, backslash, a raw control byte),
// integers spanning zero / a small negative / a small positive / s64 MIN
// (-9223372036854775808) / s64 MAX (9223372036854775807), a bool, null, an
// array, and a nested object — then serializes it two ways and asserts the
// EXACT bytes:
//
// 1. into a caller-owned `[]u8` buffer (returns bytes written),
// 2. streaming straight to a file through an 8-byte staging buffer
// (small on purpose, so the writer flushes many times and no
// whole-document string is ever held).
//
// Both must yield byte-for-byte the same pinned document, with keys in
// INSERTION ORDER. A too-small buffer must raise `error.Overflow` rather
// than truncate. The model is built through an explicit Arena allocator
// and freed in one `deinit`; the writer path allocates nothing.
#import "modules/std.sx";
#import "modules/std/json.sx";
#import "modules/fs.sx";
// The exact document the writer must produce (insertion order, escaping).
EXPECT :: "{\"name\":\"a\\\"b\\n\",\"tab\":\"x\\ty\",\"bs\":\"c\\\\d\",\"ctrl\":\"\\u0001\",\"n\":-7,\"zero\":0,\"pos\":7,\"min\":-9223372036854775808,\"max\":9223372036854775807,\"ok\":true,\"nil\":null,\"xs\":[1,-2,3],\"nested\":{\"k\":\"v\"}}";
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
build :: (alloc: Allocator) -> Value {
// A raw control byte (0x01) viewed as a 1-byte string — exercises the
// `\u00XX` path that has no named shorthand. String values are VIEWS,
// so the bytes must outlive the writes: back them with `alloc` (the
// arena), not a local that dies when `build` returns.
cbytes : [*]u8 = xx alloc.alloc(1);
cbytes[0] = 1;
ctrl := string.{ ptr = cbytes, len = 1 };
nested : Object = .{};
nested.put("k", .str("v"), alloc);
xs : Array = .{};
xs.add(.int_(1), alloc);
xs.add(.int_(0 - 2), alloc);
xs.add(.int_(3), alloc);
obj : Object = .{};
obj.put("name", .str("a\"b\n"), alloc); // quote + newline
obj.put("tab", .str("x\ty"), alloc); // tab
obj.put("bs", .str("c\\d"), alloc); // backslash
obj.put("ctrl", .str(ctrl), alloc); // raw control byte -> 
obj.put("n", .int_(0 - 7), alloc); // small negative int
obj.put("zero", .int_(0), alloc); // zero
obj.put("pos", .int_(7), alloc); // small positive int
// s64 MIN: its magnitude (9223372036854775808) is not a representable
// positive s64 literal, so build it from MAX-positive minus one.
obj.put("min", .int_(0 - 9223372036854775807 - 1), alloc);
obj.put("max", .int_(9223372036854775807), alloc); // s64 MAX
obj.put("ok", .bool_(true), alloc);
obj.put("nil", .null_, alloc);
obj.put("xs", .array(xs), alloc);
obj.put("nested", .object(nested), alloc);
return .object(obj);
}
main :: () -> ! {
gpa := GPA.init();
arena := Arena.init(xx gpa, 4096);
defer arena.deinit();
root := build(xx arena);
// 1. Write into a caller buffer; assert exact bytes + byte count.
buf : [512]u8 = ---;
n := try write_to_buffer(root, string.{ ptr = @buf[0], len = 512 });
view := string.{ ptr = @buf[0], len = n };
print("doc: {}\n", view);
report("buffer-exact", view == EXPECT);
report("buffer-len", n == EXPECT.len);
// 2. A buffer that is one byte too small must raise Overflow.
tight : []u8 = string.{ ptr = @buf[256], len = EXPECT.len - 1 };
_, oerr := write_to_buffer(root, tight);
report("overflow-raised", oerr == error.Overflow);
// 3. Stream to a file through a tiny staging buffer (forces flushes);
// read it back and assert it equals the same document. Write into the
// repo-local, gitignored scratch dir and unlink afterwards so nothing
// leaks and concurrent runs don't fight over a shared /tmp name.
if !create_dir_all(".sx-tmp") { print("mkdir: FAIL\n"); return; }
path := ".sx-tmp/sx_0713_json.json";
fh := open_file(path, .write);
if fh == null { print("open: FAIL\n"); return; }
f := fh!;
stage : [8]u8 = ---;
try write_to_file(root, @f, string.{ ptr = @stage[0], len = 8 });
f.close();
back := read_file(path);
delete_file(path);
if back == null { print("file-read: FAIL\n"); return; }
report("file-exact", back! == EXPECT);
return;
}

View File

@@ -0,0 +1,154 @@
// JSON reader (parser) from `modules/std/json.sx` — the inverse of the
// F2.1 writer.
//
// Parses a representative document (nested object + array + a
// string-with-escapes + ints incl. negatives + bool + null) into the
// shared value model, then proves:
//
// 1. STRUCTURE — the parsed tree has the expected keys (in INSERTION
// order), values, and nesting.
// 2. HEAP DISCIPLINE — an un-escaped string value is a zero-copy VIEW
// into the input buffer (its bytes lie inside `src`), while an
// escaped string is DECODED into a fresh `alloc`-ed buffer (its
// bytes lie OUTSIDE `src`). Composite nodes + the decoded string are
// the only allocations, all through the explicit Arena.
// 3. ROUND-TRIP — feeding the parsed tree back to the writer reproduces
// the canonical input byte-for-byte.
// 4. UNICODE — `\uXXXX` (BMP + 2-byte) and a surrogate pair decode to
// the right UTF-8 bytes.
// 5. FAILURE SURFACING — every malformed input raises the right
// `JsonParseError` variant on the error channel, never a bogus value.
#import "modules/std.sx";
#import "modules/std/json.sx";
// Canonical document: no insignificant whitespace, escapes in the writer's
// own form — so re-serializing the parse must reproduce it exactly.
DOC :: "{\"name\":\"plain\",\"esc\":\"a\\nb\",\"xs\":[10,-20],\"yes\":true,\"nil\":null,\"sub\":{\"k\":\"v\"}}";
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
// Half-open containment [lo, hi).
in_range :: (x: s64, lo: s64, hi: s64) -> bool {
return x >= lo and x < hi;
}
// True when `parse(src)` raised `want` — destructure captures the error
// tag without `try`, so a malformed input never aborts the example.
raises :: (src: string, want: JsonParseError, alloc: Allocator) -> bool {
_, e := parse(src, alloc);
e == want
}
// True when parsing `"a<b>b"` (a string holding the RAW control byte `b`)
// raises BadControlChar. Built from a byte buffer because a raw control
// byte can't appear in an sx string literal.
ctrl_raises :: (b: u8, alloc: Allocator) -> bool {
raw : [5]u8 = ---;
raw[0] = 34; raw[1] = 97; raw[2] = b; raw[3] = 98; raw[4] = 34; // "a<b>b"
return raises(string.{ ptr = @raw[0], len = 5 }, error.BadControlChar, alloc);
}
main :: () -> ! {
gpa := GPA.init();
arena := Arena.init(xx gpa, 8192);
defer arena.deinit();
// ── 1. Structure ─────────────────────────────────────────────────
src := DOC;
root := try parse(src, xx arena);
is_object := if root == { case .object: true; else: false; };
report("root-is-object", is_object);
o := root.object;
report("member-count", o.len == 6);
report("key-order-0", o.items[0].key == "name");
report("string-plain", o.items[0].val.str == "plain");
report("string-escaped", o.items[1].val.str == "a\nb"); // \n decoded to 0x0A
xs := o.items[2].val.array;
report("array-len", xs.len == 2);
report("array-pos", xs.items[0].int_ == 10);
report("array-neg", xs.items[1].int_ == 0 - 20);
report("bool-value", o.items[3].val.bool_ == true);
is_null := if o.items[4].val == { case .null_: true; else: false; };
report("null-value", is_null);
// The nested pair asserted as one expression — a string `==` on each
// side of `and`.
sub := o.items[5].val.object;
report("nested-pair", sub.items[0].key == "k" and sub.items[0].val.str == "v");
// ── 2. Heap discipline: view vs decoded ──────────────────────────
base : s64 = xx src.ptr;
stop := base + src.len;
p_plain : s64 = xx o.items[0].val.str.ptr; // "plain": no escape -> VIEW into src
p_esc : s64 = xx o.items[1].val.str.ptr; // "a\nb": escaped -> DECODED into arena
report("plain-is-view", in_range(p_plain, base, stop));
report("escaped-allocated", !in_range(p_esc, base, stop));
// ── 3. Round-trip back through the writer ────────────────────────
buf : [256]u8 = ---;
n := try write_to_buffer(root, string.{ ptr = @buf[0], len = 256 });
rt := string.{ ptr = @buf[0], len = n };
report("round-trip", rt == src);
// ── 4. Leading/trailing/inner whitespace is insignificant ────────
wsv := try parse(" [ 1 , 2 , 3 ] ", xx arena);
wa := wsv.array;
report("ws-count", wa.len == 3);
report("ws-first", wa.items[0].int_ == 1);
report("ws-last", wa.items[2].int_ == 3);
// Empty container literals (the manifest/db.json use these).
ea := try parse("[]", xx arena);
report("empty-array", ea.array.len == 0);
eo := try parse("{}", xx arena);
report("empty-object", eo.object.len == 0);
// ── 5. Unicode: \uXXXX (1- and 2-byte) + surrogate pair (4-byte) ──
// JSON "Aé😀" -> 'A', 'é' (C3 A9), '😀' (F0 9F 98 80). One byte per report.
univ := try parse("\"\\u0041\\u00e9\\uD83D\\uDE00\"", xx arena);
u := univ.str;
report("uni-len", u.len == 7);
report("uni-A", u[0] == 0x41); // U+0041 -> 1 byte
report("uni-e1", u[1] == 0xC3); // U+00E9 -> 2 bytes
report("uni-e2", u[2] == 0xA9);
report("uni-s0", u[3] == 0xF0); // U+1F600 (surrogate pair) -> 4 bytes
report("uni-s1", u[4] == 0x9F);
report("uni-s2", u[5] == 0x98);
report("uni-s3", u[6] == 0x80);
// ── 6. Malformed inputs each surface the right error variant ─────
report("err-truncated", raises("{\"a\":", error.UnexpectedEnd, xx arena));
report("err-bad-escape", raises("\"a\\xb\"", error.BadEscape, xx arena));
report("err-trailing-junk", raises("[1,2] x", error.TrailingGarbage, xx arena));
report("err-bad-token", raises("xyz", error.UnexpectedToken, xx arena));
report("err-fraction", raises("1.5", error.BadNumber, xx arena));
report("err-leading-zero", raises("01", error.BadNumber, xx arena));
report("err-overflow", raises("9223372036854775808", error.BadNumber, xx arena));
report("err-unterminated", raises("\"abc", error.UnexpectedEnd, xx arena));
// ── 7. RFC 8259 §7: unescaped control bytes (U+0000..U+001F) ──────
// A RAW control byte inside a string is invalid JSON -> BadControlChar.
report("err-raw-tab", ctrl_raises(9, xx arena)); // raw 0x09
report("err-raw-lf", ctrl_raises(10, xx arena)); // raw 0x0A
report("err-raw-nul", ctrl_raises(0, xx arena)); // raw 0x00
// POSITIVE: the ESCAPED control forms stay valid and decode to the
// exact bytes. JSON "\t\n\u0009" -> 0x09 0x0A 0x09 (3 bytes).
esc := try parse("\"\\t\\n\\u0009\"", xx arena);
es := esc.str;
report("esc-ctrl-len", es.len == 3);
report("esc-tab", es[0] == 0x09); // \t
report("esc-lf", es[1] == 0x0A); // \n
report("esc-u", es[2] == 0x09); // \u0009
print("=== DONE ===\n");
return;
}

View File

@@ -0,0 +1,223 @@
// Comprehensive pinned suite for `modules/std/json.sx` (writer F2.1 +
// reader F2.2). Mirrors what 0711 did for std.hash: it LOCKS IN the full
// round-trip and the complete malformed-input matrix as one coherent
// pinned example. (0713/0714 stay as the focused writer/reader demos with
// their heap-discipline narrative; this file is the correctness lock-in.)
//
// PART A — ROUND-TRIP. Build a representative document covering EVERY
// value kind (nested object + array, a string carrying every escape
// kind `\" \\ \b \f \n \r \t` and a `\u00XX` control, integers 0 /
// small-negative / s64 MIN (-9223372036854775808) / s64 MAX
// (9223372036854775807), bool, null) through an explicit Arena, then
// `build -> write -> parse -> write`: assert the writer's EXACT bytes,
// assert `parse` then re-`write` reproduces them (idempotent), and
// spot-check the parsed tree's STRUCTURE incl. INSERTION ORDER.
// PART B — DECODE POSITIVES. `\/`, the full named-escape set, `\uXXXX`
// (BMP 1- and 2-byte) and a SURROGATE PAIR, the escaped control forms,
// and raw multi-byte UTF-8 round-tripping through writer + reader.
// PART C — MALFORMED MATRIX. One assertion per `JsonParseError` variant
// and its key edges, each asserted to RAISE (never crash, never accept).
//
// Every model is built through an explicit Arena allocator (heap
// discipline): scalars carry no heap, string values are views, composites
// and decoded strings go through `alloc`, and the writer allocates nothing.
#import "modules/std.sx";
#import "modules/std/json.sx";
// The writer's EXACT output for the PART A document (insertion order,
// canonical escaping). Hand-pinned so a writer regression fails loudly in
// the example itself, not only in the captured golden.
EXPECT :: "{\"esc\":\"\\\"\\\\\\b\\t\\n\\f\\r\\u0001\",\"zero\":0,\"neg\":-7,\"min\":-9223372036854775808,\"max\":9223372036854775807,\"ok\":true,\"nil\":null,\"xs\":[1,-2,3],\"nested\":{\"k\":\"v\"}}";
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
// Half-open containment [lo, hi).
in_range :: (x: s64, lo: s64, hi: s64) -> bool {
return x >= lo and x < hi;
}
// True when `parse(src)` raised exactly `want`. Destructure captures the
// error tag without `try`, so a malformed input never aborts the example.
raises :: (src: string, want: JsonParseError, alloc: Allocator) -> bool {
_, e := parse(src, alloc);
e == want
}
// True when parsing `"a<b>b"` (a string holding the RAW control byte `b`)
// raises BadControlChar. Built from a byte buffer because a raw control
// byte can't appear in an sx string literal.
ctrl_raises :: (b: u8, alloc: Allocator) -> bool {
raw : [5]u8 = ---;
raw[0] = 34; raw[1] = 97; raw[2] = b; raw[3] = 98; raw[4] = 34; // "a<b>b"
return raises(string.{ ptr = @raw[0], len = 5 }, error.BadControlChar, alloc);
}
// Build the PART A document: every value kind, in the insertion order the
// writer must emit. The `esc` value carries one byte per escape kind; its
// bytes are backed by `alloc` (string values are VIEWS, so they must
// outlive `build`).
build :: (alloc: Allocator) -> Value {
ebytes : [*]u8 = xx alloc.alloc(8);
ebytes[0] = 34; // " -> \"
ebytes[1] = 92; // \ -> \\
ebytes[2] = 8; // BS -> \b
ebytes[3] = 9; // TAB -> \t
ebytes[4] = 10; // LF -> \n
ebytes[5] = 12; // FF -> \f
ebytes[6] = 13; // CR -> \r
ebytes[7] = 1; // SOH ->  (control with no named shorthand)
esc := string.{ ptr = ebytes, len = 8 };
nested : Object = .{};
nested.put("k", .str("v"), alloc);
xs : Array = .{};
xs.add(.int_(1), alloc);
xs.add(.int_(0 - 2), alloc);
xs.add(.int_(3), alloc);
obj : Object = .{};
obj.put("esc", .str(esc), alloc);
obj.put("zero", .int_(0), alloc);
obj.put("neg", .int_(0 - 7), alloc);
// s64 MIN: |MIN| is not a representable positive s64 literal, so build
// it as MAX-positive minus one.
obj.put("min", .int_(0 - 9223372036854775807 - 1), alloc);
obj.put("max", .int_(9223372036854775807), alloc);
obj.put("ok", .bool_(true), alloc);
obj.put("nil", .null_, alloc);
obj.put("xs", .array(xs), alloc);
obj.put("nested", .object(nested), alloc);
return .object(obj);
}
main :: () -> ! {
gpa := GPA.init();
arena := Arena.init(xx gpa, 16384);
defer arena.deinit();
a : Allocator = xx arena;
// ── PART A. build -> write -> parse -> write ─────────────────────────
root := build(a);
buf : [512]u8 = ---;
n := try write_to_buffer(root, string.{ ptr = @buf[0], len = 512 });
canon := string.{ ptr = @buf[0], len = n };
print("doc: {}\n", canon); // golden pins the exact bytes
report("rt-exact", canon == EXPECT);
report("rt-len", n == EXPECT.len);
// parse the writer's output, then re-serialize: must reproduce it byte
// for byte (writer/reader are inverses on the canonical form).
tree2 := try parse(canon, a);
buf2 : [512]u8 = ---;
n2 := try write_to_buffer(tree2, string.{ ptr = @buf2[0], len = 512 });
canon2 := string.{ ptr = @buf2[0], len = n2 };
report("rt-idempotent", canon2 == canon);
// Structure of the parsed tree: insertion order + every value kind.
o := tree2.object;
report("st-count", o.len == 9);
report("st-order",
o.items[0].key == "esc" and o.items[1].key == "zero" and
o.items[2].key == "neg" and o.items[3].key == "min" and
o.items[4].key == "max" and o.items[5].key == "ok" and
o.items[6].key == "nil" and o.items[7].key == "xs" and
o.items[8].key == "nested");
// The escaped string survives the round-trip back to its 8 raw bytes.
eexp : [8]u8 = ---;
eexp[0] = 34; eexp[1] = 92; eexp[2] = 8; eexp[3] = 9;
eexp[4] = 10; eexp[5] = 12; eexp[6] = 13; eexp[7] = 1;
report("st-esc", o.items[0].val.str == string.{ ptr = @eexp[0], len = 8 });
report("st-zero", o.items[1].val.int_ == 0);
report("st-neg", o.items[2].val.int_ == 0 - 7);
report("st-min", o.items[3].val.int_ == 0 - 9223372036854775807 - 1);
report("st-max", o.items[4].val.int_ == 9223372036854775807);
report("st-bool", o.items[5].val.bool_ == true);
is_null := if o.items[6].val == { case .null_: true; else: false; };
report("st-null", is_null);
xs := o.items[7].val.array;
report("st-xs", xs.len == 3 and xs.items[0].int_ == 1 and
xs.items[1].int_ == 0 - 2 and xs.items[2].int_ == 3);
sub := o.items[8].val.object;
report("st-nested", sub.len == 1 and sub.items[0].key == "k" and
sub.items[0].val.str == "v");
// ── PART B. decode positives ─────────────────────────────────────────
// `\/` decodes to a bare slash (the writer emits it unescaped, so this
// is a parse-only form).
slash := try parse("\"\\/\"", a);
report("dec-slash", slash.str == "/");
// The full named-escape set in one string: \" \\ \/ \b \f \n \r \t.
esc := try parse("\"\\\"\\\\\\/\\b\\f\\n\\r\\t\"", a);
sexp : [8]u8 = ---;
sexp[0] = 34; sexp[1] = 92; sexp[2] = 47; sexp[3] = 8;
sexp[4] = 12; sexp[5] = 10; sexp[6] = 13; sexp[7] = 9;
report("dec-escapes", esc.str == string.{ ptr = @sexp[0], len = 8 });
// \uXXXX: BMP 1-byte (A), BMP 2-byte (é), and a SURROGATE PAIR (😀).
// "Aé😀" -> 41 | C3 A9 | F0 9F 98 80 (7 bytes).
uni := try parse("\"\\u0041\\u00e9\\uD83D\\uDE00\"", a);
uexp : [7]u8 = ---;
uexp[0] = 0x41; uexp[1] = 0xC3; uexp[2] = 0xA9;
uexp[3] = 0xF0; uexp[4] = 0x9F; uexp[5] = 0x98; uexp[6] = 0x80;
report("dec-surrogate", uni.str == string.{ ptr = @uexp[0], len = 7 });
// POSITIVE counterpart to BadControlChar: the ESCAPED control forms
// backslash-t, backslash-n and backslash-u-0009 decode to 09 0A 09.
ectrl := try parse("\"\\t\\n\\u0009\"", a);
cexp : [3]u8 = ---;
cexp[0] = 9; cexp[1] = 10; cexp[2] = 9;
report("dec-esc-ctrl", ectrl.str == string.{ ptr = @cexp[0], len = 3 });
// Raw multi-byte UTF-8 (>= 0x80) round-trips writer -> reader unchanged.
ubytes : [*]u8 = xx a.alloc(7);
ubytes[0] = 0x41; ubytes[1] = 0xC3; ubytes[2] = 0xA9;
ubytes[3] = 0xF0; ubytes[4] = 0x9F; ubytes[5] = 0x98; ubytes[6] = 0x80;
uval : Value = .str(string.{ ptr = ubytes, len = 7 });
ubuf : [64]u8 = ---;
un := try write_to_buffer(uval, string.{ ptr = @ubuf[0], len = 64 });
uback := try parse(string.{ ptr = @ubuf[0], len = un }, a);
report("rt-utf8", uback.str == string.{ ptr = @ubytes[0], len = 7 });
// ── PART C. malformed-input matrix — one assertion per variant + edge ─
// UnexpectedToken: bad literal, non-string key, missing comma.
report("err-token-literal", raises("xyz", error.UnexpectedToken, a));
report("err-token-key", raises("{1:2}", error.UnexpectedToken, a));
report("err-token-comma", raises("[1 2]", error.UnexpectedToken, a));
// UnexpectedEnd: truncated object / array / string.
report("err-end-object", raises("{\"a\":", error.UnexpectedEnd, a));
report("err-end-array", raises("[1,", error.UnexpectedEnd, a));
report("err-end-string", raises("\"abc", error.UnexpectedEnd, a));
// BadEscape: unknown escape, non-hex \u, high surrogate not followed by
// a low surrogate.
report("err-esc-unknown", raises("\"a\\xb\"", error.BadEscape, a));
report("err-esc-bad-hex", raises("\"\\uZZZZ\"", error.BadEscape, a));
report("err-esc-surrogate", raises("\"\\uD83D\\u0041\"", error.BadEscape, a));
// BadNumber: leading zero, lone minus, fraction, exponent, and an
// integer just past s64 MAX (overflow).
report("err-num-leadzero", raises("01", error.BadNumber, a));
report("err-num-lonedash", raises("-", error.BadNumber, a));
report("err-num-fraction", raises("1.5", error.BadNumber, a));
report("err-num-exponent", raises("1e9", error.BadNumber, a));
report("err-num-overflow", raises("9223372036854775808", error.BadNumber, a));
// TrailingGarbage: junk after a complete value.
report("err-trail-array", raises("[1,2] x", error.TrailingGarbage, a));
report("err-trail-scalar", raises("null x", error.TrailingGarbage, a));
// BadControlChar: a raw control byte (< 0x20) inside a string.
report("err-ctrl-tab", ctrl_raises(9, a)); // raw 0x09
report("err-ctrl-lf", ctrl_raises(10, a)); // raw 0x0A
report("err-ctrl-nul", ctrl_raises(0, a)); // raw 0x00
print("=== DONE ===\n");
return;
}

View File

@@ -0,0 +1,31 @@
// Real OS-argv accessor from `modules/std/cli.sx` (#foreign _NSGetArgv).
//
// Only DETERMINISTIC structural invariants are asserted — the actual arg
// contents depend on how the test is invoked (under `sx run` the process
// argv is the interpreter's: ["sx", "run", "<this file>"]), so we never
// pin exact strings:
// - argc >= 1 (every process has argv[0])
// - argv[0] is non-empty (the executable path)
// - os_argc() agrees with the filled slice length (no truncation)
//
// `buf` is a stack `[64]string`; `os_args` fills it with zero-copy views
// over the C runtime's argv block — no heap, no per-arg allocation.
#import "modules/std.sx";
#import "modules/std/cli.sx";
main :: () {
buf : [64]string = ---;
args := os_args(buf[0..64]);
if args.len >= 1 { print("argc>=1: ok\n"); }
else { print("argc>=1: FAIL ({})\n", args.len); }
if args.len >= 1 {
if args[0].len > 0 { print("arg0-nonempty: ok\n"); }
else { print("arg0-nonempty: FAIL\n"); }
}
if os_argc() == args.len { print("argc-consistent: ok\n"); }
else { print("argc-consistent: FAIL (os_argc={} len={})\n", os_argc(), args.len); }
}

View File

@@ -0,0 +1,219 @@
// CLI argument PARSER from `modules/std/cli.sx` (F3.2) — subcommand
// dispatch + `--flag` parsing over an EXPLICIT logical argv (`[]string`).
//
// Every argv vector below is an explicit `[]string` literal (the caller's
// logical args, program name already removed). The suite proves:
//
// 1. DISPATCH — `<group> <command>` selects the right command in the
// caller's table; group/command are VIEWS into argv.
// 2. FLAGS — `--out VALUE` (value-taking) binds a VIEW of the next
// token; `--verbose` (boolean) records presence; the
// reserved `--json` mode flag surfaces as `parsed.json`.
// 3. SEPARATORS — `--` and the first bare operand both stop flag
// parsing; the remainder is `parsed.rest` (operand VIEWS).
// 4. HEAP — flag values / group / command / rest all point INSIDE
// the input argv (zero copy); `Parsed` is a stack value.
// 5. FAILURES — unknown command, unknown flag, missing required flag,
// and a value-flag with no value each raise the specific
// `CliError` variant on the error channel, and the
// caller-owned `Diag` names the offending token.
#import "modules/std.sx";
#import "modules/std/cli.sx";
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
// Half-open containment [lo, hi) — used to prove a view points into argv.
in_range :: (x: s64, lo: s64, hi: s64) -> bool {
return x >= lo and x < hi;
}
// True when `parse(args, cmds)` raised exactly `want`. Destructure binds
// the error tag without `try`, so a bad vector never aborts the example;
// the failing token is captured in the caller-owned `Diag`.
raises :: (args: []string, cmds: []Command, want: CliError) -> bool {
d : Diag = .{};
_, e := parse(args, cmds, @d);
return e == want;
}
main :: () -> ! {
// ── Command table (caller storage; flag specs passed as views) ────
publish_flags : []FlagSpec = .[
FlagSpec.{ name = "out", takes_value = true, required = true },
FlagSpec.{ name = "verbose", takes_value = false, required = false },
];
status_flags : []FlagSpec = .[
FlagSpec.{ name = "verbose", takes_value = false, required = false },
];
cmds : []Command = .[
Command.{ group = "ci", command = "publish", flags = publish_flags },
Command.{ group = "ci", command = "status", flags = status_flags },
];
// ── 1. Valid: <group> <command> --flag v --bool --json ───────────
d : Diag = .{};
argv : []string = .["ci", "publish", "--out", "dist", "--verbose", "--json"];
p := try parse(argv, cmds, @d);
report("dispatch-group", p.group == "ci");
report("dispatch-command", p.command == "publish");
report("dispatch-index", p.cmd_index == 0);
report("flag-value", p.value_of("out") == "dist");
report("flag-value-set", p.is_set("out"));
report("bool-set", p.is_set("verbose"));
report("json-set", p.json);
report("no-rest", p.rest.len == 0);
// ── 2. Heap discipline: flag value is a VIEW into argv ────────────
// "dist" is argv[3]; its bytes must lie inside that very element.
src : s64 = xx argv[3].ptr;
stop := src + argv[3].len;
pview : s64 = xx p.value_of("out").ptr;
report("value-is-view", in_range(pview, src, stop) or pview == src);
// group/command are argv[0]/argv[1] verbatim (same pointer, no copy).
g0 : s64 = xx argv[0].ptr;
gp : s64 = xx p.group.ptr;
report("group-is-view", gp == g0);
// ── 3. Dispatch to a different command in the table ──────────────
s_argv : []string = .["ci", "status", "--verbose"];
sp := try parse(s_argv, cmds, @d);
report("dispatch-2nd", sp.command == "status" and sp.cmd_index == 1);
report("2nd-bool", sp.is_set("verbose"));
report("2nd-json-unset", !sp.json);
// ── 4. `--` separator: rest are operand views, flags stop there ──
sep_argv : []string = .["ci", "publish", "--out", "dist", "--", "--raw", "x"];
spv := try parse(sep_argv, cmds, @d);
report("sep-value", spv.value_of("out") == "dist");
report("sep-rest-len", spv.rest.len == 2);
report("sep-rest-0", spv.rest.len == 2 and spv.rest[0] == "--raw");
report("sep-rest-1", spv.rest.len == 2 and spv.rest[1] == "x");
report("sep-no-bool", !spv.is_set("verbose"));
// ── 5. First bare operand also stops flag parsing ────────────────
bare_argv : []string = .["ci", "publish", "--out", "dist", "extra", "tail"];
bpv := try parse(bare_argv, cmds, @d);
report("bare-rest-len", bpv.rest.len == 2);
report("bare-rest-0", bpv.rest.len == 2 and bpv.rest[0] == "extra");
// ── 6. Value-flag accepts a single-dash value (not a long flag) ──
dash_argv : []string = .["ci", "publish", "--out", "-5", "--verbose"];
dpv := try parse(dash_argv, cmds, @d);
report("dash-value", dpv.value_of("out") == "-5" and dpv.is_set("verbose"));
// ── 7. Failures: each surfaces the specific variant ──────────────
a_zero_args : []string = .[]; // nothing at all
a_unknown_cmd : []string = .["ci", "deploy", "--out", "x"];
a_unknown_group : []string = .["zz", "publish", "--out", "x"];
a_too_few : []string = .["ci"];
a_unknown_flag : []string = .["ci", "publish", "--out", "x", "--nope"];
a_missing_value : []string = .["ci", "publish", "--out"];
a_value_eats : []string = .["ci", "publish", "--out", "--verbose"];
a_missing_req : []string = .["ci", "publish", "--verbose"];
// A command whose FlagSpec list exceeds the inline `Parsed.values` cap
// (16): the parser rejects it with TooManyFlags rather than silently
// truncating. 17 specs (> 16) trips the check right after dispatch
// matches (group, command), before any flag is read.
over_flags : []FlagSpec = .[
FlagSpec.{ name = "f00", takes_value = false, required = false },
FlagSpec.{ name = "f01", takes_value = false, required = false },
FlagSpec.{ name = "f02", takes_value = false, required = false },
FlagSpec.{ name = "f03", takes_value = false, required = false },
FlagSpec.{ name = "f04", takes_value = false, required = false },
FlagSpec.{ name = "f05", takes_value = false, required = false },
FlagSpec.{ name = "f06", takes_value = false, required = false },
FlagSpec.{ name = "f07", takes_value = false, required = false },
FlagSpec.{ name = "f08", takes_value = false, required = false },
FlagSpec.{ name = "f09", takes_value = false, required = false },
FlagSpec.{ name = "f10", takes_value = false, required = false },
FlagSpec.{ name = "f11", takes_value = false, required = false },
FlagSpec.{ name = "f12", takes_value = false, required = false },
FlagSpec.{ name = "f13", takes_value = false, required = false },
FlagSpec.{ name = "f14", takes_value = false, required = false },
FlagSpec.{ name = "f15", takes_value = false, required = false },
FlagSpec.{ name = "f16", takes_value = false, required = false },
];
over_cmds : []Command = .[ Command.{ group = "big", command = "cmd", flags = over_flags } ];
over_args : []string = .["big", "cmd"];
report("err-zero-args", raises(a_zero_args, cmds, error.UnknownCommand));
report("err-unknown-cmd", raises(a_unknown_cmd, cmds, error.UnknownCommand));
report("err-unknown-group", raises(a_unknown_group, cmds, error.UnknownCommand));
report("err-too-few", raises(a_too_few, cmds, error.UnknownCommand));
report("err-unknown-flag", raises(a_unknown_flag, cmds, error.UnknownFlag));
report("err-missing-value", raises(a_missing_value, cmds, error.MissingValue));
report("err-value-eats-flag", raises(a_value_eats, cmds, error.MissingValue));
report("err-missing-req", raises(a_missing_req, cmds, error.MissingRequired));
report("err-too-many-flags", raises(over_args, over_cmds, error.TooManyFlags));
// ── 8. Diag pins the offending (token, index) for EVERY raise site ─
// Each failure records the exact offending token (a VIEW into `args`,
// except missing-required / too-many which name the spec's flag / the
// command) plus its `args` index, so a caller can report which token
// failed. This covers ALL SIX raise sites in cli.sx, both UnknownCommand
// sub-branches included:
// - zero-arg -> index -1, token "" (args.len == 0)
// - too-few -> index 0, token args[0] (args.len == 1)
// - unknown pair -> index 1, token command (group OR command wrong)
// - too-many -> index -1, token command (spec count > 16 cap)
// - unknown flag -> index i, token flag tok
// - missing val -> index i, token flag tok
// - missing req -> index -1, token flag name
// The three index==-1 cases (zero-arg, too-many, missing-req) COINCIDE
// with `Diag`'s `.{}` defaults (index -1, token ""), so those Diags are
// seeded with a sentinel first: the assertion then proves `parse`
// actually WROTE the value, not that it merely left the default.
de : Diag = .{};
_, ue := parse(a_unknown_flag, cmds, @de);
report("diag-flag-tag", ue == error.UnknownFlag);
report("diag-flag-token", de.token == "--nope" and de.index == 4);
dc : Diag = .{};
_, ce := parse(a_unknown_cmd, cmds, @dc);
report("diag-cmd-tag", ce == error.UnknownCommand);
report("diag-cmd-token", dc.token == "deploy" and dc.index == 1);
dg : Diag = .{};
_, ge := parse(a_unknown_group, cmds, @dg);
report("diag-group-tag", ge == error.UnknownCommand);
report("diag-group-token", dg.token == "publish" and dg.index == 1);
df : Diag = .{};
_, fe := parse(a_too_few, cmds, @df);
report("diag-too-few-tag", fe == error.UnknownCommand);
report("diag-too-few-token", df.token == "ci" and df.index == 0);
d0 : Diag = .{ index = 999, token = "<unset>" }; // sentinel: -1/"" are defaults
_, z0e := parse(a_zero_args, cmds, @d0);
report("diag-zero-args-tag", z0e == error.UnknownCommand);
report("diag-zero-args-token", d0.token == "" and d0.index == -1);
dv : Diag = .{};
_, ve := parse(a_missing_value, cmds, @dv);
report("diag-missing-value-tag", ve == error.MissingValue);
report("diag-missing-value-token", dv.token == "--out" and dv.index == 2);
dz : Diag = .{};
_, ze := parse(a_value_eats, cmds, @dz);
report("diag-value-eats-tag", ze == error.MissingValue);
report("diag-value-eats-token", dz.token == "--out" and dz.index == 2);
dm : Diag = .{ index = 999, token = "<unset>" }; // sentinel: -1 is the default
_, me := parse(a_missing_req, cmds, @dm);
report("diag-req-tag", me == error.MissingRequired);
report("diag-req-token", dm.token == "out");
report("diag-req-index", dm.index == -1);
dt : Diag = .{ index = 999, token = "<unset>" }; // sentinel: -1 is the default
_, te := parse(over_args, over_cmds, @dt);
report("diag-too-many-tag", te == error.TooManyFlags);
report("diag-too-many-token", dt.token == "cmd" and dt.index == -1);
print("=== DONE ===\n");
return;
}

View File

@@ -0,0 +1,69 @@
// std.cli EXIT-CODE + `--json` contract (F3.3 — FOUNDATION MILESTONE CLOSE).
//
// The minimal contract `dist` (and any sx CLI front-end) relies on:
//
// 1. NAMED EXIT CODES — `EX_OK` (0) and `EX_USAGE` (64, the sysexits.h
// usage-error code) are public constants in `std.cli`.
// 2. TERMINATORS — `exit_ok()` / `exit_usage()` end the process with the
// matching code, routing through the canonical `process.exit(code: u8)`.
// 3. `--json` MODE — the reserved global `--json` flag surfaces as
// `parsed.json`: TRUE when `--json` is in the argv, FALSE when it is not.
// In json mode stdout carries ONLY the machine result; human text goes
// to stderr (here via `log.err`, which writes `ERROR: …` to fd 2).
//
// The run DELIBERATELY ends on the usage path: after the assertions it
// triggers an `UnknownCommand`, writes the human diagnostic to STDERR, and
// terminates via `exit_usage()` — so the process exits 64 (EX_USAGE),
// captured in expected/0718-…​.exit. (sx `print`/`out` are unbuffered, so the
// stdout assertion lines still appear despite the `_exit`.)
#import "modules/std.sx";
#import "modules/std/cli.sx";
log :: #import "modules/log.sx";
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
main :: () -> ! {
publish_flags : []FlagSpec = .[
FlagSpec.{ name = "out", takes_value = true, required = true },
];
cmds : []Command = .[
Command.{ group = "ci", command = "publish", flags = publish_flags },
];
// ── 1. Named exit-code constants (the dist contract) ──────────────
report("ex-ok-is-0", EX_OK == 0);
report("ex-usage-is-64", EX_USAGE == 64);
// ── 2. `--json` detection — true with the flag, false without ─────
d : Diag = .{};
with_json : []string = .["ci", "publish", "--out", "dist", "--json"];
without_json : []string = .["ci", "publish", "--out", "dist"];
pj := try parse(with_json, cmds, @d);
pn := try parse(without_json, cmds, @d);
report("json-set-true", pj.json);
report("json-set-false", !pn.json);
// ── 3. json mode keeps stdout machine-pure ────────────────────────
// When `parsed.json`, the front-end emits ONLY the machine result on
// stdout. `out` writes the bytes verbatim (no `{}` interpolation), so
// the JSON braces are literal.
if pj.json {
out("{\"out\":\"");
out(pj.value_of("out"));
out("\"}\n");
}
// ── 4. Usage error → human text to stderr → exit_usage() (= 64) ───
// A bad command raises a `CliError`. The front-end maps every usage
// error to `EX_USAGE`: it writes the human diagnostic to STDERR (stdout
// stays machine-clean) and terminates with the usage code via the
// canonical `process.exit`.
bad : []string = .["ci", "deploy", "--out", "x"]; // unknown command
_, e := parse(bad, cmds, @d);
report("usage-error-raised", e == error.UnknownCommand);
log.err("unknown command '{}' (argv index {})", d.token, d.index);
exit_usage(); // -> _exit(EX_USAGE = 64)
}

View File

@@ -0,0 +1,56 @@
// Two modules that each export a top-level `parse` — `std.cli.parse`
// (3-param subcommand dispatch) and `std.json.parse` (2-param document
// reader) — imported into ONE program under DISTINCT namespaces, with
// BOTH `parse`s actually called.
//
// Regression (issue 0100): same-name cross-module functions collided in
// the bare-name function table during IR lowering. Lowering re-resolved a
// call by SHORT name, so importing both modules and calling one bound the
// wrong-arity same-named function and tripped `lazyLowerFunction`'s
// param-count assert (panic). The fix resolves each `pkg.parse(...)` to a
// UNIQUE module-qualified FuncId, so `cli.parse` and `json.parse` are
// independent identities.
#import "modules/std.sx";
cli :: #import "modules/std/cli.sx";
json :: #import "modules/std/json.sx";
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
main :: () -> ! {
gpa := GPA.init();
arena := Arena.init(xx gpa, 8192);
defer arena.deinit();
// ── cli.parse: dispatch <group> <command> + a value flag ─────────
publish_flags : []FlagSpec = .[
FlagSpec.{ name = "out", takes_value = true, required = true },
];
cmds : []Command = .[
Command.{ group = "ci", command = "publish", flags = publish_flags },
Command.{ group = "ci", command = "status", flags = .[] },
];
argv : []string = .["ci", "publish", "--out", "dist"];
d : Diag = .{};
p := try cli.parse(argv, cmds, @d);
report("cli-group", p.group == "ci");
report("cli-command", p.command == "publish");
report("cli-index", p.cmd_index == 0);
report("cli-flag", p.value_of("out") == "dist");
// ── json.parse: read a small document into the value model ───────
doc := "{\"name\":\"sx\",\"xs\":[1,2,3]}";
root := try json.parse(doc, xx arena);
o := root.object;
report("json-members", o.len == 2);
report("json-key0", o.items[0].key == "name");
report("json-str", o.items[0].val.str == "sx");
xs := o.items[1].val.array;
report("json-arr-len", xs.len == 3);
report("json-arr-2", xs.items[2].int_ == 3);
print("=== DONE ===\n");
return;
}

View File

@@ -0,0 +1,26 @@
// Regression (issue 0100 F1): a QUALIFIED imported function that calls a
// function from its OWN flat import.
//
// `calc :: #import …` registers `calc.compute` as a module-qualified alias
// with a unique FuncId (the identity fix that resolves the cross-module
// same-name `parse` collision, issue 0100 / example 0719). That alias is
// lowered through `lazyLowerFunction`'s null-FuncId `lowerFunction` path,
// which has no declared `Function.source_file` to restore. Before the fix it
// lowered `calc.compute`'s body in the CALLER's (this file's) visibility
// context, so `compute`'s calls to `triple` / `base` — visible only from
// calc.sx's own `#import "util.sx"` — were rejected "not visible". The fix
// carries the alias's declaring source so it lowers in calc.sx's context.
#import "modules/std.sx";
calc :: #import "0720-modules-qualified-own-import/calc.sx";
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
main :: () -> s32 {
// 14 * 3 = 42, computed by calc.compute -> triple(base()), both of which
// live in calc.sx's own flat import.
report("qualified-own-import", calc.compute() == 42);
0
}

View File

@@ -0,0 +1,8 @@
// `calc` is pulled in under a QUALIFIED namespace by the consumer
// (`calc :: #import …`), yet its own body calls `triple` / `base` from
// calc.sx's OWN flat `#import "util.sx"`. The qualified alias `calc.compute`
// must lower in calc.sx's source context so those own-import callees stay
// visible — issue 0100 F1.
#import "util.sx";
compute :: () -> s64 { return triple(base()); }

View File

@@ -0,0 +1,2 @@
triple :: (x: s64) -> s64 { return x * 3; }
base :: () -> s64 { return 14; }

View File

@@ -0,0 +1,31 @@
// Regression (issue 0100 F2): lowering a QUALIFIED imported function whose
// body terminates must leave the CALLER's lowering state untouched.
//
// `m :: #import …` registers `m.foo` as a module-qualified alias with a unique
// FuncId (the identity fix, issue 0100 / example 0719) and lowers it through
// `lazyLowerFunction`'s null-FuncId `lowerFunction` path. `foo`'s body folds
// `if true { return helper(); }` to an unconditional return, so its lowering
// ends with `block_terminated = true`. The null-FuncId path used to restore
// every saved caller field EXCEPT `block_terminated`, so that flag leaked back
// into `main`, and `main`'s own trailing `print` / `return 0` were treated as
// dead-after-terminator — the compiler rejected `return 0` with "body produces
// no value". The fix routes all exit paths through one save/restore defer, so
// the qualified alias is transparent to the caller. (`helper` also lives in
// m.sx's own flat import, exercising the F1 source-context restore too.)
#import "modules/std.sx";
m :: #import "0721-modules-qualified-terminating-callee/m.sx";
report :: (label: string, ok: bool) {
if ok { print("{}: ok\n", label); } else { print("{}: FAIL\n", label); }
}
main :: () -> s32 {
// Qualified callee whose body terminates via a constant-folded `if true`.
x := m.foo();
// Caller statements AFTER the call must still be emitted (not dead).
report("terminating-callee", x == 7);
print("after\n");
// The caller's OWN return — rejected pre-fix because block_terminated leaked.
return 0;
}

View File

@@ -0,0 +1,3 @@
// Lives in m.sx's OWN flat import — reachable from `foo` but not from the
// top-level consumer that imports m.sx under a qualified namespace.
helper :: () -> s64 { return 7; }

View File

@@ -0,0 +1,10 @@
// `foo` is pulled in QUALIFIED by the consumer (`m :: #import …`). Its body
// terminates via a constant-folded `if true { return … }`, and the `return`
// calls `helper` from m.sx's OWN flat import. Lowering `foo` as a qualified
// alias must be transparent to the caller's lowering state — issue 0100 F2.
#import "helper.sx";
foo :: () -> s64 {
if true { return helper(); }
return 0;
}

View File

@@ -23,9 +23,9 @@ S :: struct {
}
main :: () {
s1 := S.{ a = 42, b = "hi", c = true };
print("{}\n", s1);
s2 := S.{ a = null, b = null, c = null };
print("{}\n", s2);
filled := S.{ a = 42, b = "hi", c = true };
print("{}\n", filled);
empty := S.{ a = null, b = null, c = null };
print("{}\n", empty);
0;
}

View File

@@ -0,0 +1,51 @@
// Postfix `!` (optional force-unwrap) chained directly with a member access.
// `opt!.field`, `opt!.method()`, `opt!.a.b`, and `opt![i]` must read the same
// value the bind-first form (`v := opt!; v.field`) produces — the unwrapped
// value's type has to flow into the chained access.
//
// Regression (issue 0101): chained `opt!.field` typed its receiver as
// `.unresolved` (inferExprType had no force_unwrap arm), so a string field read
// as garbage and `opt!.method()` failed to resolve at all.
#import "modules/std.sx";
Inner :: struct { tag: string; k: s64; }
S :: struct {
id: string;
n: s64;
inner: Inner;
greet :: (self: *S) -> string { return self.id; } // pointer receiver
bump :: (self: S, extra: s64) -> s64 { return self.n + extra; } // value receiver
}
mk :: () -> ?S {
return S.{ id = "hello", n = 42, inner = Inner.{ tag = "deep", k = 7 } };
}
arr :: () -> ?[3]s64 {
v : [3]s64 = .[10, 20, 30];
return v;
}
main :: () -> void {
// opt!.field — string and int field, chained vs bind-first.
print("chain id: {}\n", mk()!.id); // hello
print("chain n: {}\n", mk()!.n); // 42
v := mk()!;
print("bind id: {}\n", v.id); // hello
print("bind n: {}\n", v.n); // 42
// opt!.method()
print("meth ptr: {}\n", mk()!.greet()); // hello
print("meth val: {}\n", mk()!.bump(8)); // 50
// nested opt!.a.b
print("nest tag: {}\n", mk()!.inner.tag); // deep
print("nest k: {}\n", mk()!.inner.k); // 7
// opt![i]
print("index 0: {}\n", arr()![0]); // 10
print("index 2: {}\n", arr()![2]); // 30
}

View File

@@ -0,0 +1,48 @@
// A closure literal inside a `defer` / `onfail` body is its OWN function
// boundary (ERR step E1.7). Two boundary effects, both pinned here:
//
// (a) `checkCleanupNode` sees a bare lambda STATEMENT as a `.lambda` node and
// STOPS — it does not descend into the lambda body. So the bare failable
// inside the lambda is the lambda's concern, not a cleanup violation
// (were the `.lambda` arm to recurse, this bare `failing()` would reject
// like the ones in 1052).
//
// (b) value-slot liveness (E1.8) is analysed per-boundary: `flowExpr` recurses
// into the lambda via `analyzeFnBody`, so a value slot read inside the
// lambda must prove its own error absent — `v` here is live under its
// `if !err` guard. (The rejecting counterpart is 1053.)
//
// Also: `try` is legal inside the lambda (it propagates through the lambda's own
// `!E` channel) even though it is parser-banned in the cleanup body directly.
//
// Locks the closure-boundary arms of the error-flow pass before A5.2 extracts it
// into its own module. Constructible since issue 0073 (closure literal in a
// `defer` body no longer segfaults lowering — see 0310).
#import "modules/std.sx";
E :: error { Bad }
failing :: () -> !E { raise error.Bad; }
recover :: () -> (s32, !E) { return 21; }
work :: () {
defer {
// (a) bare lambda statement — checkCleanupNode stops at the `.lambda`.
() -> !E { failing(); };
// (b) called closure — its body is analysed as its own boundary.
emit := () -> !E {
v, err := recover();
if !err { print("defer closure: v={}\n", v); } // E1.8: live under guard
try failing();
};
emit() catch e print("defer closure: raised\n");
}
print("body\n");
}
main :: () -> s32 {
work();
return 0;
}

View File

@@ -0,0 +1,36 @@
// The cleanup-absorption check (ERR step E1.7) is TRANSITIVE: a bare,
// un-absorbed failable call is rejected no matter how deeply it is nested
// inside a `defer` / `onfail` body's control flow — through `if` (both
// branches), nested blocks, and loops. 1049 covers the direct-body case; this
// pins the recursive arms of `checkCleanupNode` (`.if_expr`, `.block`,
// `.while_expr`) before A5.2 extracts the pass into its own module.
//
// Three bare failables, three rejections; the program never runs (exit 1).
#import "modules/std.sx";
E :: error { Bad }
failing :: () -> !E { raise error.Bad; }
work :: (n: s32) -> !E {
defer {
if n > 0 {
failing(); // REJECTED: nested in the `if` then-branch
} else {
{ failing(); } // REJECTED: nested block in the else-branch
}
}
onfail {
while n > 0 {
failing(); // REJECTED: nested in the `while` body
}
}
if n < 0 { raise error.Bad; }
return;
}
main :: () -> s32 {
a := work(-1);
return 0;
}

View File

@@ -0,0 +1,31 @@
// Value-slot liveness (ERR step E1.8) is analysed inside a nested lambda as its
// OWN boundary: `flowExpr` recurses into a lambda literal via `analyzeFnBody`.
// Reading a failable's value slot inside the lambda where its error is NOT
// proven absent is rejected — even though the lambda is never called and the
// outer function proves nothing for it.
//
// Negative counterpart to 1051(b): were `flowExpr`'s `.lambda` recursion
// removed, the lambda body would go un-analysed and this read would slip
// through. The program never runs (exit 1).
#import "modules/std.sx";
E :: error { Bad }
parse :: (n: s32) -> (s32, !E) {
if n < 0 { raise error.Bad; }
return n * 10;
}
build :: () {
emit := () -> s32 {
v, err := parse(5);
return v; // REJECTED: err not proven absent (inside lambda)
};
print("unreached\n");
}
main :: () -> s32 {
build();
return 0;
}

View File

@@ -0,0 +1,40 @@
// Backtick raw identifier as the error-tag binding of `catch` and `onfail`. A
// reserved type-name spelling (`s2`, `u8`) is a value name when backticked, so
// it is accepted as the tag binding and a later reference resolves to it. A
// *bare* reserved spelling in the same position is still rejected (see
// examples/1123), so the backtick escape is the only way to spell these tags.
// Regression (issue 0089 — attempt-2 catch/onfail coverage).
#import "modules/std.sx";
E :: error { Bad, Empty }
parse :: (n: s32) -> (s32, !E) {
if n < 0 { raise error.Bad; }
if n == 0 { raise error.Empty; }
return n * 2;
}
// `catch` tag binding spelled `s2`, referenced in the match body.
classify :: (n: s32) -> s32 {
return parse(n) catch `s2 == {
case .Bad: 1;
case .Empty: 2;
else: 3
};
}
// `onfail` tag binding spelled `u8`, referenced in the cleanup body.
cleanup :: (n: s32) -> !E {
onfail `u8 { if `u8 == error.Bad { print("cleanup: bad\n"); } }
if n < 0 { raise error.Bad; }
return;
}
main :: () -> s32 {
print("classify(-1) = {}\n", classify(-1));
print("classify(0) = {}\n", classify(0));
print("classify(5) = {}\n", classify(5));
c := cleanup(-1);
print("done\n");
return 0;
}

View File

@@ -0,0 +1,44 @@
// Enum-valued value-carrying failable: the SUCCESS path must zero the trailing
// error slot. Regression (issue 0097). A `-> (Enum, !E)` `return .variant`
// resolves the enum literal against the function's VALUE type (the enum), not
// the failable tuple — otherwise the literal mis-resolves (tag 0) and is stamped
// with the tuple type, which the success-return lowering mistakes for a forwarded
// full tuple and leaves the error slot UNDEFINED (read back as garbage nonzero).
//
// This pins the slot at RUNTIME on the success path (cast(s64) e, bare `if e`,
// and `e == error.X`) — not only via the `if !e` proof that the compiler can
// fold away. It also exercises a non-zero ordinal (`.blue` = 2) so a value slot
// that collapses to 0 is caught, and asserts the error PATH still carries the
// right tag and `error_tag_name`.
#import "modules/std.sx";
Color :: enum { red; green; blue; }
E :: error { Nope }
pick :: (s: string) -> (Color, !E) {
if s == "red" { return .red; }
if s == "blue" { return .blue; } // non-zero ordinal (2)
raise error.Nope;
}
main :: () -> s32 {
// ── success path: error slot MUST read 0 at runtime ──
c, e := pick("red");
print("success err int = {}\n", cast(s64) e); // 0
if e { print("bare-if e: ERROR (WRONG)\n"); } else { print("bare-if e: ok\n"); }
if e == error.Nope { print("e == Nope (WRONG)\n"); } else { print("e != Nope (ok)\n"); }
if !e { print("guard !e: c = {}\n", cast(s64) c); } // 0 (red)
// ── non-zero ordinal: value slot must carry the real ordinal ──
c2, e2 := pick("blue");
if !e2 { print("blue: err int = {}, c = {}\n", cast(s64) e2, cast(s64) c2); } // 0, 2
// ── error path: the right tag flows through ──
c3, e3 := pick("xxx");
print("error err int = {}\n", cast(s64) e3); // 1
if e3 == error.Nope { print("error: is Nope (ok)\n"); } else { print("error: not Nope (WRONG)\n"); }
print("error tag name = {}\n", error_tag_name(e3)); // Nope
return 0;
}

View File

@@ -0,0 +1,66 @@
// Enum-valued value-carrying failables, two paths the bare-success fix (issue
// 0097) did NOT originally cover. Regression (issue 0097, attempt-2 review F1+F2).
//
// F1 — EXPLICIT full failable tuple return `return (.v, error.X)`. The bug was
// the inverse of 0097: narrowing the return target to the value type for ALL
// value-failables broke the explicit-tuple form (the trailing error element no
// longer resolved against the error set → `.unresolved` field → LLVM-emit
// panic). The target must stay the FULL failable tuple for an explicit tuple
// literal of full arity, and narrow to the value type only for a BARE value.
// This pins both branches in one fn: bare-value success + explicit-tuple error.
//
// F2 — COMPTIME-PARAM ($n) value-failable. The body is INLINED at the call
// site (lowerComptimeCall), so the success return takes the inline-return path,
// which the original fix skipped — it stored `{value, undef}` (error slot
// undefined) on success. Read the error slot at RUNTIME on the success path
// (cast, bare `if`, `== error.X`) so an undef slot is caught, not masked by the
// `if !e` proof.
#import "modules/std.sx";
Color :: enum { red; green; blue; }
E :: error { Nope }
// F1: bare-value success path AND explicit-tuple error path in one function.
classify :: (s: string) -> (Color, !E) {
if s == "ok" { return .blue; } // bare value → {2, 0}
return (.red, error.Nope); // explicit full tuple → {0, 1}
}
// F2: comptime parameter forces inline lowering of the body.
ct_pick :: ($n: s32, s: string) -> (Color, !E) {
if s == "red" { return .red; } // bare value, inline path → {0, 0}
if s == "blue" { return .blue; } // bare value, inline path → {2, 0}
raise error.Nope; // inline error path → {undef, 1}
}
main :: () -> s32 {
// ── F1 success (bare value, explicit-tuple error fn): error slot 0 ──
c, e := classify("ok");
print("F1 ok: err int = {}\n", cast(s64) e); // 0
if e { print("F1 ok bare-if: ERROR (WRONG)\n"); } else { print("F1 ok bare-if: ok\n"); }
if !e { print("F1 ok guard: c = {}\n", cast(s64) c); } // 2 (blue)
// ── F1 error (explicit tuple): right tag flows, no panic ──
c2, e2 := classify("bad");
print("F1 bad: err int = {}\n", cast(s64) e2); // 1
if e2 == error.Nope { print("F1 bad: is Nope (ok)\n"); } else { print("F1 bad: not Nope (WRONG)\n"); }
print("F1 bad: tag name = {}\n", error_tag_name(e2)); // Nope
// ── F2 success (comptime-param, inline path): error slot 0 at runtime ──
c3, e3 := ct_pick(7, "red");
print("F2 red: err int = {}\n", cast(s64) e3); // 0
if e3 { print("F2 red bare-if: ERROR (WRONG)\n"); } else { print("F2 red bare-if: ok\n"); }
if e3 == error.Nope { print("F2 red == Nope (WRONG)\n"); } else { print("F2 red != Nope (ok)\n"); }
if !e3 { print("F2 red guard: c = {}\n", cast(s64) c3); } // 0
c4, e4 := ct_pick(7, "blue");
if !e4 { print("F2 blue: err int = {}, c = {}\n", cast(s64) e4, cast(s64) c4); } // 0, 2
// ── F2 error (comptime-param, inline error path): right tag ──
c5, e5 := ct_pick(7, "x");
print("F2 err: err int = {}\n", cast(s64) e5); // 1
if e5 == error.Nope { print("F2 err: is Nope (ok)\n"); } else { print("F2 err: not Nope (WRONG)\n"); }
return 0;
}

View File

@@ -0,0 +1,10 @@
// A value parameter declared `T: Type` (WITHOUT the `$` generic sigil) used in
// a type position is rejected with a hint to write `$T: Type`. Previously the
// type resolver silently fabricated a 0-field struct named `T`, so the call
// compiled and rendered as `T{}` at runtime with no diagnostic.
// Regression (issue 0064). Expected: one error per `T` use site; exit 1.
idwrap :: (T: Type, f: Closure() -> T) -> T { return f(); }
main :: () -> s32 {
return idwrap(s32, closure(() -> s32 { return 7; }));
}

View File

@@ -0,0 +1,13 @@
// An identifier used in a type position that names no declared type, builtin,
// or in-scope generic parameter is rejected. Previously the type resolver's
// empty-struct-stub fallback silently interned a 0-field struct under the typo,
// so the program compiled and ran. Regression (issue 0064, broader fix).
// Expected: a clean "unknown type" error at the field; exit 1.
Point :: struct {
x: s32;
y: Coordnate; // typo for a non-existent type
}
main :: () -> s32 {
return 0;
}

View File

@@ -0,0 +1,10 @@
// An identifier used in a LOCAL variable's type annotation that names no
// declared type is rejected, exactly like a signature or struct-field use.
// Previously a body-level annotation bypassed the check: the type resolver's
// empty-struct stub silently gave the local a 0-field type, so `v: Coordnate
// = 5` compiled and ran (the `5` dropped) with no diagnostic. Regression
// (issue 0064, body-level positions). Expected: error at the annotation; exit 1.
main :: () -> s32 {
v: Coordnate = 5;
return 0;
}

View File

@@ -0,0 +1,12 @@
// An unknown type in a NESTED closure body's local annotation is rejected,
// with the enclosing function's generic params still in scope. Previously the
// body walk stopped at closure / nested-function boundaries, so a typo'd type
// inside a closure slipped through and silently became a 0-field struct.
// Regression (issue 0064, nested scopes). Expected: error at the annotation; exit 1.
main :: () -> s32 {
f := closure(() -> s32 {
bad: Coordnate = ---;
return 0;
});
return f();
}

View File

@@ -0,0 +1,12 @@
// `cast(T)` where `T` is a value parameter declared `: Type` (without the `$`
// generic sigil) is rejected with the generic-param hint. Previously it
// silently cast to a fabricated empty struct (an unknown *literal* cast target
// already errored via value resolution, but the value-param case was silent).
// Regression (issue 0064, cast position). Expected: tailored error; exit 1.
conv :: (T: Type, x: s32) -> s32 {
return cast(T) x;
}
main :: () -> s32 {
return conv(s32, 5);
}

View File

@@ -0,0 +1,13 @@
// A tuple literal used in a type position (`(s32, s32)` reinterpreted as a tuple
// type at a type-demanding site like `size_of`) must list only types. A non-type
// element — here the `1` in `(s32, 1)` — is rejected with a user-facing
// diagnostic instead of silently fabricating an `s64` field for that slot.
// Regression (issue 0067).
// Expected: a clean "tuple type element is not a type" error at the `1`; exit 1.
#import "modules/std.sx";
main :: () -> s32 {
print("bad tuple type size = {}\n", size_of((s32, 1)));
0
}

View File

@@ -0,0 +1,18 @@
// A top-level VALUE constant name used in a type position is rejected. Without
// the fix the unknown-type pass added every `const_decl` name to its declared-
// type set, so a value const (`NotAType :: 123`) satisfied the check and the
// type resolver's unknown-name fallback then fabricated an empty struct — the
// program ran and printed `NotAType{}`. Now only consts whose value introduces a
// type (declarations / type-expression aliases) count as type names.
// Regression (issue 0068).
// Expected: a clean "unknown type 'NotAType'" error at the annotation; exit 1.
#import "modules/std.sx";
NotAType :: 123;
main :: () -> s32 {
v: NotAType = ---;
print("value = {}\n", v);
return 0;
}

View File

@@ -0,0 +1,19 @@
// A top-level global initialized from a non-constant expression (here a field
// access on a module constant, `K.x`) is rejected with a diagnostic. Without
// the fix `registerTopLevelGlobal`'s init_val serializer handled only literals
// / array / struct literals / identifiers and let every other shape fall through
// to a null payload, so the global silently zero-initialized (`g=0`) — a wrong
// value with no error.
// Regression (issue 0072).
// Expected: "global 'g' must be initialized by a compile-time constant"; exit 1.
#import "modules/std.sx";
Point :: struct { x: s32; y: s32; }
K : Point : Point.{ x = 9, y = 4 };
g : s32 = K.x;
main :: () -> s32 {
print("g={}\n", g);
return g;
}

View File

@@ -0,0 +1,16 @@
// A value binding (parameter or local `var`) spelled as a reserved/builtin
// type name is rejected at the declaration site, across every declaration
// form: a parameter name (`u8`), a typed local (`s64`, `bool`), and a `:=`
// local (`string`). Such a spelling parses as a `.type_expr` rather than an
// `.identifier`, so the address-of family in lowering mis-lowers it (issue
// 0076). Expected: one error per offending name; exit 1.
#import "modules/std.sx";
takes_u8 :: (u8: s32) -> s32 { return u8; }
main :: () -> s32 {
s64 : s32 = 3;
bool : bool = true;
string := "x";
return 0;
}

View File

@@ -0,0 +1,16 @@
// A value binding spelled as a reserved type name (`s2`, the `sN` arbitrary-
// width int syntax) is rejected at its declaration site even when it lives in
// an IMPORTED module — the reserved-name binding diagnostic covers every
// compiled module, not just the main file. Without universal coverage the
// binding reaches lowering and aborts LLVM verification (a loaded aggregate
// passed by value to a `*Box` param).
//
// Regression (issue 0077): the imported-module facet of issue 0076. Expected:
// one clean diagnostic pointing at the imported module's `s2 := ...`, exit 1 —
// NOT an LLVM verifier abort.
#import "modules/std.sx";
mod :: #import "1120-diagnostics-imported-reserved-type-name/mod.sx";
main :: () -> s32 {
return mod.run_imported_reserved_name();
}

View File

@@ -0,0 +1,16 @@
#import "modules/std.sx";
Box :: struct { total: s64 = 0; count: s64 = 0; }
update :: (self: *Box, n: s64) {
self.total += n;
self.count += 1;
}
run_imported_reserved_name :: () -> s32 {
s2 := Box.{ total = 0, count = 0 };
update(@s2, 5);
s2.update(7);
print("imported s2 total={} count={}\n", s2.total, s2.count);
return 0;
}

View File

@@ -0,0 +1,30 @@
// Reserved/builtin type names are rejected as binding NAMES across every
// control-flow and destructuring form, not just plain `var`/param decls: a
// destructure name (`s2`), an `if`/`while` optional binding (`u8`/`s16`), a
// `for` capture and index name (`bool`/`s32`), and a match-arm capture
// (`string`). Each spelling parses as a `.type_expr`, so the address-of family
// in lowering mis-lowers it (a loaded aggregate passed by value to a `ptr`
// param → LLVM verifier abort). The declaration-site diagnostic comes from one
// EXHAUSTIVE binding-name walk, so no syntactic binding form can slip through.
//
// Regression (issue 0076, attempt-4 coverage). Expected: one error per
// offending name; exit 1 — NOT an LLVM verifier abort.
#import "modules/std.sx";
pair :: () -> (s64, s64) { (1, 2) }
maybe :: () -> ?s64 { return null; }
main :: () -> s32 {
s2, rest := pair(); // destructure name
if u8 := maybe() { } // if optional binding
while s16 := maybe() { break; } // while optional binding
xs := [3]s64.{ 10, 20, 30 };
for xs: (bool) { } // for capture name
for xs: (v, s32) { } // for index name
opt: ?s64 = 5;
r := if opt == { // match-arm capture
case .some: (string) { 0 }
case .none: { 0 }
};
return 0;
}

View File

@@ -0,0 +1,30 @@
// A reserved/builtin type name is rejected as a binding name inside an `impl`
// block's method too — both as a parameter (`u8`) and as a local (`s2`). The
// impl method is reached through the exhaustive binding-name walk's
// `impl_block` arm (→ each method's `fn_decl`), so an `impl` method is no more
// exempt than a free function. Without the diagnostic the reserved local's
// `@s2` mis-lowers (a loaded aggregate passed by value to a `*Box` param →
// LLVM verifier abort).
//
// Regression (issue 0076, attempt-4 coverage). Expected: one error for the
// param and one for the local; exit 1.
#import "modules/std.sx";
Box :: struct { total: s64 = 0; count: s64 = 0; }
update :: (self: *Box, n: s64) { self.total += n; self.count += 1; }
Doer :: protocol { go :: (self: *Self, n: s64); }
impl Doer for Box {
go :: (self: *Box, u8: s64) {
s2 := Box.{ total = 1 };
update(@s2, u8);
self.total += s2.total;
}
}
main :: () -> s32 {
b := Box.{};
b.go(7);
return 0;
}

View File

@@ -0,0 +1,28 @@
// A reserved/builtin type name is rejected as the error-tag binding of a
// `catch` (`u8`) and of an `onfail` (`s64`). Both are reached through the
// exhaustive binding-name walk's `catch_expr` / `onfail_stmt` arms. The tag is
// a scalar, so before the diagnostic these spellings were silently accepted
// (they never reached the address-of mis-lowering) — the binding must still be
// rejected at its declaration.
//
// Regression (issue 0076, attempt-4 coverage). Expected: one error for each
// binding; exit 1.
#import "modules/std.sx";
E :: error { Bad }
must :: (n: s32) -> !E {
if n < 0 { raise error.Bad; }
return;
}
classify :: (n: s32) -> !E {
onfail s64 { } // onfail tag binding
must(n) catch u8 { return; }; // catch tag binding
return;
}
main :: () -> s32 {
classify(-1) catch { };
return 0;
}

View File

@@ -0,0 +1,15 @@
// A reserved type name used as a DESTRUCTURE binding name (`s2`) is rejected
// even when it lives in an IMPORTED module — the exhaustive binding-name walk
// descends the `namespace_decl` an `mod :: #import` wraps and renders the
// diagnostic against that module's source (issue 0077's universal-coverage
// rule applied to the destructure form). Without it the binding reaches
// lowering and aborts LLVM verification.
//
// Regression (issues 0076 + 0077, attempt-4 coverage). Expected: one clean
// diagnostic pointing at the imported module's `s2, rest := ...`, exit 1.
#import "modules/std.sx";
mod :: #import "1124-diagnostics-imported-reserved-destructure/mod.sx";
main :: () -> s32 {
return mod.run();
}

View File

@@ -0,0 +1,8 @@
#import "modules/std.sx";
pair :: () -> (s64, s64) { (1, 2) }
run :: () -> s32 {
s2, rest := pair(); // destructure name in an IMPORTED module
return 0;
}

View File

@@ -0,0 +1,30 @@
// A reserved/builtin type name used as a PARAMETER name is rejected inside the
// two method-with-body forms that carry their params as bare name lists rather
// than `Param` nodes: a protocol default-body method (`u8`) and a sx-defined
// foreign-class (`#objc_class`) method (`s16`). The declaration-site diagnostic
// underlines the OFFENDING PARAMETER itself, not the enclosing `protocol` /
// `#objc_class` block — each method's `param_name_spans` is threaded from the
// parser so the caret lands on the parameter token.
//
// Regression (issue 0076, attempt-5 span precision). Expected: one error per
// offending parameter, each caret on the parameter name; exit 1.
#import "modules/std.sx";
#import "modules/compiler.sx";
Greeter :: protocol {
greet :: (self: *Self, u8: s64) -> s64 {
return u8;
}
}
SxFoo :: #objc_class("SxFoo") {
counter: s32;
bump :: (self: *Self, s16: s32) {
self.counter += s16;
}
}
main :: () -> s32 {
return 0;
}

View File

@@ -0,0 +1,21 @@
// A module-global aggregate with a NULL pointer field is fine (null is a
// compile-time constant), but a sibling field initialized from a NON-constant
// expression (here a runtime function call) must still be rejected loudly. The
// presence of an accepted `null` must NOT widen the gate to admit the
// non-constant neighbor.
// Regression (issue 0081): the null-pointer fix must not regress the
// reject-loud behavior for genuinely non-constant initializers (issues
// 0072/0080). Expected: "global 'boxes' must be initialized by a compile-time
// constant"; exit 1.
#import "modules/std.sx";
runtime_marker :: () -> s64 { return 7; }
Box :: struct { p: *s64; marker: s64; }
boxes : [1]Box = .[ .{ p = null, marker = runtime_marker() } ];
main :: () -> s32 {
print("marker={}\n", boxes[0].marker);
return 0;
}

View File

@@ -0,0 +1,16 @@
// A module-global enum-literal initializer naming a variant that does not exist
// must be rejected loudly — never silently zero-initialized to the first tag.
// Regression (issue 0082): the enum-literal global serializer resolves the tag
// against the destination enum type; an unknown variant emits a diagnostic and
// fails the build instead of falling back to a null (zero-tag) initializer.
// Expected: "'.purple' is not a variant of enum 'Color'"; exit 1.
#import "modules/std.sx";
Color :: enum u8 { red; green; blue; }
bad : Color = .purple;
main :: () -> s32 {
print("{}\n", bad);
return 0;
}

View File

@@ -0,0 +1,26 @@
// A comptime `#run` global initializer that yields a function reference cannot
// be serialized to a static constant: at global-init time (Pass 0) functions
// are not yet declared, and the comptime serialization path has no later
// re-emit, so the func_ref can never resolve to a real function pointer. The
// compiler must reject this with a diagnostic AND a CLEAN non-zero exit — never
// print the error and then fall through into an undef initializer that crashes
// (pre-fix: the diagnostic printed, emission continued, and the JIT segfaulted
// calling through the undef pointer → exit 134).
// Regression (issue 0079 follow-up): every global-init serialization bail now
// routes through `failGlobalInit`, which sets the halt flag so the driver aborts
// after emit() instead of shipping the placeholder.
// Expected: "comptime init of 'fp' produced a reference to function 'add'…";
// exit 1, no segfault.
#import "modules/std.sx";
add :: (a: s32, b: s32) -> s32 { a + b }
pick :: () -> (s32, s32) -> s32 { return add; }
fp :: #run pick();
main :: () -> s32 {
print("{}\n", fp(3, 4));
return 0;
}

View File

@@ -0,0 +1,24 @@
// An array dimension that is not a compile-time integer constant is a hard
// error, not a silently-fabricated 0-length array. Here a type alias's
// dimension is a runtime function call (`get()`), which is genuinely not
// compile-time-known — the registration-time resolver cannot evaluate it.
//
// (A const-FOLDABLE expression dimension such as `[M + 1]` is NOT an error — it
// folds; see examples/0144-types-const-expr-array-dim.sx. Only a dimension with
// a genuinely runtime operand halts here.)
//
// Regression (issue 0083): the stateless resolver printed a non-fatal warning
// and fabricated length 0, then let compilation continue — producing a 0-byte
// alloca and corrupt element access. It now yields the `.unresolved` sentinel,
// which the alias registration surfaces as this diagnostic, aborting the build
// with a non-zero exit.
#import "modules/std.sx";
get :: () -> s64 { return 5; }
BadArr :: [get()]s64;
main :: () {
a : BadArr = ---;
a[0] = 7;
print("a0={}\n", a[0]);
}

Some files were not shown because too many files have changed in this diff Show More