What does `const [unsafe] extern fn` feature enable?

Hi. There is currently an open stabilization PR stabilize const_extern_fn. Its tracking issue states that this feature:

This can be used to const-ify an extern fn (or equivalently, to make a const fn callable from external code).

However I don't understand what is the real practical benefit. If I want to make a const fn callable from external code, then I could just simply do:

mod foo {
    pub const fn foo() {}
}

#[no_mangle]
extern "C" fn foo() {
    foo::foo()
}

The only worry here would be "will foo::foo be inlined?". But this should be pretty obvious to the compiler. Is saving one wrapper function the only benefit of this feature?

And on the other hand, what does "const-ifing an extern fn" even mean? My understanding is that extern functions are provided by the linker, and are totally opaque to the compiler. So how such function could be called in the const context?

If the ABIs differ, some sort of transformation could be necessary. Not only for passing arguments, but also for unwinding.

extern "ABI" fn is used to define (not link) a function with a particular calling ABI. It's the dual of extern blocks. The compiler knows the definition and can call it at compile time.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.