Skip to content

Sema: a bare fn does not coerce to an extern "C" fn parameter of a generic fn or method (§12.4); the SQLite facade's retained callback needs a typed local #1609

Closed

Description

@ehartford

Found by the SQLite facade (D51 stage 12, ruling §66).

Spec §12.4: a bare fn coerces to an extern "C" fn pointer. It does through a non-generic parameter, and not through a generic fn's or generic method's parameter — the generic call path binds the type parameters from every argument at once and never gives the argument the parameter's expected type (SemaCheck.w check_call, sig_idx < 0 branch; the same for check_method_call_parts on a generic method). Stage 9 special-cased the paired callback of a facade callback method (facade_callback_expected_type); an unpaired callback parameter — sqlite3_create_function_v2's xFunc, retained by the connection — has no such path.

Repro (no facade):

fn my(x: i32) -> i32: x + 1
fn plain(f: extern "C" fn(i32) -> i32) -> i32: f(1)
fn g2[U](u: U, f: extern "C" fn(i32) -> i32) -> i32: f(2)
fn main:
    print(f"{plain(my)}")        // ok
    print(f"{g2(1, my)}")        // error: wrong argument type in call to 'g2': argument expression 'my' has type fn(i32) -> i32; argument 2 expects extern "C" fn(i32) -> i32

The same with a generic method (impl A: fn reg[U](u: U, f: extern "C" fn(i32) -> i32); a.reg(1, my)).

Expected: the coercion §12.4 states, through a generic parameter whose type names no type parameter (like the null-literal case stage 12 handles in null_arg_expected_type: resolve the template's parameter type when it mentions no type param, and check the argument against it).

Workaround in test/behavior/behav_sqlite_facade_end_to_end.w and the debug-alloc fixture: a typed local, let x_func: extern "C" fn(*mut sqlite3_context, c_int, *mut *mut sqlite3_value) -> Unit = shout — ceremony the compiler already knows; remove it with the fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions