P1: fix Zig FFI to compile and match the Idris2 ABI#39
Merged
Conversation
The Zig FFI reference implementation (src/interface/ffi/src/main.zig)
failed to compile under Zig 0.14.0. Two fixes, both confined to the FFI
layer; the Idris2 ABI remains the source of truth.
1. Syntax error (the reported `expected statement, found ';'`): the
`if (!h.initialized) { ... }` block at the end of
anvomidaviser_get_string was terminated with a stray `;`. An `if`
used as a statement takes no trailing semicolon, so the `};`
following the block body was parsed as an empty statement. Removed
the spurious semicolon.
2. `Handle` was declared as a Zig `opaque {}` type but given fields
(`allocator`, `initialized`) and instantiated with `handle.* = .{...}`
plus field access (`h.allocator`, `h.initialized`). Zig `opaque`
types are unsized and cannot hold or expose fields, so this could
never compile once the parser advanced past the syntax error.
Changed `Handle` to a concrete `struct`, matching the house idiom
in alloyiser's exemplar (its `Model` struct). It is still only ever
handed to C as a pointer, so it stays opaque across the C ABI.
No exported C symbol names changed and no Result codes changed: every
`C:anvomidaviser_*` symbol in Foreign.idr still has a matching
`export fn`, and the Result enum integer values (ok=0, error=1,
invalid_param=2, out_of_memory=3, null_pointer=4, rule_violation=5)
match Types.idr's resultToInt exactly.
Verification:
- zig test src/main.zig -lc -> all 3 tests pass, 0 errors/warnings
- idris2 --build anvomidaviser-abi.ipkg -> exit 0
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019xMKB3T4Vo5FYC7Czx3JSH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
src/interface/ffi/src/main.zigdid not compile under Zig 0.14.0. This PR fixes it with two minimal, FFI-only changes. The Idris2 ABI (src/interface/abi/Anvomidaviser/ABI/Foreign.idr+Types.idr) is the source of truth and is unchanged.Fixes
Syntax error (
expected statement, found ';', ~line 288): theif (!h.initialized) { ... }block at the end ofanvomidaviser_get_stringwas terminated with a stray;. Anifused as a statement takes no trailing semicolon, so the};after the block body was parsed as an empty statement. Removed the spurious semicolon.Handlewas a Zigopaque {}with fields: it was declaredpub const Handle = opaque { allocator, initialized }but then instantiated (handle.* = .{...}) and field-accessed (h.allocator,h.initialized). Zigopaquetypes are unsized and cannot hold or expose fields, so this could never compile once the parser passed the syntax error. ChangedHandleto a concretestruct, matching the house idiom in alloyiser's exemplar (itsModelstruct). It is still only ever handed to C as a pointer, so it remains opaque across the C ABI.ABI alignment (preserved)
C:anvomidaviser_*symbol inForeign.idrhas a matchingexport fn(19/19).Types.idr'sresultToIntexactly:ok=0, error=1, invalid_param=2, out_of_memory=3, null_pointer=4, rule_violation=5.Verification
cd src/interface/ffi && zig test src/main.zig -lc→ all 3 tests pass, 0 errors/warnings.cd src/interface/abi && idris2 --build anvomidaviser-abi.ipkg→ exit 0 (build dir removed afterward).Note: any rust-ci / Hypatia / governance red checks are pre-existing estate-infrastructure issues unrelated to this Zig-only change.
🤖 Generated with Claude Code
https://claude.ai/code/session_019xMKB3T4Vo5FYC7Czx3JSH
Generated by Claude Code