You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
char is rejected in #[device] extern signatures #875
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.
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]unsafeextern"C"{fnchar_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):
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.
Activity
[-] is rejected in extern signatures[/-][+]`char` is rejected in `#[device]` extern signatures[/+]nihalpasham commentedon Aug 14, 2026
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
The rejection is a leftover, not a decision:
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;charis already 32 bits, so it needs none, by value or 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 helperu32uses:Expected output (we validated this end to end on an RTX 5090):
Acceptance criteria (updated)
device_ffi_test(anunsigned intfunction in its.cu, acharround-trip in the kernel, exact expected value). There is noTyCtxtunit-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.improper_ctypeswarning still fires oncharexterns and callers mayallow()it.scripts/smoketest.sh device_ffi_testpasses; existing tests stay green.docs(book): the fuzzing chapter says the adapter rejects five types i…
fix(codegen): support char in #[device] extern signatures
fix(codegen): support char in #[device] extern signatures
fix(codegen): support char in #[device] extern signatures
codegen: accept `char` in device-extern signatures (NVIDIA#1179)