Skip to content

Five defects the c-interop example exposed, rebased onto main; CStr.to_owned; the old example removed until the SQLite facade (D51 §66) - #1234

Merged
ehartford merged 3 commits into
mainfrom
lend-str-in-raw-calls
Sep 20, 2026
Merged

ehartford merged 3 commits into
mainfrom
lend-str-in-raw-calls

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

#1232 was stacked on #1231's branch and squash-merged into that branch 18 seconds after #1231 merged, so its five fixes never reached main. This PR is that squash cherry-picked onto current main, plus two follow-ups.

The five fixes (4c06152, each with its test; see #1232 for the detail):

  1. A str is lent to a const char * in a raw call too (§16.3c, D47) — SemaDiag.try_ci_coercion no longer gates lending on ci_raw_syms.
  2. CStr.from_ptr (raw-surface primitive) and an owned-copy conversion.
  3. A Result[Unit, E] body that falls through after an early return Err is Ok(()) (§4.9, the spec's own validate example). Closure site waits on Calling a fn-typed parameter that returns Result[Unit, E] fails LLVM verification #1230.
  4. @[c_export] defines the prototype a c_imported header declares (the body goes into the existing declaration; LLVM no longer renames it name.1).
  5. A parameter or local shadows another module's global of its name (check_ident looked the name up again and found the other module's private global).

Follow-ups:

Battery, seed-driven at d4e5a88 on current main: build, :fixpoint, :test, :move-audit, :drop-audit, :test-green, :last-green all green.

ehartford and others added 3 commits September 20, 2026 16:09
… 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]>
…t 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.
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
ehartford merged commit be767c0 into main Sep 20, 2026
5 checks passed
@ehartford
ehartford deleted the lend-str-in-raw-calls branch September 20, 2026 20:52
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