diff --git a/internal/src/pin_data.rs b/internal/src/pin_data.rs index 8cd9bf13..55c62749 100644 --- a/internal/src/pin_data.rs +++ b/internal/src/pin_data.rs @@ -5,10 +5,10 @@ use quote::{format_ident, quote, ToTokens}; use syn::{ parse::{End, Nothing, Parse}, parse_quote, parse_quote_spanned, + punctuated::Punctuated, spanned::Spanned, visit_mut::VisitMut, Field, Fields, Generics, Ident, Index, Item, Member, PathSegment, Type, TypePath, Visibility, - WhereClause, }; use crate::{ @@ -231,21 +231,11 @@ fn generate_unpin_impl( generics: &Generics, fields: &[FieldInfo<'_>], ) -> TokenStream { - let (_, ty_generics, _) = generics.split_for_impl(); - let mut generics_with_pin_lt = generics.clone(); - generics_with_pin_lt.params.insert(0, parse_quote!('__pin)); - generics_with_pin_lt.make_where_clause(); - let ( - impl_generics_with_pin_lt, - ty_generics_with_pin_lt, - Some(WhereClause { - where_token, - predicates, - }), - ) = generics_with_pin_lt.split_for_impl() - else { - unreachable!() - }; + let (impl_generics, ty_generics, whr) = generics.split_for_impl(); + let predicates = whr + .map(|x| &x.predicates) + .unwrap_or(const { &Punctuated::new() }); + let pinned_fields = fields.iter().filter(|f| f.pinned).map(|f| { let ident = f.member.as_ident(); let ty = &f.field.ty; @@ -260,19 +250,18 @@ fn generate_unpin_impl( dead_code, // The fields below are never used. non_snake_case // The warning will be emitted on the struct definition. )] - struct __Unpin #generics_with_pin_lt - #where_token - #predicates + struct __Unpin #generics #whr { - __phantom_pin: ::pin_init::__internal::PhantomInvariantLifetime<'__pin>, __phantom: ::pin_init::__internal::PhantomInvariant<#ident #ty_generics>, #(#pinned_fields),* } #[doc(hidden)] - impl #impl_generics_with_pin_lt ::core::marker::Unpin for #ident #ty_generics - #where_token - __Unpin #ty_generics_with_pin_lt: ::core::marker::Unpin, + impl #impl_generics ::core::marker::Unpin for #ident #ty_generics + where + // the `for<'__dummy>` HRTB makes this not error without the `trivial_bounds` + // feature . + for<'__dummy> __Unpin #ty_generics: ::core::marker::Unpin, #predicates {} } diff --git a/src/__internal.rs b/src/__internal.rs index 8e9fd18b..67a354fa 100644 --- a/src/__internal.rs +++ b/src/__internal.rs @@ -42,22 +42,6 @@ impl PhantomInvariant { } } -/// Zero-sized type used to mark a lifetime as invariant. -/// -/// This is a polyfill for the [unstable type] in the standard library of the same name. -/// -/// [unstable type]: https://doc.rust-lang.org/nightly/std/marker/struct.PhantomInvariantLifetime.html -#[repr(transparent)] -#[derive(Clone, Copy, Default)] -pub struct PhantomInvariantLifetime<'a>(PhantomInvariant<&'a ()>); - -impl PhantomInvariantLifetime<'_> { - #[inline(always)] - pub const fn new() -> Self { - Self(PhantomInvariant::new()) - } -} - /// Token type to signify successful initialization. /// /// Can only be constructed via the unsafe [`Self::new`] function. The initializer macros use this diff --git a/tests/ui/compile-fail/pin_data/tuple_struct_pinned_field_not_unpin.stderr b/tests/ui/compile-fail/pin_data/tuple_struct_pinned_field_not_unpin.stderr index 05237ec5..71d6ec7b 100644 --- a/tests/ui/compile-fail/pin_data/tuple_struct_pinned_field_not_unpin.stderr +++ b/tests/ui/compile-fail/pin_data/tuple_struct_pinned_field_not_unpin.stderr @@ -2,11 +2,11 @@ error[E0277]: `PhantomPinned` cannot be unpinned --> tests/ui/compile-fail/pin_data/tuple_struct_pinned_field_not_unpin.rs:10:20 | 10 | assert_unpin::>(); - | ^^^^^^^^^^^^ within `__Unpin<'_, usize>`, the trait `Unpin` is not implemented for `PhantomPinned` + | ^^^^^^^^^^^^ within `__Unpin`, the trait `Unpin` is not implemented for `PhantomPinned` | = note: consider using the `pin!` macro consider using `Box::pin` if you need to access the pinned value outside of the current scope -note: required because it appears within the type `__Unpin<'_, usize>` +note: required because it appears within the type `__Unpin` --> tests/ui/compile-fail/pin_data/tuple_struct_pinned_field_not_unpin.rs:4:1 | 4 | #[pin_data] diff --git a/tests/ui/expand/many_generics.expanded.rs b/tests/ui/expand/many_generics.expanded.rs index 4007476e..ee352bd8 100644 --- a/tests/ui/expand/many_generics.expanded.rs +++ b/tests/ui/expand/many_generics.expanded.rs @@ -162,24 +162,18 @@ const _: () = { } } #[allow(dead_code, non_snake_case)] - struct __Unpin<'__pin, 'a, 'b: 'a, T: Bar<'b> + ?Sized + 'a, const SIZE: usize = 0> + struct __Unpin<'a, 'b: 'a, T: Bar<'b> + ?Sized + 'a, const SIZE: usize = 0> where T: Bar<'a, 1>, { - __phantom_pin: ::pin_init::__internal::PhantomInvariantLifetime<'__pin>, __phantom: ::pin_init::__internal::PhantomInvariant>, _pin: PhantomPinned, } #[doc(hidden)] - impl< - '__pin, - 'a, - 'b: 'a, - T: Bar<'b> + ?Sized + 'a, - const SIZE: usize, - > ::core::marker::Unpin for Foo<'a, 'b, T, SIZE> + impl<'a, 'b: 'a, T: Bar<'b> + ?Sized + 'a, const SIZE: usize> ::core::marker::Unpin + for Foo<'a, 'b, T, SIZE> where - __Unpin<'__pin, 'a, 'b, T, SIZE>: ::core::marker::Unpin, + for<'__dummy> __Unpin<'a, 'b, T, SIZE>: ::core::marker::Unpin, T: Bar<'a, 1>, {} impl<'a, 'b: 'a, T: Bar<'b> + ?Sized + 'a, const SIZE: usize> ::core::ops::Drop diff --git a/tests/ui/expand/pin-data.expanded.rs b/tests/ui/expand/pin-data.expanded.rs index c29811a1..93992e92 100644 --- a/tests/ui/expand/pin-data.expanded.rs +++ b/tests/ui/expand/pin-data.expanded.rs @@ -101,15 +101,14 @@ const _: () = { } } #[allow(dead_code, non_snake_case)] - struct __Unpin<'__pin> { - __phantom_pin: ::pin_init::__internal::PhantomInvariantLifetime<'__pin>, + struct __Unpin { __phantom: ::pin_init::__internal::PhantomInvariant, _pin: PhantomPinned, } #[doc(hidden)] - impl<'__pin> ::core::marker::Unpin for Foo + impl ::core::marker::Unpin for Foo where - __Unpin<'__pin>: ::core::marker::Unpin, + for<'__dummy> __Unpin: ::core::marker::Unpin, {} trait MustNotImplDrop {} impl MustNotImplDrop for T {} diff --git a/tests/ui/expand/pinned_drop.expanded.rs b/tests/ui/expand/pinned_drop.expanded.rs index bd1ff78a..6681e531 100644 --- a/tests/ui/expand/pinned_drop.expanded.rs +++ b/tests/ui/expand/pinned_drop.expanded.rs @@ -101,15 +101,14 @@ const _: () = { } } #[allow(dead_code, non_snake_case)] - struct __Unpin<'__pin> { - __phantom_pin: ::pin_init::__internal::PhantomInvariantLifetime<'__pin>, + struct __Unpin { __phantom: ::pin_init::__internal::PhantomInvariant, _pin: PhantomPinned, } #[doc(hidden)] - impl<'__pin> ::core::marker::Unpin for Foo + impl ::core::marker::Unpin for Foo where - __Unpin<'__pin>: ::core::marker::Unpin, + for<'__dummy> __Unpin: ::core::marker::Unpin, {} impl ::core::ops::Drop for Foo { fn drop(&mut self) { diff --git a/tests/ui/expand/tuple_struct.expanded.rs b/tests/ui/expand/tuple_struct.expanded.rs index 61aefecc..a06c074f 100644 --- a/tests/ui/expand/tuple_struct.expanded.rs +++ b/tests/ui/expand/tuple_struct.expanded.rs @@ -116,15 +116,14 @@ const _: () = { } } #[allow(dead_code, non_snake_case)] - struct __Unpin<'__pin, 'a, T: Copy, const N: usize> { - __phantom_pin: ::pin_init::__internal::PhantomInvariantLifetime<'__pin>, + struct __Unpin<'a, T: Copy, const N: usize> { __phantom: ::pin_init::__internal::PhantomInvariant>, _1: PhantomPinned, } #[doc(hidden)] - impl<'__pin, 'a, T: Copy, const N: usize> ::core::marker::Unpin for Foo<'a, T, N> + impl<'a, T: Copy, const N: usize> ::core::marker::Unpin for Foo<'a, T, N> where - __Unpin<'__pin, 'a, T, N>: ::core::marker::Unpin, + for<'__dummy> __Unpin<'a, T, N>: ::core::marker::Unpin, {} trait MustNotImplDrop {} impl MustNotImplDrop for T {}