Skip to content

Invalid MIR: closure literal as a facade callback method's userdata is passed by value (missing borrow) #1904

Closed

Description

@ehartford

Spec: §16.2b.9 (callback userdata), §12.4 (closures). Post-Sema invalid MIR is a compiler bug (D65).

Repro (place in test/compile_errors/ so the header path resolves):

use c_import("../behavior/c_facade_callbacks.h")

c facade dbc:
    resource Database wraps *mut db
        from db_new
        drop db_close
    fn db_exec
        callback param 1 userdata param 2

fn invoke(f: &fn(c_int) -> c_int, n: c_int) -> c_int: f(n)

fn main:
    let l = log_new()
    let d = Database.new(l, 1).unwrap()
    d.exec(invoke, n => n + 1, 1)
    log_free(l)

Expected: the check passes (a captureless closure is a valid &U userdata), as it does for a free facade callback bridge (each(invoke, value => value)) and for let f = n => n + 1; d.exec(invoke, f, 1).

Actual (installed v0.15.3.0-g173d8aa4d, and impl-d75 stage1):

error: invalid MIR before codegen: call argument 2 is a value where the callee parameter is a reference to it (a missing borrow)
 --> ...:20:32
20 |     d.exec(invoke, n => n + 1, 1)

So a closure literal given directly as a receiver callback method's &U userdata is not auto-borrowed in MIR. Workaround: bind the closure first. Found while fixing the call-once regression on impl-d75 (stack #1898).

Activity

  1. ehartford commented on Oct 3, 2026

    @ehartford
    ContributorAuthor

    Fixed on main by 792351b (stack #2007, battery green on 0f13a62). A closure literal to a &U parameter of a generic method is auto-borrowed like lower_call_arg does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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