From: Gary Guo <gary@garyguo.net>
To: "Benno Lossin" <lossin@kernel.org>,
"Miguel Ojeda" <ojeda@kernel.org>,
"Boqun Feng" <boqun@kernel.org>,
"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
"Andreas Hindborg" <a.hindborg@kernel.org>,
"Alice Ryhl" <aliceryhl@google.com>,
"Trevor Gross" <tmgross@umich.edu>,
"Danilo Krummrich" <dakr@kernel.org>,
"Daniel Almeida" <daniel.almeida@collabora.com>,
"Tamir Duberstein" <tamird@kernel.org>,
"Alexandre Courbot" <acourbot@nvidia.com>,
"Onur Özkan" <work@onurozkan.dev>
Cc: linux-kernel@vger.kernel.org, rust-for-linux@vger.kernel.org,
Gary Guo <gary@garyguo.net>
Subject: [PATCH 15/20] rust: pin-init: internal: pin_data: parse explicit `#[uses]` annotation
Date: Thu, 08 Oct 2026 14:24:02 +0200 [thread overview]
Message-ID: <20261008-dev-selfref-v1-15-6c1eb269fe57@garyguo.net> (raw)
In-Reply-To: <20261008-dev-selfref-v1-0-6c1eb269fe57@garyguo.net>
Add support for explicit self-referential annotations.
`#[uses]` attribute is used to mark what other field lifetimes are
captured by this field, and also the variance of the type in respect to the
field lifetimes. For example,
#[uses('a: covariant, 'b: invariant)]
indicates that the field captures the field lifetime `'a` covariantly, and
field lifetime `'b` invariantly. Many types are covariant, so this is the
default variance if the variance is omitted (e.g. `#[uses('a)]`),
consistent with the automatically inferred borrow.
Signed-off-by: Gary Guo <gary@garyguo.net>
---
rust/pin-init/internal/src/pin_data.rs | 99 ++++++++++++++++++++++++++++++++--
1 file changed, 95 insertions(+), 4 deletions(-)
diff --git a/rust/pin-init/internal/src/pin_data.rs b/rust/pin-init/internal/src/pin_data.rs
index 54f0efef3226..d42d86ffd853 100644
--- a/rust/pin-init/internal/src/pin_data.rs
+++ b/rust/pin-init/internal/src/pin_data.rs
@@ -21,6 +21,9 @@
};
pub(crate) mod kw {
+ syn::custom_keyword!(covariant);
+ syn::custom_keyword!(invariant);
+ syn::custom_keyword!(contravariant);
syn::custom_keyword!(PinnedDrop);
}
@@ -93,9 +96,37 @@ struct BorrowedInfo {
#[derive(Clone, Copy, Default, PartialEq, Eq)]
enum Variance {
- /// Implicitly inferred variance.
+ /// `covariant` annotation, or implicitly inferred.
#[default]
Covariant,
+ // `invariant` annotation.
+ Invariant,
+}
+
+impl Parse for Variance {
+ fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
+ let lh = input.lookahead1();
+ Ok(if lh.peek(kw::covariant) {
+ let _: kw::covariant = input.parse()?;
+ Variance::Covariant
+ } else if lh.peek(kw::invariant) {
+ let _: kw::invariant = input.parse()?;
+ Variance::Invariant
+ } else if lh.peek(kw::contravariant) {
+ // Field lifetimes are inherently covariant, so combining with contravariance,
+ // we would constrain it to be invariant.
+ let token: kw::contravariant = input.parse()?;
+ DiagCtxt::current(|dcx| {
+ dcx.error(
+ token,
+ "field lifetimes cannot be `contravariant`; use `invariant` instead",
+ )
+ });
+ Variance::Invariant
+ } else {
+ Err(lh.error())?
+ })
+ }
}
/// Information about field lifetimes captured in a type.
@@ -131,7 +162,65 @@ fn cmp(&self, other: &Self) -> std::cmp::Ordering {
}
}
-#[expect(unused)]
+impl Parse for Capture {
+ fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
+ let lifetime = input.parse()?;
+ let variance = if input.peek(Token![:]) {
+ let _: Token![:] = input.parse()?;
+ input.parse()?
+ } else {
+ // If variance is not explicitly specified, infer covariance by default.
+ Variance::Covariant
+ };
+ Ok(Capture { variance, lifetime })
+ }
+}
+
+impl Capture {
+ fn parse_list(
+ dcx: &mut DiagCtxt,
+ attrs: &mut Vec<Attribute>,
+ bound_lifetimes: &BTreeSet<&Lifetime>,
+ field_idx_map: &BTreeMap<Ident, usize>,
+ ) -> Option<(BTreeSet<Capture>, Variance)> {
+ let attr = attrs.extract_single_attr(dcx, "uses")?;
+ let punctuated: Punctuated<Capture, Token![,]> = attr
+ .parse_args_with(Punctuated::parse_terminated)
+ .map_err(ErrorGuaranteed::from)
+ .ok()?;
+
+ // Check for misuses inside attribute.
+ let mut set = BTreeSet::new();
+ for borrow in punctuated {
+ let lt = &borrow.lifetime;
+ if set.contains(&borrow) {
+ dcx.error(lt, format!("lifetime `{lt}` is mentioned more than once"));
+ continue;
+ }
+
+ if bound_lifetimes.contains(lt) {
+ dcx.error(
+ lt,
+ format!("`{lt}` is a struct generics and cannot be used in `#[uses]`"),
+ );
+ continue;
+ }
+
+ if lt.ident != "_" && !field_idx_map.contains_key(<.ident) {
+ dcx.error(lt, format!("`{lt}` is not a field name"));
+ continue;
+ }
+
+ set.insert(borrow);
+ }
+
+ let wildcard = Lifetime::new("'_", Span::mixed_site());
+ let wildcard_variance = set.take(&wildcard).map(|b| b.variance).unwrap_or_default();
+ Some((set, wildcard_variance))
+ }
+}
+
+#[allow(unused)]
struct FieldInfo {
field: Field,
member: Member,
@@ -275,8 +364,10 @@ fn expand(
}),
};
- let mut captures = BTreeSet::new();
- let wildcard_variance = Variance::default();
+ // Parse `#[uses]` attribute.
+ let (mut captures, wildcard_variance) =
+ Capture::parse_list(dcx, &mut field.attrs, &bound_lifetimes, &field_idx_map)
+ .unwrap_or_default();
let mut generic_lt_captures = BTreeSet::new();
let mut generic_ty_captures = BTreeSet::new();
--
2.54.0
next prev parent reply other threads:[~2026-10-08 12:25 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-08 12:23 [PATCH 00/20] rust: pin-init: create self references safely Gary Guo
2026-10-08 12:23 ` [PATCH 01/20] kbuild: rust: allow `clippy::comparison_chain` globally Gary Guo
2026-10-08 12:23 ` [PATCH 02/20] rust: pin-init: internal: pin_data: infer self-referential struct Gary Guo
2026-10-08 12:23 ` [PATCH 03/20] rust: pin-init: internal: pin_data: rewrite fields that borrow others Gary Guo
2026-10-08 12:23 ` [PATCH 04/20] rust: pin-init: internal: pin_data: pin borrowed fields with wrapper Gary Guo
2026-10-08 12:23 ` [PATCH 05/20] rust: pin-init: internal: pin_data: teach drop check about generics that cannot dangle Gary Guo
2026-10-08 12:23 ` [PATCH 06/20] rust: pin-init: internal: pin_data: self-referential drop order checks Gary Guo
2026-10-08 12:23 ` [PATCH 07/20] rust: pin-init: internal: pin_data: check covariance of self-referential fields Gary Guo
2026-10-08 12:23 ` [PATCH 08/20] rust: pin-init: internal: pin_data: implement initialization of borrowed structs Gary Guo
2026-10-08 12:23 ` [PATCH 09/20] rust: pin-init: internal: pin_data: project self-referential fields Gary Guo
2026-10-08 12:23 ` [PATCH 10/20] rust: pin-init: internal: pin_data: add `with_project` method Gary Guo
2026-10-08 12:23 ` [PATCH 11/20] rust: pin-init: internal: pin_data: enable self-referential support Gary Guo
2026-10-08 12:23 ` [PATCH 12/20] rust: pin-init: internal: pin_data: allow lifetime to be shortened per field drop order Gary Guo
2026-10-08 12:24 ` [PATCH 13/20] rust: pin-init: internal: pin_data: parse explicit `#[borrowed]` annotation Gary Guo
2026-10-08 12:24 ` [PATCH 14/20] rust: pin-init: internal: pin_data: support mutable borrows Gary Guo
2026-10-08 12:24 ` Gary Guo [this message]
2026-10-08 12:24 ` [PATCH 16/20] rust: pin-init: internal: pin_data: make field lifetime invariance imply type invariance Gary Guo
2026-10-08 12:24 ` [PATCH 17/20] rust: pin-init: internal: pin_data: complete invariant borrow support Gary Guo
2026-10-08 12:24 ` [PATCH 18/20] rust: pin-init: internal: pin_data: perform AST lifetime replacement if possible Gary Guo
2026-10-08 12:24 ` [PATCH 19/20] rust: pin-init: internal: pin_data: support shared projection Gary Guo
2026-10-08 12:24 ` [PATCH 20/20] rust: pin-init: internal: pin_data: support existential lifetimes Gary Guo
2026-10-08 16:20 ` [PATCH 00/20] rust: pin-init: create self references safely Benno Lossin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261008-dev-selfref-v1-15-6c1eb269fe57@garyguo.net \
--to=gary@garyguo.net \
--cc=a.hindborg@kernel.org \
--cc=acourbot@nvidia.com \
--cc=aliceryhl@google.com \
--cc=bjorn3_gh@protonmail.com \
--cc=boqun@kernel.org \
--cc=dakr@kernel.org \
--cc=daniel.almeida@collabora.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lossin@kernel.org \
--cc=ojeda@kernel.org \
--cc=rust-for-linux@vger.kernel.org \
--cc=tamird@kernel.org \
--cc=tmgross@umich.edu \
--cc=work@onurozkan.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®