Skip to content

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
ehartford merged 5 commits into
int-to-ptr-sign-extendsfrom
lend-str-in-raw-calls
Sep 20, 2026
Merged

ehartford merged 5 commits into
int-to-ptr-sign-extendsfrom
lend-str-in-raw-calls

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Stacked on #1231. Each fix is its own commit with its own test. Found by rewriting examples/c-interop (the example itself is parked on c-interop-example: it still needs unsafe, which is the compiler's defect to fix first, §16.2a).

  1. A str is lent to a const char * in a raw call too (§16.3c, D47). 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. I added that gate in A user program is a contract: the spiral as published, no unsafe in 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.
  2. CStr.from_ptr and CStr.to_str (§16.1). The spec spells CStr.from_ptr(p).to_str(); CStr had only len() and ptr(), and the stdlib read C strings through a with_* seam a program may not name. Test: behav_cstr_from_ptr_to_str.w.
  3. 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.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.
  4. @[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 renamed name.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.
  5. A parameter or local shadows another module's global of its name. A private top-level let ptr broke every imported function with a ptr parameter ("symbol 'ptr' is not visible from this module"), for free functions, type-level functions and methods. check_ident found the parameter, then looked the name up again and found the other module's global. On main today; exposed when fix 2 put a ptr parameter in the prelude and the first battery went red on behav_annotated_top_level_runtime_init.w. Test: behav_param_shadows_another_modules_global.w.

Filed, not fixed here: #1229 (array literal to a []T parameter segfaults; root-caused), #1230 (calling a fn -> 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.

Eric Hartford 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
ehartford merged commit 6e683c8 into int-to-ptr-sign-extends Sep 20, 2026
5 checks passed
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant