Skip to content

Sema: a struct field of type extern "C" fn(&T) -> R is refused as "ephemeral references cannot be stored in structs" #1833

Closed

Description

@ehartford

Spec: §5 (ephemerality), §16.6 (function pointers). A struct field whose type is a C function pointer taking a reference is refused as if the field held a reference:

type Holder { f: extern "C" fn(&i32) -> i32 }
fn read(x: &i32) -> i32: *x
fn main:
    let h = Holder { f: read }
    let v = 5
    print(f"{(h.f)(&v)}")
error: ephemeral references cannot be stored in structs
 --> fnfield.w:1:18
1 | type Holder { f: extern "C" fn(&i32) -> i32 }

Expected: accepted, prints 5. An extern "C" fn(&T) -> R is a code pointer (§16.6: pointer-sized, Copy, storable in repr(C) structs); it takes a view per call and holds none, so it is not ephemeral. The struct-field check looks into the callable's parameter types.

Found by the D76 wrapper (#1779): the box a registration hands C carries the program's extern "C" fn(Context, &[Value], &U) callbacks; the rendering stores them as *mut c_void and transmutes back at the call instead (FacadeRender.w facade_render_wrapper) — remove that when this lands.

Reproduced with the installed compiler (v0.15.2.1-g99939352f) and a stage1 of 355c20b.

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