Skip to content

Commit 302d730

Browse files
committed
add some run-pass tests for darwin-objc
1 parent a269980 commit 302d730

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

tests/ui/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ Tests for FFI with C varargs (`va_list`).
350350

351351
Tests for detection and handling of cyclic trait dependencies.
352352

353+
## `tests/ui/darwin-objc/`: Darwin Objective-C
354+
355+
Tests exercising `#![feature(darwin_objc)]`.
356+
357+
See [Tracking Issue for `darwin_objc` #145496](https://github.com/rust-lang/rust/issues/145496).
358+
353359
## `tests/ui/dataflow_const_prop/`
354360

355361
Contains a single regression test for const prop in `SwitchInt` pass crashing when `ptr2int` transmute is involved.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Call `[NSObject class]` using `objc::class!` and `objc::selector!`.
2+
3+
//@ edition: 2024
4+
//@ only-apple
5+
//@ run-pass
6+
7+
#![feature(darwin_objc)]
8+
9+
use std::mem::transmute;
10+
use std::os::darwin::objc;
11+
12+
#[link(name = "Foundation", kind = "framework")]
13+
unsafe extern "C" {}
14+
15+
#[link(name = "objc", kind = "dylib")]
16+
unsafe extern "C" {
17+
unsafe fn objc_msgSend();
18+
}
19+
20+
fn main() {
21+
let msg_send_fn = unsafe {
22+
transmute::<
23+
unsafe extern "C" fn(),
24+
unsafe extern "C" fn(objc::Class, objc::SEL) -> objc::Class,
25+
>(objc_msgSend)
26+
};
27+
let static_sel = objc::selector!("class");
28+
let static_class = objc::class!("NSObject");
29+
let runtime_class = unsafe { msg_send_fn(static_class, static_sel) };
30+
assert_eq!(static_class, runtime_class);
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Test that `objc::class!` returns the same thing as `objc_lookUpClass`.
2+
3+
//@ edition: 2024
4+
//@ only-apple
5+
//@ run-pass
6+
7+
#![feature(darwin_objc)]
8+
9+
use std::ffi::c_char;
10+
use std::os::darwin::objc;
11+
12+
#[link(name = "Foundation", kind = "framework")]
13+
unsafe extern "C" {}
14+
15+
#[link(name = "objc")]
16+
unsafe extern "C" {
17+
fn objc_lookUpClass(methname: *const c_char) -> objc::Class;
18+
}
19+
20+
fn get_object_class() -> objc::Class {
21+
objc::class!("NSObject")
22+
}
23+
24+
fn lookup_object_class() -> objc::Class {
25+
unsafe { objc_lookUpClass(c"NSObject".as_ptr()) }
26+
}
27+
28+
fn get_string_class() -> objc::Class {
29+
objc::class!("NSString")
30+
}
31+
32+
fn lookup_string_class() -> objc::Class {
33+
unsafe { objc_lookUpClass(c"NSString".as_ptr()) }
34+
}
35+
36+
fn main() {
37+
assert_eq!(get_object_class(), lookup_object_class());
38+
assert_eq!(get_string_class(), lookup_string_class());
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Test that `objc::selector!` returns the same thing as `sel_registerName`.
2+
3+
//@ edition: 2024
4+
//@ only-apple
5+
//@ run-pass
6+
7+
#![feature(darwin_objc)]
8+
9+
use std::ffi::c_char;
10+
use std::os::darwin::objc;
11+
12+
#[link(name = "objc")]
13+
unsafe extern "C" {
14+
fn sel_registerName(methname: *const c_char) -> objc::SEL;
15+
}
16+
17+
fn get_alloc_selector() -> objc::SEL {
18+
objc::selector!("alloc")
19+
}
20+
21+
fn register_alloc_selector() -> objc::SEL {
22+
unsafe { sel_registerName(c"alloc".as_ptr()) }
23+
}
24+
25+
fn get_init_selector() -> objc::SEL {
26+
objc::selector!("initWithCString:encoding:")
27+
}
28+
29+
fn register_init_selector() -> objc::SEL {
30+
unsafe { sel_registerName(c"initWithCString:encoding:".as_ptr()) }
31+
}
32+
33+
fn main() {
34+
assert_eq!(get_alloc_selector(), register_alloc_selector());
35+
assert_eq!(get_init_selector(), register_init_selector());
36+
}

0 commit comments

Comments
 (0)