Skip to content

Commit

Permalink
Rollup merge of #113334 - fmease:revert-lexing-c-str-lits, r=compiler…
Browse files Browse the repository at this point in the history
…-errors

Revert the lexing of `c"…"` string literals

Fixes \[after beta-backport\] #113235.
Further progress is tracked in #113333.

This PR *manually* reverts parts of #108801 (since a git-revert would've been too coarse-grained & messy)
and git-reverts #111647.

CC `@fee1-dead` (#108801) `@klensy` (#111647)
r? `@compiler-errors`

`@rustbot` label F-c_str_literals beta-nominated
  • Loading branch information
fee1-dead authored Jul 6, 2023
2 parents 19faeaa + 776d882 commit b8dab3e
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 26 deletions.
1 change: 0 additions & 1 deletion std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@
#![feature(allocator_internals)]
#![feature(allow_internal_unsafe)]
#![feature(allow_internal_unstable)]
#![feature(c_str_literals)]
#![feature(c_unwind)]
#![feature(cfg_target_thread_local)]
#![feature(concat_idents)]
Expand Down
16 changes: 7 additions & 9 deletions std/src/sys/unix/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,13 @@ mod imp {
let mut res = Vec::new();

unsafe {
let process_info_sel =
sel_registerName(c"processInfo".as_ptr() as *const libc::c_uchar);
let arguments_sel = sel_registerName(c"arguments".as_ptr() as *const libc::c_uchar);
let utf8_sel = sel_registerName(c"UTF8String".as_ptr() as *const libc::c_uchar);
let count_sel = sel_registerName(c"count".as_ptr() as *const libc::c_uchar);
let object_at_sel =
sel_registerName(c"objectAtIndex:".as_ptr() as *const libc::c_uchar);

let klass = objc_getClass(c"NSProcessInfo".as_ptr() as *const libc::c_uchar);
let process_info_sel = sel_registerName("processInfo\0".as_ptr());
let arguments_sel = sel_registerName("arguments\0".as_ptr());
let utf8_sel = sel_registerName("UTF8String\0".as_ptr());
let count_sel = sel_registerName("count\0".as_ptr());
let object_at_sel = sel_registerName("objectAtIndex:\0".as_ptr());

let klass = objc_getClass("NSProcessInfo\0".as_ptr());
let info = objc_msgSend(klass, process_info_sel);
let args = objc_msgSend(info, arguments_sel);

Expand Down
2 changes: 1 addition & 1 deletion std/src/sys/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ impl File {
cfg_has_statx! {
if let Some(ret) = unsafe { try_statx(
fd,
c"".as_ptr() as *const c_char,
b"\0" as *const _ as *const c_char,
libc::AT_EMPTY_PATH | libc::AT_STATX_SYNC_AS_STAT,
libc::STATX_ALL,
) } {
Expand Down
7 changes: 4 additions & 3 deletions std/src/sys/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(missing_docs, nonstandard_style)]

use crate::ffi::CStr;
use crate::io::ErrorKind;

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

unsafe fn sanitize_standard_fds() {
Expand Down Expand Up @@ -121,7 +122,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
if pfd.revents & libc::POLLNVAL == 0 {
continue;
}
if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
// If the stream is closed but we failed to reopen it, abort the
// process. Otherwise we wouldn't preserve the safety of
// operations on the corresponding Rust object Stdin, Stdout, or
Expand Down Expand Up @@ -151,7 +152,7 @@ pub unsafe fn init(argc: isize, argv: *const *const u8, sigpipe: u8) {
use libc::open64;
for fd in 0..3 {
if libc::fcntl(fd, libc::F_GETFD) == -1 && errno() == libc::EBADF {
if open64(c"/dev/null".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
if open64("/dev/null\0".as_ptr().cast(), libc::O_RDWR, 0) == -1 {
// If the stream is closed but we failed to reopen it, abort the
// process. Otherwise we wouldn't preserve the safety of
// operations on the corresponding Rust object Stdin, Stdout, or
Expand Down
9 changes: 5 additions & 4 deletions std/src/sys/unix/process/process_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ cfg_if::cfg_if! {
if #[cfg(target_os = "fuchsia")] {
// fuchsia doesn't have /dev/null
} else if #[cfg(target_os = "redox")] {
const DEV_NULL: &CStr = c"null:";
const DEV_NULL: &str = "null:\0";
} else if #[cfg(target_os = "vxworks")] {
const DEV_NULL: &CStr = c"/null";
const DEV_NULL: &str = "/null\0";
} else {
const DEV_NULL: &CStr = c"/dev/null";
const DEV_NULL: &str = "/dev/null\0";
}
}

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

Expand Down
3 changes: 2 additions & 1 deletion std/src/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,10 @@ impl Thread {
#[cfg(target_os = "netbsd")]
pub fn set_name(name: &CStr) {
unsafe {
let cname = CStr::from_bytes_with_nul_unchecked(b"%s\0".as_slice());
let res = libc::pthread_setname_np(
libc::pthread_self(),
c"%s".as_ptr(),
cname.as_ptr(),
name.as_ptr() as *mut libc::c_void,
);
debug_assert_eq!(res, 0);
Expand Down
4 changes: 2 additions & 2 deletions std/src/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ pub unsafe fn NtWriteFile(
// Functions that aren't available on every version of Windows that we support,
// but we still use them and just provide some form of a fallback implementation.
compat_fn_with_fallback! {
pub static KERNEL32: &CStr = c"kernel32";
pub static KERNEL32: &CStr = ansi_str!("kernel32");

// >= Win10 1607
// https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setthreaddescription
Expand Down Expand Up @@ -354,7 +354,7 @@ compat_fn_optional! {
}

compat_fn_with_fallback! {
pub static NTDLL: &CStr = c"ntdll";
pub static NTDLL: &CStr = ansi_str!("ntdll");

pub fn NtCreateKeyedEvent(
KeyedEventHandle: LPHANDLE,
Expand Down
6 changes: 3 additions & 3 deletions std/src/sys/windows/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ macro_rules! compat_fn_optional {
/// Load all needed functions from "api-ms-win-core-synch-l1-2-0".
pub(super) fn load_synch_functions() {
fn try_load() -> Option<()> {
const MODULE_NAME: &CStr = c"api-ms-win-core-synch-l1-2-0";
const WAIT_ON_ADDRESS: &CStr = c"WaitOnAddress";
const WAKE_BY_ADDRESS_SINGLE: &CStr = c"WakeByAddressSingle";
const MODULE_NAME: &CStr = ansi_str!("api-ms-win-core-synch-l1-2-0");
const WAIT_ON_ADDRESS: &CStr = ansi_str!("WaitOnAddress");
const WAKE_BY_ADDRESS_SINGLE: &CStr = ansi_str!("WakeByAddressSingle");

// Try loading the library and all the required functions.
// If any step fails, then they all fail.
Expand Down
4 changes: 2 additions & 2 deletions std/src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(missing_docs, nonstandard_style)]

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

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

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

0 comments on commit b8dab3e

Please sign in to comment.