Skip to content

Commit

Permalink
Bug 1836420 - Upgrade pin-project to 1.1.0. r=emilio,supply-chain-rev…
Browse files Browse the repository at this point in the history
…iewers

Differential Revision: https://phabricator.services.mozilla.com/D179760
  • Loading branch information
glandium committed Jun 5, 2023
1 parent 8d36b34 commit 05cb69f
Show file tree
Hide file tree
Showing 62 changed files with 601 additions and 497 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions supply-chain/audits.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2112,11 +2112,21 @@ who = "Mike Hommey <[email protected]>"
criteria = "safe-to-run"
delta = "1.0.10 -> 1.0.12"

[[audits.pin-project]]
who = "Mike Hommey <[email protected]>"
criteria = "safe-to-run"
delta = "1.0.12 -> 1.1.0"

[[audits.pin-project-internal]]
who = "Mike Hommey <[email protected]>"
criteria = "safe-to-run"
delta = "1.0.10 -> 1.0.12"

[[audits.pin-project-internal]]
who = "Mike Hommey <[email protected]>"
criteria = "safe-to-run"
delta = "1.0.12 -> 1.1.0"

[[audits.pkcs11-bindings]]
who = "Dana Keeler <[email protected]>"
criteria = "safe-to-deploy"
Expand Down
2 changes: 1 addition & 1 deletion third_party/rust/pin-project-internal/.cargo-checksum.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"files":{"Cargo.toml":"ae1169ef7f2dc32d4b461732284ad5d66019fd3d2a5897254c240759b61e675d","LICENSE-APACHE":"0d542e0c8804e39aa7f37eb00da5a762149dc682d7829451287e11b938e94594","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","src/lib.rs":"3c1b4ae774d0c05d74997e5f88fe43d95162df14f5eaf6ebbf03ca92b2ebdf7d","src/pin_project/args.rs":"903be7b22a2eda4ed59d890feb75bd4c98e243b4faaa809aff0621d15cd06431","src/pin_project/attribute.rs":"3e392493e875f0c935525579ba9bd888d8e00b57b3d6594b7fde040c31790784","src/pin_project/derive.rs":"0060e78285d89f5807126eb7f3d8a721dd4592a9e070ca0cda2f6cf2bf34e27b","src/pin_project/mod.rs":"83e6fc982a8c136811332512abc7d368e5d09b94f245de5d19490f835e85943a","src/pinned_drop.rs":"f3d386e00ce2fe25fc817ac57f07569f9e43a519e12d977db39f4c239be4dcf4","src/utils.rs":"24372d39be74fb9b6728bca08d74ed0a8ed7915de97ada6b20ebb6243ae6eed0"},"package":"069bdb1e05adc7a8990dce9cc75370895fbe4e3d58b9b73bf1aee56359344a55"}
{"files":{"Cargo.toml":"211aa0caf26529737530a9979eefc1a3acea84a4d7ce1943efb935ca0de3cf02","LICENSE-APACHE":"0d542e0c8804e39aa7f37eb00da5a762149dc682d7829451287e11b938e94594","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","src/lib.rs":"19e5ab2e30ce5ca53e877826e4eadf7cb7311e72b568573be38ecc8c39556896","src/pin_project/args.rs":"14a11ef2e85c4f4cf00e29d3527d622500b87bdd90c60e7b0223278b1f0989ca","src/pin_project/attribute.rs":"3e392493e875f0c935525579ba9bd888d8e00b57b3d6594b7fde040c31790784","src/pin_project/derive.rs":"5e8b1dc2fbcec3a0f724fc1806116784c0fec46507a9b1ce73c34580ac4372b5","src/pin_project/mod.rs":"83e6fc982a8c136811332512abc7d368e5d09b94f245de5d19490f835e85943a","src/pinned_drop.rs":"1d4cffdd4176a16c440a2a3a5039dfcf14ff42ef9e2aa7069134dfac8ba69658","src/utils.rs":"6dc65cccfbd9c8f706dbc7003458c583d57db026d2860a965c4f12222ebb3720"},"package":"39407670928234ebc5e6e580247dd567ad73a3578460c5990f9503df207e8f07"}
6 changes: 3 additions & 3 deletions third_party/rust/pin-project-internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

