From 73294976d0a1ff7fa985d9abebd89a6153829838 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 26 Jun 2026 22:05:50 +0000 Subject: [PATCH] P1: fix Zig FFI to build and match the Idris2 ABI The Zig FFI (src/interface/ffi/src/main.zig) failed `zig test src/main.zig -lc` under Zig 0.14.0 with two distinct problems. The Idris2 ABI (Wokelangiser/ABI/Foreign.idr + Types.idr) is the source of truth; all exported C symbol names and Result-enum integer values are preserved. Errors found ------------ 1. Eight `pointless discard of function parameter` compile errors. Each TODO-stub (inject_consent, extract_strings, format_locale, check_sensitivity, suggest_alternative) ended with `_ = param;` on parameters that were already used in their null/range validation checks. Zig 0.14 rejects discarding an already-used parameter. 2. After the compile errors cleared, `test "contrast ratio calculation"` failed: black-on-white returned ~120 instead of the expected ~2100. wokelangiser_contrast_ratio used the additive constant 5.0 instead of the WCAG 0.05 offset in `(L1 + 0.05) / (L2 + 0.05)` (luminances are in [0,1]). It also had a dead `if (darker + 5 == 0)` guard that could never fire. Fixes ----- 1. Removed the redundant `_ = param;` discards; the parameters are genuinely consumed by the preceding validation, so the discards were pointless. Replaced them with explanatory comments. No signature or behaviour change. 2. Corrected the contrast formula to use the WCAG 0.05 offset, matching the existing code comment and the ABI doc ("ratio * 100, e.g. 450 = 4.50:1"). Removed the dead division-by-zero guard (denominator is now always >= 0.05). Verification ------------ - `zig test src/main.zig -lc`: all 8 tests pass, zero errors/warnings. - `idris2 --build wokelangiser-abi.ipkg`: exit 0. - All 18 `C:wokelangiser_*` symbols in Foreign.idr have matching `export fn`; Result enum values (0-7) match resultToInt. Only src/interface/ffi/src/main.zig is touched. Co-Authored-By: Claude Opus 4.8 Claude-Session: https://claude.ai/code/session_019xMKB3T4Vo5FYC7Czx3JSH --- src/interface/ffi/src/main.zig | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/interface/ffi/src/main.zig b/src/interface/ffi/src/main.zig index 834de8f..489cb8b 100644 --- a/src/interface/ffi/src/main.zig +++ b/src/interface/ffi/src/main.zig @@ -185,7 +185,7 @@ export fn wokelangiser_inject_consent( } // TODO: Implement consent point injection into source AST - _ = location_ptr; + // (location_ptr is validated above; injection into the source AST is future work.) clearError(); return .ok; @@ -376,9 +376,9 @@ export fn wokelangiser_contrast_ratio(foreground: u32, background: u32) u32 { const lighter = @max(fg_lum, bg_lum); const darker = @min(fg_lum, bg_lum); - // Contrast ratio = (L1 + 0.05) / (L2 + 0.05), scaled by 100 - if (darker + 5 == 0) return 2100; // Maximum contrast (black on white) - return @intFromFloat((lighter + 5.0) / (darker + 5.0) * 100.0); + // Contrast ratio = (L1 + 0.05) / (L2 + 0.05), scaled by 100. + // Black-on-white gives (1.0 + 0.05) / (0.0 + 0.05) * 100 = 2100 (21:1). + return @intFromFloat((lighter + 0.05) / (darker + 0.05) * 100.0); } /// Calculate relative luminance of a 24-bit RGB colour. @@ -429,8 +429,7 @@ export fn wokelangiser_extract_strings( } // TODO: Implement string extraction from source AST - _ = source_ptr; - _ = output_ptr; + // (source_ptr/output_ptr are validated above; extraction is future work.) clearError(); return .ok; @@ -464,8 +463,7 @@ export fn wokelangiser_format_locale( } // TODO: Implement locale-specific formatting - _ = locale_ptr; - _ = value_ptr; + // (locale_ptr/value_ptr are validated above; formatting is future work.) clearError(); return .ok; @@ -502,7 +500,7 @@ export fn wokelangiser_check_sensitivity( } // TODO: Implement cultural sensitivity checking against term database - _ = content_ptr; + // (content_ptr is validated above; sensitivity analysis is future work.) clearError(); return .ok; @@ -536,8 +534,7 @@ export fn wokelangiser_suggest_alternative( } // TODO: Implement alternative suggestion lookup - _ = term_ptr; - _ = output_ptr; + // (term_ptr/output_ptr are validated above; suggestion lookup is future work.) clearError(); return .ok;