Skip to content

stabilize c_variadic_naked_functions#159746

Draft
folkertdev wants to merge 1 commit into
rust-lang:mainfrom
folkertdev:stabilize-c-variadic-naked-functions
Draft

stabilize c_variadic_naked_functions#159746
folkertdev wants to merge 1 commit into
rust-lang:mainfrom
folkertdev:stabilize-c-variadic-naked-functions

Conversation

@folkertdev

@folkertdev folkertdev commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

tracking issue: #148767
reference PR: forthcoming

Stabilization report

Summary

Stabilize the ability to use #[unsafe(naked)] functions to define c-variadic functions. These c-variadic naked functions accept the same set of ABIs as c-variadic foreign functions, this set is larger than what we currently accept for c-variadic definitions.

#[unsafe(naked)]
unsafe extern "aapcs" fn variadic_aapcs(_: f64, _: ...) -> f64 {
    core::arch::naked_asm!(
        r#"
        sub     sp, sp, #12
        stmib   sp, {{r2, r3}}
        vmov    d0, r0, r1
        add     r0, sp, #4
        vldr    d1, [sp, #4]
        add     r0, r0, #15
        bic     r0, r0, #7
        vadd.f64        d0, d0, d1
        add     r1, r0, #8
        str     r1, [sp]
        vldr    d1, [r0]
        vadd.f64        d0, d0, d1
        vmov    r0, r1, d0
        add     sp, sp, #12
        bx      lr
    "#,
    )
}

Accepted ABIs

The set of accepted ABIs is the same as for c-variadic foreign functions, defined as rule items.extern.variadic.conventions:

  • "aapcs"
  • "C"
  • "cdecl"
  • "efiapi"
  • "system"
  • "sysv64"
  • "win64"

And their corresponding -unwind variants. Given that naked functions desugar to a block of module assembly and a foreign definition, it makes sense to support the same set as source-level foreign definitions.

For c-variadic definitions we only accept "C" and "C-unwind".

Multiple c-variadic ABIs in the same program

LLVM supports c-variadic calls of different ABIs in the same program. We test both an arm and x86 configuration

Note that GCC, Clang and LLVM do not support c-variadic definitions of multiple ABIs: the va_start, va_arg etc. macros are always expanded using the default C calling convention. Clang and GCC reject a variable argument list on definitions that use a non-default calling convention.

History

The stabilization report of feature(c_variadic) mentions this feature:

Unresolved questions

None.

@folkertdev folkertdev added the F-c_variadic `#![feature(c_variadic)]` label Jul 22, 2026
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 22, 2026
Comment on lines +293 to +294
/// of such functions via `extern` blocks and definition via naked functions; there's a
/// separate check during AST construction guarding *definitions* of variadic functions.

@folkertdev folkertdev Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the reference we now have to differentiate between naked and non-naked (clothed?) definitions. I guess we'll figure out some terminology in the reference PR.

View changes since the review

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

Labels

F-c_variadic `#![feature(c_variadic)]` S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants