Skip to content

Commit e424e7f

Browse files
committed
Avoid extra cast()s after CStr::as_ptr()
These used to be `&str` literals that did need a pointer cast, but that became a no-op after switching to `c""` literals in #118566.
1 parent a971212 commit e424e7f

File tree

9 files changed

+26
-27
lines changed

9 files changed

+26
-27
lines changed

compiler/rustc_codegen_llvm/src/allocator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn create_wrapper_function(
149149
}
150150
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
151151

152-
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast());
152+
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr());
153153

154154
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
155155
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);

compiler/rustc_codegen_llvm/src/back/lto.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ pub(crate) fn run_pass_manager(
616616
llvm::LLVMRustAddModuleFlagU32(
617617
module.module_llvm.llmod(),
618618
llvm::LLVMModFlagBehavior::Error,
619-
c"LTOPostLink".as_ptr().cast(),
619+
c"LTOPostLink".as_ptr(),
620620
1,
621621
);
622622
}

compiler/rustc_codegen_llvm/src/back/write.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ unsafe fn embed_bitcode(
10311031
let llglobal = llvm::LLVMAddGlobal(
10321032
llmod,
10331033
common::val_ty(llconst),
1034-
c"rustc.embedded.module".as_ptr().cast(),
1034+
c"rustc.embedded.module".as_ptr(),
10351035
);
10361036
llvm::LLVMSetInitializer(llglobal, llconst);
10371037

@@ -1044,7 +1044,7 @@ unsafe fn embed_bitcode(
10441044
let llglobal = llvm::LLVMAddGlobal(
10451045
llmod,
10461046
common::val_ty(llconst),
1047-
c"rustc.embedded.cmdline".as_ptr().cast(),
1047+
c"rustc.embedded.cmdline".as_ptr(),
10481048
);
10491049
llvm::LLVMSetInitializer(llglobal, llconst);
10501050
let section = if is_apple {
@@ -1054,7 +1054,7 @@ unsafe fn embed_bitcode(
10541054
} else {
10551055
c".llvmcmd"
10561056
};
1057-
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
1057+
llvm::LLVMSetSection(llglobal, section.as_ptr());
10581058
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
10591059
} else {
10601060
// We need custom section flags, so emit module-level inline assembly.
@@ -1107,7 +1107,7 @@ fn create_msvc_imps(
11071107
.collect::<Vec<_>>();
11081108

11091109
for (imp_name, val) in globals {
1110-
let imp = llvm::LLVMAddGlobal(llmod, ptr_ty, imp_name.as_ptr().cast());
1110+
let imp = llvm::LLVMAddGlobal(llmod, ptr_ty, imp_name.as_ptr());
11111111
llvm::LLVMSetInitializer(imp, val);
11121112
llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
11131113
}

compiler/rustc_codegen_llvm/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl<'ll> CodegenCx<'ll, '_> {
525525
let val = llvm::LLVMMetadataAsValue(self.llcx, meta);
526526
llvm::LLVMAddNamedMetadataOperand(
527527
self.llmod,
528-
c"wasm.custom_sections".as_ptr().cast(),
528+
c"wasm.custom_sections".as_ptr(),
529529
val,
530530
);
531531
}

compiler/rustc_codegen_llvm/src/context.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,15 @@ pub unsafe fn create_module<'ll>(
207207
// If skipping the PLT is enabled, we need to add some module metadata
208208
// to ensure intrinsic calls don't use it.
209209
if !sess.needs_plt() {
210-
let avoid_plt = c"RtLibUseGOT".as_ptr().cast();
210+
let avoid_plt = c"RtLibUseGOT".as_ptr();
211211
unsafe {
212212
llvm::LLVMRustAddModuleFlagU32(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1);
213213
}
214214
}
215215

216216
// Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.)
217217
if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
218-
let canonical_jump_tables = c"CFI Canonical Jump Tables".as_ptr().cast();
218+
let canonical_jump_tables = c"CFI Canonical Jump Tables".as_ptr();
219219
unsafe {
220220
llvm::LLVMRustAddModuleFlagU32(
221221
llmod,
@@ -228,7 +228,7 @@ pub unsafe fn create_module<'ll>(
228228

229229
// Enable LTO unit splitting if specified or if CFI is enabled. (See https://reviews.llvm.org/D53891.)
230230
if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
231-
let enable_split_lto_unit = c"EnableSplitLTOUnit".as_ptr().cast();
231+
let enable_split_lto_unit = c"EnableSplitLTOUnit".as_ptr();
232232
unsafe {
233233
llvm::LLVMRustAddModuleFlagU32(
234234
llmod,
@@ -241,7 +241,7 @@ pub unsafe fn create_module<'ll>(
241241

242242
// Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.)
243243
if sess.is_sanitizer_kcfi_enabled() {
244-
let kcfi = c"kcfi".as_ptr().cast();
244+
let kcfi = c"kcfi".as_ptr();
245245
unsafe {
246246
llvm::LLVMRustAddModuleFlagU32(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1);
247247
}
@@ -280,26 +280,26 @@ pub unsafe fn create_module<'ll>(
280280
llvm::LLVMRustAddModuleFlagU32(
281281
llmod,
282282
llvm::LLVMModFlagBehavior::Min,
283-
c"branch-target-enforcement".as_ptr().cast(),
283+
c"branch-target-enforcement".as_ptr(),
284284
bti.into(),
285285
);
286286
llvm::LLVMRustAddModuleFlagU32(
287287
llmod,
288288
llvm::LLVMModFlagBehavior::Min,
289-
c"sign-return-address".as_ptr().cast(),
289+
c"sign-return-address".as_ptr(),
290290
pac_ret.is_some().into(),
291291
);
292292
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
293293
llvm::LLVMRustAddModuleFlagU32(
294294
llmod,
295295
llvm::LLVMModFlagBehavior::Min,
296-
c"sign-return-address-all".as_ptr().cast(),
296+
c"sign-return-address-all".as_ptr(),
297297
pac_opts.leaf.into(),
298298
);
299299
llvm::LLVMRustAddModuleFlagU32(
300300
llmod,
301301
llvm::LLVMModFlagBehavior::Min,
302-
c"sign-return-address-with-bkey".as_ptr().cast(),
302+
c"sign-return-address-with-bkey".as_ptr(),
303303
u32::from(pac_opts.key == PAuthKey::B),
304304
);
305305
}
@@ -317,7 +317,7 @@ pub unsafe fn create_module<'ll>(
317317
llvm::LLVMRustAddModuleFlagU32(
318318
llmod,
319319
llvm::LLVMModFlagBehavior::Override,
320-
c"cf-protection-branch".as_ptr().cast(),
320+
c"cf-protection-branch".as_ptr(),
321321
1,
322322
);
323323
}
@@ -327,7 +327,7 @@ pub unsafe fn create_module<'ll>(
327327
llvm::LLVMRustAddModuleFlagU32(
328328
llmod,
329329
llvm::LLVMModFlagBehavior::Override,
330-
c"cf-protection-return".as_ptr().cast(),
330+
c"cf-protection-return".as_ptr(),
331331
1,
332332
);
333333
}
@@ -338,7 +338,7 @@ pub unsafe fn create_module<'ll>(
338338
llvm::LLVMRustAddModuleFlagU32(
339339
llmod,
340340
llvm::LLVMModFlagBehavior::Error,
341-
c"Virtual Function Elim".as_ptr().cast(),
341+
c"Virtual Function Elim".as_ptr(),
342342
1,
343343
);
344344
}

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
3434
let c_section_var_name = c"__rustc_debug_gdb_scripts_section__";
3535
let section_var_name = c_section_var_name.to_str().unwrap();
3636

37-
let section_var =
38-
unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr().cast()) };
37+
let section_var = unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr()) };
3938

