Skip to content

cuda-macros: reject a variadic #[device] extern at expansion - #1051

Merged
nihalpasham merged 1 commit into
NVIDIA:mainfrom
NgoQuocViet2001:device-extern-variadic-diagnostic
Aug 20, 2026
Merged

nihalpasham merged 1 commit into
NVIDIA:mainfrom
NgoQuocViet2001:device-extern-variadic-diagnostic

Conversation

@NgoQuocViet2001

Copy link
Copy Markdown
Contributor

Fixes #876, following @nihalpasham's spec in this comment — including both corrections.

The change

generate_device_extern_block now refuses ... where it is written, six lines modelled on the spanned ABI check just above it:

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

Both holes close together:

before after
declared and called late error, no file, no line, no span spanned error at expansion
declared, never called compiles clean spanned error at expansion

The second was the quieter one: the collector only registers externs reached from kernel MIR, so a variadic device extern nobody called went through untouched.

What the user sees now:

error: #[device] externs cannot be variadic
 --> tests/compile_fail/device_extern_variadic.rs:8:32
  |
8 |     fn variadic_helper(x: f32, ...) -> f32;
  |                                ^^^

What deliberately did not change

  • The c_variadic rejection in device_codegen.rs stays a real error, not an assertion. The collector detects device externs by their reserved name prefix, so a hand-written extern block never passes through this macro and the backend check is the only gate on that path.
  • Nothing was lifted for char / f128 / arrays-by-value. Those need resolved types — type MyF = f128; or a generic hides the type from a syntactic macro. ... is the one purely syntactic check, which is why it belongs here.

Test plan

  • Added the trybuild fixture pair tests/compile_fail/device_extern_variadic.{rs,stderr}, modelled on device_extern_wrong_abi, and registered it in tests/macro_guard.rs beside it.
  • Ran: trybuild against the fixture on the pinned nightly-2026-04-03 — passes, and the checked-in .stderr is the exact output trybuild generated rather than a hand-written guess.
  • Ran: cargo fmt --all -- --check — clean.
  • Not run: cargo test -p cuda-macros in full. Its dev-dependency chain reaches cuda-bindings, which needs cuda.h, and this machine has no CUDA toolkit. The fixture was therefore exercised through a scratch harness depending only on cuda-macros (which is all the fixture uses — use cuda_macros::device;), so the fixture path and hence the --> line in the .stderr match what macro_guard.rs will produce.

A variadic device extern was only refused in device codegen, and only when
a kernel actually called it. Two holes:

  declared AND called    late error, no span, no file, no line
  declared, never called compiles clean -- the collector only registers
                         externs reached from kernel MIR

`...` is purely syntactic, so `generate_device_extern_block` can refuse it
where the user wrote it, next to the existing spanned ABI check:

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

The codegen rejection stays as-is. The collector recognises device externs
by their reserved name prefix, so a hand-written extern block never passes
through this macro and the backend check is the only gate on that path.

Fixes NVIDIA#876
@nihalpasham nihalpasham added codegen Device code-generation pipeline (Rust MIR to IR to PTX) QoL-improvement Quality-of-life improvement; not a correctness fix labels Aug 20, 2026
@nihalpasham

Copy link
Copy Markdown
Collaborator
  • Verified on the pinned nightly: the trybuild fixture fails against main and passes with the checked-in .stderr byte-for-byte; cargo test -p cuda-macros is green across all suites (99 unit tests, macro_guard, doc-tests), fmt and clippy clean.
  • The rejection is the Variadic #[device] externs surface as a codegen-time error instead of a #[device]-time diagnostic #876 snippet at the specified site, modelled on the ABI check above it: pure syn inspection, no other effects. Nothing in the repo declares a variadic device extern, so no regression surface.
  • Merging as-is once CI confirms.

@nihalpasham
nihalpasham merged commit be940ee into NVIDIA:main Aug 20, 2026
49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codegen Device code-generation pipeline (Rust MIR to IR to PTX) QoL-improvement Quality-of-life improvement; not a correctness fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants