Skip to content

Commit cdea41a

Browse files
committed
Test that msg_send! consumes the receiver
1 parent 0f35f4f commit cdea41a

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

tests/ui/msg_send_mutable.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Test that `msg_send!` consumes their arguments, including the receiver.
2+
//!
3+
//! Ideally, it shouldn't be so, but that's how it works currently.
4+
use objc2::{msg_send, class};
5+
use objc2::runtime::Object;
6+
7+
fn main() {
8+
let cls = class!(NSObject);
9+
let obj: &mut Object = unsafe { msg_send![cls, new] };
10+
11+
let _: () = unsafe { msg_send![obj, selector] };
12+
// Could be solved with a reborrow
13+
let _: () = unsafe { msg_send![obj, selector] };
14+
}

tests/ui/msg_send_mutable.stderr

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0382]: use of moved value: `obj`
2+
--> ui/msg_send_mutable.rs:13:36
3+
|
4+
9 | let obj: &mut Object = unsafe { msg_send![cls, new] };
5+
| --- move occurs because `obj` has type `&mut objc2::runtime::Object`, which does not implement the `Copy` trait
6+
10 |
7+
11 | let _: () = unsafe { msg_send![obj, selector] };
8+
| ------------------------ `obj` moved due to this method call
9+
12 | // Could be solved with a reborrow
10+
13 | let _: () = unsafe { msg_send![obj, selector] };
11+
| ^^^ value used here after move
12+
|
13+
note: this function takes ownership of the receiver `self`, which moves `obj`
14+
--> $WORKSPACE/objc2/src/message/mod.rs
15+
|
16+
| unsafe fn send_message<A, R>(self, sel: Sel, args: A) -> Result<R, MessageError>
17+
| ^^^^

tests/ui/msg_send_not_encode.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
//! Test that return types that are not `Encode` are not accepted.
1+
//! Test that types that are not `Encode` are not accepted.
22
use objc2::{class, msg_send};
33

44
fn main() {
5+
let cls = class!(NSData);
56
unsafe {
6-
let cls = class!(NSData);
77
let _: Vec<u8> = msg_send![cls, new];
8+
9+
let x: Vec<u8>;
10+
let _: () = msg_send![cls, newWith: x];
811
}
912
}

tests/ui/msg_send_not_encode.stderr

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,27 @@ note: required by a bound in `send_message`
2020
| R: Encode,
2121
| ^^^^^^ required by this bound in `send_message`
2222
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)
23+
24+
error[E0277]: the trait bound `Vec<u8>: Encode` is not satisfied
25+
--> ui/msg_send_not_encode.rs:10:21
26+
|
27+
10 | let _: () = msg_send![cls, newWith: x];
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Encode` is not implemented for `Vec<u8>`
29+
|
30+
= help: the following other types implement trait `Encode`:
31+
&'a T
32+
&'a mut T
33+
()
34+
*const T
35+
*const c_void
36+
*mut T
37+
*mut c_void
38+
ManuallyDrop<T>
39+
and 142 others
40+
= note: required because of the requirements on the impl of `MessageArguments` for `(Vec<u8>,)`
41+
note: required by a bound in `send_message`
42+
--> $WORKSPACE/objc2/src/message/mod.rs
43+
|
44+
| A: MessageArguments,
45+
| ^^^^^^^^^^^^^^^^ required by this bound in `send_message`
46+
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)

0 commit comments

Comments
 (0)