You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Modeled c_import calls returning Option[*T] mistype the None/aggregate destination as void in codegen #603
A modeled (non-unsafe) c_import function whose return type is Option[*T] fails during LLVM codegen: building the Option aggregate (e.g. None) for the call result resolves its destination struct type to void, aborting compilation.
src/CodegenDispatch.w:3303. dest_sema=14 is ty_void (the 14th base type registered in Sema init).
WITH_DEBUG_MIR_CODEGEN=1 shows: [mir-call] callee=getenv then [mir-agg] fn=main count=0 dest_ty_kind=0 — the None aggregate's destination LLVM type is absent and the sema type resolves to void.
Why it matters / blocking relationship
Blocks #379. The nullable_ptr borrowed-return fact (strchr/getenv/strstr/strrchr/strpbrk) cannot be modeled until this is fixed: those functions must present as safe calls returning Option[*T], which trips this bug. The cstr_in core of #379 shipped in a88df01; the remaining #379 facts are gated on this.
Reproduction (instant — compile-time, no full rebuild)
With the nullable_ptr changes applied (see #379 comment with the exact patch), a minimal program:
use c_import("char *getenv(const char *name);\n")
fn main:
let v = getenv("ABSENT") // modeled: Option[*const u8]
if v == None: print("ok") else: print("bad")
fails as above via ./out/release/bin/with build <file.w>.
Working contrast
malloc (void * _Nullable malloc(size_t) → Option[*mut c_void], exercised by test/behavior/behav_c_import_nullable_ptr_none.w) works — but it is raw, called under unsafe. The modeled call path (which also applies the str→cstr argument coercion) is the one that loses the aggregate destination type. This path was never exercised before because every previously-modeled c_import function had a scalar return.
Status
Root-cause analysis, fix plan, and fix to follow in this issue. Sema appears correct (it constructs the None aggregate, i.e. it typed the result as Option); the gap is in MIR→LLVM lowering / type resolution for the aggregate destination of a modeled c_import Option return.
The "codegen bug" was an artifact of an incorrect implementation approach, not a real blocker. Raw C pointers are natively nullable in With, so the correct fix models the borrowed return as a plain pointer (no Option wrap, no codegen change). Fixed in 490f61b / c9c1d6b (#379).
Isolation experiments (all on the current binary, no rebuild)
Hand-written extern fn xchr(...) -> Option[*const u8], called under unsafe: codegen succeeds (reaches the linker). → not a general bug in Option[*T] extern returns.
Real bindings in ~/.cache/with/c_import:extern fn malloc(__param___size: c_ulong) -> *mut c_void — a plain pointer. Yet test/behavior/behav_c_import_nullable_ptr_none.w uses == None and .unwrap() on malloc's result.
extern fn give() -> *mut u8 with p == None: codegen succeeds. → raw pointers *T carry native Option-like nullability.
translate_type_recursive (ClangBridge.w) never emits "Option[". → the bridge never produces Option returns; inline _Nullable does not wrap either.
Modeled c_import struct-by-value return (DivT mydiv(...)): works ("ok"). → modeled aggregate returns are fine in general.
Why it happened
My nullable_ptr attempt text-wrapped the c_import pointer return as Option[*T] in CImport binding generation, on the false assumption that this matched malloc's representation. It did not — malloc returns a plain *mut c_void, and raw pointers already carry native nullability. The spurious Option[*T]enum binding, when its result aggregate (None) was constructed in the modeled (non-unsafe) call-lowering path, hit an untested aggregate-destination resolution that produced a void struct type (CodegenDispatch.w:3303, dest_sema=14=ty_void). The raw/unsafe path handled it (experiment 1); the modeled path did not. No correct code path produces a modeled c_import call returning Option[*T]: the bridge never emits Option, and the natively-nullable plain pointer is the correct representation.
Fix (implemented)
ci_overlay_return_is_borrowed_ptr (src/SemaDecl.w) exempts curated borrowed nullable-pointer returns from the raw-ABI classification, leaving the return a plain raw pointer. No Option binding-text wrap, no CImport change, no codegen change. strchr/strrchr/strstr/strpbrk/getenv are now modeled, returning native-nullable pointers (== None / .unwrap() / deref-under-unsafe).
Regression tests
Positive:behav_c_import_overlay_strchr_borrowed (Some via .unwrap() + deref, miss == None), behav_c_import_overlay_getenv_none (missing var == None).
Closing as resolved by root cause. The void-aggregate path is unreachable by any correct code — it was triggered only by the incorrect Option[*T] binding-text wrap, which the correct fix never produces (verified: hand-written Option[*T] externs under unsafe, and modeled struct-by-value returns, both work). #379 is unblocked and its nullable_ptr fact has shipped. Reopen only if a future overlay fact deliberately introduces a modeled Option-returning binding, in which case the modeled-call aggregate-return lowering would need to resolve the destination struct type for enum returns.
Activity
Model c_import borrowed nullable-pointer returns (#379)
Add negative test: uncurated pointer return stays raw (#379, #603)
ehartford commentedon Jun 18, 2026
Root-cause analysis
TL;DR
The "codegen bug" was an artifact of an incorrect implementation approach, not a real blocker. Raw C pointers are natively nullable in With, so the correct fix models the borrowed return as a plain pointer (no
Optionwrap, no codegen change). Fixed in 490f61b / c9c1d6b (#379).Isolation experiments (all on the current binary, no rebuild)
extern fn xchr(...) -> Option[*const u8], called underunsafe: codegen succeeds (reaches the linker). → not a general bug inOption[*T]extern returns.~/.cache/with/c_import:extern fn malloc(__param___size: c_ulong) -> *mut c_void— a plain pointer. Yettest/behavior/behav_c_import_nullable_ptr_none.wuses== Noneand.unwrap()on malloc's result.extern fn give() -> *mut u8withp == None: codegen succeeds. → raw pointers*Tcarry native Option-like nullability.translate_type_recursive(ClangBridge.w) never emits"Option[". → the bridge never producesOptionreturns; inline_Nullabledoes not wrap either.DivT mydiv(...)): works ("ok"). → modeled aggregate returns are fine in general.Why it happened
My nullable_ptr attempt text-wrapped the c_import pointer return as
Option[*T]inCImportbinding generation, on the false assumption that this matched malloc's representation. It did not — malloc returns a plain*mut c_void, and raw pointers already carry native nullability. The spuriousOption[*T]enum binding, when its result aggregate (None) was constructed in the modeled (non-unsafe) call-lowering path, hit an untested aggregate-destination resolution that produced a void struct type (CodegenDispatch.w:3303,dest_sema=14=ty_void). The raw/unsafepath handled it (experiment 1); the modeled path did not. No correct code path produces a modeled c_import call returningOption[*T]: the bridge never emitsOption, and the natively-nullable plain pointer is the correct representation.Fix (implemented)
ci_overlay_return_is_borrowed_ptr(src/SemaDecl.w) exempts curated borrowed nullable-pointer returns from the raw-ABI classification, leaving the return a plain raw pointer. NoOptionbinding-text wrap, noCImportchange, no codegen change. strchr/strrchr/strstr/strpbrk/getenv are now modeled, returning native-nullable pointers (== None/.unwrap()/ deref-under-unsafe).Regression tests
behav_c_import_overlay_strchr_borrowed(Some via.unwrap()+ deref, miss== None),behav_c_import_overlay_getenv_none(missing var== None).err_c_import_uncurated_ptr_return_raw(an uncurated pointer-returning c_import fn stays raw / requiresunsafe).Resolution
Closing as resolved by root cause. The void-aggregate path is unreachable by any correct code — it was triggered only by the incorrect
Option[*T]binding-text wrap, which the correct fix never produces (verified: hand-writtenOption[*T]externs underunsafe, and modeled struct-by-value returns, both work). #379 is unblocked and its nullable_ptr fact has shipped. Reopen only if a future overlay fact deliberately introduces a modeledOption-returning binding, in which case the modeled-call aggregate-return lowering would need to resolve the destination struct type for enum returns.Update Phase 8 plan: mark status + add discovered issues (#601/#603/#604