Compiler support for statically asserting variance?

โš“ Rust    ๐Ÿ“… 2026-02-05    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 8      

surdeus

There has been a bit of discussion recently about statically checking variance of lifetimes. This is a problem that appears in the yoke create, in the lender crate, and in some crates under development.

The idea is that some implementations contains transmute or similar statements that are sound only if a certain lifetime is covariant (but the same can be said of contravariant or bivariant). They should fail at compile time if it is not.

The current solutions involve writing some no-op conversion of pointers forcing the statement, or more complicated workarounds, but from the discussions I'm reading and from my experience it seems really that what we need is a compiler feature. The techniques used, while obviously brilliant, are so convoluted and preposterous that really look like a fight against the compiler rather than collaboration.

Before even trying to open an issue, I'd like to pick thoughts of peopleโ€”how would you imagine such a feature? Macro (with some internal support)? An attribute?

2 posts - 1 participant

Read full topic

๐Ÿท๏ธ Rust_feed