aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/compiletest.rs7
-rw-r--r--tests/custom_linkme_path.rs37
-rw-r--r--tests/distributed_slice.rs72
-rw-r--r--tests/example.rs32
-rw-r--r--tests/fn_element.rs31
-rw-r--r--tests/module/mod.rs18
-rw-r--r--tests/module_2015.rs5
-rw-r--r--tests/module_2021.rs3
-rw-r--r--tests/ui/bad_crate_path.rs20
-rw-r--r--tests/ui/bad_crate_path.stderr33
-rw-r--r--tests/ui/generic_fn.rs14
-rw-r--r--tests/ui/generic_fn.stderr11
-rw-r--r--tests/ui/mismatched_types.rs16
-rw-r--r--tests/ui/mismatched_types.stderr31
-rw-r--r--tests/ui/mutable.rs11
-rw-r--r--tests/ui/mutable.stderr11
-rw-r--r--tests/ui/unsupported_item.rs11
-rw-r--r--tests/ui/unsupported_item.stderr5
-rw-r--r--tests/ui/zerosized.rs10
-rw-r--r--tests/ui/zerosized.stderr7
20 files changed, 385 insertions, 0 deletions
diff --git a/tests/compiletest.rs b/tests/compiletest.rs
new file mode 100644
index 0000000..7974a62
--- /dev/null
+++ b/tests/compiletest.rs
@@ -0,0 +1,7 @@
+#[rustversion::attr(not(nightly), ignore)]
+#[cfg_attr(miri, ignore)]
+#[test]
+fn ui() {
+ let t = trybuild::TestCases::new();
+ t.compile_fail("tests/ui/*.rs");
+}
diff --git a/tests/custom_linkme_path.rs b/tests/custom_linkme_path.rs
new file mode 100644
index 0000000..daee74f
--- /dev/null
+++ b/tests/custom_linkme_path.rs
@@ -0,0 +1,37 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+
+use linkme as link_me;
+
+mod declaration {
+ use crate::link_me::distributed_slice;
+
+ #[distributed_slice]
+ #[linkme(crate = crate::link_me)]
+ pub static SLICE: [i32] = [..];
+
+ #[test]
+ fn test_slice() {
+ assert!(!SLICE.is_empty());
+ }
+
+ #[distributed_slice]
+ #[linkme(crate = crate::link_me)]
+ pub static FUNCTIONS: [fn()] = [..];
+
+ #[test]
+ fn test_functions() {
+ assert!(!FUNCTIONS.is_empty());
+ }
+}
+
+mod usage {
+ use crate::link_me::distributed_slice;
+
+ #[distributed_slice(super::declaration::SLICE)]
+ #[linkme(crate = crate::link_me)]
+ pub static N: i32 = 9;
+
+ #[distributed_slice(super::declaration::FUNCTIONS)]
+ #[linkme(crate = crate::link_me)]
+ fn test_me() {}
+}
diff --git a/tests/distributed_slice.rs b/tests/distributed_slice.rs
new file mode 100644
index 0000000..f8ab487
--- /dev/null
+++ b/tests/distributed_slice.rs
@@ -0,0 +1,72 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+
+use linkme::distributed_slice;
+use once_cell::sync::Lazy;
+
+#[distributed_slice]
+static SHENANIGANS: [i32] = [..];
+
+#[distributed_slice(SHENANIGANS)]
+static N: i32 = 9;
+
+#[distributed_slice(SHENANIGANS)]
+static NN: i32 = 99;
+
+#[distributed_slice(SHENANIGANS)]
+static NNN: i32 = 999;
+
+#[test]
+fn test() {
+ assert_eq!(SHENANIGANS.len(), 3);
+
+ let mut sum = 0;
+ for n in SHENANIGANS {
+ sum += n;
+ }
+
+ assert_eq!(sum, 9 + 99 + 999);
+}
+
+#[test]
+fn test_empty() {
+ #[distributed_slice]
+ static EMPTY: [i32] = [..];
+
+ assert!(EMPTY.is_empty());
+}
+
+#[test]
+fn test_non_copy() {
+ struct NonCopy(i32);
+
+ #[distributed_slice]
+ static NONCOPY: [NonCopy] = [..];
+
+ #[distributed_slice(NONCOPY)]
+ static ELEMENT: NonCopy = NonCopy(9);
+
+ assert!(!NONCOPY.is_empty());
+}
+
+#[test]
+fn test_interior_mutable() {
+ #[distributed_slice]
+ static MUTABLE: [Lazy<i32>] = [..];
+
+ #[distributed_slice(MUTABLE)]
+ static ELEMENT: Lazy<i32> = Lazy::new(|| -1);
+
+ assert!(MUTABLE.len() == 1);
+ assert!(*MUTABLE[0] == -1);
+}
+
+#[test]
+fn test_elided_lifetime() {
+ #[distributed_slice]
+ pub static MYSLICE: [&str] = [..];
+
+ #[distributed_slice(MYSLICE)]
+ static ELEMENT: &str = "...";
+
+ assert!(!MYSLICE.is_empty());
+}
diff --git a/tests/example.rs b/tests/example.rs
new file mode 100644
index 0000000..bbe439b
--- /dev/null
+++ b/tests/example.rs
@@ -0,0 +1,32 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+#![deny(warnings)]
+#![allow(clippy::no_effect_underscore_binding)]
+
+use linkme::distributed_slice;
+
+pub struct Bencher;
+
+#[distributed_slice]
+pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
+
+#[distributed_slice(BENCHMARKS)]
+static BENCH_DESERIALIZE: fn(&mut Bencher) = bench_deserialize;
+
+fn bench_deserialize(_b: &mut Bencher) {
+ /* ... */
+}
+
+#[test]
+fn readme() {
+ // Iterate the elements.
+ for _bench in BENCHMARKS { /* ... */ }
+
+ // Index into the elements.
+ let _first = BENCHMARKS[0];
+
+ // Slice the elements.
+ let _except_first = &BENCHMARKS[1..];
+
+ // Invoke methods on the underlying slice.
+ let _len = BENCHMARKS.len();
+}
diff --git a/tests/fn_element.rs b/tests/fn_element.rs
new file mode 100644
index 0000000..a80508f
--- /dev/null
+++ b/tests/fn_element.rs
@@ -0,0 +1,31 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+#![allow(clippy::needless_lifetimes, clippy::trivially_copy_pass_by_ref)]
+
+use linkme::distributed_slice;
+
+#[distributed_slice]
+pub static SLICE1: [fn()] = [..];
+
+#[distributed_slice(SLICE1)]
+fn foo() {}
+
+#[distributed_slice]
+pub static SLICE2: [for<'a, 'b> fn(&'a &'b ())] = [..];
+
+#[distributed_slice(SLICE2)]
+fn bar<'a, 'b>(_: &'a &'b ()) {}
+
+#[distributed_slice]
+pub static SLICE3: [unsafe extern "C" fn() -> i32] = [..];
+
+#[distributed_slice(SLICE3)]
+unsafe extern "C" fn baz() -> i32 {
+ 42
+}
+
+#[test]
+fn test_slices() {
+ assert!(!SLICE1.is_empty());
+ assert!(!SLICE2.is_empty());
+ assert!(!SLICE3.is_empty());
+}
diff --git a/tests/module/mod.rs b/tests/module/mod.rs
new file mode 100644
index 0000000..0fe3e5b
--- /dev/null
+++ b/tests/module/mod.rs
@@ -0,0 +1,18 @@
+mod declaration {
+ use linkme::distributed_slice;
+
+ #[distributed_slice]
+ pub static SLICE: [i32] = [..];
+
+ #[test]
+ fn test_mod_slice() {
+ assert!(!SLICE.is_empty());
+ }
+}
+
+mod usage {
+ use linkme::distributed_slice;
+
+ #[distributed_slice(super::declaration::SLICE)]
+ pub static N: i32 = 9;
+}
diff --git a/tests/module_2015.rs b/tests/module_2015.rs
new file mode 100644
index 0000000..1a5d049
--- /dev/null
+++ b/tests/module_2015.rs
@@ -0,0 +1,5 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+
+extern crate linkme;
+
+include!("module/mod.rs");
diff --git a/tests/module_2021.rs b/tests/module_2021.rs
new file mode 100644
index 0000000..b7f1e0a
--- /dev/null
+++ b/tests/module_2021.rs
@@ -0,0 +1,3 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+
+include!("module/mod.rs");
diff --git a/tests/ui/bad_crate_path.rs b/tests/ui/bad_crate_path.rs
new file mode 100644
index 0000000..ce2e563
--- /dev/null
+++ b/tests/ui/bad_crate_path.rs
@@ -0,0 +1,20 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+
+use linkme::distributed_slice;
+
+mod path {
+ pub mod to {}
+}
+
+#[distributed_slice]
+#[linkme(crate = path::to::missing)]
+pub static SLICE1: [&'static str] = [..];
+
+#[distributed_slice]
+pub static SLICE2: [&'static str] = [..];
+
+#[distributed_slice(SLICE2)]
+#[linkme(crate = path::to::missing)]
+static ELEMENT: &str = "";
+
+fn main() {}
diff --git a/tests/ui/bad_crate_path.stderr b/tests/ui/bad_crate_path.stderr
new file mode 100644
index 0000000..5ac1d6a
--- /dev/null
+++ b/tests/ui/bad_crate_path.stderr
@@ -0,0 +1,33 @@
+error[E0433]: failed to resolve: could not find `missing` in `to`
+ --> tests/ui/bad_crate_path.rs:10:28
+ |
+10 | #[linkme(crate = path::to::missing)]
+ | ^^^^^^^ could not find `missing` in `to`
+
+error[E0433]: failed to resolve: could not find `missing` in `to`
+ --> tests/ui/bad_crate_path.rs:17:28
+ |
+17 | #[linkme(crate = path::to::missing)]
+ | ^^^^^^^ could not find `missing` in `to`
+
+error[E0433]: failed to resolve: could not find `missing` in `to`
+ --> tests/ui/bad_crate_path.rs:10:28
+ |
+10 | #[linkme(crate = path::to::missing)]
+ | ^^^^^^^ could not find `missing` in `to`
+ |
+help: consider importing this struct
+ |
+3 | use linkme::DistributedSlice;
+ |
+
+error[E0433]: failed to resolve: could not find `missing` in `to`
+ --> tests/ui/bad_crate_path.rs:17:28
+ |
+17 | #[linkme(crate = path::to::missing)]
+ | ^^^^^^^ could not find `missing` in `to`
+ |
+help: consider importing this struct
+ |
+3 | use linkme::DistributedSlice;
+ |
diff --git a/tests/ui/generic_fn.rs b/tests/ui/generic_fn.rs
new file mode 100644
index 0000000..cb9be03
--- /dev/null
+++ b/tests/ui/generic_fn.rs
@@ -0,0 +1,14 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+
+use linkme::distributed_slice;
+
+#[distributed_slice]
+pub static SLICES: [fn()] = [..];
+
+#[distributed_slice(SLICES)]
+fn type_param<T>() {}
+
+#[distributed_slice(SLICES)]
+fn const_param<const N: usize>() {}
+
+fn main() {}
diff --git a/tests/ui/generic_fn.stderr b/tests/ui/generic_fn.stderr
new file mode 100644
index 0000000..2730c8d
--- /dev/null
+++ b/tests/ui/generic_fn.stderr
@@ -0,0 +1,11 @@
+error: cannot have generic parameters on distributed slice element
+ --> tests/ui/generic_fn.rs:9:15
+ |
+9 | fn type_param<T>() {}
+ | ^
+
+error: cannot have generic parameters on distributed slice element
+ --> tests/ui/generic_fn.rs:12:16
+ |
+12 | fn const_param<const N: usize>() {}
+ | ^^^^^^^^^^^^^^
diff --git a/tests/ui/mismatched_types.rs b/tests/ui/mismatched_types.rs
new file mode 100644
index 0000000..611c8a9
--- /dev/null
+++ b/tests/ui/mismatched_types.rs
@@ -0,0 +1,16 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+
+use linkme::distributed_slice;
+
+pub struct Bencher;
+
+#[distributed_slice]
+pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
+
+#[distributed_slice(BENCHMARKS)]
+static BENCH_WTF: usize = 999;
+
+#[distributed_slice(BENCHMARKS)]
+fn wrong_bench_fn<'a>(_: &'a mut ()) {}
+
+fn main() {}
diff --git a/tests/ui/mismatched_types.stderr b/tests/ui/mismatched_types.stderr
new file mode 100644
index 0000000..643d1a1
--- /dev/null
+++ b/tests/ui/mismatched_types.stderr
@@ -0,0 +1,31 @@
+error[E0308]: mismatched types
+ --> tests/ui/mismatched_types.rs:11:19
+ |
+10 | #[distributed_slice(BENCHMARKS)]
+ | -------------------------------- arguments to this function are incorrect
+11 | static BENCH_WTF: usize = 999;
+ | ^^^^^ expected fn pointer, found `usize`
+ |
+ = note: expected fn pointer `for<'a> fn(&'a mut Bencher)`
+ found type `usize`
+note: method defined here
+ --> src/distributed_slice.rs
+ |
+ | pub unsafe fn private_typecheck(self, element: T) {
+ | ^^^^^^^^^^^^^^^^^
+
+error[E0308]: mismatched types
+ --> tests/ui/mismatched_types.rs:14:1
+ |
+13 | #[distributed_slice(BENCHMARKS)]
+ | -------------------------------- arguments to this function are incorrect
+14 | fn wrong_bench_fn<'a>(_: &'a mut ()) {}
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Bencher`, found `()`
+ |
+ = note: expected fn pointer `for<'a> fn(&'a mut Bencher)`
+ found fn pointer `for<'a> fn(&'a mut ())`
+note: method defined here
+ --> src/distributed_slice.rs
+ |
+ | pub unsafe fn private_typecheck(self, element: T) {
+ | ^^^^^^^^^^^^^^^^^
diff --git a/tests/ui/mutable.rs b/tests/ui/mutable.rs
new file mode 100644
index 0000000..39db0d0
--- /dev/null
+++ b/tests/ui/mutable.rs
@@ -0,0 +1,11 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+
+use linkme::distributed_slice;
+
+#[distributed_slice]
+pub static mut SLICE: [i32] = [..];
+
+#[distributed_slice(BENCHMARKS)]
+static mut ELEMENT: i32 = -1;
+
+fn main() {}
diff --git a/tests/ui/mutable.stderr b/tests/ui/mutable.stderr
new file mode 100644
index 0000000..c23e194
--- /dev/null
+++ b/tests/ui/mutable.stderr
@@ -0,0 +1,11 @@
+error: static mut is not supported by distributed_slice
+ --> tests/ui/mutable.rs:6:12
+ |
+6 | pub static mut SLICE: [i32] = [..];
+ | ^^^
+
+error: static mut is not supported by distributed_slice
+ --> tests/ui/mutable.rs:9:8
+ |
+9 | static mut ELEMENT: i32 = -1;
+ | ^^^
diff --git a/tests/ui/unsupported_item.rs b/tests/ui/unsupported_item.rs
new file mode 100644
index 0000000..e218ce5
--- /dev/null
+++ b/tests/ui/unsupported_item.rs
@@ -0,0 +1,11 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+
+use linkme::distributed_slice;
+
+#[distributed_slice]
+pub static SLICE: [&'static str] = [..];
+
+#[distributed_slice(SLICE)]
+extern crate std as _std;
+
+fn main() {}
diff --git a/tests/ui/unsupported_item.stderr b/tests/ui/unsupported_item.stderr
new file mode 100644
index 0000000..b0eae07
--- /dev/null
+++ b/tests/ui/unsupported_item.stderr
@@ -0,0 +1,5 @@
+error: distributed element must be either static or function item
+ --> tests/ui/unsupported_item.rs:9:1
+ |
+9 | extern crate std as _std;
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/tests/ui/zerosized.rs b/tests/ui/zerosized.rs
new file mode 100644
index 0000000..3965cee
--- /dev/null
+++ b/tests/ui/zerosized.rs
@@ -0,0 +1,10 @@
+#![cfg_attr(feature = "used_linker", feature(used_with_arg))]
+
+use linkme::distributed_slice;
+
+pub struct Unit;
+
+#[distributed_slice]
+pub static ZEROSIZED: [Unit] = [..];
+
+fn main() {}
diff --git a/tests/ui/zerosized.stderr b/tests/ui/zerosized.stderr
new file mode 100644
index 0000000..2443140
--- /dev/null
+++ b/tests/ui/zerosized.stderr
@@ -0,0 +1,7 @@
+error[E0080]: could not evaluate static initializer
+ --> tests/ui/zerosized.rs:7:1
+ |
+7 | #[distributed_slice]
+ | ^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'assertion failed: ::linkme::__private::mem::size_of::<<[Unit] as\n ::linkme::__private::Slice>::Element>() > 0', $DIR/tests/ui/zerosized.rs:7:1
+ |
+ = note: this error originates in the macro `::linkme::__private::assert` (in Nightly builds, run with -Z macro-backtrace for more info)