Skip to content

Commit

Permalink
Test that msg_send! consumes the receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jun 6, 2022
1 parent 0f35f4f commit cdea41a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
14 changes: 14 additions & 0 deletions tests/ui/msg_send_mutable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Test that `msg_send!` consumes their arguments, including the receiver.
//!
//! Ideally, it shouldn't be so, but that's how it works currently.
use objc2::{msg_send, class};
use objc2::runtime::Object;

fn main() {
let cls = class!(NSObject);
let obj: &mut Object = unsafe { msg_send![cls, new] };

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

fn main() {
let cls = class!(NSData);
unsafe {
let cls = class!(NSData);
let _: Vec<u8> = msg_send![cls, new];

let x: Vec<u8>;
let _: () = msg_send![cls, newWith: x];
}
}
24 changes: 24 additions & 0 deletions tests/ui/msg_send_not_encode.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,27 @@ note: required by a bound in `send_message`
| R: Encode,
| ^^^^^^ required by this bound in `send_message`
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `Vec<u8>: Encode` is not satisfied
--> ui/msg_send_not_encode.rs:10:21
|
10 | let _: () = msg_send![cls, newWith: x];
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Encode` is not implemented for `Vec<u8>`
|
= help: the following other types implement trait `Encode`:
&'a T
&'a mut T
()
*const T
*const c_void
*mut T
*mut c_void
ManuallyDrop<T>
and 142 others
= note: required because of the requirements on the impl of `MessageArguments` for `(Vec<u8>,)`
note: required by a bound in `send_message`
--> $WORKSPACE/objc2/src/message/mod.rs
|
| A: MessageArguments,
| ^^^^^^^^^^^^^^^^ required by this bound in `send_message`
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit cdea41a

Please sign in to comment.