embassy-rs / static-cell

Statically allocated, runtime initialized cell.
Apache License 2.0
25 stars 15 forks source link

make_static!() does not work anymore since rustc nightly-2024-06-13 #16

Open aurelj opened 1 month ago

aurelj commented 1 month ago

make_static!() does not work anymore since rustc nightly-2024-06-13 or more precisely since this commit: https://github.com/rust-lang/rust/commit/02c7a5921e3de5c2b3ecb2e0082c1dafce0729a1

Here is the detailed error I get when compiling with RUSTFLAGS="-Zmacro-backtrace":

error: item does not constrain `T::{opaque#0}`, but has it in its signature
   --> devel/embedded/static-cell/src/lib.rs:239:16
    |
234 | macro_rules! make_static {
    | ------------------------
    | |
    | in this expansion of `make_static!` (#1)
    | in this expansion of `$crate::make_static!` (#2)
235 |     ($val:expr) => ($crate::make_static!($val, ));
    |                     ---------------------------- in this macro invocation (#2)
...
239 |         static STATIC_CELL: $crate::StaticCell<T> = $crate::StaticCell::new();
    |                ^^^^^^^^^^^
    |
   ::: src/main.rs:133:24
    |
133 |     let merge_buffer = make_static!([0; 4096]);
    |                        ----------------------- in this macro invocation (#1)
    |
    = note: consider moving the opaque type's declaration and defining uses into a separate module
note: this opaque type is in the signature
   --> devel/embedded/static-cell/src/lib.rs:237:18
    |
234 | macro_rules! make_static {
    | ------------------------
    | |
    | in this expansion of `make_static!` (#1)
    | in this expansion of `$crate::make_static!` (#2)
235 |     ($val:expr) => ($crate::make_static!($val, ));
    |                     ---------------------------- in this macro invocation (#2)
236 |     ($val:expr, $(#[$m:meta])*) => {{
237 |         type T = impl ::core::marker::Sized;
    |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
   ::: src/main.rs:133:24
    |
133 |     let merge_buffer = make_static!([0; 4096]);
    |                        ----------------------- in this macro invocation (#1)

It seems the TAIT trick currently used in make_static!() is not allowed anymore. I tried to find an alternative without success.

Dirbaio commented 1 month ago

yes, this is quite annoying. I've also failed to find an alternative! :(

kesyog commented 2 weeks ago

It's not as ergonomic, but I did the simple/naive thing in my project and fixed this by making a new version of the make_static! macro that adds an extra argument to pass in the type:

#[macro_export]
macro_rules! make_static {
    ($t:ty, $val:expr) => ($crate::make_static!($t, $val,));
    ($t:ty, $val:expr, $(#[$m:meta])*) => {{
        $(#[$m])*
        static STATIC_CELL: static_cell::StaticCell<$t> = static_cell::StaticCell::new();
        STATIC_CELL.init_with(|| $val)
    }};
}