Skip to content

D102: non-null extern fn; nullable C function pointers as pointer-sized Option (#2215, #2216) - #2218

Merged
ehartford merged 6 commits into
mainfrom
fn-ptr-nullable
Oct 6, 2026
Merged

ehartford merged 6 commits into
mainfrom
fn-ptr-nullable

Conversation

@ehartford

@ehartford ehartford commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Implements D102 (Eric, 2026-10-06; #2215, #2216). The spec words landed in the first commit (§16.6 non-null rule and the c_import direction mapping, with-abi.md §3 layout guarantee, §16.2b.8 nullable param N, decision log D102); implicit Some is deferred to #2217.

  • ABI guarantee pinned by behav_d102_option_fn_pointer_abi: Option[extern "C" fn], Option[unsafe extern "C" fn] and Option[&T] are pointer-sized, None is null, Some(p) is p.
  • c_import maps by use site: a typedef is the non-null type; a record field, array element, pointee (fn_t *out), return and global are Option of it; a parameter stays non-null.
  • null is refused for extern "C" fn with the §16.6 message (safety: a safe extern "C" fn accepts null and safe code calls it (SIGTRAP) #2216: a null in the safe type was callable from safe code and trapped with no diagnostic).
  • Facade: nullable param N widens any function-pointer parameter to Option and bridges it by transmute under the ABI guarantee; the paired-callback bridge no longer writes null into a non-null local.
  • Migrator: a C body's function-pointer locals and its copies of function-pointer parameters are Option (C may hold NULL in them); a function flowing into an Option place wraps in Some, an Option flowing into a non-null place or a call unwraps — a NULL call panics where C's is undefined behavior. The four corpora are re-promoted.
  • Tests: behav_d102_nullable_fn_param, err_d102_null_extern_fn, err_d102_fn_pointer_return_is_option, and a c-migrator-basic-tests case for the field/call/store/local shapes.

Battery pending.

Eric Hartford added 3 commits October 6, 2026 12:47
…ointer-sized Option, placed by direction

Ruled by Eric 2026-10-06 (#2215, #2216). §16.6 states non-null and the
c_import direction rule (typedef non-null; field, return and callback
out-parameter Option; parameter non-null); with-abi.md §3 makes the
pointer-sized Option[extern fn]/Option[&T] layout a guarantee; §16.2b.8
names the widening clause `nullable param N`. Implicit Some deferred
(#2217). The implementation is non-compliant until the following commits.
…xtern fn is non-null; migrator and std follow

c_import: a function pointer is Option of the non-null type at a record
field, array element, pointee (fn_t *out), return and global; a typedef
and a c_import prototype's parameter stay non-null. A migrated definition
is C itself, so its parameters keep C's nullability (Option): corpus code
calls pcre2_set_callout(ctx, NULL, NULL). Sema refuses null for extern fn
with the §16.6 message (#2216). The facade accepts nullable param N on any
function-pointer parameter and bridges by transmute under the with-abi.md
§3 guarantee; the paired-callback bridge no longer writes null into a
non-null local; a free item with the clause is bridged and redirected.

Migrator: locals, parameter copies, fields, pointees, globals and returns
of function-pointer type are Option; a function into an Option place wraps
in Some, an Option into a non-null place or a call unwraps (a NULL call
panics where C's is undefined behavior); C's (fn_t)0 and NULL are null; a
module that names Option imports std.option. Codegen folds Some(fn) and
None in a global initializer as the nullable pointer (a Some in one field
had demoted the whole global to runtime init, which a bundle never runs;
#2219 asks for that to be loud). std.option imports std.traits and
std.traits imports std.builtins so a no-prelude module can use std.option.
std.regex and the sorted_vec/binary_heap/hash_index facades spell Some(f)
into migrated definitions (D103 will remove it). The four corpora are
re-promoted; corpus-drift-check and c-migrator-basic-tests pass.

Tests: behav_d102_option_fn_pointer_abi, behav_d102_nullable_fn_param,
err_d102_null_extern_fn, err_d102_fn_pointer_return_is_option, and a
c-migrator-basic-tests case for the field/call/store/local shapes.
Eric Hartford added 3 commits October 6, 2026 13:40
…wrapped and fixed callbacks as Option, migrated elements and returns

The c-interop example (a contract; this is a spec change, D102, so the
example follows it and says so): sqlite3_create_function_v2 takes NULL for
xStep and xFinal, so its facade states nullable param xStep / xFinal and
the program passes None, None. The facade renderer presents a wrapped
callback the clause names as Option of its shape, lets the clause name
any function-pointer parameter (the paired callback's nullability stays
the pair's), and crosses a fixed null and an absent wrapped callback as
None under the with-abi.md §3 guarantee instead of writing null into a
non-null local. The migrator types an indexed function-pointer element as
Option (run_test(tests[i]) no longer wraps a second Some); the four
corpora are re-promoted. unit-return-reviews.tsv records run_test's
annotation under its D102 signature (the tail is an if/else whose empty
arm renders 0). #2223 files the two-owners drift this exposed.
…xample's copy is byte-identical); the changed-signature annotations are reviewed

lib/facades/sqlite3.w is the source the example copies, so the same two
clauses land there. docs/unit-return-reviews.tsv records the six migrated
signatures whose text changed under D102 (list_sort, slist_sort,
tommy_chain_mergesort, run_test in both corpora copies): the annotation
is the migrator's and unchanged.
…sts and expectations follow the ruling

A migrated definition's function-pointer parameter is Option in every
unit of the corpus — the rule is "declared by a system header" (libc's
qsort stays non-null), not "defined in this unit", since a corpus test
program sees the library only through prototypes. A global of nullable
function-pointer type initialized by a function name, (fn_t)0 or 0 is
Some(name)/null, per element for an array; an array of function pointers
takes the raw-pointer unsafe rule on its element so the declared type and
a call through an element agree. Tests that spelled null into extern fn,
called through a C field, or passed a callback bare now spell Option,
Some and unwrap as §16.6 says (spec change, the tests follow); the
migrator-core expectations read the Option return and Some(callback).
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on 9902e27 (branch base 99aec86; main since then differs only by docs/mission.md, not a green input): gate 108s, uat 15s, battery-checks 779s. D102 implemented end to end: c_import direction mapping, non-null extern fn (#2216), facade nullable param N on any function-pointer parameter, migrator Option typing with Some/unwrap/null, Some(fn) folded in globals, std.option self-contained for no-prelude modules, four corpora re-promoted, sqlite example and toolchain facade updated under the ruling. Follow-ups: #2217 (D103 lands the Some removal), #2219, #2223.

@ehartford
ehartford marked this pull request as ready for review October 6, 2026 22:31
@ehartford
ehartford merged commit 73c7cf8 into main Oct 6, 2026
@ehartford
ehartford deleted the fn-ptr-nullable branch October 6, 2026 22:31
ehartford added a commit that referenced this pull request Oct 7, 2026
…Sema's contract (#2223, Law 3) (#2231)

Sema's contract gains `bridged`, set at collect_fn_clause by the one
predicate `Ast.facade_clause_bridges(kind)` (a buffer pairing, a fixed
argument, an `ok` contract, `nullable param N`); facade_contract_presented,
the verifier's "record the bridge" gate (which had its own fn-pointer-only
reading of `nullable`) and the renamed-bridge export list read it, and the
renderer's lend item calls the same predicate instead of its own clause
list. The three derivations drifted on #2218; now one list changes them
all.

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