about summary refs log tree commit diff
path: root/library/std
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-01-22 18:22:32 +0000
committerbors <bors@rust-lang.org>2024-01-22 18:22:32 +0000
commitd5fd0997291ca0135401a39dff25c8a9c13b8961 (patch)
tree3fd6447ffcb2d9dc5d5aad5fc7c921ae173a5476 /library/std
parent021861aea8de20c76c7411eb8ada7e8235e3d9b5 (diff)
parent0b0f0be4198de16f3a23f05e824b3a054dda1355 (diff)
downloadrust-d5fd0997291ca0135401a39dff25c8a9c13b8961.tar.gz
rust-d5fd0997291ca0135401a39dff25c8a9c13b8961.zip
Auto merge of #120242 - matthiaskrgr:rollup-a93yj3i, r=matthiaskrgr
Rollup of 10 pull requests

Successful merges:

 - #117910 (Refactor uses of `objc_msgSend` to no longer have clashing definitions)
 - #118639 (Undeprecate lint `unstable_features` and make use of it in the compiler)
 - #119801 (Fix deallocation with wrong allocator in (A)Rc::from_box_in)
 - #120058 (bootstrap: improvements for compiler builds)
 - #120059 (Make generic const type mismatches not hide trait impls from the trait solver)
 - #120097 (Report unreachable subpatterns consistently)
 - #120137 (Validate AggregateKind types in MIR)
 - #120164 (`maybe_lint_impl_trait`: separate `is_downgradable` from `is_object_safe`)
 - #120181 (Allow any `const` expression blocks in `thread_local!`)
 - #120218 (rustfmt: Check that a token can begin a nonterminal kind before parsing it as a macro arg)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'library/std')
