Files
sx/examples/1221-ffi-cstring-returns.sx
agra d88bdd7242 fix(0128): foreign cstring returns + conflicting same-symbol bindings
Two genuine defects behind the 0128 filing (whose original repros were
both poisoned by binding getenv, which std already declares -> *u8):

1. Re-declaring a C symbol was silent first-wins: every call through
   the later declaration was typed by the older signature. Foreign
   registration now dedupes — equal signatures share one FuncId,
   conflicting ones are diagnosed.

2. Foreign -> string / -> ?string returns read garbage: C returns one
   char*, but the LLVM signature declared the fat {ptr,i64} (len =
   register garbage), and ?string was mis-declared SRET (the hidden
   out-pointer landed in the callee's first arg register). cstrRetKind
   now classifies such returns, declares them as plain ptr (never
   sret), and the call site synthesizes {ptr, strlen} via a
   branch-guarded strlen (NULL -> {null,0} / optional null), wrapping
   {string, i1} for ?string.

?[:0]u8 itself resolves fine (it is ?string); the spelling works in
return, param, local, and alias positions.

Regression: examples/1221 (plain + optional non-null + NULL paths) and
examples/1172 (conflict diagnostic); both FAIL pre-fix. The extern
dedupe collapses duplicate libc decls, so affected .ir snapshots were
regenerated. zig build test 426/426; run_examples 602/602;
distribution suite 21/21.
2026-06-12 14:13:01 +03:00

32 lines
1.2 KiB
Plaintext

// Foreign `-> [:0]u8` / `-> ?[:0]u8` returns: C hands back ONE `char *`;
// the fat sx string is synthesized at the call boundary ({ptr, strlen};
// NULL maps to the optional's null / an empty string) — issue 0128.
// Pre-fix, the call read the pointer register pair as {ptr, len} and the
// length was garbage (bus error on print).
#import "modules/std.sx";
libc :: #library "c";
err_text :: (code: i32) -> [:0]u8 #foreign libc "strerror";
sig_text :: (sig: i32) -> ?[:0]u8 #foreign libc "strsignal";
dlerror :: () -> ?[:0]u8 #foreign libc;
main :: () -> i32 {
// plain: strerror(0) = "Undefined error: 0" on macOS — assert shape,
// not the exact text (locale/platform variance)
t := err_text(2);
if t.len < 5 { print("BUG: strerror too short ({})\n", t.len); return 1; }
print("strerror(2) len ok\n");
// optional, non-null branch
o := sig_text(2);
if o == null { print("BUG: strsignal null\n"); return 2; }
if o!.len < 3 { print("BUG: strsignal too short\n"); return 3; }
print("optional non-null ok\n");
// optional, NULL branch: dlerror() with no pending error is NULL
d := dlerror();
if d != null { print("BUG: dlerror non-null\n"); return 4; }
print("optional null ok\n");
return 0;
}