4039
section_var.unwrap_or_else(|| {
4140
let mut section_contents = Vec::new();
@@ -70,7 +69,7 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
7069
let section_var = cx
7170
.define_global(section_var_name, llvm_type)
7271
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
73-
llvm::LLVMSetSection(section_var, c".debug_gdb_scripts".as_ptr().cast());
72+
llvm::LLVMSetSection(section_var, c".debug_gdb_scripts".as_ptr());
7473
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
7574
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
7675
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -952,7 +952,7 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
952952
producer.as_ptr().cast(),
953953
producer.len(),
954954
tcx.sess.opts.optimize != config::OptLevel::No,
955-
c"".as_ptr().cast(),
955+
c"".as_ptr(),
956956
0,
957957
// NB: this doesn't actually have any perceptible effect, it seems. LLVM will instead
958958
// put the path supplied to `MCSplitDwarfFile` into the debug info of the final

compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,15 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
109109
llvm::LLVMRustAddModuleFlagU32(
110110
self.llmod,
111111
llvm::LLVMModFlagBehavior::Warning,
112-
c"Dwarf Version".as_ptr().cast(),
112+
c"Dwarf Version".as_ptr(),
113113
dwarf_version,
114114
);
115115
} else {
116116
// Indicate that we want CodeView debug information on MSVC
117117
llvm::LLVMRustAddModuleFlagU32(
118118
self.llmod,
119119
llvm::LLVMModFlagBehavior::Warning,
120-
c"CodeView".as_ptr().cast(),
120+
c"CodeView".as_ptr(),
121121
1,
122122
)
123123
}
@@ -126,7 +126,7 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
126126
llvm::LLVMRustAddModuleFlagU32(
127127
self.llmod,
128128
llvm::LLVMModFlagBehavior::Warning,
129-
c"Debug Info Version".as_ptr().cast(),
129+
c"Debug Info Version".as_ptr(),
130130
llvm::LLVMRustDebugMetadataVersion(),
131131
);
132132
}

library/std/src/sys/pal/unix/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
116116
if pfd.revents & libc::POLLNVAL == 0 {
117117
continue;
118118
}
119-
if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
119+
if open64(c"/dev/null".as_ptr(), libc::O_RDWR, 0) == -1 {
120120
// If the stream is closed but we failed to reopen it, abort the
121121
// process. Otherwise we wouldn't preserve the safety of
122122
// operations on the corresponding Rust object Stdin, Stdout, or
@@ -147,7 +147,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
147147
use crate::sys::os::errno;
148148
for fd in 0..3 {
149149
if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
150-
if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
150+
if open64(c"/dev/null".as_ptr(), libc::O_RDWR, 0) == -1 {
151151
// If the stream is closed but we failed to reopen it, abort the
152152
// process. Otherwise we wouldn't preserve the safety of
153153
// operations on the corresponding Rust object Stdin, Stdout, or

0 commit comments

Comments
 (0)