Skip to content

Commit 7b9f9e5

Browse files
committed
Use cstr! macro.
1 parent 3697b4d commit 7b9f9e5

File tree

15 files changed

+22
-68
lines changed

15 files changed

+22
-68
lines changed

Cargo.lock

-11
Original file line numberDiff line numberDiff line change
@@ -1023,16 +1023,6 @@ dependencies = [
10231023
"winapi",
10241024
]
10251025

1026-
[[package]]
1027-
name = "cstr"
1028-
version = "0.2.8"
1029-
source = "registry+https://github.com/rust-lang/crates.io-index"
1030-
checksum = "c11a39d776a3b35896711da8a04dc1835169dcd36f710878187637314e47941b"
1031-
dependencies = [
1032-
"proc-macro2",
1033-
"quote",
1034-
]
1035-
10361026
[[package]]
10371027
name = "curl"
10381028
version = "0.4.43"
@@ -3275,7 +3265,6 @@ name = "rustc_codegen_llvm"
32753265
version = "0.0.0"
32763266
dependencies = [
32773267
"bitflags",
3278-
"cstr",
32793268
"libc",
32803269
"measureme",
32813270
"object 0.29.0",

compiler/rustc_codegen_llvm/Cargo.toml

-1
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.29.0", default-features = false, features = ["std", "read_core", "archive", "coff", "elf", "macho", "pe"] }

compiler/rustc_codegen_llvm/src/base.rs

+1-2
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::*;
@@ -34,6 +32,7 @@ use rustc_session::config::DebugInfo;
3432
use rustc_span::symbol::Symbol;
3533
use rustc_target::spec::SanitizerSet;
3634

35+
use std::ffi::cstr;
3736
use std::time::Instant;
3837

3938
pub struct ValueIter<'ll> {

compiler/rustc_codegen_llvm/src/builder.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::llvm::{self, AtomicOrdering, AtomicRmwBinOp, BasicBlock};
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};
@@ -23,7 +22,7 @@ use rustc_span::Span;
2322
use rustc_target::abi::{self, call::FnAbi, Align, Size, WrappingRange};
2423
use rustc_target::spec::{HasTargetSpec, Target};
2524
use std::borrow::Cow;
26-
use std::ffi::CStr;
25+
use std::ffi::{cstr, CStr};
2726
use std::iter;
2827
use std::ops::Deref;
2928
use std::ptr;
@@ -44,7 +43,7 @@ impl Drop for Builder<'_, '_, '_> {
4443
}
4544

4645
// FIXME(eddyb) use a checked constructor when they become `const fn`.
47-
const EMPTY_C_STR: &CStr = unsafe { CStr::from_bytes_with_nul_unchecked(b"\0") };
46+
const EMPTY_C_STR: &CStr = cstr!();
4847

4948
/// Empty string, to be used where LLVM expects an instruction name, indicating
5049
/// that the instruction is to be left unnamed (i.e. numbered, in textual IR).

compiler/rustc_codegen_llvm/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::llvm_util;
66
use crate::type_::Type;
77
use crate::type_of::LayoutLlvmExt;
88
use crate::value::Value;
9-
use cstr::cstr;
109
use libc::c_uint;
1110
use rustc_codegen_ssa::traits::*;
1211
use rustc_hir::def_id::DefId;
@@ -22,6 +21,7 @@ use rustc_middle::{bug, span_bug};
2221
use rustc_target::abi::{
2322
AddressSpace, Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange,
2423
};
24+
use std::ffi::cstr;
2525
use std::ops::Range;
2626

2727
pub fn const_alloc_to_llvm<'ll>(cx: &CodegenCx<'ll, '_>, alloc: ConstAllocation<'_>) -> &'ll Value {

compiler/rustc_codegen_llvm/src/context.rs

+1-2
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;
@@ -33,7 +32,7 @@ use rustc_target::spec::{HasTargetSpec, RelocModel, Target, TlsModel};
3332
use smallvec::SmallVec;
3433

