Skip to contentSkip to conversation

rust: pin-init: reduce visibility of projection methods to declutter rustdoc

PATCH Applied Gary Guo 3 participants 8 messages Nov 18 – Nov 22

Subsystems (2)
lore
Share & tools
lore

Gary Guo

author
#0 lore raw

cover rust: pin-init: reduce visibility of projection methods to declutter rustdoc

Currently all types that use #[pin_data] will have projection method generated and visible in rustdoc. But for many types, all fields are private and thus these projection methods are not useful as public APIs.

Thus, compute a least upper bound (LUB) of field visibility and use that as visibility for projection methods instead. This means that if none of the fields are accessible publicly, the projection methods won't be as well and thus will be hidden from rustdoc.

We could also just mark these methods as #[doc(hidden)] -- but LSPs will hide these methods too -- and bthese are indeed APIs that people would want to use.

---
Gary Guo (3):
rust: pin-init: internal: allow clippy::large_unopt_stack_frames
rust: pin-init: internal: use visibility of the most visible field for projection
rust: pin-init: internal: hide HasPinData impl

 rust/pin-init/internal/src/init.rs     |  2 +-
 rust/pin-init/internal/src/pin_data.rs | 36 ++++++++++++++++----------
 rust/pin-init/internal/src/util.rs     | 47 +++++++++++++++++++++++++++++++++-
 3 files changed, 69 insertions(+), 16 deletions(-)

---
base-commit: b7218f1996f8491dbe28632b70f444cb73f554df
change-id: 20261118-dev-project-vis-ff83ea9e6926

Best regards,

Signature · 1 line
Gary Guo <[email protected]>
author
#1 lore raw

PATCH 1/3 rust: pin-init: internal: allow `clippy::large_unopt_stack_frames`

From: Gary Guo <[email protected]>

Since Rust 1.102, Clippy renames large_stack_frames to large_unopt_stack_frames and make it warn by default. This is a good thing as in such cases pin-init should be used rather than potentially leave unoptimized large moves on stack. However, this also triggers in unexecutable code paths that pin-init generates for field duplication and exhaustiveness checks. Allow it on the generated check closure.

---
 rust/pin-init/internal/src/init.rs | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
