Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions internal/src/pin_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use syn::{
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::{
Expand Down Expand Up @@ -1332,14 +1332,21 @@ fn generate_variance_check(info: &StructInfo) -> TokenStream {

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 be 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![
Expand Down Expand Up @@ -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;
Expand All @@ -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)*
Expand All @@ -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;
Expand All @@ -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)*
Expand Down Expand Up @@ -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;
Expand All @@ -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)*
Expand Down Expand Up @@ -1813,7 +1820,7 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
/// 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.
Expand All @@ -1829,7 +1836,7 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
/// 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
Expand All @@ -1848,7 +1855,7 @@ fn generate_projections(info: &StructInfo) -> TokenStream {
/// 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
Expand Down Expand Up @@ -2074,6 +2081,7 @@ fn generate_the_pin_data(info: &StructInfo) -> TokenStream {

// 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
{
Expand Down
47 changes: 46 additions & 1 deletion internal/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, ToTokens};
use syn::{
parse_quote, visit::Visit, visit_mut::VisitMut, Attribute, BoundLifetimes, GenericParam,
Generics, Index, Lifetime, Member, Token, Type, TypePath,
Generics, Index, Lifetime, Member, Token, Type, TypePath, VisRestricted, Visibility,
};

use crate::DiagCtxt;
Expand Down Expand Up @@ -498,3 +498,48 @@ impl TypeExt for Type {
ret
}
}

pub(crate) trait VisibilityExt {
/// Returns the least upper bound of `self` and `other`.
///
/// If the exact upper bound cannot be determined, this may return a greater visiblity (more
/// visible).
fn semilattice_join(self, other: &Self) -> Visibility;
}

impl VisibilityExt for Visibility {
fn semilattice_join(self, other: &Self) -> Visibility {
match (&self, other) {
(Visibility::Public(_), _) | (_, Visibility::Inherited) => self,
(_, Visibility::Public(_)) | (Visibility::Inherited, _) => other.clone(),
// `pub(in some::module)` is too common, so if we get this case just bail out and return
// `pub(crate)`.
(Visibility::Restricted(a), Visibility::Restricted(b))
if a.in_token.is_some() || b.in_token.is_some() =>
{
Visibility::Restricted(VisRestricted {
pub_token: a.pub_token,
paren_token: a.paren_token,
in_token: None,
path: parse_quote!(crate),
})
}
(Visibility::Restricted(a), Visibility::Restricted(b)) => {
let a = a.path.get_ident().unwrap();
let b = b.path.get_ident().unwrap();

if a == "crate" {
self
} else if b == "crate" {
other.clone()
} else if a == "super" {
self
} else if b == "super" {
other.clone()
} else {
self
}
}
}
}
}
1 change: 1 addition & 0 deletions tests/ui/expand/many_generics.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ const _: () = {
unsafe { ::core::mem::zeroed() }
}
}
#[doc(hidden)]
unsafe impl<
'a,
'b: 'a,
Expand Down
11 changes: 6 additions & 5 deletions tests/ui/expand/pin-data.expanded.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::marker::PhantomPinned;
use pin_init::*;
struct Foo {
pub struct Foo {
array: [u8; 1024 * 1024],
_pin: PhantomPinned,
}
Expand Down Expand Up @@ -88,7 +88,7 @@ const _: () = {
}
#[doc(hidden)]
#[allow(non_snake_case, explicit_outlives_requirements)]
struct __PinDataLt {
pub struct __PinDataLt {
array: ::pin_init::__internal::PhantomInvariant<[u8; 1024 * 1024]>,
_pin: ::pin_init::__internal::PhantomInvariant<PhantomPinned>,
__pin_phantom: ::core::marker::PhantomData<Foo>,
Expand Down Expand Up @@ -138,7 +138,7 @@ const _: () = {
}
}
#[doc(hidden)]
struct __ThePinData {
pub struct __ThePinData {
__phantom: ::pin_init::__internal::PhantomInvariant<Foo>,
}
impl ::core::clone::Clone for __ThePinData {
Expand All @@ -151,7 +151,7 @@ const _: () = {
impl __ThePinData {
/// Type inference helper function.
#[inline(always)]
fn __make_closure<__F, __E>(self, f: __F) -> __F
pub fn __make_closure<__F, __E>(self, f: __F) -> __F
where
__F: ::core::ops::FnOnce(
*mut Foo,
Expand All @@ -161,10 +161,11 @@ const _: () = {
f
}
#[inline(always)]
fn __with_lt(self) -> __PinDataLt {
pub fn __with_lt(self) -> __PinDataLt {
unsafe { ::core::mem::zeroed() }
}
}
#[doc(hidden)]
unsafe impl ::pin_init::__internal::HasPinData for Foo {
type PinData = __ThePinData;
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/expand/pin-data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::marker::PhantomPinned;
use pin_init::*;

#[pin_data]
struct Foo {
pub struct Foo {
array: [u8; 1024 * 1024],
#[pin]
_pin: PhantomPinned,
Expand Down
1 change: 1 addition & 0 deletions tests/ui/expand/pinned_drop.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ const _: () = {
unsafe { ::core::mem::zeroed() }
}
}
#[doc(hidden)]
unsafe impl ::pin_init::__internal::HasPinData for Foo {
type PinData = __ThePinData;
#[inline]
Expand Down
1 change: 1 addition & 0 deletions tests/ui/expand/tuple_struct.expanded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ const _: () = {
unsafe { ::core::mem::zeroed() }
}
}
#[doc(hidden)]
unsafe impl<'a, T: Copy, const N: usize> ::pin_init::__internal::HasPinData
for Foo<'a, T, N> {
type PinData = __ThePinData<'a, T, N>;
Expand Down
Loading