3534
use std::cell::{Cell, RefCell};
36-
use std::ffi::CStr;
35+
use std::ffi::{cstr, CStr};
3736
use std::str;
3837

3938
/// There is one `CodegenCx` per compilation unit. Each one has its own LLVM

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

+1-1
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::*;
@@ -45,6 +44,7 @@ use smallvec::smallvec;
4544

4645
use libc::{c_char, c_longlong, c_uint};
4746
use std::borrow::Cow;
47+
use std::ffi::cstr;
4848
use std::fmt::{self, Write};
4949
use std::hash::{Hash, Hasher};
5050
use std::iter;

compiler/rustc_codegen_llvm/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#![feature(let_chains)]
1010
#![feature(extern_types)]
1111
#![feature(once_cell)]
12+
#![feature(cstr_macro)]
1213
#![feature(iter_intersperse)]
1314
#![recursion_limit = "256"]
1415
#![allow(rustc::potential_query_instability)]

library/core/src/ffi/c_str.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl fmt::Debug for CStr {
177177
}
178178
}
179179

180-
/// Converts a string or byte literal to a `&'static Cstr`.
180+
/// Converts a `const` string or byte slice to a `&'static Cstr`.
181181
///
182182
/// # Examples
183183
///
@@ -208,7 +208,7 @@ macro_rules! __cstr_macro_impl {
208208
() => {
209209
__cstr_macro_impl!("")
210210
};
211-
($s:literal) => {{
211+
($s:expr) => {{
212212
const BYTES: &[u8] = $crate::ffi::__cstr_macro_impl_as_bytes($s);
213213
const BYTES_WITH_NUL: [u8; { BYTES.len() + 1 }] =
214214
$crate::ffi::__cstr_macro_impl_to_bytes_with_nul(BYTES);

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@
314314
#![feature(maybe_uninit_uninit_array)]
315315
#![feature(const_maybe_uninit_uninit_array)]
316316
#![feature(const_waker)]
317+
#![feature(cstr_macro)]
317318
//
318319
// Library features (alloc):
319320
#![feature(alloc_layout_extra)]

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(missing_docs, nonstandard_style)]
22

3-
use crate::ffi::CStr;
3+
use crate::ffi::cstr;
44
use crate::io::ErrorKind;
55

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

8181
unsafe fn sanitize_standard_fds() {

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![cfg_attr(test, allow(dead_code))]
55
#![unstable(issue = "none", feature = "windows_c")]
66

7-
use crate::ffi::CStr;
7+
use crate::ffi::{cstr, CStr};
88
use crate::mem;
99
use crate::os::raw::{c_char, c_int, c_long, c_longlong, c_uint, c_ulong, c_ushort};
1010
use crate::os::windows::io::{BorrowedHandle, HandleOrInvalid, HandleOrNull};
@@ -1245,7 +1245,7 @@ extern "system" {
12451245
// Functions that aren't available on every version of Windows that we support,
12461246
// but we still use them and just provide some form of a fallback implementation.
12471247
compat_fn_with_fallback! {
1248-
pub static KERNEL32: &CStr = ansi_str!("kernel32");
1248+
pub static KERNEL32: &CStr = cstr!("kernel32");
12491249

12501250
// >= Win10 1607
12511251
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
@@ -1280,7 +1280,7 @@ compat_fn_optional! {
12801280
}
12811281

12821282
compat_fn_with_fallback! {
1283-
pub static NTDLL: &CStr = ansi_str!("ntdll");
1283+
pub static NTDLL: &CStr = cstr!("ntdll");
12841284

12851285
pub fn NtCreateFile(
12861286
FileHandle: *mut HANDLE,

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

+5-37
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! function is called. In the worst case, multiple threads may all end up
2020
//! importing the same function unnecessarily.
2121
22-
use crate::ffi::{c_void, CStr};
22+
use crate::ffi::{c_void, cstr, CStr};
2323
use crate::ptr::NonNull;
2424
use crate::sync::atomic::Ordering;
2525
use crate::sys::c;
@@ -67,38 +67,6 @@ unsafe extern "C" fn init() {
6767
load_synch_functions();
6868
}
6969

70-
/// Helper macro for creating CStrs from literals and symbol names.
71-
macro_rules! ansi_str {
72-
(sym $ident:ident) => {{
73-
#[allow(unused_unsafe)]
74-
crate::sys::compat::const_cstr_from_bytes(concat!(stringify!($ident), "\0").as_bytes())
75-
}};
76-
($lit:literal) => {{ crate::sys::compat::const_cstr_from_bytes(concat!($lit, "\0").as_bytes()) }};
77-
}
78-
79-
/// Creates a C string wrapper from a byte slice, in a constant context.
80-
///
81-
/// This is a utility function used by the [`ansi_str`] macro.
82-
///
83-
/// # Panics
84-
///
85-
/// Panics if the slice is not null terminated or contains nulls, except as the last item
86-
pub(crate) const fn const_cstr_from_bytes(bytes: &'static [u8]) -> &'static CStr {
87-
if !matches!(bytes.last(), Some(&0)) {
88-
panic!("A CStr must be null terminated");
89-
}
90-
let mut i = 0;
91-
// At this point `len()` is at least 1.
92-
while i < bytes.len() - 1 {
93-
if bytes[i] == 0 {
94-
panic!("A CStr must not have interior nulls")
95-
}
96-
i += 1;
97-
}
98-
// SAFETY: The safety is ensured by the above checks.
99-
unsafe { crate::ffi::CStr::from_bytes_with_nul_unchecked(bytes) }
100-
}
101-
10270
/// Represents a loaded module.
10371
///
10472
/// Note that the modules std depends on must not be unloaded.
@@ -161,7 +129,7 @@ macro_rules! compat_fn_with_fallback {
161129

162130
fn load_from_module(module: Option<Module>) -> F {
163131
unsafe {
164-
static SYMBOL_NAME: &CStr = ansi_str!(sym $symbol);
132+
static SYMBOL_NAME: &CStr = cstr!(stringify!($symbol));
165133
if let Some(f) = module.and_then(|m| m.proc_address(SYMBOL_NAME)) {
166134
PTR.store(f.as_ptr(), Ordering::Relaxed);
167135
mem::transmute(f)
@@ -224,9 +192,9 @@ macro_rules! compat_fn_optional {
224192
/// Load all needed functions from "api-ms-win-core-synch-l1-2-0".
225193
pub(super) fn load_synch_functions() {
226194
fn try_load() -> Option<()> {
227-
const MODULE_NAME: &CStr = ansi_str!("api-ms-win-core-synch-l1-2-0");
228-
const WAIT_ON_ADDRESS: &CStr = ansi_str!("WaitOnAddress");
229-
const WAKE_BY_ADDRESS_SINGLE: &CStr = ansi_str!("WakeByAddressSingle");
195+
const MODULE_NAME: &CStr = cstr!("api-ms-win-core-synch-l1-2-0");
196+
const WAIT_ON_ADDRESS: &CStr = cstr!("WaitOnAddress");
197+
const WAKE_BY_ADDRESS_SINGLE: &CStr = cstr!("WakeByAddressSingle");
230198

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

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

+2-2
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::{cstr, OsStr, OsString};
44
use crate::io::ErrorKind;
55
use crate::mem::MaybeUninit;
66
use crate::os::windows::ffi::{OsStrExt, OsStringExt};
@@ -53,7 +53,7 @@ pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {
5353

5454
// Normally, `thread::spawn` will call `Thread::set_name` but since this thread already
5555
// exists, we have to call it ourselves.
56-
thread::Thread::set_name(&CStr::from_bytes_with_nul_unchecked(b"main\0"));
56+
thread::Thread::set_name(cstr!("main"));
5757
}
5858

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

src/tools/tidy/src/deps.rs

-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ const PERMITTED_RUSTC_DEPENDENCIES: &[&str] = &[
105105
"crossbeam-epoch",
106106
"crossbeam-utils",
107107
"crypto-common",
108-
"cstr",
109108
"datafrog",
110109
"difference",
111110
"digest",

0 commit comments

Comments
 (0)