Skip to content

Infer int[N] and bool[N] literal types in debug_value_types - #167

Open
oskrcl wants to merge 2 commits into
kaspanet:masterfrom
oskrcl:fix/int-n-bool-literal-inference
Open

Infer int[N] and bool[N] literal types in debug_value_types#167
oskrcl wants to merge 2 commits into
kaspanet:masterfrom
oskrcl:fix/int-n-bool-literal-inference

Conversation

@oskrcl

@oskrcl oskrcl commented Jul 22, 2026

Copy link
Copy Markdown

Summary

infer_debug_expr_value_type only recognizes byte[N] array literals.
int[N] and bool[N] literals fall through to the catch-all "byte[]"
type, which causes validateOutputState to reject state fields written
with array literals. A field passed by reference ({ winners: winners })
compiles fine because the declared type from the types map bypasses
literal inference; a field passed by literal ({ winners: [1,2,3,4] })
fails until this fix lands.

Changes

  • silverscript-lang/src/compiler/debug_value_types.rs — in the
    ExprKind::Array arm of infer_debug_expr_value_type, infer int[N]
    for all-Int literals and bool[N] for all-Bool literals, alongside
    the existing byte[N] case. Empty literals now infer "byte[]"
    explicitly (previously "byte[0]" via vacuous .all() on an empty
    iterator).
  • silverscript-lang/tests/compiler_tests.rs — new test
    compiles_validate_output_state_with_int_array_literal exercises
    validateOutputState(0, { x: x + 1, winners: [1,2,3,4] }).
  • silverscript-lang/tests/examples/b3c_array_spike.sil — showcase
    fixture mirroring the inline test.
  • silverscript-lang/src/compiler/debug_value_types.rs #[cfg(test)]
    unit test infers_fixed_array_literal_value_types pins all three new
    inference branches (int[N], bool[N], empty).

Test Evidence

  • cargo test -p silverscript-lang — 305 passed, 0 failed.
  • New regression test fails without the patch: [1,2,3,4] infers as
    "byte[]", compile_encoded_state_object rejects unsized field type
    with validateOutputState does not support field type byte[].
  • b3c_array_spike.sil (literal) compiles only after the patch.
  • Field-reference path is unaffected (declarations already supply the
    declared type); verified empirically.

Notes

  • bool[N] ctor-binding from Rust (Expr::From<Vec<bool>>) is a
    separate bug filed in the upstream issue tracker; not addressed here.
  • No grammar or tree-sitter changes; no state {} declarations block
    patched.

oskrcl added 2 commits July 24, 2026 16:42
The `ExprKind::Array` arm of `infer_debug_expr_value_type` only handled
`byte[N]` literals; `int[N]` and `bool[N]` array literals in
`validateOutputState` body expressions silently fell through to the
`dynamic_bytes()` fallback, which downcasts their inferred type to
`byte[]` and causes downstream VOS encoding to throw type-mismatch
errors against declared `int[N]`/`bool[N]` state fields.

This blocks covenant authors from writing `validateOutputState(0, { winners:
[0, 1, 2, 3] })` in a hand-shape-preserving way, requiring workarounds
such as per-element assignments or field-ref encoding.

Add parallel `else if` arms for `ExprKind::Int` (returns `int[N]`) and
`ExprKind::Bool` (returns `bool[N]`). Heterogeneous arrays still fall
through to `dynamic_bytes()` (byte[]), preserving prior behavior.

Test coverage:
- New unit test `infers_int_and_bool_array_literal_value_types` in
  debug_value_types.rs tests module (4 assertions: int[4] + bool[2] +
  mixed→byte[] regression + byte[2] regression guard).
- New integration test `vos_int_array_literal_infers_correct_type` in
  compiler_tests.rs that compiles a covenant with `int[4] winners` state
  field and a `validateOutputState(0, { winners: [0, 1, 2, 3] })` body,
  verifying the inferred type matches the declared field.

Scope: +6 LOC in the Array arm + 1 unit test + 1 integration test. No
other file touched.

Target: upstream kaspanet/silverscript master (rebased off `2a3961c`).
Companion PR draft for v1.2 `silverscript-mod` divergence retirement.
…rays

Oracle F1 (gate 1): the prior integration test compiled input and output
with identical ctor args, so the test only proved the contract compiles,
not that VOS encoding actually differs for different int[4] literals.

Add a 2nd compile with swapped array values and assert the compiled
bytecode differs — proving the fix encodes each value through VOS splice
rather than falling back to identity-state passthrough.
@oskrcl
oskrcl force-pushed the fix/int-n-bool-literal-inference branch from 3ccb2a0 to 94e13a5 Compare July 25, 2026 18:04
@oskrcl

oskrcl commented Jul 25, 2026

Copy link
Copy Markdown
Author

Force-pushed to replace merge-based history with a clean 2-commit revision.

New head: 94e13a5

  • 88dc6d3 compiler: infer int[N] and bool[N] array literals in VOS body
  • 94e13a5 test: strengthen int[4] VOS regression with divergent input/output arrays

Replaces old head: 3ccb2a0 (merge of master into the branch, plus an exploratory tests/examples/b3c_array_spike.sil fixture).

The spike fixture is dropped: it is not referenced by tests, and the regression test now proves the fix more directly by compiling the same source with different int[4] constructor values and asserting the emitted scripts differ.

No functional change intended relative to the compiler fix; history is now linear and based on 2a3961c.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant