Repository navigation
Five defects the c-interop example exposed: str lending in raw calls, CStr.from_ptr, §4.9 fall-through, @[c_export] over a declared prototype, parameter shadowing - #1232
Merged
Conversation
added 5 commits
September 20, 2026 11:54
The blessed sentence has no condition on the function's other parameters. SemaDiag.try_ci_coercion gated lending on ci_raw_syms, so any function with a raw-pointer parameter (sqlite3_open, sqlite3_prepare_v2, strtol) refused a str for its text input and sent the caller to c"...".ptr and CString. I added that gate in #1222; it was never part of the ruling.
The spec spells CStr.from_ptr(p).to_str(); CStr had only len() and ptr(), and the stdlib read C strings through with_str_from_cstr, which a program may not name. sqlite3_errmsg and sqlite3_column_text left every user writing an unsafe byte loop. from_ptr is unsafe (the caller vouches for the pointer); to_str is an owned copy.
…s Ok(()) (§4.9) The spec's own validate example was 'missing return': SemaCheck flagged any Unit-typed body that holds an explicit value return and can fall through, with no exemption for Result[Unit, E], where falling through is the Ok(()) §4.9 promises. The closure site waits on #1230.
A C library that calls back into With declares the callback in its header. The c_imported prototype and the With definition became two LLVM functions of one name: the body was renamed name.1 and the prototype the C code calls stayed undefined at link. The body now goes into the existing declaration, as it already does for runtime names, so every call site that holds the prototype holds the definition.
A private top-level `let ptr` broke every imported function that has a `ptr` parameter: "symbol 'ptr' is not visible from this module", for a free function, a type-level function and a method alike. check_ident (SemaCheck.w) found the parameter through scope_lookup, then ran the visibility checks through binding_decl_node / global_value_decl_kind, which look the NAME up again and found the other module's global. Found by an lldb breakpoint on emit_private_symbol_error. A binding made below the module-level scope is a local, and visibility does not apply to it. Exposed by CStr.from_ptr(ptr: ...), which put a `ptr` parameter in the prelude and collided with behav_annotated_top_level_runtime_init.w's global.
ehartford
added a commit
that referenced
this pull request
Sep 20, 2026
… CStr.from_ptr, §4.9 fall-through, @[c_export] over a declared prototype, parameter shadowing (#1232) * A str is lent to a const char * in a raw call too (§16.3c, D47) The blessed sentence has no condition on the function's other parameters. SemaDiag.try_ci_coercion gated lending on ci_raw_syms, so any function with a raw-pointer parameter (sqlite3_open, sqlite3_prepare_v2, strtol) refused a str for its text input and sent the caller to c"...".ptr and CString. I added that gate in #1222; it was never part of the ruling. * CStr.from_ptr and CStr.to_str: read the C string C handed back (§16.1) The spec spells CStr.from_ptr(p).to_str(); CStr had only len() and ptr(), and the stdlib read C strings through with_str_from_cstr, which a program may not name. sqlite3_errmsg and sqlite3_column_text left every user writing an unsafe byte loop. from_ptr is unsafe (the caller vouches for the pointer); to_str is an owned copy. * A Result[Unit, E] body that falls through after an early return Err is Ok(()) (§4.9) The spec's own validate example was 'missing return': SemaCheck flagged any Unit-typed body that holds an explicit value return and can fall through, with no exemption for Result[Unit, E], where falling through is the Ok(()) §4.9 promises. The closure site waits on #1230. * @[c_export] defines the prototype a c_imported header declares A C library that calls back into With declares the callback in its header. The c_imported prototype and the With definition became two LLVM functions of one name: the body was renamed name.1 and the prototype the C code calls stayed undefined at link. The body now goes into the existing declaration, as it already does for runtime names, so every call site that holds the prototype holds the definition. * A parameter or local shadows another module's global of its name A private top-level `let ptr` broke every imported function that has a `ptr` parameter: "symbol 'ptr' is not visible from this module", for a free function, a type-level function and a method alike. check_ident (SemaCheck.w) found the parameter through scope_lookup, then ran the visibility checks through binding_decl_node / global_value_decl_kind, which look the NAME up again and found the other module's global. Found by an lldb breakpoint on emit_private_symbol_error. A binding made below the module-level scope is a local, and visibility does not apply to it. Exposed by CStr.from_ptr(ptr: ...), which put a `ptr` parameter in the prelude and collided with behav_annotated_top_level_runtime_init.w's global. --------- Co-authored-by: Eric Hartford <[email protected]>
ehartford
pushed a commit
that referenced
this pull request
Sep 20, 2026
The old example did not compile, its test tested strlen, its README described a program that no longer existed, and a compiled binary was checked in; the three source files went with #1232. The README and with.toml that remained described nothing. The example returns rewritten against the facade surface, after the SQLite facade compiles, as the ruling orders.
ehartford
added a commit
that referenced
this pull request
Sep 20, 2026
…o_owned; the old example removed until the SQLite facade (D51 §66) (#1234) * Five defects the c-interop example exposed: str lending in raw calls, CStr.from_ptr, §4.9 fall-through, @[c_export] over a declared prototype, parameter shadowing (#1232) * A str is lent to a const char * in a raw call too (§16.3c, D47) The blessed sentence has no condition on the function's other parameters. SemaDiag.try_ci_coercion gated lending on ci_raw_syms, so any function with a raw-pointer parameter (sqlite3_open, sqlite3_prepare_v2, strtol) refused a str for its text input and sent the caller to c"...".ptr and CString. I added that gate in #1222; it was never part of the ruling. * CStr.from_ptr and CStr.to_str: read the C string C handed back (§16.1) The spec spells CStr.from_ptr(p).to_str(); CStr had only len() and ptr(), and the stdlib read C strings through with_str_from_cstr, which a program may not name. sqlite3_errmsg and sqlite3_column_text left every user writing an unsafe byte loop. from_ptr is unsafe (the caller vouches for the pointer); to_str is an owned copy. * A Result[Unit, E] body that falls through after an early return Err is Ok(()) (§4.9) The spec's own validate example was 'missing return': SemaCheck flagged any Unit-typed body that holds an explicit value return and can fall through, with no exemption for Result[Unit, E], where falling through is the Ok(()) §4.9 promises. The closure site waits on #1230. * @[c_export] defines the prototype a c_imported header declares A C library that calls back into With declares the callback in its header. The c_imported prototype and the With definition became two LLVM functions of one name: the body was renamed name.1 and the prototype the C code calls stayed undefined at link. The body now goes into the existing declaration, as it already does for runtime names, so every call site that holds the prototype holds the definition. * A parameter or local shadows another module's global of its name A private top-level `let ptr` broke every imported function that has a `ptr` parameter: "symbol 'ptr' is not visible from this module", for a free function, a type-level function and a method alike. check_ident (SemaCheck.w) found the parameter through scope_lookup, then ran the visibility checks through binding_decl_node / global_value_decl_kind, which look the NAME up again and found the other module's global. Found by an lldb breakpoint on emit_private_symbol_error. A binding made below the module-level scope is a local, and visibility does not apply to it. Exposed by CStr.from_ptr(ptr: ...), which put a `ptr` parameter in the prelude and collided with behav_annotated_top_level_runtime_init.w's global. --------- Co-authored-by: Eric Hartford <[email protected]> * CStr.to_owned, not to_str: the allocating conversion is named for what it does (D51 §41) The ruling reserves to_str() for the validating conversion and names the owned copy to_owned(). Renamed before the name lands. * examples/c-interop is removed until the SQLite facade exists (D51 §66) The old example did not compile, its test tested strlen, its README described a program that no longer existed, and a compiled binary was checked in; the three source files went with #1232. The README and with.toml that remained described nothing. The example returns rewritten against the facade surface, after the SQLite facade compiles, as the ruling orders. --------- Co-authored-by: Eric Hartford <[email protected]>
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.
Stacked on #1231. Each fix is its own commit with its own test. Found by rewriting
examples/c-interop(the example itself is parked onc-interop-example: it still needsunsafe, which is the compiler's defect to fix first, §16.2a).stris lent to aconst char *in a raw call too (§16.3c, D47).SemaDiag.try_ci_coerciongated lending onci_raw_syms, so any function with a raw-pointer parameter (sqlite3_open,sqlite3_prepare_v2,strtol) refused astr. I added that gate in A user program is a contract: the spiral as published, nounsafein UATs; c_import lends strings (D47); the spec stops cataloguing lib/std (D48) #1222; the blessed sentence has no such condition. Test:behav_c_import_lends_str_in_a_raw_call.w.CStr.from_ptrandCStr.to_str(§16.1). The spec spellsCStr.from_ptr(p).to_str();CStrhad onlylen()andptr(), and the stdlib read C strings through awith_*seam a program may not name. Test:behav_cstr_from_ptr_to_str.w.Result[Unit, E]body that falls through after an earlyreturn ErrisOk(())(§4.9). The spec's ownvalidateexample was "missing return" (SemaCheck.w~2385). The closure site waits on Calling a fn-typed parameter that returns Result[Unit, E] fails LLVM verification #1230. Test:behav_result_unit_falls_through_after_early_err.w.@[c_export]defines the prototype a c_imported header declares. A C library that calls back into With declares the callback in its header; prototype and definition became two LLVM functions of one name, the body was renamedname.1, and the link failed. The body now goes into the existing declaration (Codegen.w~4439), as it already does for runtime names. Test:behav_c_export_defines_a_c_imported_prototype.w.let ptrbroke every imported function with aptrparameter ("symbol 'ptr' is not visible from this module"), for free functions, type-level functions and methods.check_identfound the parameter, then looked the name up again and found the other module's global. On main today; exposed when fix 2 put aptrparameter in the prelude and the first battery went red onbehav_annotated_top_level_runtime_init.w. Test:behav_param_shadows_another_modules_global.w.Filed, not fixed here: #1229 (array literal to a
[]Tparameter segfaults; root-caused), #1230 (calling afn -> Result[Unit, E]value fails LLVM verification).Battery, seed-driven, on top of #1231: build, :fixpoint, :test, :move-audit, :drop-audit, :test-green, :last-green all green.