mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-09-18 23:09:29 +02:00
rust: types: introduce ForLt base trait for CovariantForLt
Add a new ForLt trait as a base for CovariantForLt:
- ForLt (non-unsafe): represents a type generic over a lifetime, with
no covariance guarantee.
- CovariantForLt (unsafe): becomes a subtrait of ForLt that
additionally proves the type is covariant over its lifetime
parameter, providing a safe cast_ref() method.
This split allows non-covariant types (e.g. types behind a Mutex) to
implement ForLt and participate in DevresLt / registration data patterns
that use HRTB closures for sound access, without requiring a covariance
proof that would fail to compile.
Both macros share the UnsafeForLtImpl helper type, distinguished by
a const generic N: ForLt! emits N = 0 (no covariance proof),
CovariantForLt! emits N = 1 (with compile-time covariance proof).
Reviewed-by: Gary Guo <gary@garyguo.net>
Acked-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://patch.msgid.link/20260626183630.2585057-3-dakr@kernel.org
[ Merge ForLt pub use, inline resolve_hrt/ty_static, add intra-doc
links. - Danilo ]
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
@@ -13,7 +13,10 @@ use pin_init::{PinInit, Wrapper, Zeroable};
|
||||
|
||||
#[doc(hidden)]
|
||||
pub mod for_lt;
|
||||
pub use for_lt::CovariantForLt;
|
||||
pub use for_lt::{
|
||||
CovariantForLt,
|
||||
ForLt, //
|
||||
};
|
||||
|
||||
/// Used to transfer ownership to and from foreign (non-Rust) languages.
|
||||
///
|
||||
|
||||
+63
-17
@@ -1,22 +1,67 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
//! Provide implementation and test of the `CovariantForLt` trait and macro.
|
||||
//! Provide implementation and test of the [`trait@ForLt`] and [`trait@CovariantForLt`] traits and
|
||||
//! macros.
|
||||
//!
|
||||
//! This module is hidden and user should just use `CovariantForLt!` directly.
|
||||
//! This module is hidden and users should just use [`ForLt!`](macro@ForLt) /
|
||||
//! [`CovariantForLt!`](macro@CovariantForLt) directly.
|
||||
|
||||
use core::marker::PhantomData;
|
||||
|
||||
/// Representation of types generic over a lifetime.
|
||||
///
|
||||
/// The type must be covariant over the generic lifetime, i.e. the lifetime parameter
|
||||
/// can be soundly shortened.
|
||||
/// # Macro
|
||||
///
|
||||
/// The lifetime involved must be covariant.
|
||||
/// It is not recommended to implement this trait directly. [`ForLt!`](macro@ForLt) macro is
|
||||
/// provided to obtain a type that implements this trait.
|
||||
///
|
||||
/// The full syntax is
|
||||
///
|
||||
/// ```
|
||||
/// # use kernel::types::ForLt;
|
||||
/// # fn expect_lt<F: ForLt>() {}
|
||||
/// # struct TypeThatUse<'a>(&'a ());
|
||||
/// # expect_lt::<
|
||||
/// ForLt!(for<'a> TypeThatUse<'a>)
|
||||
/// # >();
|
||||
/// ```
|
||||
///
|
||||
/// which gives a type so that `<ForLt!(for<'a> TypeThatUse<'a>) as ForLt>::Of<'b>`
|
||||
/// is `TypeThatUse<'b>`.
|
||||
///
|
||||
/// You may also use a short-hand syntax which works similar to lifetime elision.
|
||||
/// The macro also accepts types that do not involve a lifetime at all.
|
||||
///
|
||||
/// ```
|
||||
/// # use kernel::types::ForLt;
|
||||
/// # fn expect_lt<F: ForLt>() {}
|
||||
/// # struct TypeThatUse<'a>(&'a ());
|
||||
/// # expect_lt::<
|
||||
/// ForLt!(TypeThatUse<'_>) // Equivalent to `ForLt!(for<'a> TypeThatUse<'a>)`.
|
||||
/// # >();
|
||||
/// # expect_lt::<
|
||||
/// ForLt!(&u32) // Equivalent to `ForLt!(for<'a> &'a u32)`.
|
||||
/// # >();
|
||||
/// # expect_lt::<
|
||||
/// ForLt!(u32) // Equivalent to `ForLt!(for<'a> u32)`.
|
||||
/// # >();
|
||||
/// ```
|
||||
pub trait ForLt {
|
||||
/// The type parameterized by the lifetime.
|
||||
type Of<'a>: 'a;
|
||||
}
|
||||
pub use macros::ForLt;
|
||||
|
||||
/// [`trait@ForLt`] subtrait for types that are covariant over their lifetime parameter.
|
||||
///
|
||||
/// Provides a safe [`cast_ref`](CovariantForLt::cast_ref) method for types that are proven to be
|
||||
/// covariant. The `CovariantForLt!` macro syntax is the same as `ForLt!`.
|
||||
///
|
||||
/// # Macro
|
||||
///
|
||||
/// It is not recommended to implement this trait directly. `CovariantForLt!` macro is provided to
|
||||
/// obtain a type that implements this trait.
|
||||
/// It is not recommended to implement this trait directly.
|
||||
/// [`CovariantForLt!`](macro@CovariantForLt) macro is provided to obtain a type that implements
|
||||
/// this trait.
|
||||
///
|
||||
/// The full syntax is
|
||||
///
|
||||
@@ -84,10 +129,7 @@ use core::marker::PhantomData;
|
||||
/// # Safety
|
||||
///
|
||||
/// `Self::Of<'a>` must be covariant over the lifetime `'a`.
|
||||
pub unsafe trait CovariantForLt {
|
||||
/// The type parameterized by the lifetime.
|
||||
type Of<'a>: 'a;
|
||||
|
||||
pub unsafe trait CovariantForLt: ForLt {
|
||||
/// Cast a reference to a shorter lifetime.
|
||||
#[inline(always)]
|
||||
fn cast_ref<'r, 'short: 'r, 'long: 'short>(long: &'r Self::Of<'long>) -> &'r Self::Of<'short> {
|
||||
@@ -99,25 +141,29 @@ pub use macros::CovariantForLt;
|
||||
|
||||
/// This is intended to be an "unsafe-to-refer-to" type.
|
||||
///
|
||||
/// Must only be used by the `CovariantForLt!` macro.
|
||||
/// Must only be used by the [`ForLt!`](macro@ForLt) / [`CovariantForLt!`](macro@CovariantForLt)
|
||||
/// macros.
|
||||
///
|
||||
/// `T` is the magic `dyn for<'a> WithLt<'a, TypeThatUse<'a>>` generated by macro.
|
||||
///
|
||||
/// `WF` is a type that the macro can use to assert some specific type is well-formed.
|
||||
///
|
||||
/// `N` is to provide the macro a place to emit arbitrary items, in case it needs to prove
|
||||
/// additional properties.
|
||||
/// additional properties. [`ForLt!`](macro@ForLt) emits `N = 0`;
|
||||
/// [`CovariantForLt!`](macro@CovariantForLt) emits `N = 1` after a covariance proof.
|
||||
#[doc(hidden)]
|
||||
pub struct UnsafeForLtImpl<T: ?Sized, WF, const N: usize>(PhantomData<(WF, T)>);
|
||||
|
||||
// This is a helper trait for implementation `CovariantForLt` to be able to use HRTB.
|
||||
// This is a helper trait for implementation of `ForLt` / `CovariantForLt` to be able to use HRTB.
|
||||
#[doc(hidden)]
|
||||
pub trait WithLt<'a> {
|
||||
type Of: 'a;
|
||||
}
|
||||
|
||||
// SAFETY: In `CovariantForLt!` macro, a covariance proof is generated when naming
|
||||
// `UnsafeForLtImpl` and it will fail to evaluate if the type is not covariant.
|
||||
unsafe impl<T: ?Sized + for<'a> WithLt<'a>, WF> CovariantForLt for UnsafeForLtImpl<T, WF, 0> {
|
||||
impl<T: ?Sized + for<'a> WithLt<'a>, WF, const N: usize> ForLt for UnsafeForLtImpl<T, WF, N> {
|
||||
type Of<'a> = <T as WithLt<'a>>::Of;
|
||||
}
|
||||
|
||||
// SAFETY: In `CovariantForLt!` macro, a covariance proof is generated in the `N` const generic
|
||||
// and it will fail to evaluate if the type is not covariant. Only `N = 1` gets this impl.
|
||||
unsafe impl<T: ?Sized + for<'a> WithLt<'a>, WF> CovariantForLt for UnsafeForLtImpl<T, WF, 1> {}
|
||||
|
||||
+27
-10
@@ -176,7 +176,12 @@ impl<'a> Prover<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn covariant_for_lt(input: HigherRankedType) -> TokenStream {
|
||||
/// Shared implementation for both `ForLt!` and `CovariantForLt!`.
|
||||
///
|
||||
/// Both macros run the prover and emit `ProveWf` structs to check well-formedness for all lifetime
|
||||
/// instances (workaround for <https://github.com/rust-lang/rust/issues/152489>). `CovariantForLt!`
|
||||
/// additionally emits covariance proof functions and sets `N = 1`.
|
||||
fn for_lt_inner(input: HigherRankedType, prove_covariance: bool) -> TokenStream {
|
||||
let (ty, lifetime) = match input {
|
||||
HigherRankedType::Explicit { lifetime, ty, .. } => (ty, lifetime),
|
||||
HigherRankedType::Implicit { ty } => {
|
||||
@@ -211,14 +216,16 @@ pub(crate) fn covariant_for_lt(input: HigherRankedType) -> TokenStream {
|
||||
));
|
||||
|
||||
// Insert a proof that the type is covariant.
|
||||
let cov_proof_name = format_ident!("prove_covariant_{idx}");
|
||||
proof.push(quote!(
|
||||
fn #cov_proof_name<'__short, '__long: '__short>(
|
||||
long: #wf_proof_name<'__long>
|
||||
) -> #wf_proof_name<'__short> {
|
||||
long
|
||||
}
|
||||
));
|
||||
if prove_covariance {
|
||||
let cov_proof_name = format_ident!("prove_covariant_{idx}");
|
||||
proof.push(quote!(
|
||||
fn #cov_proof_name<'__short, '__long: '__short>(
|
||||
long: #wf_proof_name<'__long>
|
||||
) -> #wf_proof_name<'__short> {
|
||||
long
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure that the type is wellformed when substituting lifetime with `'static`.
|
||||
@@ -234,6 +241,8 @@ pub(crate) fn covariant_for_lt(input: HigherRankedType) -> TokenStream {
|
||||
},
|
||||
);
|
||||
|
||||
let n: usize = prove_covariance.into();
|
||||
|
||||
quote!(
|
||||
::kernel::types::for_lt::UnsafeForLtImpl::<
|
||||
dyn for<#lifetime> ::kernel::types::for_lt::WithLt<#lifetime, Of = #ty>,
|
||||
@@ -241,8 +250,16 @@ pub(crate) fn covariant_for_lt(input: HigherRankedType) -> TokenStream {
|
||||
{
|
||||
#(#proof)*
|
||||
|
||||
0
|
||||
#n
|
||||
}
|
||||
>
|
||||
)
|
||||
}
|
||||
|
||||
pub(crate) fn for_lt(input: HigherRankedType) -> TokenStream {
|
||||
for_lt_inner(input, false)
|
||||
}
|
||||
|
||||
pub(crate) fn covariant_for_lt(input: HigherRankedType) -> TokenStream {
|
||||
for_lt_inner(input, true)
|
||||
}
|
||||
|
||||
+18
-1
@@ -491,11 +491,28 @@ pub fn kunit_tests(attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
.into()
|
||||
}
|
||||
|
||||
/// Obtain a type that implements [`CovariantForLt`] for the given higher-ranked type.
|
||||
/// Obtain a type that implements [`ForLt`] for the given higher-ranked type.
|
||||
///
|
||||
/// Please refer to the documentation of the [`ForLt`] trait.
|
||||
///
|
||||
/// [`ForLt`]: trait.ForLt.html
|
||||
#[proc_macro]
|
||||
#[allow(non_snake_case)]
|
||||
pub fn ForLt(input: TokenStream) -> TokenStream {
|
||||
for_lt::for_lt(parse_macro_input!(input)).into()
|
||||
}
|
||||
|
||||
/// Obtain a type that implements [`CovariantForLt`] (and [`ForLt`]) for the given higher-ranked
|
||||
/// type.
|
||||
///
|
||||
/// Unlike [`ForLt!`], this macro additionally proves that the type is covariant over the lifetime,
|
||||
/// providing a safe [`CovariantForLt::cast_ref`] method.
|
||||
///
|
||||
/// Please refer to the documentation of the [`CovariantForLt`] trait.
|
||||
///
|
||||
/// [`CovariantForLt`]: trait.CovariantForLt.html
|
||||
/// [`CovariantForLt::cast_ref`]: trait.CovariantForLt.html#method.cast_ref
|
||||
/// [`ForLt`]: trait.ForLt.html
|
||||
#[proc_macro]
|
||||
#[allow(non_snake_case)]
|
||||
pub fn CovariantForLt(input: TokenStream) -> TokenStream {
|
||||
|
||||
Reference in New Issue
Block a user