Skip to content

char is rejected in #[device] extern signatures #875

Closed

Description

@Ayush7614

Background

A #[device] extern that takes or returns Rust char fails codegen with:

Rust char is not supported in device extern signatures; use u32 in a C-compatible wrapper

char is a 4-byte Unicode scalar value, so it is ABI-compatible with u32. Rejecting it
forces an awkward C wrapper even though the layout already matches the NVPTX i32 ABI.

Location

crates/rustc-codegen-cuda/src/device_codegen.rs:241-243 (the TyKind::Char arm of
rustc_ty_to_device_extern_type).

Proposed fix

Map TyKind::Char to E::Integer(32) (or E::ZeroExtInteger(32) for by-value results,
matching the bool/u32 handling in the same function) and emit a u32/i32 device
symbol, exactly as the existing bool arm already does at lines 231-240. Document the
char -> u32 mapping in the device-extern ABI docs.

Acceptance criteria (so a PR passes)

  • A #[device] extern with a char parameter and char return value lowers and links.
  • New unit test in crates/rustc-codegen-cuda asserting the char -> i32 mapping.
  • Existing device-extern signature tests in that crate stay green.

Activity

  1. changed the title [-] is rejected in extern signatures[/-] [+]`char` is rejected in `#[device]` extern signatures[/+] on Aug 13, 2026
  2. added
    bug
    codegen
    on Aug 14, 2026
  3. nihalpasham commented on Aug 14, 2026

    @nihalpasham
    Collaborator

    Verified: this is a real gap, and we'd welcome a PR for it. One correction to the proposed mapping below, so here is the full spec.

    The problem

    #[device]
    unsafe extern "C" {
        fn char_identity(c: char) -> char;   // implemented in a .cu file
    }
    error: [rustc_codegen_cuda] Device codegen failed: Invalid device-extern
           signature: `char_identity` parameter 0 (`char`): Rust `char` is not
           supported in device extern signatures; use `u32` in a C-compatible wrapper
    

    The rejection is a leftover, not a decision:

    initial release   char -> i32           permissive
    PR #101           char, bool REJECTED   strictness pass for typed NVVM IR
    PR #412           bool, i8..u16, f16    restored with the clang-compatible ABI
                      char                  forgotten here  <- this issue
    

    Kernels already handle char (the importer types it as unsigned i32); the extern-signature arm is the only place it is refused.

    The fix we expect

    Not the bool-style mapping, and not ZeroExtInteger(32). Extension attributes exist to widen sub-32-bit values into 32-bit parameter slots; char is already 32 bits, so it needs none, by value or in memory:

                          by value       behind a pointer
    bool                  zeroext i1     i8      (1 bit widened / 1 byte in memory)
    char (this issue)     plain i32      i32     (already fills the slot / 4 bytes in memory)
    

    So the compiler change is one line in rustc_ty_to_device_extern_type (crates/rustc-codegen-cuda/src/device_codegen.rs), routed through the same helper u32 uses:

    TyKind::Char => unsigned_integer(32),

    Expected output (we validated this end to end on an RTX 5090):

    .ll:   declare i32 @char_identity(i32)     call i32 @char_identity(i32 65)
    .ptx:  .extern .func (.param .b32 func_retval0) char_identity (.param .b32 ...)
    

    Acceptance criteria (updated)

    • The one-line mapping above; no zeroext, identical in parameter, result, and pointee positions.
    • Test: extend device_ffi_test (an unsigned int function in its .cu, a char round-trip in the kernel, exact expected value). There is no TyCtxt unit-test harness in rustc-codegen-cuda, so the original unit-test criterion is not implementable; the FFI example plus the smoketest ltoir gate is the test surface.
    • Document the contract: a value from C outside the Unicode scalar range (above 0x10FFFF, or a surrogate in 0xD800..=0xDFFF) is UB on the Rust side, the same caveat family as bool's 0/1 rule. Also note rustc's improper_ctypes warning still fires on char externs and callers may allow() it.
    • scripts/smoketest.sh device_ffi_test passes; existing tests stay green.
  4. added 3 commits that reference this issue on Aug 22, 2026
    bfaa65a
    2f278bc
    3f39a6f
  5. added a commit that references this issue on Aug 24, 2026
    b22efa9
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

    bugcodegen

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions