Skip to content

Commit f23c3fc

Browse files
authored
Merge pull request #35 from madsmtm/trybuild
Use `trybuild` for testing compilation failures
2 parents 141f25c + ddf483a commit f23c3fc

29 files changed

+470
-87
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ jobs:
4242
- name: Test macOS 10.15
4343
os: macos-10.15
4444
target: x86_64-apple-darwin
45-
- name: Test macOS 11
45+
- name: Test macOS 11 w. ui tests
4646
os: macos-11
4747
target: x86_64-apple-darwin
48+
args: --features tests/ui
4849
- name: Build macOS AArch64
4950
os: macos-11
5051
target: aarch64-apple-darwin
@@ -54,11 +55,12 @@ jobs:
5455
target: x86_64-apple-darwin
5556
# Oldest macOS version we support
5657
sdk: 10.7
57-
- name: Test macOS nightly
58+
- name: Test macOS nightly w. ui tests
5859
os: macos-latest
5960
target: x86_64-apple-darwin
6061
rust:
6162
toolchain: nightly
63+
args: --features tests/ui
6264
- name: Build macOS 32bit
6365
os: macos-10.15
6466
target: i686-apple-darwin

objc2-foundation/src/array.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,6 @@ unsafe impl<T: Sync + Send> Send for NSArray<T, Shared> {}
4141
unsafe impl<T: Sync> Sync for NSArray<T, Owned> {}
4242
unsafe impl<T: Send> Send for NSArray<T, Owned> {}
4343

44-
/// ```compile_fail
45-
/// use objc2::rc::Shared;
46-
/// use objc2::runtime::Object;
47-
/// use objc2_foundation::NSArray;
48-
/// fn needs_send_sync<T: Send + Sync>() {}
49-
/// needs_send_sync::<NSArray<Object, Shared>>();
50-
/// ```
51-
#[cfg(doctest)]
52-
pub struct NSArrayWithObjectNotSendSync;
53-
5444
object! {
5545
// TODO: Ensure that this deref to NSArray is safe!
5646
unsafe pub struct NSMutableArray<T, O: Ownership>: NSArray<T, O> {

objc2-foundation/src/object.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,6 @@ object! {
1010
unsafe pub struct NSObject: Object;
1111
}
1212

13-
/// ```compile_fail
14-
/// use objc2_foundation::NSObject;
15-
/// fn needs_sync<T: Sync>() {}
16-
/// needs_sync::<NSObject>();
17-
/// ```
18-
/// ```compile_fail
19-
/// use objc2_foundation::NSObject;
20-
/// fn needs_send<T: Send>() {}
21-
/// needs_send::<NSObject>();
22-
/// ```
23-
#[cfg(doctest)]
24-
pub struct NSObjectNotSendNorSync;
25-
2613
impl NSObject {
2714
unsafe_def_fn!(pub fn new -> Owned);
2815

objc2-foundation/src/string.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,6 @@ impl NSString {
4343
}
4444

4545
/// TODO
46-
///
47-
/// ```compile_fail
48-
/// # use objc2::rc::autoreleasepool;
49-
/// # use objc2_foundation::NSString;
50-
/// autoreleasepool(|pool| {
51-
/// let ns_string = NSString::new();
52-
/// let s = ns_string.as_str(pool);
53-
/// drop(ns_string);
54-
/// println!("{}", s);
55-
/// });
56-
/// ```
57-
///
58-
/// ```compile_fail
59-
/// # use objc2::rc::autoreleasepool;
60-
/// # use objc2_foundation::NSString;
61-
/// let ns_string = NSString::new();
62-
/// let s = autoreleasepool(|pool| ns_string.as_str(pool));
63-
/// ```
6446
pub fn as_str<'r, 's: 'r, 'p: 'r>(&'s self, pool: &'p AutoreleasePool) -> &'r str {
6547
// This is necessary until `auto` types stabilizes.
6648
pool.__verify_is_inner();

objc2-foundation/src/value.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,6 @@ impl<T: 'static + Copy + Encode + fmt::Display> fmt::Display for NSValue<T> {
110110
}
111111
}
112112

113-
/// ```compile_fail
114-
/// use objc2_foundation::NSValue;
115-
/// fn needs_eq<T: Eq>() {}
116-
/// needs_eq::<NSValue<f32>>();
117-
/// ```
118-
#[cfg(doctest)]
119-
pub struct NSValueFloatNotEq;
120-
121113
#[cfg(test)]
122114
mod tests {
123115
use alloc::format;

objc2/src/rc/autorelease.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,6 @@ pub struct AutoreleasePool {
2525
p: PhantomData<*mut UnsafeCell<c_void>>,
2626
}
2727

28-
/// ```
29-
/// use objc2::rc::AutoreleasePool;
30-
/// fn needs_nothing<T>() {}
31-
/// needs_nothing::<AutoreleasePool>();
32-
/// ```
33-
/// ```compile_fail
34-
/// use objc2::rc::AutoreleasePool;
35-
/// fn needs_sync<T: Sync>() {}
36-
/// needs_sync::<AutoreleasePool>();
37-
/// ```
38-
/// ```compile_fail
39-
/// use objc2::rc::AutoreleasePool;
40-
/// fn needs_send<T: Send>() {}
41-
/// needs_send::<AutoreleasePool>();
42-
/// ```
43-
#[cfg(doctest)]
44-
pub struct AutoreleasePoolNotSendNorSync;
45-
4628
#[cfg(all(debug_assertions, not(feature = "unstable_autoreleasesafe")))]
4729
thread_local! {
4830
/// We track the thread's pools to verify that object lifetimes are only

objc2/src/runtime.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -613,24 +613,6 @@ impl Object {
613613
// objc_removeAssociatedObjects
614614
}
615615

616-
/// ```
617-
/// use objc2::runtime::Object;
618-
/// fn needs_nothing<T: ?Sized>() {}
619-
/// needs_nothing::<Object>();
620-
/// ```
621-
/// ```compile_fail
622-
/// use objc2::runtime::Object;
623-
/// fn needs_sync<T: ?Sized + Sync>() {}
624-
/// needs_sync::<Object>();
625-
/// ```
626-
/// ```compile_fail
627-
/// use objc2::runtime::Object;
628-
/// fn needs_send<T: ?Sized + Send>() {}
629-
/// needs_send::<Object>();
630-
/// ```
631-
#[cfg(doctest)]
632-
pub struct ObjectNotSendNorSync;
633-
634616
unsafe impl RefEncode for Object {
635617
const ENCODING_REF: Encoding<'static> = Encoding::Object;
636618
}

tests/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,26 @@ license = "MIT"
99

1010
build = "build.rs"
1111

12+
[features]
13+
# Enable UI tests
14+
#
15+
# These are not enabled by default because trybuild doesn't pass feature flags
16+
# (like --features gnustep-1-9) to its cargo invocation, and hence they don't
17+
# always work.
18+
#
19+
# Also, they're slower than most tests.
20+
ui = ["trybuild"]
21+
1222
[dependencies]
1323
block2 = { path = "../block2" }
1424
block-sys = { path = "../block-sys" }
1525
objc-sys = { path = "../objc-sys" }
1626
objc2 = { path = "../objc2" }
1727
objc2-encode = { path = "../objc2-encode" }
28+
objc2-foundation = { path = "../objc2-foundation" }
29+
30+
# Put here instead of dev-dependencies because we want to make it optional
31+
trybuild = { version = "1.0", optional = true }
1832

1933
[build-dependencies]
2034
cc = "1.0"

tests/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,11 @@ mod tests {
140140
let block = block.copy();
141141
assert_eq!(invoke_large_struct_block(&block, data), new_data);
142142
}
143+
144+
#[test]
145+
#[cfg(feature = "ui")]
146+
fn ui() {
147+
let t = trybuild::TestCases::new();
148+
t.compile_fail("ui/*.rs");
149+
}
143150
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//! Test that AutoreleasePool is not Send and Sync, because it internally
2+
//! works with thread locals.
3+
4+
use objc2::rc::AutoreleasePool;
5+
6+
fn needs_sync<T: ?Sized + Sync>() {}
7+
fn needs_send<T: ?Sized + Send>() {}
8+
9+
fn main() {
10+
needs_sync::<AutoreleasePool>();
11+
needs_send::<AutoreleasePool>();
12+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
error[E0277]: `*mut c_void` cannot be shared between threads safely
2+
--> ui/autoreleasepool_not_send_sync.rs:10:5
3+
|
4+
10 | needs_sync::<AutoreleasePool>();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*mut c_void` cannot be shared between threads safely
6+
|
7+
= help: within `AutoreleasePool`, the trait `Sync` is not implemented for `*mut c_void`
8+
= note: required because it appears within the type `AutoreleasePool`
9+
note: required by a bound in `needs_sync`
10+
--> ui/autoreleasepool_not_send_sync.rs:6:27
11+
|
12+
6 | fn needs_sync<T: ?Sized + Sync>() {}
13+
| ^^^^ required by this bound in `needs_sync`
14+
15+
error[E0277]: `*mut UnsafeCell<c_void>` cannot be shared between threads safely
16+
--> ui/autoreleasepool_not_send_sync.rs:10:5
17+
|
18+
10 | needs_sync::<AutoreleasePool>();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*mut UnsafeCell<c_void>` cannot be shared between threads safely
20+
|
21+
= help: within `AutoreleasePool`, the trait `Sync` is not implemented for `*mut UnsafeCell<c_void>`
22+
= note: required because it appears within the type `PhantomData<*mut UnsafeCell<c_void>>`
23+
= note: required because it appears within the type `AutoreleasePool`
24+
note: required by a bound in `needs_sync`
25+
--> ui/autoreleasepool_not_send_sync.rs:6:27
26+
|
27+
6 | fn needs_sync<T: ?Sized + Sync>() {}
28+
| ^^^^ required by this bound in `needs_sync`
29+
30+
error[E0277]: `*mut c_void` cannot be sent between threads safely
31+
--> ui/autoreleasepool_not_send_sync.rs:11:5
32+
|
33+
11 | needs_send::<AutoreleasePool>();
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*mut c_void` cannot be sent between threads safely
35+
|
36+
= help: within `AutoreleasePool`, the trait `Send` is not implemented for `*mut c_void`
37+
= note: required because it appears within the type `AutoreleasePool`
38+
note: required by a bound in `needs_send`
39+
--> ui/autoreleasepool_not_send_sync.rs:7:27
40+
|
41+
7 | fn needs_send<T: ?Sized + Send>() {}
42+
| ^^^^ required by this bound in `needs_send`
43+
44+
error[E0277]: `*mut UnsafeCell<c_void>` cannot be sent between threads safely
45+
--> ui/autoreleasepool_not_send_sync.rs:11:5
46+
|
47+
11 | needs_send::<AutoreleasePool>();
48+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*mut UnsafeCell<c_void>` cannot be sent between threads safely
49+
|
50+
= help: within `AutoreleasePool`, the trait `Send` is not implemented for `*mut UnsafeCell<c_void>`
51+
= note: required because it appears within the type `PhantomData<*mut UnsafeCell<c_void>>`
52+
= note: required because it appears within the type `AutoreleasePool`
53+
note: required by a bound in `needs_send`
54+
--> ui/autoreleasepool_not_send_sync.rs:7:27
55+
|
56+
7 | fn needs_send<T: ?Sized + Send>() {}
57+
| ^^^^ required by this bound in `needs_send`

tests/ui/global_block_not_encode.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use block2::global_block;
2+
3+
global_block! {
4+
pub static BLOCK = |_b: Box<i32>| {};
5+
}
6+
7+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0277]: the trait bound `Box<i32>: objc2_encode::encode::Encode` is not satisfied
2+
--> ui/global_block_not_encode.rs:3:1
3+
|
4+
3 | / global_block! {
5+
4 | | pub static BLOCK = |_b: Box<i32>| {};
6+
5 | | }
7+
| |_^ the trait `objc2_encode::encode::Encode` is not implemented for `Box<i32>`
8+
|
9+
= note: required because of the requirements on the impl of `objc2_encode::encode::EncodeArguments` for `(Box<i32>,)`
10+
= note: required because of the requirements on the impl of `Sync` for `GlobalBlock<(Box<i32>,)>`
11+
= note: shared static variables must have a type that implements `Sync`
12+
= note: this error originates in the macro `global_block` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/ui/msg_send_no_return_type.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//! Test that forgetting to annotate the return type fails
2+
//! See https://github.com/SSheldon/rust-objc/issues/62
3+
use objc2::{class, msg_send};
4+
5+
fn main() {
6+
unsafe {
7+
let cls = class!(NSObject);
8+
msg_send![cls, new];
9+
}
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error[E0282]: type annotations needed
2+
--> ui/msg_send_no_return_type.rs:8:9
3+
|
4+
8 | msg_send![cls, new];
5+
| ^^^^^^^^^^^^^^^^^^^ consider giving `result` a type
6+
|
7+
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/ui/msg_send_not_encode.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Test that return types that are not `Encode` are not accepted.
2+
use objc2::{class, msg_send};
3+
4+
fn main() {
5+
unsafe {
6+
let cls = class!(NSData);
7+
let _: Vec<u8> = msg_send![cls, new];
8+
}
9+
}

tests/ui/msg_send_not_encode.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0277]: the trait bound `Vec<u8>: Encode` is not satisfied
2+
--> ui/msg_send_not_encode.rs:7:26
3+
|
4+
7 | let _: Vec<u8> = msg_send![cls, new];
5+
| ^^^^^^^^^^^^^^^^^^^ the trait `Encode` is not implemented for `Vec<u8>`
6+
|
7+
note: required by a bound in `send_message`
8+
--> $WORKSPACE/objc2/src/message/mod.rs
9+
|
10+
| R: Encode,
11+
| ^^^^^^ required by this bound in `send_message`
12+
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/ui/msg_send_only_message.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! Test that messages can only be sent to objects.
2+
use objc2::msg_send;
3+
4+
fn main() {
5+
unsafe { msg_send![1, new] };
6+
}

tests/ui/msg_send_only_message.stderr

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0277]: the trait bound `{integer}: MessageReceiver` is not satisfied
2+
--> ui/msg_send_only_message.rs:5:14
3+
|
4+
5 | unsafe { msg_send![1, new] };
5+
| ^^^^^^^^^^^^^^^^^ the trait `MessageReceiver` is not implemented for `{integer}`
6+
|
7+
= help: the following implementations were found:
8+
<&'a T as MessageReceiver>
9+
<&'a mut T as MessageReceiver>
10+
<*const T as MessageReceiver>
11+
<*mut T as MessageReceiver>
12+
and 3 others
13+
= note: this error originates in the macro `msg_send` (in Nightly builds, run with -Z macro-backtrace for more info)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use objc2::rc::Shared;
2+
use objc2::runtime::Object;
3+
use objc2_foundation::NSArray;
4+
5+
fn needs_sync<T: ?Sized + Sync>() {}
6+
fn needs_send<T: ?Sized + Send>() {}
7+
8+
fn main() {
9+
needs_sync::<NSArray<Object, Shared>>();
10+
needs_send::<NSArray<Object, Shared>>();
11+
}

0 commit comments

Comments
 (0)