Skip to content

Commit adec617

Browse files
committed
added darwin_objc incorrect usage tests
1 parent 5807880 commit adec617

File tree

6 files changed

+187
-0
lines changed

6 files changed

+187
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Test that `objc::class!` and `objc::selector!` only take string literals.
2+
3+
//@ edition: 2024
4+
//@ only-apple
5+
6+
#![feature(darwin_objc)]
7+
8+
use std::os::darwin::objc;
9+
10+
pub fn main() {
11+
let s = "NSObject";
12+
objc::class!(s);
13+
//~^ ERROR attribute value must be a literal
14+
15+
objc::class!(NSObject);
16+
//~^ ERROR attribute value must be a literal
17+
18+
objc::class!(123);
19+
//~^ ERROR malformed `rustc_objc_class` attribute input [E0539]
20+
21+
let s = "alloc";
22+
objc::selector!(s);
23+
//~^ ERROR attribute value must be a literal
24+
25+
objc::selector!(alloc);
26+
//~^ ERROR attribute value must be a literal
27+
28+
objc::selector!(123);
29+
//~^ ERROR malformed `rustc_objc_selector` attribute input [E0539]
30+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error: attribute value must be a literal
2+
--> $DIR/darwin-objc-bad-arg.rs:12:18
3+
|
4+
LL | objc::class!(s);
5+
| ^
6+
7+
error: attribute value must be a literal
8+
--> $DIR/darwin-objc-bad-arg.rs:15:18
9+
|
10+
LL | objc::class!(NSObject);
11+
| ^^^^^^^^
12+
13+
WARN rustc_errors::emitter Invalid span $SRC_DIR/core/src/os/darwin/objc.rs:LL:COL (#6), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/core/src/os/darwin/objc.rs" }) }
14+
error[E0539]: malformed `rustc_objc_class` attribute input
15+
--> $DIR/darwin-objc-bad-arg.rs:18:5
16+
|
17+
LL | objc::class!(123);
18+
| ^^^^^^^^^^^^^---^
19+
| | |
20+
| | expected a string literal here
21+
| help: must be of the form: `#[rustc_objc_class = "ClassName"]`
22+
|
23+
= note: this error originates in the macro `objc::class` (in Nightly builds, run with -Z macro-backtrace for more info)
24+
25+
error: attribute value must be a literal
26+
--> $DIR/darwin-objc-bad-arg.rs:22:21
27+
|
28+
LL | objc::selector!(s);
29+
| ^
30+
31+
error: attribute value must be a literal
32+
--> $DIR/darwin-objc-bad-arg.rs:25:21
33+
|
34+
LL | objc::selector!(alloc);
35+
| ^^^^^
36+
37+
WARN rustc_errors::emitter Invalid span $SRC_DIR/core/src/os/darwin/objc.rs:LL:COL (#9), error=SourceNotAvailable { filename: Real(Remapped { local_path: None, virtual_name: "$SRC_DIR/core/src/os/darwin/objc.rs" }) }
38+
error[E0539]: malformed `rustc_objc_selector` attribute input
39+
--> $DIR/darwin-objc-bad-arg.rs:28:5
40+
|
41+
LL | objc::selector!(123);
42+
| ^^^^^^^^^^^^^^^^---^
43+
| | |
44+
| | expected a string literal here
45+
| help: must be of the form: `#[rustc_objc_selector = "methodName"]`
46+
|
47+
= note: this error originates in the macro `objc::selector` (in Nightly builds, run with -Z macro-backtrace for more info)
48+
49+
error: aborting due to 6 previous errors
50+
51+
For more information about this error, try `rustc --explain E0539`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Test that `objc::class!` and `objc::selector!` aren't `const` expressions.
2+
// The system gives them their final values at dynamic load time.
3+
4+
//@ edition: 2024
5+
//@ only-apple
6+
7+
#![feature(darwin_objc)]
8+
9+
use std::os::darwin::objc;
10+
11+
pub const CLASS: objc::Class = objc::class!("NSObject");
12+
//~^ ERROR cannot access extern static `CLASS::VAL` [E0080]
13+
14+
pub const SELECTOR: objc::SEL = objc::selector!("alloc");
15+
//~^ ERROR cannot access extern static `SELECTOR::VAL` [E0080]
16+
17+
pub fn main() {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0080]: cannot access extern static `CLASS::VAL`
2+
--> $DIR/darwin-objc-bad-const.rs:11:32
3+
|
4+
LL | pub const CLASS: objc::Class = objc::class!("NSObject");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `CLASS` failed here
6+
|
7+
= note: this error originates in the macro `objc::class` (in Nightly builds, run with -Z macro-backtrace for more info)
8+
9+
error[E0080]: cannot access extern static `SELECTOR::VAL`
10+
--> $DIR/darwin-objc-bad-const.rs:14:33
11+
|
12+
LL | pub const SELECTOR: objc::SEL = objc::selector!("alloc");
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `SELECTOR` failed here
14+
|
15+
= note: this error originates in the macro `objc::selector` (in Nightly builds, run with -Z macro-backtrace for more info)
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0080`.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Test that `objc::class!` and `objc::selector!` can't be returned by reference.
2+
// A single instance may have multiple addresses (e.g. across dylib boundaries).
3+
4+
//@ edition: 2024
5+
//@ only-apple
6+
7+
#![feature(darwin_objc)]
8+
9+
use std::os::darwin::objc;
10+
11+
pub fn class_ref<'a>() -> &'a objc::Class {
12+
&objc::class!("NSObject")
13+
//~^ ERROR cannot return reference to temporary value [E0515]
14+
}
15+
16+
pub fn class_ref_static() -> &'static objc::Class {
17+
&objc::class!("NSObject")
18+
//~^ ERROR cannot return reference to temporary value [E0515]
19+
}
20+
21+
pub fn selector_ref<'a>() -> &'a objc::SEL {
22+
&objc::selector!("alloc")
23+
//~^ ERROR cannot return reference to temporary value [E0515]
24+
}
25+
26+
pub fn selector_ref_static() -> &'static objc::SEL {
27+
&objc::selector!("alloc")
28+
//~^ ERROR cannot return reference to temporary value [E0515]
29+
}
30+
31+
pub fn main() {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
error[E0515]: cannot return reference to temporary value
2+
--> $DIR/darwin-objc-bad-ref.rs:12:5
3+
|
4+
LL | &objc::class!("NSObject")
5+
| ^------------------------
6+
| ||
7+
| |temporary value created here
8+
| returns a reference to data owned by the current function
9+
10+
error[E0515]: cannot return reference to temporary value
11+
--> $DIR/darwin-objc-bad-ref.rs:17:5
12+
|
13+
LL | &objc::class!("NSObject")
14+
| ^------------------------
15+
| ||
16+
| |temporary value created here
17+
| returns a reference to data owned by the current function
18+
19+
error[E0515]: cannot return reference to temporary value
20+
--> $DIR/darwin-objc-bad-ref.rs:22:5
21+
|
22+
LL | &objc::selector!("alloc")
23+
| ^------------------------
24+
| ||
25+
| |temporary value created here
26+
| returns a reference to data owned by the current function
27+
28+
error[E0515]: cannot return reference to temporary value
29+
--> $DIR/darwin-objc-bad-ref.rs:27:5
30+
|
31+
LL | &objc::selector!("alloc")
32+
| ^------------------------
33+
| ||
34+
| |temporary value created here
35+
| returns a reference to data owned by the current function
36+
37+
error: aborting due to 4 previous errors
38+
39+
For more information about this error, try `rustc --explain E0515`.

0 commit comments

Comments
 (0)