diff options
author | Joel Galenson <jgalenson@google.com> | 2021-05-19 16:16:11 -0700 |
---|---|---|
committer | Joel Galenson <jgalenson@google.com> | 2021-05-19 16:16:11 -0700 |
commit | e20c266df7abfec352c73613c625603a29e4d701 (patch) | |
tree | f5da1b44f7b9db0b7deec0fbeec648e799f42a2d | |
parent | b14652fb5dabe359e794e0a95e382894f927a309 (diff) | |
download | pin-project-internal-e20c266df7abfec352c73613c625603a29e4d701.tar.gz |
Upgrade rust/crates/pin-project-internal to 1.0.7
Test: make
Change-Id: I1d08b98f24f55578bcc92e3a61f91c436a43ef05
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 4 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | Cargo.toml.orig | 2 | ||||
-rw-r--r-- | METADATA | 8 | ||||
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/pin_project/derive.rs | 100 | ||||
-rw-r--r-- | src/pinned_drop.rs | 3 | ||||
-rw-r--r-- | src/utils.rs | 15 |
9 files changed, 91 insertions, 47 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 6cfdedc..51b8b66 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "a51d39fcdb31fe78cc61c0053ead2beb65a4156d" + "sha1": "c110ce7bc8ad8473c735f8a2f8b4ca0f8bd4b315" } } @@ -71,5 +71,5 @@ rust_test_host { // dependent_library ["feature_list"] // proc-macro2-1.0.26 "default,proc-macro" // quote-1.0.9 "default,proc-macro" -// syn-1.0.68 "clone-impls,default,derive,full,parsing,printing,proc-macro,quote,visit-mut" -// unicode-xid-0.2.1 "default" +// syn-1.0.72 "clone-impls,default,derive,full,parsing,printing,proc-macro,quote,visit-mut" +// unicode-xid-0.2.2 "default" @@ -13,7 +13,7 @@ [package] edition = "2018" name = "pin-project-internal" -version = "1.0.6" +version = "1.0.7" authors = ["Taiki Endo <te316e89@gmail.com>"] description = "Implementation detail of the `pin-project` crate.\n" documentation = "https://docs.rs/pin-project-internal" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index 707c32b..71fc35a 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "pin-project-internal" -version = "1.0.6" +version = "1.0.7" authors = ["Taiki Endo <te316e89@gmail.com>"] edition = "2018" license = "Apache-2.0 OR MIT" @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/pin-project-internal/pin-project-internal-1.0.6.crate" + value: "https://static.crates.io/crates/pin-project-internal/pin-project-internal-1.0.7.crate" } - version: "1.0.6" + version: "1.0.7" license_type: NOTICE last_upgrade_date { year: 2021 - month: 4 - day: 1 + month: 5 + day: 19 } } @@ -9,7 +9,7 @@ ))] #![warn(unsafe_code)] #![warn(future_incompatible, rust_2018_idioms, single_use_lifetimes, unreachable_pub)] -#![warn(clippy::all, clippy::default_trait_access)] +#![warn(clippy::default_trait_access)] #![allow(clippy::needless_doctest_main)] // older compilers require explicit `extern crate`. diff --git a/src/pin_project/derive.rs b/src/pin_project/derive.rs index 82539ca..7dc1ba5 100644 --- a/src/pin_project/derive.rs +++ b/src/pin_project/derive.rs @@ -3,7 +3,7 @@ use quote::{format_ident, quote, quote_spanned, ToTokens}; use syn::{ parse_quote, token, visit_mut::VisitMut, Attribute, Data, DataEnum, DeriveInput, Error, Field, Fields, FieldsNamed, FieldsUnnamed, Generics, Ident, Index, Lifetime, LifetimeDef, Meta, - MetaList, NestedMeta, Result, Token, Type, Variant, Visibility, WhereClause, + MetaList, MetaNameValue, NestedMeta, Result, Token, Type, Variant, Visibility, WhereClause, }; use super::{ @@ -152,7 +152,7 @@ struct Context<'a> { /// The projected types. proj: ProjectedType, /// Types of the pinned fields. - pinned_fields: Vec<Type>, + pinned_fields: Vec<&'a Type>, /// Kind of the original type: struct or enum kind: TypeKind, @@ -320,13 +320,13 @@ fn validate_enum(brace_token: token::Brace, variants: &Variants) -> Result<()> { } } -fn parse_struct( - cx: &mut Context<'_>, - fields: &Fields, +fn parse_struct<'a>( + cx: &mut Context<'a>, + fields: &'a Fields, generate: &mut GenerateTokens, ) -> Result<()> { // Do this first for a better error message. - let packed_check = ensure_not_packed(&cx.orig, fields)?; + let packed_check = ensure_not_packed(&cx.orig, Some(fields))?; validate_struct(cx.orig.ident, fields)?; @@ -402,9 +402,9 @@ fn parse_struct( Ok(()) } -fn parse_enum( - cx: &mut Context<'_>, - DataEnum { brace_token, variants, .. }: &DataEnum, +fn parse_enum<'a>( + cx: &mut Context<'a>, + DataEnum { brace_token, variants, .. }: &'a DataEnum, generate: &mut GenerateTokens, ) -> Result<()> { if let ProjReplace::Unnamed { span } = &cx.project_replace { @@ -414,8 +414,12 @@ fn parse_enum( )); } - // We don't need to check for `#[repr(packed)]`, - // since it does not apply to enums. + // #[repr(packed)] cannot be apply on enums and will be rejected by rustc. + // However, we should not rely on the behavior of rustc that rejects this. + // https://github.com/taiki-e/pin-project/pull/324#discussion_r612388001 + // + // Do this first for a better error message. + ensure_not_packed(&cx.orig, None)?; validate_enum(*brace_token, variants)?; @@ -483,7 +487,7 @@ fn parse_enum( Ok(()) } -fn visit_variants(cx: &mut Context<'_>, variants: &Variants) -> Result<ProjectedVariants> { +fn visit_variants<'a>(cx: &mut Context<'a>, variants: &'a Variants) -> Result<ProjectedVariants> { let mut proj_variants = TokenStream::new(); let mut proj_ref_variants = TokenStream::new(); let mut proj_own_variants = TokenStream::new(); @@ -540,10 +544,10 @@ fn visit_variants(cx: &mut Context<'_>, variants: &Variants) -> Result<Projected }) } -fn visit_fields( - cx: &mut Context<'_>, +fn visit_fields<'a>( + cx: &mut Context<'a>, variant_ident: Option<&Ident>, - fields: &Fields, + fields: &'a Fields, delim: Delimiter, ) -> Result<ProjectedFields> { fn surround(delim: Delimiter, tokens: TokenStream) -> TokenStream { @@ -561,8 +565,8 @@ fn visit_fields( for (i, Field { attrs, vis, ident, colon_token, ty }) in fields.iter().enumerate() { let binding = ident.clone().unwrap_or_else(|| format_ident!("_{}", i)); proj_pat.extend(quote!(#binding,)); + let lifetime = &cx.proj.lifetime; if attrs.position_exact(PIN)?.is_some() { - let lifetime = &cx.proj.lifetime; proj_fields.extend(quote! { #vis #ident #colon_token ::pin_project::__private::Pin<&#lifetime mut (#ty)>, }); @@ -579,10 +583,9 @@ fn visit_fields( #ident #colon_token ::pin_project::__private::PhantomData, }); - cx.pinned_fields.push(ty.clone()); + cx.pinned_fields.push(ty); pinned_bindings.push(binding); } else { - let lifetime = &cx.proj.lifetime; proj_fields.extend(quote! { #vis #ident #colon_token &#lifetime mut (#ty), }); @@ -996,8 +999,8 @@ fn make_proj_impl( /// This currently does two checks: /// * Checks the attributes of structs to ensure there is no `[repr(packed)]`. /// * Generates a function that borrows fields without an unsafe block and -/// forbidding `safe_packed_borrows` lint. -fn ensure_not_packed(orig: &OriginalType<'_>, fields: &Fields) -> Result<TokenStream> { +/// forbidding `unaligned_references` lint. +fn ensure_not_packed(orig: &OriginalType<'_>, fields: Option<&Fields>) -> Result<TokenStream> { for meta in orig.attrs.iter().filter_map(|attr| attr.parse_meta().ok()) { if let Meta::List(list) = meta { if list.path.is_ident("repr") { @@ -1005,22 +1008,36 @@ fn ensure_not_packed(orig: &OriginalType<'_>, fields: &Fields) -> Result<TokenSt match repr { NestedMeta::Meta(Meta::Path(path)) | NestedMeta::Meta(Meta::List(MetaList { path, .. })) - if path.is_ident("packed") => - { - return Err(error!( - repr, - "#[pin_project] attribute may not be used on #[repr(packed)] types" - )); + | NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, .. })) => { + if path.is_ident("packed") { + let msg = if fields.is_none() { + // #[repr(packed)] cannot be apply on enums and will be rejected by rustc. + // However, we should not rely on the behavior of rustc that rejects this. + // https://github.com/taiki-e/pin-project/pull/324#discussion_r612388001 + "#[repr(packed)] attribute should be applied to a struct or union" + } else if let NestedMeta::Meta(Meta::NameValue(..)) = repr { + // #[repr(packed = "")] is not valid format of #[repr(packed)] and will be + // rejected by rustc. + // However, we should not rely on the behavior of rustc that rejects this. + // https://github.com/taiki-e/pin-project/pull/324#discussion_r612388001 + "#[repr(packed)] attribute should not be name-value pair" + } else { + "#[pin_project] attribute may not be used on #[repr(packed)] types" + }; + return Err(error!(repr, msg)); + } } - _ => {} + NestedMeta::Lit(..) => {} } } } } } - // As proc-macro-derive can't rewrite the structure definition, - // it's probably no longer necessary, but it keeps it for now. + let fields = match fields { + Some(fields) => fields, + None => return Ok(TokenStream::new()), + }; // Workaround for https://github.com/taiki-e/pin-project/issues/32 // Through the tricky use of proc macros, it's possible to bypass @@ -1029,7 +1046,7 @@ fn ensure_not_packed(orig: &OriginalType<'_>, fields: &Fields) -> Result<TokenSt // struct, we generate code like this: // // ```rust - // #[forbid(safe_packed_borrows)] + // #[forbid(unaligned_references)] // fn assert_not_repr_packed(val: &MyStruct) { // let _field1 = &val.field1; // let _field2 = &val.field2; @@ -1038,10 +1055,8 @@ fn ensure_not_packed(orig: &OriginalType<'_>, fields: &Fields) -> Result<TokenSt // } // ``` // - // Taking a reference to a packed field is unsafe, and applying - // `#[forbid(safe_packed_borrows)]` makes sure that doing this without - // an `unsafe` block (which we deliberately do not generate) - // is a hard error. + // Taking a reference to a packed field is UB, and applying + // `#[forbid(unaligned_references)]` makes sure that doing this is a hard error. // // If the struct ends up having `#[repr(packed)]` applied somehow, // this will generate an (unfriendly) error message. Under all reasonable @@ -1061,6 +1076,21 @@ fn ensure_not_packed(orig: &OriginalType<'_>, fields: &Fields) -> Result<TokenSt // `#[repr(packed)]` in the first place. // // See also https://github.com/taiki-e/pin-project/pull/34. + // + // Note: + // - pin-project v0.4.3 or later (#135, v0.4.0-v0.4.2 are already yanked for + // another reason) is internally proc-macro-derive, so they are not + // affected by the problem that the struct definition is rewritten by + // another macro after the #[pin_project] is expanded. + // So this is probably no longer necessary, but it keeps it for now. + // + // - Lint-based tricks aren't perfect, but they're much better than nothing: + // https://github.com/taiki-e/pin-project-lite/issues/26 + // + // - Enable both unaligned_references and safe_packed_borrows lints + // because unaligned_references lint does not exist in older compilers: + // https://github.com/taiki-e/pin-project-lite/pull/55 + // https://github.com/rust-lang/rust/pull/82525 let mut field_refs = vec![]; match fields { Fields::Named(FieldsNamed { named, .. }) => { @@ -1080,7 +1110,7 @@ fn ensure_not_packed(orig: &OriginalType<'_>, fields: &Fields) -> Result<TokenSt let (impl_generics, ty_generics, where_clause) = orig.generics.split_for_impl(); let ident = orig.ident; Ok(quote! { - #[forbid(safe_packed_borrows)] + #[forbid(unaligned_references, safe_packed_borrows)] fn __assert_not_repr_packed #impl_generics (this: &#ident #ty_generics) #where_clause { #(let _ = #field_refs;)* } diff --git a/src/pinned_drop.rs b/src/pinned_drop.rs index ecf52dd..a28a701 100644 --- a/src/pinned_drop.rs +++ b/src/pinned_drop.rs @@ -189,6 +189,9 @@ fn expand_impl(item: &mut ItemImpl) { None } + // `PinnedDrop` is a private trait and should not appear in docs. + item.attrs.push(parse_quote!(#[doc(hidden)])); + let path = &mut item.trait_.as_mut().unwrap().1; *path = parse_quote_spanned! { path.span() => ::pin_project::__private::PinnedDrop diff --git a/src/utils.rs b/src/utils.rs index 37f35ba..2db00e2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -176,7 +176,9 @@ impl<'a> ParseBufferExt<'a> for ParseBuffer<'a> { // visitors // Replace `self`/`Self` with `__self`/`self_ty`. -// Based on https://github.com/dtolnay/async-trait/blob/0.1.35/src/receiver.rs +// Based on: +// - https://github.com/dtolnay/async-trait/blob/0.1.35/src/receiver.rs +// - https://github.com/dtolnay/async-trait/commit/6029cbf375c562ca98fa5748e9d950a8ff93b0e7 pub(crate) struct ReplaceReceiver<'a>(pub(crate) &'a TypePath); @@ -313,7 +315,6 @@ impl VisitMut for ReplaceReceiver<'_> { // `Self::method` -> `<Receiver>::method` fn visit_expr_path_mut(&mut self, expr: &mut ExprPath) { if expr.qself.is_none() { - prepend_underscore_to_self(&mut expr.path.segments[0].ident); self.self_to_qself(&mut expr.qself, &mut expr.path); } visit_mut::visit_expr_path_mut(self, expr); @@ -341,6 +342,16 @@ impl VisitMut for ReplaceReceiver<'_> { visit_mut::visit_pat_tuple_struct_mut(self, pat); } + fn visit_path_mut(&mut self, path: &mut Path) { + if path.segments.len() == 1 { + // Replace `self`, but not `self::function`. + prepend_underscore_to_self(&mut path.segments[0].ident); + } + for segment in &mut path.segments { + self.visit_path_arguments_mut(&mut segment.arguments); + } + } + fn visit_item_mut(&mut self, item: &mut Item) { match item { // Visit `macro_rules!` because locally defined macros can refer to `self`. |