Skip to content

Tracking issue for const extern fn and const unsafe extern fn #64926

@Aaron1011

Description

@Aaron1011
Member

This is a tracking issue for implementing and stabilizing defining a const extern fn and const unsafe extern fn

Stabilizaton Report

Summary

This allows writing const unsafe extern "calling-convention" fn and const extern "calling-convention" fn:

Today Rust and C calling conventions are stable (as of 1.62, see #95346). All other calling conventions are still unstable.

const extern fn foo1(val: u8) -> u8 { val + 1 }
const extern "C" fn foo2(val: u8) -> u8 { val + 1}
const unsafe extern fn bar1(val: bool) -> bool { !val }
const unsafe extern "C" fn bar2(val: bool) -> bool { !val }

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

Test cases

  1. Test for defining and calling in Rust code: https://github.com/rust-lang/rust/blob/master/src/test/ui/consts/const-extern-fn/const-extern-fn.rs
  2. Test that calling with the wrong calling convention (from a const context) leads to an error: https://github.com/rust-lang/rust/blob/master/src/test/ui/consts/miri_unleashed/abi-mismatch.rs

Activity

added
A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
B-unstableBlocker: Implemented in the nightly compiler and unstable.
C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
T-langRelevant to the language team
on Sep 30, 2019
RalfJung

RalfJung commented on Oct 1, 2019

@RalfJung
Member

Could you explain a bit more what this means and what the point is? What does the bare extern keyword even do? extern "Rust" fn and fn are the same thing, after all. (extern fn is poorly named; AFAIK it really is about specifying the ABI and has little to do with extern visibility.)

Aaron1011

Aaron1011 commented on Oct 1, 2019

@Aaron1011
MemberAuthor

@RalfJung: The point is to be able to define an const extern fn (i.e. const extern "C" fn), which has a C-compatible ABI and can be invoked from const contexts.

added a commit that references this issue on Oct 2, 2019
5d0db38
Centril

Centril commented on Oct 7, 2019

@Centril
Contributor

Implemented in #64906 (2019-10-07).

44 remaining items

added
S-tracking-ready-to-stabilizeStatus: This is ready to stabilize; it may need a stabilization report and a PR
and removed
S-waiting-on-teamStatus: Awaiting decision from the relevant subteam (see the T-<team> label).
on Jun 22, 2022
joshtriplett

joshtriplett commented on Jun 22, 2022

@joshtriplett
Member

It looks like "C" and "Rust" calling conventions were stabilized for the upcoming 1.62 release.

Other calling conventions could be stabilized as well; is there a blocker for doing so?

RalfJung

RalfJung commented on Jun 22, 2022

@RalfJung
Member

I am not aware of any calling convention being special here. (Other than variadics, but those are generally unstable, not just for const fn.)

added a commit that references this issue on Sep 14, 2024
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

    A-FFIArea: Foreign function interface (FFI)A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)B-unstableBlocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCF-const_extern_fn`#![feature(const_extern_fn)]`S-tracking-ready-to-stabilizeStatus: This is ready to stabilize; it may need a stabilization report and a PRT-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @nikomatsakis@joshtriplett@RalfJung@oli-obk@Centril

      Issue actions

        Tracking issue for `const extern fn` and `const unsafe extern fn` · Issue #64926 · rust-lang/rust