Skip to content

Conversation

Daanvdplas
Copy link
Contributor

@Daanvdplas Daanvdplas commented Apr 11, 2025

Updates the repository to rust 2024, partially closes use-ink/ink-alliance#43

  • as_option macro (used for event fields marked with ink::topic attribute)
#[macro_export]
#[doc(hidden)]
macro_rules! as_option {
    ( $e:expr $(,)? ) => {{
        #[allow(unused_imports)]
        use $crate::option_info::AsOptionFallback as _;
        $crate::option_info::AsOption(&$e).value()
    }};
}

How It Worked (Rust 2021):
Our original macro simply borrowed the field via &$e and then called a helper (AsOption(&$e).value()), which used specialized impls or fallback traits to return an Option<&T>. Because Rust 2021 would often extend the lifetime of these temporaries implicitly, this approach worked without lifetime issues.

Stricter Lifetime Enforcement (Rust 2024):
Rust 2024 enforces lifetimes more strictly. Now, when the macro creates a temporary binding inside its block—say with let tmp = $e;—and then takes a reference (&tmp), that reference’s lifetime is limited to the block. The temporary is dropped at the end of the block, making the returned reference invalid. This “lifetime extension” no longer happens automatically, and so we see errors like “temporary value dropped while borrowed.”

Current Stable‑Rust Solution and Remaining Concern:
We implement our stable‑Rust solution entirely with macro pattern matching.

Concern:
Because we’re relying solely on token matching rather than Rust’s type system, users must explicitly annotate Option literals—bare None won’t be recognized correctly, which risks ambiguity.

  • [ ]

@Daanvdplas Daanvdplas force-pushed the daan/chore-rust_2024 branch from 764f093 to 041a473 Compare April 14, 2025 15:53
@davidsemakula
Copy link
Collaborator

Closed in favor of #2624

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Upgrade to Rust edition 2024
2 participants