Skip to content

Allow = [..] to be omitted #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ operates entirely during compilation and linking.
### Declaration

A static distributed slice is declared by writing `#[distributed_slice]` on a
static item whose type is `[T]` for some type `T`. The initializer expression
must be `[..]` to indicate that elements come from elsewhere.
static item whose type is `[T]` for some type `T`.

```rust
use linkme::distributed_slice;

#[distributed_slice]
pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
pub static BENCHMARKS: [fn(&mut Bencher)];
```

### Elements
Expand Down Expand Up @@ -120,7 +119,7 @@ definition to place a pointer to that function into a distributed slice.
use linkme::distributed_slice;

#[distributed_slice]
pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
pub static BENCHMARKS: [fn(&mut Bencher)];

// Equivalent to:
//
Expand Down
10 changes: 6 additions & 4 deletions impl/src/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ impl Parse for Declaration {
let ident: Ident = input.parse()?;
input.parse::<Token![:]>()?;
let ty: Type = input.parse()?;
input.parse::<Token![=]>()?;

let content;
bracketed!(content in input);
content.parse::<Token![..]>()?;
let eq_token: Option<Token![=]> = input.parse()?;
if eq_token.is_some() {
let content;
bracketed!(content in input);
content.parse::<Token![..]>()?;
}

input.parse::<Token![;]>()?;

Expand Down
13 changes: 6 additions & 7 deletions src/distributed_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ use crate::__private::Slice;
/// ## Declaration
///
/// A static distributed slice may be declared by writing `#[distributed_slice]`
/// on a static item whose type is `[T]` for some type `T`. The initializer
/// expression must be `[..]` to indicate that elements come from elsewhere.
/// on a static item whose type is `[T]` for some type `T`.
///
/// ```
/// # #![cfg_attr(feature = "used_linker", feature(used_with_arg))]
Expand All @@ -27,7 +26,7 @@ use crate::__private::Slice;
/// use linkme::distributed_slice;
///
/// #[distributed_slice]
/// pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
/// pub static BENCHMARKS: [fn(&mut Bencher)];
/// ```
///
/// The attribute rewrites the `[T]` type of the static into
Expand All @@ -54,7 +53,7 @@ use crate::__private::Slice;
/// # pub struct Bencher;
/// #
/// # #[distributed_slice]
/// # pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
/// # pub static BENCHMARKS: [fn(&mut Bencher)];
/// # }
/// #
/// # use other_crate::Bencher;
Expand All @@ -81,7 +80,7 @@ use crate::__private::Slice;
/// # pub struct Bencher;
/// #
/// # #[distributed_slice]
/// # pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
/// # pub static BENCHMARKS: [fn(&mut Bencher)];
/// # }
/// #
/// # use linkme::distributed_slice;
Expand Down Expand Up @@ -117,7 +116,7 @@ use crate::__private::Slice;
/// use linkme::distributed_slice;
///
/// #[distributed_slice]
/// pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
/// pub static BENCHMARKS: [fn(&mut Bencher)];
///
/// // Equivalent to:
/// //
Expand Down Expand Up @@ -235,7 +234,7 @@ impl<T> DistributedSlice<[T]> {
/// use linkme::distributed_slice;
///
/// #[distributed_slice]
/// static BENCHMARKS: [fn(&mut Bencher)] = [..];
/// static BENCHMARKS: [fn(&mut Bencher)];
///
/// fn main() {
/// // Iterate the elements.
Expand Down
9 changes: 4 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
//! of the API. The basic idea is as follows.
//!
//! A static distributed slice is declared by writing `#[distributed_slice]` on
//! a static item whose type is `[T]` for some type `T`. The initializer
//! expression must be `[..]` to indicate that elements come from elsewhere.
//! a static item whose type is `[T]` for some type `T`.
//!
//! ```
//! # #![cfg_attr(feature = "used_linker", feature(used_with_arg))]
Expand All @@ -43,7 +42,7 @@
//! use linkme::distributed_slice;
//!
//! #[distributed_slice]
//! pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
//! pub static BENCHMARKS: [fn(&mut Bencher)];
//! ```
//!
//! Slice elements may be registered into a distributed slice by a
Expand All @@ -60,7 +59,7 @@
//! # pub struct Bencher;
//! #
//! # #[distributed_slice]
//! # pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
//! # pub static BENCHMARKS: [fn(&mut Bencher)];
//! # }
//! #
//! # use other_crate::Bencher;
Expand All @@ -86,7 +85,7 @@
//! # struct Bencher;
//! #
//! # #[distributed_slice]
//! # static BENCHMARKS: [fn(&mut Bencher)] = [..];
//! # static BENCHMARKS: [fn(&mut Bencher)];
//! #
//! fn main() {
//! // Iterate the elements.
Expand Down
2 changes: 1 addition & 1 deletion tests/cortex/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cortex_m_semihosting::{debug, hprintln};
use linkme::distributed_slice;

#[distributed_slice]
static SHENANIGANS: [i32] = [..];
static SHENANIGANS: [i32];

#[distributed_slice(SHENANIGANS)]
static N: i32 = 9;
Expand Down
4 changes: 2 additions & 2 deletions tests/custom_linkme_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod declaration {

#[distributed_slice]
#[linkme(crate = crate::link_me)]
pub static SLICE: [i32] = [..];
pub static SLICE: [i32];

#[test]
fn test_slice() {
Expand All @@ -16,7 +16,7 @@ mod declaration {

#[distributed_slice]
#[linkme(crate = crate::link_me)]
pub static FUNCTIONS: [fn()] = [..];
pub static FUNCTIONS: [fn()];

#[test]
fn test_functions() {
Expand Down
17 changes: 12 additions & 5 deletions tests/distributed_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use linkme::distributed_slice;
use once_cell::sync::Lazy;

#[distributed_slice]
static SHENANIGANS: [i32] = [..];
static SHENANIGANS: [i32];

#[distributed_slice(SHENANIGANS)]
static N: i32 = 9;
Expand All @@ -30,7 +30,7 @@ fn test() {
#[test]
fn test_empty() {
#[distributed_slice]
static EMPTY: [i32] = [..];
static EMPTY: [i32];

assert!(EMPTY.is_empty());
}
Expand All @@ -40,7 +40,7 @@ fn test_non_copy() {
struct NonCopy(i32);

#[distributed_slice]
static NONCOPY: [NonCopy] = [..];
static NONCOPY: [NonCopy];

#[distributed_slice(NONCOPY)]
static ELEMENT: NonCopy = NonCopy(9);
Expand All @@ -51,7 +51,7 @@ fn test_non_copy() {
#[test]
fn test_interior_mutable() {
#[distributed_slice]
static MUTABLE: [Lazy<i32>] = [..];
static MUTABLE: [Lazy<i32>];

#[distributed_slice(MUTABLE)]
static ELEMENT: Lazy<i32> = Lazy::new(|| -1);
Expand All @@ -63,11 +63,18 @@ fn test_interior_mutable() {
#[test]
fn test_elided_lifetime() {
#[distributed_slice]
pub static MYSLICE: [&str] = [..];
pub static MYSLICE: [&str];

#[distributed_slice(MYSLICE)]
static ELEMENT: &str = "...";

assert!(!MYSLICE.is_empty());
assert_eq!(MYSLICE[0], "...");
}

#[test]
fn test_legacy_syntax() {
// Rustc older than 1.43 requires an initializer expression.
#[distributed_slice]
pub static LEGACY: [&str] = [..];
}
2 changes: 1 addition & 1 deletion tests/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use linkme::distributed_slice;
pub struct Bencher;

#[distributed_slice]
pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
pub static BENCHMARKS: [fn(&mut Bencher)];

#[distributed_slice(BENCHMARKS)]
static BENCH_DESERIALIZE: fn(&mut Bencher) = bench_deserialize;
Expand Down
6 changes: 3 additions & 3 deletions tests/fn_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
use linkme::distributed_slice;

#[distributed_slice]
pub static SLICE1: [fn()] = [..];
pub static SLICE1: [fn()];

#[distributed_slice(SLICE1)]
fn foo() {}

#[distributed_slice]
pub static SLICE2: [for<'a, 'b> fn(&'a &'b ())] = [..];
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] = [..];
pub static SLICE3: [unsafe extern "C" fn() -> i32];

#[distributed_slice(SLICE3)]
unsafe extern "C" fn baz() -> i32 {
Expand Down
2 changes: 1 addition & 1 deletion tests/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ mod declaration {
use linkme::distributed_slice;

#[distributed_slice]
pub static SLICE: [i32] = [..];
pub static SLICE: [i32];

#[test]
fn test_mod_slice() {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/bad_crate_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ mod path {

#[distributed_slice]
#[linkme(crate = path::to::missing)]
pub static SLICE1: [&'static str] = [..];
pub static SLICE1: [&'static str];

#[distributed_slice]
pub static SLICE2: [&'static str] = [..];
pub static SLICE2: [&'static str];

#[distributed_slice(SLICE2)]
#[linkme(crate = path::to::missing)]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/generic_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use linkme::distributed_slice;

#[distributed_slice]
pub static SLICES: [fn()] = [..];
pub static SLICES: [fn()];

#[distributed_slice(SLICES)]
fn type_param<T>() {}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mismatched_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use linkme::distributed_slice;
pub struct Bencher;

#[distributed_slice]
pub static BENCHMARKS: [fn(&mut Bencher)] = [..];
pub static BENCHMARKS: [fn(&mut Bencher)];

#[distributed_slice(BENCHMARKS)]
static BENCH_WTF: usize = 999;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use linkme::distributed_slice;

#[distributed_slice]
pub static mut SLICE: [i32] = [..];
pub static mut SLICE: [i32];

#[distributed_slice(BENCHMARKS)]
static mut ELEMENT: i32 = -1;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mutable.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
error: static mut is not supported by distributed_slice
--> tests/ui/mutable.rs:6:12
|
6 | pub static mut SLICE: [i32] = [..];
6 | pub static mut SLICE: [i32];
| ^^^

error: static mut is not supported by distributed_slice
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/unsupported_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use linkme::distributed_slice;

#[distributed_slice]
pub static SLICE: [&'static str] = [..];
pub static SLICE: [&'static str];

#[distributed_slice(SLICE)]
extern crate std as _std;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/zerosized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ use linkme::distributed_slice;
pub struct Unit;

#[distributed_slice]
pub static ZEROSIZED: [Unit] = [..];
pub static ZEROSIZED: [Unit];

fn main() {}
2 changes: 1 addition & 1 deletion tests/win_status_access_violation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use linkme::distributed_slice;

#[distributed_slice]
static ITEMS: [&'static str] = [..];
static ITEMS: [&'static str];

#[distributed_slice(ITEMS)]
static ITEM1: &'static str = "item1";
Expand Down
2 changes: 1 addition & 1 deletion tests/win_status_illegal_instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Item {
}

#[distributed_slice]
static ITEMS: [Item] = [..];
static ITEMS: [Item];

#[distributed_slice(ITEMS)]
static ITEM1: Item = Item { name: "item1" };
Expand Down