-rw-r--r--library/std/src/sys/pal/unix/args.rs81
-rw-r--r--library/std/src/thread/local.rs4
-rw-r--r--library/std/tests/thread.rs22
3 files changed, 68 insertions, 39 deletions
diff --git a/library/std/src/sys/pal/unix/args.rs b/library/std/src/sys/pal/unix/args.rs
index 9f7dcc0416e..78e82d9c194 100644
--- a/library/std/src/sys/pal/unix/args.rs
+++ b/library/std/src/sys/pal/unix/args.rs
@@ -201,9 +201,9 @@ mod imp {
 
     // As _NSGetArgc and _NSGetArgv aren't mentioned in iOS docs
     // and use underscores in their names - they're most probably
-    // are considered private and therefore should be avoided
-    // Here is another way to get arguments using Objective C
-    // runtime
+    // are considered private and therefore should be avoided.
+    // Here is another way to get arguments using the Objective-C
+    // runtime.
     //
     // In general it looks like:
     // res = Vec::new()
@@ -213,53 +213,60 @@ mod imp {
     // res
     #[cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos"))]
     pub fn args() -> Args {
-        use crate::ffi::OsString;
+        use crate::ffi::{c_char, c_void, OsString};
         use crate::mem;
         use crate::str;
 
-        extern "C" {
-            fn sel_registerName(name: *const libc::c_uchar) -> Sel;
-            fn objc_getClass(class_name: *const libc::c_uchar) -> NsId;
-        }
+        type Sel = *const c_void;
+        type NsId = *const c_void;
+        type NSUInteger = usize;
 
-        #[cfg(target_arch = "aarch64")]
         extern "C" {
-            fn objc_msgSend(obj: NsId, sel: Sel) -> NsId;
-            #[allow(clashing_extern_declarations)]
-            #[link_name = "objc_msgSend"]
-            fn objc_msgSend_ul(obj: NsId, sel: Sel, i: libc::c_ulong) -> NsId;
-        }
+            fn sel_registerName(name: *const c_char) -> Sel;
+            fn objc_getClass(class_name: *const c_char) -> NsId;
 
-        #[cfg(not(target_arch = "aarch64"))]
-        extern "C" {
-            fn objc_msgSend(obj: NsId, sel: Sel, ...) -> NsId;
-            #[allow(clashing_extern_declarations)]
-            #[link_name = "objc_msgSend"]
-            fn objc_msgSend_ul(obj: NsId, sel: Sel, ...) -> NsId;
+            // This must be transmuted to an appropriate function pointer type before being called.
+            fn objc_msgSend();
         }
 
-        type Sel = *const libc::c_void;
-        type NsId = *const libc::c_void;
+        const MSG_SEND_PTR: unsafe extern "C" fn() = objc_msgSend;
+        const MSG_SEND_NO_ARGUMENTS_RETURN_PTR: unsafe extern "C" fn(NsId, Sel) -> *const c_void =
+            unsafe { mem::transmute(MSG_SEND_PTR) };
+        const MSG_SEND_NO_ARGUMENTS_RETURN_NSUINTEGER: unsafe extern "C" fn(
+            NsId,
+            Sel,
+        ) -> NSUInteger = unsafe { mem::transmute(MSG_SEND_PTR) };
+        const MSG_SEND_NSINTEGER_ARGUMENT_RETURN_PTR: unsafe extern "C" fn(
+            NsId,
+            Sel,
+            NSUInteger,
+        )
+            -> *const c_void = unsafe { mem::transmute(MSG_SEND_PTR) };
 
         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 info = objc_msgSend(klass, process_info_sel);
-            let args = objc_msgSend(info, arguments_sel);
-
-            let cnt: usize = mem::transmute(objc_msgSend(args, count_sel));
+            let process_info_sel = sel_registerName(c"processInfo".as_ptr());
+            let arguments_sel = sel_registerName(c"arguments".as_ptr());
+            let count_sel = sel_registerName(c"count".as_ptr());
+            let object_at_index_sel = sel_registerName(c"objectAtIndex:".as_ptr());
+            let utf8string_sel = sel_registerName(c"UTF8String".as_ptr());
+
+            let klass = objc_getClass(c"NSProcessInfo".as_ptr());
+            // `+[NSProcessInfo processInfo]` returns an object with +0 retain count, so no need to manually `retain/release`.
+            let info = MSG_SEND_NO_ARGUMENTS_RETURN_PTR(klass, process_info_sel);
+
+            // `-[NSProcessInfo arguments]` returns an object with +0 retain count, so no need to manually `retain/release`.
+            let args = MSG_SEND_NO_ARGUMENTS_RETURN_PTR(info, arguments_sel);
+
+            let cnt = MSG_SEND_NO_ARGUMENTS_RETURN_NSUINTEGER(args, count_sel);
             for i in 0..cnt {
-                let tmp = objc_msgSend_ul(args, object_at_sel, i as libc::c_ulong);
-                let utf_c_str: *const libc::c_char = mem::transmute(objc_msgSend(tmp, utf8_sel));
+                // `-[NSArray objectAtIndex:]` returns an object whose lifetime is tied to the array, so no need to manually `retain/release`.
+                let ns_string =
+                    MSG_SEND_NSINTEGER_ARGUMENT_RETURN_PTR(args, object_at_index_sel, i);
+                // The lifetime of this pointer is tied to the NSString, as well as the current autorelease pool, which is why we heap-allocate the string below.
+                let utf_c_str: *const c_char =
+                    MSG_SEND_NO_ARGUMENTS_RETURN_PTR(ns_string, utf8string_sel).cast();
                 let bytes = CStr::from_ptr(utf_c_str).to_bytes();
                 res.push(OsString::from(str::from_utf8(bytes).unwrap()))
             }
diff --git a/library/std/src/thread/local.rs b/library/std/src/thread/local.rs
index 338567777f7..2e13f433dcf 100644
--- a/library/std/src/thread/local.rs
+++ b/library/std/src/thread/local.rs
@@ -186,12 +186,12 @@ macro_rules! thread_local {
     // empty (base case for the recursion)
     () => {};
 
-    ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => (
+    ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const $init:block; $($rest:tt)*) => (
         $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
         $crate::thread_local!($($rest)*);
     );
 
-    ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }) => (
+    ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const $init:block) => (
         $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
     );
 
diff --git a/library/std/tests/thread.rs b/library/std/tests/thread.rs
index 754b264c6ad..4ce81f2846e 100644
--- a/library/std/tests/thread.rs
+++ b/library/std/tests/thread.rs
@@ -1,3 +1,4 @@
+use std::cell::{Cell, RefCell};
 use std::sync::{Arc, Mutex};
 use std::thread;
 use std::time::Duration;
@@ -14,3 +15,24 @@ fn sleep() {
     thread::sleep(Duration::from_millis(100));
     assert_eq!(*finished.lock().unwrap(), false);
 }
+
+#[test]
+fn thread_local_containing_const_statements() {
+    // This exercises the `const $init:block` cases of the thread_local macro.
+    // Despite overlapping with expression syntax, the `const { ... }` is not
+    // parsed as `$init:expr`.
+    thread_local! {
+        static CELL: Cell<u32> = const {
+            let value = 1;
+            Cell::new(value)
+        };
+
+        static REFCELL: RefCell<u32> = const {
+            let value = 1;
+            RefCell::new(value)
+        };
+    }
+
+    assert_eq!(CELL.get(), 1);
+    assert_eq!(REFCELL.take(), 1);
+}