Skip to content

Commit 9365c91

Browse files
committed
Auto merge of rust-lang#120242 - matthiaskrgr:rollup-a93yj3i, r=matthiaskrgr
Rollup of 10 pull requests Successful merges: - rust-lang#117910 (Refactor uses of `objc_msgSend` to no longer have clashing definitions) - rust-lang#118639 (Undeprecate lint `unstable_features` and make use of it in the compiler) - rust-lang#119801 (Fix deallocation with wrong allocator in (A)Rc::from_box_in) - rust-lang#120058 (bootstrap: improvements for compiler builds) - rust-lang#120059 (Make generic const type mismatches not hide trait impls from the trait solver) - rust-lang#120097 (Report unreachable subpatterns consistently) - rust-lang#120137 (Validate AggregateKind types in MIR) - rust-lang#120164 (`maybe_lint_impl_trait`: separate `is_downgradable` from `is_object_safe`) - rust-lang#120181 (Allow any `const` expression blocks in `thread_local!`) - rust-lang#120218 (rustfmt: Check that a token can begin a nonterminal kind before parsing it as a macro arg) r? `@ghost` `@rustbot` modify labels: rollup
2 parents e9b1f97 + c02d5b3 commit 9365c91

File tree

5 files changed

+70
-41
lines changed

5 files changed

+70
-41
lines changed

alloc/src/rc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,7 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
19241924

19251925
// Free the allocation without dropping its contents
19261926
let (bptr, alloc) = Box::into_raw_with_allocator(src);
1927-
let src = Box::from_raw(bptr as *mut mem::ManuallyDrop<T>);
1927+
let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, alloc.by_ref());
19281928
drop(src);
19291929

19301930
Self::from_ptr_in(ptr, alloc)

alloc/src/sync.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
18691869

18701870
// Free the allocation without dropping its contents
18711871
let (bptr, alloc) = Box::into_raw_with_allocator(src);
1872-
let src = Box::from_raw(bptr as *mut mem::ManuallyDrop<T>);
1872+
let src = Box::from_raw_in(bptr as *mut mem::ManuallyDrop<T>, alloc.by_ref());
18731873
drop(src);
18741874

18751875
Self::from_ptr_in(ptr, alloc)

std/src/sys/pal/unix/args.rs

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ mod imp {
201201

202202
// As _NSGetArgc and _NSGetArgv aren't mentioned in iOS docs
203203
// and use underscores in their names - they're most probably
204-
// are considered private and therefore should be avoided
205-
// Here is another way to get arguments using Objective C
206-
// runtime
204+
// are considered private and therefore should be avoided.
205+
// Here is another way to get arguments using the Objective-C
206+
// runtime.
207207
//
208208
// In general it looks like:
209209
// res = Vec::new()
@@ -213,53 +213,60 @@ mod imp {
213213
// res
214214
#[cfg(any(target_os = "ios", target_os = "tvos", target_os = "watchos"))]
215215
pub fn args() -> Args {
216-
use crate::ffi::OsString;
216+
use crate::ffi::{c_char, c_void, OsString};
217217
use crate::mem;
218218
use crate::str;
219219

220-
extern "C" {
221-
fn sel_registerName(name: *const libc::c_uchar) -> Sel;
222-
fn objc_getClass(class_name: *const libc::c_uchar) -> NsId;
223-
}
220+
type Sel = *const c_void;
221+
type NsId = *const c_void;
222+
type NSUInteger = usize;
224223

225-
#[cfg(target_arch = "aarch64")]
226224
extern "C" {
227-
fn objc_msgSend(obj: NsId, sel: Sel) -> NsId;
228-
#[allow(clashing_extern_declarations)]
229-
#[link_name = "objc_msgSend"]
230-
fn objc_msgSend_ul(obj: NsId, sel: Sel, i: libc::c_ulong) -> NsId;
231-
}
225+
fn sel_registerName(name: *const c_char) -> Sel;
226+
fn objc_getClass(class_name: *const c_char) -> NsId;
232227

233-
#[cfg(not(target_arch = "aarch64"))]
234-
extern "C" {
235-
fn objc_msgSend(obj: NsId, sel: Sel, ...) -> NsId;
236-
#[allow(clashing_extern_declarations)]
237-
#[link_name = "objc_msgSend"]
238-
fn objc_msgSend_ul(obj: NsId, sel: Sel, ...) -> NsId;
228+
// This must be transmuted to an appropriate function pointer type before being called.
229+
fn objc_msgSend();
239230
}
240231

241-
type Sel = *const libc::c_void;
242-
type NsId = *const libc::c_void;
232+
const MSG_SEND_PTR: unsafe extern "C" fn() = objc_msgSend;
233+
const MSG_SEND_NO_ARGUMENTS_RETURN_PTR: unsafe extern "C" fn(NsId, Sel) -> *const c_void =
234+
unsafe { mem::transmute(MSG_SEND_PTR) };
235+
const MSG_SEND_NO_ARGUMENTS_RETURN_NSUINTEGER: unsafe extern "C" fn(
236+
NsId,
237+
Sel,
238+
) -> NSUInteger = unsafe { mem::transmute(MSG_SEND_PTR) };
239+
const MSG_SEND_NSINTEGER_ARGUMENT_RETURN_PTR: unsafe extern "C" fn(
240+
NsId,
241+
Sel,
242+
NSUInteger,
243+
)
244+
-> *const c_void = unsafe { mem::transmute(MSG_SEND_PTR) };
243245

244246
let mut res = Vec::new();
245247

246248
unsafe {
247-
let process_info_sel =
248-
sel_registerName(c"processInfo".as_ptr() as *const libc::c_uchar);
249-
let arguments_sel = sel_registerName(c"arguments".as_ptr() as *const libc::c_uchar);
250-
let utf8_sel = sel_registerName(c"UTF8String".as_ptr() as *const libc::c_uchar);
251-
let count_sel = sel_registerName(c"count".as_ptr() as *const libc::c_uchar);
252-
let object_at_sel =
253-
sel_registerName(c"objectAtIndex:".as_ptr() as *const libc::c_uchar);
254-
255-
let klass = objc_getClass(c"NSProcessInfo".as_ptr() as *const libc::c_uchar);
256-
let info = objc_msgSend(klass, process_info_sel);
257-
let args = objc_msgSend(info, arguments_sel);
258-
259-
let cnt: usize = mem::transmute(objc_msgSend(args, count_sel));
249+
let process_info_sel = sel_registerName(c"processInfo".as_ptr());
250+
let arguments_sel = sel_registerName(c"arguments".as_ptr());
251+
let count_sel = sel_registerName(c"count".as_ptr());
252+
let object_at_index_sel = sel_registerName(c"objectAtIndex:".as_ptr());
253+
let utf8string_sel = sel_registerName(c"UTF8String".as_ptr());
254+
255+
let klass = objc_getClass(c"NSProcessInfo".as_ptr());
256+
// `+[NSProcessInfo processInfo]` returns an object with +0 retain count, so no need to manually `retain/release`.
257+
let info = MSG_SEND_NO_ARGUMENTS_RETURN_PTR(klass, process_info_sel);
258+
259+
// `-[NSProcessInfo arguments]` returns an object with +0 retain count, so no need to manually `retain/release`.
260+
let args = MSG_SEND_NO_ARGUMENTS_RETURN_PTR(info, arguments_sel);
261+
262+
let cnt = MSG_SEND_NO_ARGUMENTS_RETURN_NSUINTEGER(args, count_sel);
260263
for i in 0..cnt {
261-
let tmp = objc_msgSend_ul(args, object_at_sel, i as libc::c_ulong);
262-
let utf_c_str: *const libc::c_char = mem::transmute(objc_msgSend(tmp, utf8_sel));
264+
// `-[NSArray objectAtIndex:]` returns an object whose lifetime is tied to the array, so no need to manually `retain/release`.
265+
let ns_string =
266+
MSG_SEND_NSINTEGER_ARGUMENT_RETURN_PTR(args, object_at_index_sel, i);
267+
// 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.
268+
let utf_c_str: *const c_char =
269+
MSG_SEND_NO_ARGUMENTS_RETURN_PTR(ns_string, utf8string_sel).cast();
263270
let bytes = CStr::from_ptr(utf_c_str).to_bytes();
264271
res.push(OsString::from(str::from_utf8(bytes).unwrap()))
265272
}

std/src/thread/local.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,12 @@ macro_rules! thread_local {
186186
// empty (base case for the recursion)
187187
() => {};
188188

189-
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => (
189+
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const $init:block; $($rest:tt)*) => (
190190
$crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
191191
$crate::thread_local!($($rest)*);
192192
);
193193

194-
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }) => (
194+
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const $init:block) => (
195195
$crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
196196
);
197197

std/tests/thread.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::cell::{Cell, RefCell};
12
use std::sync::{Arc, Mutex};
23
use std::thread;
34
use std::time::Duration;
@@ -14,3 +15,24 @@ fn sleep() {
1415
thread::sleep(Duration::from_millis(100));
1516
assert_eq!(*finished.lock().unwrap(), false);
1617
}
18+
19+
#[test]
20+
fn thread_local_containing_const_statements() {
21+
// This exercises the `const $init:block` cases of the thread_local macro.
22+
// Despite overlapping with expression syntax, the `const { ... }` is not
23+
// parsed as `$init:expr`.
24+
thread_local! {
25+
static CELL: Cell<u32> = const {
26+
let value = 1;
27+
Cell::new(value)
28+
};
29+
30+
static REFCELL: RefCell<u32> = const {
31+
let value = 1;
32+
RefCell::new(value)
33+
};
34+
}
35+
36+
assert_eq!(CELL.get(), 1);
37+
assert_eq!(REFCELL.take(), 1);
38+
}

0 commit comments

Comments
 (0)