[package]
edition = "2018"
rust-version = "1.37"
rust-version = "1.56"
name = "pin-project-internal"
version = "1.0.12"
version = "1.1.0"
description = """
Implementation detail of the `pin-project` crate.
"""
Expand Down Expand Up @@ -42,7 +42,7 @@ version = "1"
version = "1"

[dependencies.syn]
version = "1.0.56"
version = "2.0.1"
features = [
"full",
"visit-mut",
Expand Down
3 changes: 1 addition & 2 deletions third_party/rust/pin-project-internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
clippy::needless_doctest_main,
clippy::similar_names,
clippy::single_match_else,
clippy::too_many_lines,
clippy::unnested_or_patterns
clippy::too_many_lines
)]

// older compilers require explicit `extern crate`.
Expand Down
11 changes: 5 additions & 6 deletions third_party/rust/pin-project-internal/src/pin_project/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ pub(super) fn parse_args(attrs: &[Attribute]) -> Result<Args> {
impl Parse for Input {
fn parse(input: ParseStream<'_>) -> Result<Self> {
Ok(Self((|| {
let content = input.parenthesized().ok()?;
let private = content.parse::<Ident>().ok()?;
let private = input.parse::<Ident>().ok()?;
if private == "__private" {
content.parenthesized().ok()?.parse::<TokenStream>().ok()
input.parenthesized().ok()?.parse::<TokenStream>().ok()
} else {
None
}
Expand All @@ -31,10 +30,10 @@ pub(super) fn parse_args(attrs: &[Attribute]) -> Result<Args> {
bail!(attr, "duplicate #[pin_project] attribute");
}

let mut attrs = attrs.iter().filter(|attr| attr.path.is_ident(PIN));
let mut attrs = attrs.iter().filter(|attr| attr.path().is_ident(PIN));

let prev = if let Some(attr) = attrs.next() {
(attr, syn::parse2::<Input>(attr.tokens.clone()).unwrap().0)
(attr, syn::parse2::<Input>(attr.meta.require_list()?.tokens.clone())?.0)
} else {
// This only fails if another macro removes `#[pin]`.
bail!(TokenStream::new(), "#[pin_project] attribute has been removed");
Expand All @@ -46,7 +45,7 @@ pub(super) fn parse_args(attrs: &[Attribute]) -> Result<Args> {
// has the same span as `#[pin_project]`, it is possible
// that a useless error message will be generated.
// So, use the span of `prev_attr` if it is not a valid attribute.
let res = syn::parse2::<Input>(attr.tokens.clone()).unwrap().0;
let res = syn::parse2::<Input>(attr.meta.require_list()?.tokens.clone())?.0;
let span = match (prev_res, res) {
(Some(_), _) => attr,
(None, _) => prev_attr,
Expand Down
67 changes: 31 additions & 36 deletions third_party/rust/pin-project-internal/src/pin_project/derive.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use proc_macro2::{Delimiter, Group, Span, TokenStream};
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, MetaNameValue, NestedMeta, Result, Token, Type, Variant, Visibility, WhereClause,
parse_quote, punctuated::Punctuated, token, visit_mut::VisitMut, Attribute, Data, DataEnum,
DeriveInput, Error, Field, Fields, FieldsNamed, FieldsUnnamed, Generics, Ident, Index,
Lifetime, LifetimeParam, Meta, Result, Token, Type, Variant, Visibility, WhereClause,
};

use super::{
Expand Down Expand Up @@ -235,7 +235,7 @@ impl<'a> Context<'a> {
}
}

#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Copy, Clone, PartialEq)]
enum TypeKind {
Enum,
Struct,
Expand Down Expand Up @@ -305,7 +305,7 @@ fn validate_struct(ident: &Ident, fields: &Fields) -> Result<()> {
fn validate_enum(brace_token: token::Brace, variants: &Variants) -> Result<()> {
if variants.is_empty() {
return Err(Error::new(
brace_token.span,
brace_token.span.join(),
"#[pin_project] attribute may not be used on enums without variants",
));
}
Expand Down Expand Up @@ -569,7 +569,9 @@ fn visit_fields<'a>(
let mut proj_move = TokenStream::new();
let mut pinned_bindings = Vec::with_capacity(fields.len());

for (i, Field { attrs, vis, ident, colon_token, ty }) in fields.iter().enumerate() {
for (i, Field { attrs, vis, ident, colon_token, ty, mutability: _ }) in
fields.iter().enumerate()
{
let binding = ident.clone().unwrap_or_else(|| format_ident!("_{}", i));
proj_pat.extend(quote!(#binding,));
let lifetime = &cx.proj.lifetime;
Expand Down Expand Up @@ -768,7 +770,7 @@ fn make_unpin_impl(cx: &Context<'_>) -> TokenStream {
// This ensures that any unused type parameters
// don't end up with `Unpin` bounds.
let lifetime_fields = cx.orig.generics.lifetimes().enumerate().map(
|(i, LifetimeDef { lifetime, .. })| {
|(i, LifetimeParam { lifetime, .. })| {
let field_ident = format_ident!("__lifetime{}", i);
quote!(#field_ident: &#lifetime ())
},
Expand Down Expand Up @@ -1016,33 +1018,26 @@ fn make_proj_impl(
/// - Generates a function that borrows fields without an unsafe block and
/// 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 {
for attr in orig.attrs {
if let Meta::List(ref list) = attr.meta {
if list.path.is_ident("repr") {
for repr in list.nested.iter() {
match repr {
NestedMeta::Meta(Meta::Path(path))
| NestedMeta::Meta(Meta::List(MetaList { path, .. }))
| 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"
};
bail!(repr, msg);
}
}
NestedMeta::Lit(..) => {}
for repr in list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)? {
if repr.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 repr.require_name_value().is_ok() {
// #[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"
};
bail!(repr, msg);
}
}
}
Expand All @@ -1063,10 +1058,10 @@ fn ensure_not_packed(orig: &OriginalType<'_>, fields: Option<&Fields>) -> Result
// ```rust
// #[forbid(unaligned_references)]
// fn assert_not_repr_packed(val: &MyStruct) {
// let _field1 = &val.field1;
// let _field2 = &val.field2;
// let _field_1 = &val.field_1;
// let _field_2 = &val.field_2;
// ...
// let _fieldn = &val.fieldn;
// let _field_n = &val.field_n;
// }
// ```
//
Expand Down
59 changes: 34 additions & 25 deletions third_party/rust/pin-project-internal/src/pinned_drop.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens};
use syn::{
parse_quote, spanned::Spanned, visit_mut::VisitMut, Error, FnArg, GenericArgument, ImplItem,
ItemImpl, Pat, PatIdent, Path, PathArguments, Result, ReturnType, Signature, Token, Type,
TypePath, TypeReference,
parse_quote, spanned::Spanned, token::Colon, visit_mut::VisitMut, Error, FnArg,
GenericArgument, Ident, ImplItem, ItemImpl, Pat, PatIdent, PatType, Path, PathArguments,
Result, ReturnType, Signature, Token, Type, TypePath, TypeReference,
};

use crate::utils::{parse_as_empty, prepend_underscore_to_self, ReplaceReceiver, SliceExt};
use crate::utils::{ReplaceReceiver, SliceExt};

pub(crate) fn attribute(args: &TokenStream, mut input: ItemImpl) -> TokenStream {
let res = (|| -> Result<()> {
parse_as_empty(args)?;
if !args.is_empty() {
bail!(args, "unexpected argument: `{}`", args)
}
validate_impl(&input)?;
expand_impl(&mut input);
Ok(())
Expand Down Expand Up @@ -85,7 +87,7 @@ fn validate_impl(item: &ItemImpl) -> Result<()> {
ImplItem::Type(item) => {
bail!(item, "type `{}` is not a member of trait `PinnedDrop`", item.ident)
}
ImplItem::Method(method) => {
ImplItem::Fn(method) => {
validate_sig(&method.sig)?;
if i == 0 {
Ok(())
Expand Down Expand Up @@ -124,14 +126,15 @@ fn validate_sig(sig: &Signature) -> Result<()> {

match sig.inputs.len() {
1 => {}
0 => return Err(Error::new(sig.paren_token.span, INVALID_ARGUMENT)),
0 => return Err(Error::new(sig.paren_token.span.join(), INVALID_ARGUMENT)),
_ => bail!(sig.inputs, INVALID_ARGUMENT),
}

if let Some(FnArg::Typed(arg)) = sig.receiver() {
if let Some(arg) = sig.receiver() {
// (mut) self: <path>
if let Some(path) = get_ty_path(&arg.ty) {
let ty = path.segments.last().unwrap();
let ty =
path.segments.last().expect("Type paths should always have at least one segment");
if let PathArguments::AngleBracketed(args) = &ty.arguments {
// (mut) self: (<path>::)<ty><&mut <elem>..>
if let Some(GenericArgument::Type(Type::Reference(TypeReference {
Expand Down Expand Up @@ -175,25 +178,16 @@ fn validate_sig(sig: &Signature) -> Result<()> {
// }
//
fn expand_impl(item: &mut ItemImpl) {
fn get_arg_pat(arg: &mut FnArg) -> Option<&mut PatIdent> {
if let FnArg::Typed(arg) = arg {
if let Pat::Ident(ident) = &mut *arg.pat {
return Some(ident);
}
}
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;
let path = &mut item.trait_.as_mut().expect("unexpected inherent impl").1;
*path = parse_quote_spanned! { path.span() =>
::pin_project::__private::PinnedDrop
};

let method =
if let ImplItem::Method(method) = &mut item.items[0] { method } else { unreachable!() };
if let ImplItem::Fn(method) = &mut item.items[0] { method } else { unreachable!() };

// `fn drop(mut self: Pin<&mut Self>)` -> `fn __drop_inner<T>(mut __self: Pin<&mut Receiver>)`
let drop_inner = {
Expand All @@ -203,8 +197,20 @@ fn expand_impl(item: &mut ItemImpl) {
drop_inner.block.stmts.insert(0, parse_quote!(fn #ident() {}));
drop_inner.sig.ident = ident;
drop_inner.sig.generics = item.generics.clone();
let self_pat = get_arg_pat(&mut drop_inner.sig.inputs[0]).unwrap();
prepend_underscore_to_self(&mut self_pat.ident);
let receiver = drop_inner.sig.receiver().expect("drop() should have a receiver").clone();
let pat = Box::new(Pat::Ident(PatIdent {
attrs: Vec::new(),
by_ref: None,
mutability: receiver.mutability,
ident: Ident::new("__self", receiver.self_token.span()),
subpat: None,
}));
drop_inner.sig.inputs[0] = FnArg::Typed(PatType {
attrs: receiver.attrs,
pat,
colon_token: Colon::default(),
ty: receiver.ty,
});
let self_ty = if let Type::Path(ty) = &*item.self_ty { ty } else { unreachable!() };
let mut visitor = ReplaceReceiver(self_ty);
visitor.visit_signature_mut(&mut drop_inner.sig);
Expand All @@ -214,9 +220,12 @@ fn expand_impl(item: &mut ItemImpl) {

// `fn drop(mut self: Pin<&mut Self>)` -> `unsafe fn drop(self: Pin<&mut Self>)`
method.sig.unsafety = Some(<Token![unsafe]>::default());
let self_pat = get_arg_pat(&mut method.sig.inputs[0]).unwrap();
self_pat.mutability = None;
let self_token = &self_pat.ident;
let self_token = if let FnArg::Receiver(ref mut rec) = method.sig.inputs[0] {
rec.mutability = None;
&rec.self_token
} else {
panic!("drop() should have a receiver")
};

method.block.stmts = parse_quote! {
#[allow(clippy::needless_pass_by_value)] // This lint does not warn the receiver.
Expand Down
Loading

0 comments on commit 05cb69f

Please sign in to comment.