rust/pin-init/internal/src/init.rs +1 -1
index ded2b5283376..c78f8d3ca309 100644
@@ -572,7 +572,7 @@ fn make_field_check(
     };
 
     quote_spanned! { Span::mixed_site() =>
-        #[allow(unreachable_code)]
+        #[allow(unreachable_code, clippy::large_unopt_stack_frames)]
         // We use unreachable code to perform field checks. They're still checked by the compiler.
         // SAFETY: this code is never executed.
         let _ = || unsafe {
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH 1/3] rust: pin-init: internal: allow `clippy::large_unopt_stack_frames`

Quoted 11 lines · Gary Guo

From: Gary Guo <[email protected]>

Since Rust 1.102, Clippy renames large_stack_frames to large_unopt_stack_frames and make it warn by default. This is a good thing as in such cases pin-init should be used rather than potentially leave unoptimized large moves on stack. However, this also triggers in unexecutable code paths that pin-init generates for field duplication and exhaustiveness checks. Allow it on the generated check closure.

We hit this in stable kernels, so:

Cc [email protected] # 6.12.x (Rust is pinned in older LTSs)

Cheers,
Miguel

maintainer
#3 lore raw

Re: [PATCH 1/3] rust: pin-init: internal: allow `clippy::large_unopt_stack_frames`

Quoted 11 lines · Miguel Ojeda
Quoted 10 lines · Gary Guo

From: Gary Guo <[email protected]>

Since Rust 1.102, Clippy renames large_stack_frames to large_unopt_stack_frames and make it warn by default. This is a good thing as in such cases pin-init should be used rather than potentially leave unoptimized large moves on stack. However, this also triggers in unexecutable code paths that pin-init generates for field duplication and exhaustiveness checks. Allow it on the generated check closure.

Quoted 5 lines
Quoted 1 line

We hit this in stable kernels, so:

Cc [email protected] # 6.12.x (Rust is pinned in older LTSs)

Do you think this is worth taking through rust-fixes, or are you okay with this landing via pin-init-next?

Best,
Gary

#4 lore raw

Re: [PATCH 1/3] rust: pin-init: internal: allow `clippy::large_unopt_stack_frames`

Quoted 3 lines · Gary Guo

Do you think this is worth taking through rust-fixes, or are you okay with this landing via pin-init-next?

It is nightly-only unless I am missing something, so pin-init-next is good (and given Linus doesn't want less important fixes nowadays during -rcs, probably it would be best to avoid it there anyway).

Thanks!

Cheers,
Miguel

author
#5 lore raw

PATCH 2/3 rust: pin-init: internal: use visibility of the most visible field for projection

In reply to Gary Guo

From: Gary Guo <[email protected]>

Currently, we use the struct visibility for projection methods. This means that if a public struct uses #[pin_data], the projection methods will be defined on them with public visibility despite the projection struct having no publicly accessible field. This both exposes the implementation detail of such structs, and add noise to the generated documentation.

Use the combined field visibility for this instead, where that of the most visible field is used instead.

---
 rust/pin-init/internal/src/pin_data.rs | 35 +++++++++++++++----------
 rust/pin-init/internal/src/util.rs     | 47 +++++++++++++++++++++++++++++++++-
 2 files changed, 67 insertions(+), 15 deletions(-)
rust/pin-init/internal/src/pin_data.rs +21 -14
index 08e11128d353..bcbdf33f65ae 100644
@@ -13,7 +13,7 @@
     visit_mut::VisitMut,
     Attribute, Field, Fields, GenericArgument, GenericParam, Generics, Ident, Index, Item,
     ItemStruct, Lifetime, LifetimeParam, Member, Meta, PathArguments, PathSegment, Token, Type,
-    TypeParamBound, TypePath, WhereClause, WherePredicate,
+    TypeParamBound, TypePath, Visibility, WhereClause, WherePredicate,
 };
 
 use crate::{
@@ -1332,14 +1332,21 @@ fn #check_name #combined_impl_generics (long: #long_ty) -> #short_ty #whr {
 
 fn generate_projections(info: &StructInfo) -> TokenStream {
     let ItemStruct {
-        vis,
-        ident,
-        generics,
-        ..
+        ident, generics, ..
     } = &info.struct_;
     let this_lt = Lifetime::new("'__this", Span::mixed_site());
     let this_lt_generics: Generics = parse_quote!(<#this_lt>);
 
+    // Compute the least upper bound of field visibility.
+    //
+    // This will be used for generated projection methods instead of the visibility of the struct;
+    // if the struct visibility is used, the projection method will exist on public structs that
+    // use `#[pin_data]` but the caller can do nothing with it (as all fields of the projection
+    // struct will be invisible).
+    let field_vis_lub = info.fields.iter().fold(Visibility::Inherited, |accum, f| {
+        accum.semilattice_join(&f.field.vis)
+    });
+
     // Wrap in `CombinedGenerics` because it's ty generics will always output `<>`, so it can be
     // used with `for`.
     let field_lts = CombinedGenerics(vec![
@@ -1541,7 +1548,7 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
     let (projection_def, projection_init) = if info.is_tuple_struct {
         (
             quote! {
-                #vis struct __Projection #generics_with_this_lt (
+                #field_vis_lub struct __Projection #generics_with_this_lt (
                     #(#fields_decl)*
                     ::core::marker::PhantomData<&'__this mut #ident #ty_generics>,
                 ) #whr;
@@ -1556,7 +1563,7 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
     } else {
         (
             quote! {
-                #vis struct __Projection #generics_with_this_lt
+                #field_vis_lub struct __Projection #generics_with_this_lt
                     #whr
                 {
                     #(#fields_decl)*
@@ -1576,7 +1583,7 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
     let (projection_lt_def, projection_lt_init) = if info.is_tuple_struct {
         (
             quote! {
-                #vis struct #projection_lt #generics_with_this_field_lt (
+                #field_vis_lub struct #projection_lt #generics_with_this_field_lt (
                     #(#fields_decl_lt)*
                     ::core::marker::PhantomData<&'__this mut #ident #ty_generics>,
                 ) #whr;
@@ -1591,7 +1598,7 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
     } else {
         (
             quote! {
-                #vis struct #projection_lt #generics_with_this_field_lt
+                #field_vis_lub struct #projection_lt #generics_with_this_field_lt
                     #whr
                 {
                     #(#fields_decl_lt)*
@@ -1750,7 +1757,7 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
     let (projection_ref_lt_def, projection_ref_lt_init) = if info.is_tuple_struct {
         (
             quote!(
-                #vis struct #projection_ref_lt #generics_with_this_field_ref_lt(
+                #field_vis_lub struct #projection_ref_lt #generics_with_this_field_ref_lt(
                     #(#fields_ref_decl_lt)*
                     ::core::marker::PhantomData<&'__this #ident #ty_generics>,
                 ) #whr;
@@ -1765,7 +1772,7 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
     } else {
         (
             quote! {
-                #vis struct #projection_ref_lt #generics_with_this_field_ref_lt
+                #field_vis_lub struct #projection_ref_lt #generics_with_this_field_ref_lt
                     #whr
                 {
                     #(#fields_ref_decl_lt)*
@@ -1813,7 +1820,7 @@ impl #impl_generics #ident #ty_generics
             /// These fields are **not** structurally pinned:
             #(#[doc = #not_structurally_pinned_fields_docs])*
             #[inline]
-            #vis fn project<'__this>(
+            #field_vis_lub fn project<'__this>(
                 self: ::core::pin::Pin<&'__this mut Self>,
             ) -> __Projection #ty_generics_with_this_lt {
                 // SAFETY: we only give access to `&mut` for fields not structurally pinned.
@@ -1829,7 +1836,7 @@ impl #impl_generics #ident #ty_generics
             /// These fields are **not** structurally pinned:
             #(#[doc = #not_structurally_pinned_fields_docs])*
             #[inline]
-            #vis fn with_project<'__this, R>(
+            #field_vis_lub fn with_project<'__this, R>(
                 self: ::core::pin::Pin<&'__this mut Self>,
                 f: impl for #field_lt_ty_generics ::core::ops::FnOnce(
                     #projection_lt #ty_generics_with_this_field_lt
@@ -1848,7 +1855,7 @@ impl #impl_generics #ident #ty_generics
             /// These fields are **not** structurally pinned:
             #(#[doc = #not_structurally_pinned_fields_docs])*
             #[inline]
-            #vis fn with_project_ref<'__this, R>(
+            #field_vis_lub fn with_project_ref<'__this, R>(
                 self: ::core::pin::Pin<&'__this Self>,
                 f: impl for #field_lt_ty_generics ::core::ops::FnOnce(
                     #projection_ref_lt #ty_generics_with_this_field_ref_lt
rust/pin-init/internal/src/util.rs +46 -1

56 lines not on this page · raw message

Signature · 1 line
2.54.0
author
#6 lore raw

PATCH 3/3 rust: pin-init: internal: hide `HasPinData` impl

In reply to Gary Guo

From: Gary Guo <[email protected]>

HasPinData is an implementation detail of pin-init to communicate from #[pin_data] macro to pin_init!() macro on how to initialize the struct. Thus, HasPinData is marked as #[doc(hidden)]; however, Rustdoc still renders the impl. Thus, also mark impl as #[doc(hidden)]

---
 rust/pin-init/internal/src/pin_data.rs | 1 +
 1 file changed, 1 insertion(+)
rust/pin-init/internal/src/pin_data.rs +1 -0
index bcbdf33f65ae..df1de7b27e1b 100644
@@ -2081,6 +2081,7 @@ impl #impl_generics __ThePinData #ty_generics
 
         // SAFETY: We have added the correct projection functions above to `__ThePinData` and
         // we also use the least restrictive generics possible.
+        #[doc(hidden)]
         unsafe impl #impl_generics ::pin_init::__internal::HasPinData for #struct_name #ty_generics
             #whr
         {
Signature · 1 line
2.54.0
maintainer Applied
#7 lore raw
In reply to Gary Guo
Quoted 10 lines · Gary Guo

Currently all types that use #[pin_data] will have projection method generated and visible in rustdoc. But for many types, all fields are private and thus these projection methods are not useful as public APIs.

Thus, compute a least upper bound (LUB) of field visibility and use that as visibility for projection methods instead. This means that if none of the fields are accessible publicly, the projection methods won't be as well and thus will be hidden from rustdoc.

[...]

Applied, thanks!

[1/3] rust: pin-init: internal: allow clippy::large_unopt_stack_frames

      commit: 5a2024c03eb697463e738bb216b3d321aa278877
[2/3] rust: pin-init: internal: use visibility of the most visible field for projection
      commit: ac4e562262056c02851e33163b19fab9feea6d60
[3/3] rust: pin-init: internal: hide `HasPinData` impl
      commit: 5eb5b404e6e7631a986333f00e7b3b18aafcfaa8

Best regards,

Signature · 1 line
Gary Guo <[email protected]>