From f935b44c51adc3f5fec680a1a9614d416b24ecd3 Mon Sep 17 00:00:00 2001 From: Zalathar Date: Sat, 10 Oct 2026 17:23:56 +1100 Subject: [PATCH] Avoid all remaining uses of `as_c_char_ptr` --- compiler/rustc_codegen_llvm/src/common.rs | 20 +-------- .../rustc_codegen_llvm/src/llvm/enzyme_ffi.rs | 10 +++-- compiler/rustc_codegen_llvm/src/llvm/ffi.rs | 23 +++++++---- compiler/rustc_codegen_llvm/src/llvm/mod.rs | 41 ++++++++----------- 4 files changed, 40 insertions(+), 54 deletions(-) diff --git a/compiler/rustc_codegen_llvm/src/common.rs b/compiler/rustc_codegen_llvm/src/common.rs index 651082cee5d43..6e8f005be9569 100644 --- a/compiler/rustc_codegen_llvm/src/common.rs +++ b/compiler/rustc_codegen_llvm/src/common.rs @@ -2,7 +2,7 @@ use std::borrow::Borrow; -use libc::{c_char, c_uint}; +use libc::c_uint; use rustc_abi::Primitive::Pointer; use rustc_abi::{self as abi, ExternAbi, HasDataLayout as _}; use rustc_ast::Mutability; @@ -511,21 +511,3 @@ pub(crate) fn get_dllimport<'tcx>( tcx.native_library(id) .and_then(|lib| lib.dll_imports.iter().find(|di| di.name.as_str() == name)) } - -/// Extension trait for explicit casts to `*const c_char`. -pub(crate) trait AsCCharPtr { - /// Equivalent to `self.as_ptr().cast()`, but only casts to `*const c_char`. - fn as_c_char_ptr(&self) -> *const c_char; -} - -impl AsCCharPtr for str { - fn as_c_char_ptr(&self) -> *const c_char { - self.as_ptr().cast() - } -} - -impl AsCCharPtr for [u8] { - fn as_c_char_ptr(&self) -> *const c_char { - self.as_ptr().cast() - } -} diff --git a/compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs index 2e76223c68c12..8f6cc30e1166d 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/enzyme_ffi.rs @@ -1,6 +1,6 @@ #![expect(dead_code)] -use libc::{c_char, c_uint}; +use libc::{c_char, c_uchar, c_uint}; use super::MetadataKindId; use super::ffi::{AttributeKind, BasicBlock, Context, Metadata, Module, Type, Value}; @@ -47,10 +47,14 @@ unsafe extern "C" { pub(crate) fn LLVMRustGetArrayNumElements(Ty: &Type) -> u64; pub(crate) fn LLVMRustHasFnAttribute( F: &Value, - Name: *const c_char, + Name: *const c_uchar, // See "PTR_LEN_STR". NameLen: libc::size_t, ) -> bool; - pub(crate) fn LLVMRustRemoveFnAttribute(F: &Value, Name: *const c_char, NameLen: libc::size_t); + pub(crate) fn LLVMRustRemoveFnAttribute( + F: &Value, + Name: *const c_uchar, // See "PTR_LEN_STR". + NameLen: libc::size_t, + ); pub(crate) fn LLVMGetFirstFunction(M: &Module) -> Option<&Value>; pub(crate) fn LLVMGetNextFunction(Fn: &Value) -> Option<&Value>; pub(crate) fn LLVMRustRemoveEnumAttributeAtIndex( diff --git a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs index 749ef8120da6f..30ecd09adf8fb 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/ffi.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/ffi.rs @@ -982,7 +982,11 @@ unsafe extern "C" { // Operations on all values pub(crate) fn LLVMTypeOf(Val: &Value) -> &Type; pub(crate) fn LLVMGetValueName2(Val: &Value, Length: *mut size_t) -> *const c_char; - pub(crate) fn LLVMSetValueName2(Val: &Value, Name: *const c_char, NameLen: size_t); + pub(crate) fn LLVMSetValueName2( + Val: &Value, + Name: *const c_uchar, // See "PTR_LEN_STR". + NameLen: size_t, + ); pub(crate) fn LLVMReplaceAllUsesWith<'a>(OldVal: &'a Value, NewVal: &'a Value); pub(crate) safe fn LLVMGetMetadata<'a>( Val: &'a Value, @@ -1117,9 +1121,9 @@ unsafe extern "C" { // Operations on attributes pub(crate) fn LLVMCreateStringAttribute( C: &Context, - Name: *const c_char, + Name: *const c_uchar, // See "PTR_LEN_STR". NameLen: c_uint, - Value: *const c_char, + Value: *const c_uchar, // See "PTR_LEN_STR". ValueLen: c_uint, ) -> &Attribute; @@ -1139,7 +1143,10 @@ unsafe extern "C" { ) -> &'a Value; // Operations about llvm intrinsics - pub(crate) fn LLVMLookupIntrinsicID(Name: *const c_char, NameLen: size_t) -> c_uint; + pub(crate) fn LLVMLookupIntrinsicID( + Name: *const c_uchar, // See "PTR_LEN_STR". + NameLen: size_t, + ) -> c_uint; pub(crate) fn LLVMIntrinsicIsOverloaded(ID: NonZero) -> Bool; pub(crate) fn LLVMGetIntrinsicDeclaration<'a>( Mod: &'a Module, @@ -1675,7 +1682,7 @@ unsafe extern "C" { pub(crate) fn LLVMSetComdat(V: &Value, C: &Comdat); pub(crate) fn LLVMCreateOperandBundle( - Tag: *const c_char, + Tag: *const c_uchar, // See "PTR_LEN_STR". TagLen: size_t, Args: *const &'_ Value, NumArgs: c_uint, @@ -2186,7 +2193,7 @@ unsafe extern "C" { pub(crate) fn LLVMRustAddModuleFlagU32( M: &Module, MergeBehavior: ModuleFlagMergeBehavior, - Name: *const c_char, + Name: *const c_uchar, // See "PTR_LEN_STR". NameLen: size_t, Value: u32, ); @@ -2194,9 +2201,9 @@ unsafe extern "C" { pub(crate) fn LLVMRustAddModuleFlagString( M: &Module, MergeBehavior: ModuleFlagMergeBehavior, - Name: *const c_char, + Name: *const c_uchar, // See "PTR_LEN_STR". NameLen: size_t, - Value: *const c_char, + Value: *const c_uchar, // See "PTR_LEN_STR". ValueLen: size_t, ); diff --git a/compiler/rustc_codegen_llvm/src/llvm/mod.rs b/compiler/rustc_codegen_llvm/src/llvm/mod.rs index 89e4d60656d34..e189dc758d8e7 100644 --- a/compiler/rustc_codegen_llvm/src/llvm/mod.rs +++ b/compiler/rustc_codegen_llvm/src/llvm/mod.rs @@ -14,7 +14,6 @@ pub(crate) use self::CodeGenOptSize::*; pub(crate) use self::conversions::*; pub(crate) use self::ffi::*; pub(crate) use self::metadata_kind::*; -use crate::common::AsCCharPtr; mod conversions; pub(crate) mod diagnostic; @@ -46,11 +45,11 @@ pub(crate) fn AddFunctionAttributes<'ll>( } pub(crate) fn HasStringAttribute<'ll>(llfn: &'ll Value, name: &str) -> bool { - unsafe { LLVMRustHasFnAttribute(llfn, name.as_c_char_ptr(), name.len()) } + unsafe { LLVMRustHasFnAttribute(llfn, name.as_ptr(), name.len()) } } pub(crate) fn RemoveStringAttrFromFn<'ll>(llfn: &'ll Value, name: &str) { - unsafe { LLVMRustRemoveFnAttribute(llfn, name.as_c_char_ptr(), name.len()) } + unsafe { LLVMRustRemoveFnAttribute(llfn, name.as_ptr(), name.len()) } } pub(crate) fn AddCallSiteAttributes<'ll>( @@ -71,9 +70,9 @@ pub(crate) fn CreateAttrStringValue<'ll>( unsafe { LLVMCreateStringAttribute( llcx, - attr.as_c_char_ptr(), + attr.as_ptr(), attr.len().try_into().unwrap(), - value.as_c_char_ptr(), + value.as_ptr(), value.len().try_into().unwrap(), ) } @@ -83,13 +82,15 @@ pub(crate) fn CreateAttrStringValueFromCStr<'ll>( attr: &std::ffi::CStr, value: &std::ffi::CStr, ) -> &'ll Attribute { + let attr = attr.to_bytes(); + let value = value.to_bytes(); unsafe { LLVMCreateStringAttribute( llcx, - (*attr).as_ptr(), - (*attr).to_bytes().len() as c_uint, - (*value).as_ptr(), - (*value).to_bytes().len() as c_uint, + attr.as_ptr(), + attr.len() as c_uint, + value.as_ptr(), + value.len() as c_uint, ) } } @@ -98,7 +99,7 @@ pub(crate) fn CreateAttrString<'ll>(llcx: &'ll Context, attr: &str) -> &'ll Attr unsafe { LLVMCreateStringAttribute( llcx, - attr.as_c_char_ptr(), + attr.as_ptr(), attr.len().try_into().unwrap(), std::ptr::null(), 0, @@ -340,7 +341,7 @@ pub(crate) struct Intrinsic { impl Intrinsic { pub(crate) fn lookup(name: &[u8]) -> Option { - let id = unsafe { LLVMLookupIntrinsicID(name.as_c_char_ptr(), name.len()) }; + let id = unsafe { LLVMLookupIntrinsicID(name.as_ptr(), name.len()) }; NonZero::new(id).map(|id| Self { id }) } @@ -365,10 +366,7 @@ impl Intrinsic { /// Safe wrapper for `LLVMSetValueName2` from a byte slice pub(crate) fn set_value_name(value: &Value, name: &[u8]) { - unsafe { - let data = name.as_c_char_ptr(); - LLVMSetValueName2(value, data, name.len()); - } + unsafe { LLVMSetValueName2(value, name.as_ptr(), name.len()) }; } pub(crate) fn build_string(f: impl FnOnce(&RustString)) -> Result { @@ -408,12 +406,7 @@ pub(crate) struct OperandBundleBox<'a> { impl<'a> OperandBundleBox<'a> { pub(crate) fn new(name: &str, vals: &[&'a Value]) -> Self { let raw = unsafe { - LLVMCreateOperandBundle( - name.as_c_char_ptr(), - name.len(), - vals.as_ptr(), - vals.len() as c_uint, - ) + LLVMCreateOperandBundle(name.as_ptr(), name.len(), vals.as_ptr(), vals.len() as c_uint) }; Self { raw: ptr::NonNull::new(raw).unwrap() } } @@ -444,7 +437,7 @@ pub(crate) fn add_module_flag_u32( value: u32, ) { unsafe { - LLVMRustAddModuleFlagU32(module, merge_behavior, key.as_c_char_ptr(), key.len(), value); + LLVMRustAddModuleFlagU32(module, merge_behavior, key.as_ptr(), key.len(), value); } } @@ -458,9 +451,9 @@ pub(crate) fn add_module_flag_str( LLVMRustAddModuleFlagString( module, merge_behavior, - key.as_c_char_ptr(), + key.as_ptr(), key.len(), - value.as_c_char_ptr(), + value.as_ptr(), value.len(), ); }