Skip to content

Variadic #[device] externs surface as a codegen-time error instead of a #[device]-time diagnostic #876

Closed

Description

@Ayush7614

Background

A extern \"C\" fn marked #[device] that is variadic is only caught deep in device
codegen, where the error has no connection to the offending #[device] attribute or the
function signature. Users see a generic "variadic device externs are not supported" string
rather than a pointed diagnostic at the definition.

Location

  • Rejection: crates/rustc-codegen-cuda/src/device_codegen.rs:546-551
    (if fn_sig.c_variadic { ... }).
  • Attribute expansion / validation: crates/cuda-macros/src/lib.rs (the #[device] proc
    macro) and crates/rustc-codegen-cuda/src/collector.rs.

Proposed fix

Move the c_variadic check into the #[device] macro / collector validation pass so it
is reported with a span pointing at the function signature, e.g.
"#[device] externs cannot be variadic". Keep the codegen guard as a defensive
assertion only. This matches how other unsupported device-extern shapes (e.g. char,
f128, arrays-by-value) could also be lifted to attribute time.

Acceptance criteria (so a PR passes)

  • A variadic #[device] extern produces a compile_error!-style diagnostic with a span
    on the function, not a runtime codegen panic/string.
  • A compile_fail trybuild fixture is added asserting the new message.
  • cargo test -p cuda-macros (and the existing device-extern tests) stay green.

Activity

  1. changed the title [-]Variadic externs surface as a codegen-time error instead of a -time diagnostic[/-] [+]Variadic `#[device]` externs surface as a codegen-time error instead of a `#[device]`-time diagnostic[/+] on Aug 13, 2026
  2. added
    QoL-improvement
    codegen
    on Aug 14, 2026
  3. nihalpasham commented on Aug 14, 2026

    @nihalpasham
    Collaborator

    Verified: real gap, and we'd welcome a PR. Two corrections to the proposal below, so here is the full spec.

    The problem

    #[device]
    unsafe extern "C" {
        fn variadic_helper(x: f32, ...) -> f32;
    }

    What you get today, and only if the function is actually called from a kernel:

    error: [rustc_codegen_cuda] Device codegen failed: Invalid device-extern
           signature: `variadic_helper` is variadic; variadic device externs
           are not supported
            ^ no file, no line, no span; fires at final-binary codegen
    

    (One nit on the issue text: this is a clean rustc fatal with exit 101, not a codegen panic, and nothing is swallowed. The gap is purely the missing span and the late stage.)

    And the hole nobody noticed: a variadic #[device] extern that is declared but never called compiles silently, because the collector only registers externs reached from kernel MIR.

    The fix we expect

    In the macro, generate_device_extern_block (crates/cuda-macros/src/lib.rs), about six lines modeled on the spanned ABI check a few lines above it. syn's Signature.variadic exposes the ... tokens directly:

    if let Some(variadic) = &foreign_fn.sig.variadic {
        return syn::Error::new_spanned(variadic, "#[device] externs cannot be variadic")
            .to_compile_error()
            .into();
    }

    What the user sees after:

    error: #[device] externs cannot be variadic
      --> src/main.rs:12:37
       |
    12 |     fn variadic_helper(x: f32, ...) -> f32;
       |                                ^^^
    

    Both holes close at once:

                                today          after
    declared AND called         late, no span  spanned error at macro expansion
    declared, never called      compiles!      spanned error at macro expansion
    

    Two corrections to the proposal

    • The codegen guard at device_codegen.rs stays a real error, not a defensive assertion: the collector detects device externs by the reserved name prefix, so a hand-written extern block bypasses the macro entirely, and the backend check is the only gate on that path.
    • Drop the aside about lifting char/f128/array checks to macro time. Those need resolved types; type MyF = f128; or a generic hides the type from a syntactic macro. Variadic ... is the one purely syntactic check, which is why it belongs here.

    Acceptance criteria (updated)

    • The spanned macro error above.
    • A trybuild fixture pair modeled on the existing device_extern_wrong_abi.{rs,stderr}, registered in tests/macro_guard.rs (trybuild is already a dev-dependency; zero new infrastructure).
    • The existing codegen rejection untouched; cargo test -p cuda-macros green.
  4. added a commit that references this issue on Aug 20, 2026
    be940ee
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

    QoL-improvementcodegen

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions