Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a3b639c

Browse files
authoredJun 2, 2023
Rollup merge of #111647 - klensy:cstr, r=oli-obk
use c literals in compiler and library Use c literals #108801 in compiler and library currently blocked on: * <strike>rustfmt: don't know how to format c literals</strike> nope, nightly one works. * <strike>bootstrap</strike> r? `@ghost` `@rustbot` blocked
2 parents 0939ec1 + 2f459f7 commit a3b639c

File tree

23 files changed

+74
-98
lines changed

23 files changed

+74
-98
lines changed
 

‎Cargo.lock

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -871,16 +871,6 @@ dependencies = [
871871
"typenum",
872872
]
873873

874-
[[package]]
875-
name = "cstr"
876-
version = "0.2.8"
877-
source = "registry+https://github.com/rust-lang/crates.io-index"
878-
checksum = "c11a39d776a3b35896711da8a04dc1835169dcd36f710878187637314e47941b"
879-
dependencies = [
880-
"proc-macro2",
881-
"quote",
882-
]
883-
884874
[[package]]
885875
name = "ctrlc"
886876
version = "3.3.1"
@@ -3179,7 +3169,6 @@ name = "rustc_codegen_llvm"
31793169
version = "0.0.0"
31803170
dependencies = [
31813171
"bitflags",
3182-
"cstr",
31833172
"libc",
31843173
"measureme",
31853174
"object 0.31.1",

‎compiler/rustc_codegen_llvm/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ test = false
88

99
[dependencies]
1010
bitflags = "1.0"
11-
cstr = "0.2"
1211
libc = "0.2"
1312
measureme = "10.0.0"
1413
object = { version = "0.31.1", default-features = false, features = [

‎compiler/rustc_codegen_llvm/src/allocator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub(crate) unsafe fn codegen(
7777
llvm::LLVMRustGetOrInsertFunction(llmod, callee.as_ptr().cast(), callee.len(), ty);
7878
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
7979

80-
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
80+
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast());
8181

8282
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
8383
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);
@@ -129,7 +129,7 @@ pub(crate) unsafe fn codegen(
129129
attributes::apply_to_llfn(callee, llvm::AttributePlace::Function, &[no_return]);
130130
llvm::LLVMRustSetVisibility(callee, llvm::Visibility::Hidden);
131131

132-
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, "entry\0".as_ptr().cast());
132+
let llbb = llvm::LLVMAppendBasicBlockInContext(llcx, llfn, c"entry".as_ptr().cast());
133133

134134
let llbuilder = llvm::LLVMCreateBuilderInContext(llcx);
135135
llvm::LLVMPositionBuilderAtEnd(llbuilder, llbb);

‎compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ pub(crate) fn run_pass_manager(
595595
llvm::LLVMRustAddModuleFlag(
596596
module.module_llvm.llmod(),
597597
llvm::LLVMModFlagBehavior::Error,
598-
"LTOPostLink\0".as_ptr().cast(),
598+
c"LTOPostLink".as_ptr().cast(),
599599
1,
600600
);
601601
}

‎compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -891,11 +891,11 @@ unsafe fn embed_bitcode(
891891
let llglobal = llvm::LLVMAddGlobal(
892892
llmod,
893893
common::val_ty(llconst),
894-
"rustc.embedded.module\0".as_ptr().cast(),
894+
c"rustc.embedded.module".as_ptr().cast(),
895895
);
896896
llvm::LLVMSetInitializer(llglobal, llconst);
897897

898-
let section = if is_apple { "__LLVM,__bitcode\0" } else { ".llvmbc\0" };
898+
let section = if is_apple { c"__LLVM,__bitcode" } else { c".llvmbc" };
899899
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
900900
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
901901
llvm::LLVMSetGlobalConstant(llglobal, llvm::True);
@@ -904,10 +904,10 @@ unsafe fn embed_bitcode(
904904
let llglobal = llvm::LLVMAddGlobal(
905905
llmod,
906906
common::val_ty(llconst),
907-
"rustc.embedded.cmdline\0".as_ptr().cast(),
907+
c"rustc.embedded.cmdline".as_ptr().cast(),
908908
);
909909
llvm::LLVMSetInitializer(llglobal, llconst);
910-
let section = if is_apple { "__LLVM,__cmdline\0" } else { ".llvmcmd\0" };
910+
let section = if is_apple { c"__LLVM,__cmdline" } else { c".llvmcmd" };
911911
llvm::LLVMSetSection(llglobal, section.as_ptr().cast());
912912
llvm::LLVMRustSetLinkage(llglobal, llvm::Linkage::PrivateLinkage);
913913
} else {

‎compiler/rustc_codegen_llvm/src/base.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ use crate::context::CodegenCx;
1919
use crate::llvm;
2020
use crate::value::Value;
2121

22-
use cstr::cstr;
23-
2422
use rustc_codegen_ssa::base::maybe_create_entry_wrapper;
2523
use rustc_codegen_ssa::mono_item::MonoItemExt;
2624
use rustc_codegen_ssa::traits::*;
@@ -110,11 +108,11 @@ pub fn compile_codegen_unit(tcx: TyCtxt<'_>, cgu_name: Symbol) -> (ModuleCodegen
110108

111109
// Create the llvm.used and llvm.compiler.used variables.
112110
if !cx.used_statics.borrow().is_empty() {
113-
cx.create_used_variable_impl(cstr!("llvm.used"), &*cx.used_statics.borrow());
111+
cx.create_used_variable_impl(c"llvm.used", &*cx.used_statics.borrow());
114112
}
115113
if !cx.compiler_used_statics.borrow().is_empty() {
116114
cx.create_used_variable_impl(
117-
cstr!("llvm.compiler.used"),
115+
c"llvm.compiler.used",
118116
&*cx.compiler_used_statics.borrow(),
119117
);
120118
}

‎compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock, False, True}
66
use crate::type_::Type;
77
use crate::type_of::LayoutLlvmExt;
88
use crate::value::Value;
9-
use cstr::cstr;
109
use libc::{c_char, c_uint};
1110
use rustc_codegen_ssa::common::{IntPredicate, RealPredicate, SynchronizationScope, TypeKind};
1211
use rustc_codegen_ssa::mir::operand::{OperandRef, OperandValue};
@@ -25,7 +24,6 @@ use rustc_symbol_mangling::typeid::{kcfi_typeid_for_fnabi, typeid_for_fnabi, Typ
2524
use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
2625
use rustc_target::spec::{HasTargetSpec, SanitizerSet, Target};
2726
use std::borrow::Cow;
28-
use std::ffi::CStr;
2927
use std::iter;
3028
use std::ops::Deref;
3129
use std::ptr;
@@ -45,13 +43,10 @@ impl Drop for Builder<'_, '_, '_> {
4543
}
4644
}
4745

48-
// FIXME(eddyb) use a checked constructor when they become `const fn`.
49-
const EMPTY_C_STR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") };
50-
5146
/// Empty string, to be used where LLVM expects an instruction name, indicating
5247
/// that the instruction is to be left unnamed (i.e. numbered, in textual IR).
5348
// FIXME(eddyb) pass `&CStr` directly to FFI once it's a thin pointer.
54-
const UNNAMED: *const c_char = EMPTY_C_STR.as_ptr();
49+
const UNNAMED: *const c_char = c"".as_ptr();
5550

5651
impl<'ll, 'tcx> BackendTypes for Builder<'_, 'll, 'tcx> {
5752
type Value = <CodegenCx<'ll, 'tcx> as BackendTypes>::Value;
@@ -1010,14 +1005,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10101005
}
10111006

10121007
fn cleanup_pad(&mut self, parent: Option<&'ll Value>, args: &[&'ll Value]) -> Funclet<'ll> {
1013-
let name = cstr!("cleanuppad");
10141008
let ret = unsafe {
10151009
llvm::LLVMBuildCleanupPad(
10161010
self.llbuilder,
10171011
parent,
10181012
args.as_ptr(),
10191013
args.len() as c_uint,
1020-
name.as_ptr(),
1014+
c"cleanuppad".as_ptr(),
10211015
)
10221016
};
10231017
Funclet::new(ret.expect("LLVM does not have support for cleanuppad"))
@@ -1031,14 +1025,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10311025
}
10321026

10331027
fn catch_pad(&mut self, parent: &'ll Value, args: &[&'ll Value]) -> Funclet<'ll> {
1034-
let name = cstr!("catchpad");
10351028
let ret = unsafe {
10361029
llvm::LLVMBuildCatchPad(
10371030
self.llbuilder,
10381031
parent,
10391032
args.as_ptr(),
10401033
args.len() as c_uint,
1041-
name.as_ptr(),
1034+
c"catchpad".as_ptr(),
10421035
)
10431036
};
10441037
Funclet::new(ret.expect("LLVM does not have support for catchpad"))
@@ -1050,14 +1043,13 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
10501043
unwind: Option<&'ll BasicBlock>,
10511044
handlers: &[&'ll BasicBlock],
10521045
) -> &'ll Value {
1053-
let name = cstr!("catchswitch");
10541046
let ret = unsafe {
10551047
llvm::LLVMBuildCatchSwitch(
10561048
self.llbuilder,
10571049
parent,
10581050
unwind,
10591051
handlers.len() as c_uint,
1060-
name.as_ptr(),
1052+
c"catchswitch".as_ptr(),
10611053
)
10621054
};
10631055
let ret = ret.expect("LLVM does not have support for catchswitch");

‎compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::llvm::{self, True};
88
use crate::type_::Type;
99
use crate::type_of::LayoutLlvmExt;
1010
use crate::value::Value;
11-
use cstr::cstr;
1211
use rustc_codegen_ssa::traits::*;
1312
use rustc_hir::def_id::DefId;
1413
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
@@ -482,9 +481,9 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
482481
.all(|&byte| byte == 0);
483482

484483
let sect_name = if all_bytes_are_zero {
485-
cstr!("__DATA,__thread_bss")
484+
c"__DATA,__thread_bss"
486485
} else {
487-
cstr!("__DATA,__thread_data")
486+
c"__DATA,__thread_data"
488487
};
489488
llvm::LLVMSetSection(g, sect_name.as_ptr());
490489
}
@@ -513,7 +512,7 @@ impl<'ll> StaticMethods for CodegenCx<'ll, '_> {
513512
let val = llvm::LLVMMetadataAsValue(self.llcx, meta);
514513
llvm::LLVMAddNamedMetadataOperand(
515514
self.llmod,
516-
"wasm.custom_sections\0".as_ptr().cast(),
515+
c"wasm.custom_sections".as_ptr().cast(),
517516
val,
518517
);
519518
}

‎compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use crate::llvm_util;
88
use crate::type_::Type;
99
use crate::value::Value;
1010

11-
use cstr::cstr;
1211
use rustc_codegen_ssa::base::wants_msvc_seh;
1312
use rustc_codegen_ssa::traits::*;
1413
use rustc_data_structures::base_n;
@@ -224,36 +223,42 @@ pub unsafe fn create_module<'ll>(
224223
// If skipping the PLT is enabled, we need to add some module metadata
225224
// to ensure intrinsic calls don't use it.
226225
if !sess.needs_plt() {
227-
let avoid_plt = "RtLibUseGOT\0".as_ptr().cast();
228-
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Warning, avoid_plt, 1);
226+
llvm::LLVMRustAddModuleFlag(
227+
llmod,
228+
llvm::LLVMModFlagBehavior::Warning,
229+
c"RtLibUseGOT".as_ptr().cast(),
230+
1,
231+
);
229232
}
230233

231234
// Enable canonical jump tables if CFI is enabled. (See https://reviews.llvm.org/D65629.)
232235
if sess.is_sanitizer_cfi_canonical_jump_tables_enabled() && sess.is_sanitizer_cfi_enabled() {
233-
let canonical_jump_tables = "CFI Canonical Jump Tables\0".as_ptr().cast();
234236
llvm::LLVMRustAddModuleFlag(
235237
llmod,
236238
llvm::LLVMModFlagBehavior::Override,
237-
canonical_jump_tables,
239+
c"CFI Canonical Jump Tables".as_ptr().cast(),
238240
1,
239241
);
240242
}
241243

242244
// Enable LTO unit splitting if specified or if CFI is enabled. (See https://reviews.llvm.org/D53891.)
243245
if sess.is_split_lto_unit_enabled() || sess.is_sanitizer_cfi_enabled() {
244-
let enable_split_lto_unit = "EnableSplitLTOUnit\0".as_ptr().cast();
245246
llvm::LLVMRustAddModuleFlag(
246247
llmod,
247248
llvm::LLVMModFlagBehavior::Override,
248-
enable_split_lto_unit,
249+
c"EnableSplitLTOUnit".as_ptr().cast(),
249250
1,
250251
);
251252
}
252253

253254
// Add "kcfi" module flag if KCFI is enabled. (See https://reviews.llvm.org/D119296.)
254255
if sess.is_sanitizer_kcfi_enabled() {
255-
let kcfi = "kcfi\0".as_ptr().cast();
256-
llvm::LLVMRustAddModuleFlag(llmod, llvm::LLVMModFlagBehavior::Override, kcfi, 1);
256+
llvm::LLVMRustAddModuleFlag(
257+
llmod,
258+
llvm::LLVMModFlagBehavior::Override,
259+
c"kcfi".as_ptr().cast(),
260+
1,
261+
);
257262
}
258263

259264
// Control Flow Guard is currently only supported by the MSVC linker on Windows.
@@ -265,7 +270,7 @@ pub unsafe fn create_module<'ll>(
265270
llvm::LLVMRustAddModuleFlag(
266271
llmod,
267272
llvm::LLVMModFlagBehavior::Warning,
268-
"cfguard\0".as_ptr() as *const _,
273+
c"cfguard".as_ptr() as *const _,
269274
1,
270275
)
271276
}
@@ -274,7 +279,7 @@ pub unsafe fn create_module<'ll>(
274279
llvm::LLVMRustAddModuleFlag(
275280
llmod,
276281
llvm::LLVMModFlagBehavior::Warning,
277-
"cfguard\0".as_ptr() as *const _,
282+
c"cfguard".as_ptr() as *const _,
278283
2,
279284
)
280285
}
@@ -292,26 +297,26 @@ pub unsafe fn create_module<'ll>(
292297
llvm::LLVMRustAddModuleFlag(
293298
llmod,
294299
behavior,
295-
"branch-target-enforcement\0".as_ptr().cast(),
300+
c"branch-target-enforcement".as_ptr().cast(),
296301
bti.into(),
297302
);
298303
llvm::LLVMRustAddModuleFlag(
299304
llmod,
300305
behavior,
301-
"sign-return-address\0".as_ptr().cast(),
306+
c"sign-return-address".as_ptr().cast(),
302307
pac_ret.is_some().into(),
303308
);
304309
let pac_opts = pac_ret.unwrap_or(PacRet { leaf: false, key: PAuthKey::A });
305310
llvm::LLVMRustAddModuleFlag(
306311
llmod,
307312
behavior,
308-
"sign-return-address-all\0".as_ptr().cast(),
313+
c"sign-return-address-all".as_ptr().cast(),
309314
pac_opts.leaf.into(),
310315
);
311316
llvm::LLVMRustAddModuleFlag(
312317
llmod,
313318
behavior,
314-
"sign-return-address-with-bkey\0".as_ptr().cast(),
319+
c"sign-return-address-with-bkey".as_ptr().cast(),
315320
u32::from(pac_opts.key == PAuthKey::B),
316321
);
317322
} else {
@@ -327,15 +332,15 @@ pub unsafe fn create_module<'ll>(
327332
llvm::LLVMRustAddModuleFlag(
328333
llmod,
329334
llvm::LLVMModFlagBehavior::Override,
330-
"cf-protection-branch\0".as_ptr().cast(),
335+
c"cf-protection-branch".as_ptr().cast(),
331336
1,
332337
)
333338
}
334339
if let CFProtection::Return | CFProtection::Full = sess.opts.unstable_opts.cf_protection {
335340
llvm::LLVMRustAddModuleFlag(
336341
llmod,
337342
llvm::LLVMModFlagBehavior::Override,
338-
"cf-protection-return\0".as_ptr().cast(),
343+
c"cf-protection-return".as_ptr().cast(),
339344
1,
340345
)
341346
}
@@ -344,7 +349,7 @@ pub unsafe fn create_module<'ll>(
344349
llvm::LLVMRustAddModuleFlag(
345350
llmod,
346351
llvm::LLVMModFlagBehavior::Error,
347-
"Virtual Function Elim\0".as_ptr().cast(),
352+
c"Virtual Function Elim".as_ptr().cast(),
348353
1,
349354
);
350355
}
@@ -476,14 +481,13 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
476481
}
477482

478483
pub(crate) fn create_used_variable_impl(&self, name: &'static CStr, values: &[&'ll Value]) {
479-
let section = cstr!("llvm.metadata");
480484
let array = self.const_array(self.type_ptr_to(self.type_i8()), values);
481485

482486
unsafe {
483487
let g = llvm::LLVMAddGlobal(self.llmod, self.val_ty(array), name.as_ptr());
484488
llvm::LLVMSetInitializer(g, array);
485489
llvm::LLVMRustSetLinkage(g, llvm::Linkage::AppendingLinkage);
486-
llvm::LLVMSetSection(g, section.as_ptr());
490+
llvm::LLVMSetSection(g, c"llvm.metadata".as_ptr());
487491
}
488492
}
489493
}

‎compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
3838
unsafe { llvm::LLVMGetNamedGlobal(cx.llmod, c_section_var_name.as_ptr().cast()) };
3939

4040
section_var.unwrap_or_else(|| {
41-
let section_name = b".debug_gdb_scripts\0";
4241
let mut section_contents = Vec::new();
4342

4443
// Add the pretty printers for the standard library first.
@@ -71,7 +70,7 @@ pub fn get_or_insert_gdb_debug_scripts_section_global<'ll>(cx: &CodegenCx<'ll, '
7170
let section_var = cx
7271
.define_global(section_var_name, llvm_type)
7372
.unwrap_or_else(|| bug!("symbol `{}` is already defined", section_var_name));
74-
llvm::LLVMSetSection(section_var, section_name.as_ptr().cast());
73+
llvm::LLVMSetSection(section_var, c".debug_gdb_scripts".as_ptr().cast());
7574
llvm::LLVMSetInitializer(section_var, cx.const_bytes(section_contents));
7675
llvm::LLVMSetGlobalConstant(section_var, llvm::True);
7776
llvm::LLVMSetUnnamedAddress(section_var, llvm::UnnamedAddr::Global);

‎compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use crate::llvm::debuginfo::{
2020
};
2121
use crate::value::Value;
2222

23-
use cstr::cstr;
2423
use rustc_codegen_ssa::debuginfo::type_names::cpp_like_debuginfo;
2524
use rustc_codegen_ssa::debuginfo::type_names::VTableNameKind;
2625
use rustc_codegen_ssa::traits::*;
@@ -812,7 +811,6 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
812811

813812
let name_in_debuginfo = name_in_debuginfo.to_string_lossy();
814813
let work_dir = tcx.sess.opts.working_dir.to_string_lossy(FileNameDisplayPreference::Remapped);
815-
let flags = "\0";
816814
let output_filenames = tcx.output_filenames(());
817815
let split_name = if tcx.sess.target_can_use_split_dwarf() {
818816
output_filenames
@@ -849,7 +847,7 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
849847
producer.as_ptr().cast(),
850848
producer.len(),
851849
tcx.sess.opts.optimize != config::OptLevel::No,
852-
flags.as_ptr().cast(),
850+
c"".as_ptr().cast(),
853851
0,
854852
// NB: this doesn't actually have any perceptible effect, it seems. LLVM will instead
855853
// put the path supplied to `MCSplitDwarfFile` into the debug info of the final
@@ -878,8 +876,7 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
878876
);
879877
let val = llvm::LLVMMetadataAsValue(debug_context.llcontext, gcov_metadata);
880878

881-
let llvm_gcov_ident = cstr!("llvm.gcov");
882-
llvm::LLVMAddNamedMetadataOperand(debug_context.llmod, llvm_gcov_ident.as_ptr(), val);
879+
llvm::LLVMAddNamedMetadataOperand(debug_context.llmod, c"llvm.gcov".as_ptr(), val);
883880
}
884881

885882
// Insert `llvm.ident` metadata on the wasm targets since that will
@@ -892,7 +889,7 @@ pub fn build_compile_unit_di_node<'ll, 'tcx>(
892889
);
893890
llvm::LLVMAddNamedMetadataOperand(
894891
debug_context.llmod,
895-
cstr!("llvm.ident").as_ptr(),
892+
c"llvm.ident".as_ptr(),
896893
llvm::LLVMMDNodeInContext(debug_context.llcontext, &name_metadata, 1),
897894
);
898895
}

‎compiler/rustc_codegen_llvm/src/debuginfo/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,25 +113,24 @@ impl<'ll, 'tcx> CodegenUnitDebugContext<'ll, 'tcx> {
113113
llvm::LLVMRustAddModuleFlag(
114114
self.llmod,
115115
llvm::LLVMModFlagBehavior::Warning,
116-
"Dwarf Version\0".as_ptr().cast(),
116+
c"Dwarf Version".as_ptr().cast(),
117117
dwarf_version,
118118
);
119119
} else {
120120
// Indicate that we want CodeView debug information on MSVC
121121
llvm::LLVMRustAddModuleFlag(
122122
self.llmod,
123123
llvm::LLVMModFlagBehavior::Warning,
124-
"CodeView\0".as_ptr().cast(),
124+
c"CodeView".as_ptr().cast(),
125125
1,
126126
)
127127
}
128128

129129
// Prevent bitcode readers from deleting the debug info.
130-
let ptr = "Debug Info Version\0".as_ptr();
131130
llvm::LLVMRustAddModuleFlag(
132131
self.llmod,
133132
llvm::LLVMModFlagBehavior::Warning,
134-
ptr.cast(),
133+
c"Debug Info Version".as_ptr().cast(),
135134
llvm::LLVMRustDebugMetadataVersion(),
136135
);
137136
}

‎compiler/rustc_codegen_llvm/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(let_chains)]
1212
#![feature(never_type)]
1313
#![feature(impl_trait_in_assoc_type)]
14+
#![feature(c_str_literals)]
1415
#![recursion_limit = "256"]
1516
#![allow(rustc::potential_query_instability)]
1617
#![deny(rustc::untranslatable_diagnostic)]

‎library/std/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@
240240
#![feature(allocator_internals)]
241241
#![feature(allow_internal_unsafe)]
242242
#![feature(allow_internal_unstable)]
243+
#![feature(c_str_literals)]
243244
#![feature(c_unwind)]
244245
#![feature(cfg_target_thread_local)]
245246
#![feature(concat_idents)]

‎library/std/src/sys/unix/args.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,13 +242,15 @@ mod imp {
242242
let mut res = Vec::new();
243243

244244
unsafe {
245-
let process_info_sel = sel_registerName("processInfo\0".as_ptr());
246-
let arguments_sel = sel_registerName("arguments\0".as_ptr());
247-
let utf8_sel = sel_registerName("UTF8String\0".as_ptr());
248-
let count_sel = sel_registerName("count\0".as_ptr());
249-
let object_at_sel = sel_registerName("objectAtIndex:\0".as_ptr());
250-
251-
let klass = objc_getClass("NSProcessInfo\0".as_ptr());
245+
let process_info_sel =
246+
sel_registerName(c"processInfo".as_ptr() as *const libc::c_uchar);
247+
let arguments_sel = sel_registerName(c"arguments".as_ptr() as *const libc::c_uchar);
248+
let utf8_sel = sel_registerName(c"UTF8String".as_ptr() as *const libc::c_uchar);
249+
let count_sel = sel_registerName(c"count".as_ptr() as *const libc::c_uchar);
250+
let object_at_sel =
251+
sel_registerName(c"objectAtIndex:".as_ptr() as *const libc::c_uchar);
252+
253+
let klass = objc_getClass(c"NSProcessInfo".as_ptr() as *const libc::c_uchar);
252254
let info = objc_msgSend(klass, process_info_sel);
253255
let args = objc_msgSend(info, arguments_sel);
254256

‎library/std/src/sys/unix/fs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ impl File {
10631063
cfg_has_statx! {
10641064
if let Some(ret) = unsafe { try_statx(
10651065
fd,
1066-
b"\0" as *const _ as *const c_char,
1066+
c"".as_ptr() as *const c_char,
10671067
libc::AT_EMPTY_PATH | libc::AT_STATX_SYNC_AS_STAT,
10681068
libc::STATX_ALL,
10691069
) } {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#![allow(missing_docs, nonstandard_style)]
22

3-
use crate::ffi::CStr;
43
use crate::io::ErrorKind;
54

65
pub use self::rand::hashmap_random_keys;
@@ -75,7 +74,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
7574
// thread-id for the main thread and so renaming the main thread will rename the
7675
// process and we only want to enable this on platforms we've tested.
7776
if cfg!(target_os = "macos") {
78-
thread::Thread::set_name(&CStr::from_bytes_with_nul_unchecked(b"main\0"));
77+
thread::Thread::set_name(&c"main");
7978
}
8079

8180
unsafe fn sanitize_standard_fds() {
@@ -121,7 +120,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
121120
if pfd.revents & libc::POLLNVAL == 0 {
122121
continue;
123122
}
124-
if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
123+
if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
125124
// If the stream is closed but we failed to reopen it, abort the
126125
// process. Otherwise we wouldn't preserve the safety of
127126
// operations on the corresponding Rust object Stdin, Stdout, or
@@ -151,7 +150,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
151150
use libc::open64;
152151
for fd in 0..3 {
153152
if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
154-
if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
153+
if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
155154
// If the stream is closed but we failed to reopen it, abort the
156155
// process. Otherwise we wouldn't preserve the safety of
157156
// operations on the corresponding Rust object Stdin, Stdout, or

‎library/std/src/sys/unix/process/process_common.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ cfg_if::cfg_if! {
2424
if #[cfg(target_os = "fuchsia")] {
2525
// fuchsia doesn't have /dev/null
2626
} else if #[cfg(target_os = "redox")] {
27-
const DEV_NULL: &str = "null:\0";
27+
const DEV_NULL: &CStr = c"null:";
2828
} else if #[cfg(target_os = "vxworks")] {
29-
const DEV_NULL: &str = "/null\0";
29+
const DEV_NULL: &CStr = c"/null";
3030
} else {
31-
const DEV_NULL: &str = "/dev/null\0";
31+
const DEV_NULL: &CStr = c"/dev/null";
3232
}
3333
}
3434

@@ -474,8 +474,7 @@ impl Stdio {
474474
let mut opts = OpenOptions::new();
475475
opts.read(readable);
476476
opts.write(!readable);
477-
let path = unsafe { CStr::from_ptr(DEV_NULL.as_ptr() as *const _) };
478-
let fd = File::open_c(&path, &opts)?;
477+
let fd = File::open_c(DEV_NULL, &opts)?;
479478
Ok((ChildStdio::Owned(fd.into_inner()), None))
480479
}
481480

‎library/std/src/sys/unix/thread.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,9 @@ impl Thread {
163163
#[cfg(target_os = "netbsd")]
164164
pub fn set_name(name: &CStr) {
165165
unsafe {
166-
let cname = CStr::from_bytes_with_nul_unchecked(b"%s\0".as_slice());
167166
let res = libc::pthread_setname_np(
168167
libc::pthread_self(),
169-
cname.as_ptr(),
168+
c"%s".as_ptr(),
170169
name.as_ptr() as *mut libc::c_void,
171170
);
172171
debug_assert_eq!(res, 0);

‎library/std/src/sys/windows/c.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ pub unsafe fn NtWriteFile(
317317
// Functions that aren't available on every version of Windows that we support,
318318
// but we still use them and just provide some form of a fallback implementation.
319319
compat_fn_with_fallback! {
320-
pub static KERNEL32: &CStr = ansi_str!("kernel32");
320+
pub static KERNEL32: &CStr = c"kernel32";
321321

322322
// >= Win10 1607
323323
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
@@ -350,7 +350,7 @@ compat_fn_optional! {
350350
}
351351

352352
compat_fn_with_fallback! {
353-
pub static NTDLL: &CStr = ansi_str!("ntdll");
353+
pub static NTDLL: &CStr = c"ntdll";
354354

355355
pub fn NtCreateKeyedEvent(
356356
KeyedEventHandle: LPHANDLE,

‎library/std/src/sys/windows/compat.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ macro_rules! compat_fn_optional {
228228
/// Load all needed functions from "api-ms-win-core-synch-l1-2-0".
229229
pub(super) fn load_synch_functions() {
230230
fn try_load() -> Option<()> {
231-
const MODULE_NAME: &CStr = ansi_str!("api-ms-win-core-synch-l1-2-0");
232-
const WAIT_ON_ADDRESS: &CStr = ansi_str!("WaitOnAddress");
233-
const WAKE_BY_ADDRESS_SINGLE: &CStr = ansi_str!("WakeByAddressSingle");
231+
const MODULE_NAME: &CStr = c"api-ms-win-core-synch-l1-2-0";
232+
const WAIT_ON_ADDRESS: &CStr = c"WaitOnAddress";
233+
const WAKE_BY_ADDRESS_SINGLE: &CStr = c"WakeByAddressSingle";
234234

235235
// Try loading the library and all the required functions.
236236
// If any step fails, then they all fail.

‎library/std/src/sys/windows/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(missing_docs, nonstandard_style)]
22

3-
use crate::ffi::{CStr, OsStr, OsString};
3+
use crate::ffi::{OsStr, OsString};
44
use crate::io::ErrorKind;
55
use crate::mem::MaybeUninit;
66
use crate::os::windows::ffi::{OsStrExt, OsStringExt};
@@ -51,7 +51,7 @@ pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {
5151

5252
// Normally, `thread::spawn` will call `Thread::set_name` but since this thread already
5353
// exists, we have to call it ourselves.
54-
thread::Thread::set_name(&CStr::from_bytes_with_nul_unchecked(b"main\0"));
54+
thread::Thread::set_name(&c"main");
5555
}
5656

5757
// SAFETY: must be called only once during runtime cleanup.

‎src/tools/tidy/src/deps.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
132132
"crossbeam-epoch",
133133
"crossbeam-utils",
134134
"crypto-common",
135-
"cstr",
136135
"datafrog",
137136
"derive_more",
138137
"digest",

0 commit comments

Comments
 (0)
Please sign in to comment.