diff options
Diffstat (limited to 'third_party/rust/zerofrom-derive')
-rw-r--r-- | third_party/rust/zerofrom-derive/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | third_party/rust/zerofrom-derive/Cargo.lock | 159 | ||||
-rw-r--r-- | third_party/rust/zerofrom-derive/Cargo.toml | 66 | ||||
-rw-r--r-- | third_party/rust/zerofrom-derive/LICENSE | 44 | ||||
-rw-r--r-- | third_party/rust/zerofrom-derive/README.md | 11 | ||||
-rw-r--r-- | third_party/rust/zerofrom-derive/examples/zf_derive.rs | 113 | ||||
-rw-r--r-- | third_party/rust/zerofrom-derive/src/lib.rs | 294 | ||||
-rw-r--r-- | third_party/rust/zerofrom-derive/src/visitor.rs | 120 |
8 files changed, 808 insertions, 0 deletions
diff --git a/third_party/rust/zerofrom-derive/.cargo-checksum.json b/third_party/rust/zerofrom-derive/.cargo-checksum.json new file mode 100644 index 0000000000..18bdf7b251 --- /dev/null +++ b/third_party/rust/zerofrom-derive/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.lock":"b62effc5afdc9fb930c8fbe35b9bd81ed4a1c2a1b565fbfe784368dc1fa0887e","Cargo.toml":"e9741aae41f8ae616b3dcb825b41778ba28302d7e8b6d95d9f5dfa00ea4e9bb1","LICENSE":"853f87c96f3d249f200fec6db1114427bc8bdf4afddc93c576956d78152ce978","README.md":"2ef78913a98fb26c912c47c3c96167b9b57524754dc4857fe18a2122fec4985e","examples/zf_derive.rs":"abc9e44e0baf26f2109b57c34fa69d6e550613dc7dad234c67eebd4bcde564a8","src/lib.rs":"ffca0d1bea178643e6855e084e36f85ce47f7ce0edc4b7faff086892c4cec650","src/visitor.rs":"74c1fd52b81e45d048f9fa8534ff2274e5e891ed320aede268265d2a51e742ce"},"package":"e6a647510471d372f2e6c2e6b7219e44d8c574d24fdc11c610a61455782f18c3"}
\ No newline at end of file diff --git a/third_party/rust/zerofrom-derive/Cargo.lock b/third_party/rust/zerofrom-derive/Cargo.lock new file mode 100644 index 0000000000..827c03e852 --- /dev/null +++ b/third_party/rust/zerofrom-derive/Cargo.lock @@ -0,0 +1,159 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "proc-macro2" +version = "1.0.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d433d9f1a3e8c1263d9456598b16fec66f4acc9a74dacffd35c7bb09b3a1328" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.188" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.37", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3" + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "2.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7303ef2c05cd654186cb250d29049a24840ca25d2747c25c0381c8d9e2f582e8" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "synstructure" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f36bdaa60a83aca3921b5259d5400cbf5e90fc51931376a9bd4a0eb79aa7210f" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", + "unicode-xid", +] + +[[package]] +name = "synstructure" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "285ba80e733fac80aa4270fbcdf83772a79b80aa35c97075320abfee4a915b06" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.37", + "unicode-xid", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unicode-xid" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c" + +[[package]] +name = "yoke" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61e38c508604d6bbbd292dadb3c02559aa7fff6b654a078a36217cad871636e4" +dependencies = [ + "serde", + "stable_deref_trait", + "zerofrom", +] + +[[package]] +name = "zerofrom" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df54d76c3251de27615dfcce21e636c172dafb2549cd7fd93e21c66f6ca6bea2" +dependencies = [ + "zerofrom-derive 0.1.2", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4eae7c1f7d4b8eafce526bc0771449ddc2f250881ae31c50d22c032b5a1c499" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", + "synstructure 0.12.6", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.37", + "synstructure 0.13.0", + "zerofrom", + "zerovec", +] + +[[package]] +name = "zerovec" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "198f54134cd865f437820aa3b43d0ad518af4e68ee161b444cdd15d8e567c8ea" +dependencies = [ + "yoke", + "zerofrom", +] diff --git a/third_party/rust/zerofrom-derive/Cargo.toml b/third_party/rust/zerofrom-derive/Cargo.toml new file mode 100644 index 0000000000..46e1a26c39 --- /dev/null +++ b/third_party/rust/zerofrom-derive/Cargo.toml @@ -0,0 +1,66 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2021" +name = "zerofrom-derive" +version = "0.1.3" +authors = ["Manish Goregaokar <manishsmail@gmail.com>"] +description = "Custom derive for the zerofrom crate" +readme = "README.md" +keywords = [ + "zerocopy", + "serialization", + "lifetime", + "borrow", +] +categories = [ + "data-structures", + "memory-management", + "caching", + "no-std", +] +license-file = "LICENSE" +repository = "https://github.com/unicode-org/icu4x" + +[package.metadata.workspaces] +independent = true + +[lib] +path = "src/lib.rs" +proc_macro = true + +[dependencies.proc-macro2] +version = "1.0.27" + +[dependencies.quote] +version = "1.0.9" + +[dependencies.syn] +version = "2" +features = [ + "derive", + "fold", + "visit", +] + +[dependencies.synstructure] +version = "0.13" + +[dev-dependencies.zerofrom] +version = "0.1.1" +features = ["derive"] +default-features = false + +[dev-dependencies.zerovec] +version = "0.9.4" +features = ["yoke"] +default-features = false diff --git a/third_party/rust/zerofrom-derive/LICENSE b/third_party/rust/zerofrom-derive/LICENSE new file mode 100644 index 0000000000..9845aa5f48 --- /dev/null +++ b/third_party/rust/zerofrom-derive/LICENSE @@ -0,0 +1,44 @@ +UNICODE LICENSE V3 + +COPYRIGHT AND PERMISSION NOTICE + +Copyright © 2020-2023 Unicode, Inc. + +NOTICE TO USER: Carefully read the following legal agreement. BY +DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR +SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE +TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT +DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of data files and any associated documentation (the "Data Files") or +software and any associated documentation (the "Software") to deal in the +Data Files or Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, and/or sell +copies of the Data Files or Software, and to permit persons to whom the +Data Files or Software are furnished to do so, provided that either (a) +this copyright and permission notice appear with all copies of the Data +Files or Software, or (b) this copyright and permission notice appear in +associated Documentation. + +THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF +THIRD PARTY RIGHTS. + +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE +BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, +OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, +WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, +ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA +FILES OR SOFTWARE. + +Except as contained in this notice, the name of a copyright holder shall +not be used in advertising or otherwise to promote the sale, use or other +dealings in these Data Files or Software without prior written +authorization of the copyright holder. + +— + +Portions of ICU4X may have been adapted from ICU4C and/or ICU4J. +ICU 1.8.1 to ICU 57.1 © 1995-2016 International Business Machines Corporation and others. diff --git a/third_party/rust/zerofrom-derive/README.md b/third_party/rust/zerofrom-derive/README.md new file mode 100644 index 0000000000..ad803c5994 --- /dev/null +++ b/third_party/rust/zerofrom-derive/README.md @@ -0,0 +1,11 @@ +# zerofrom-derive [![crates.io](https://img.shields.io/crates/v/zerofrom-derive)](https://crates.io/crates/zerofrom-derive) + +<!-- cargo-rdme start --> + +Custom derives for `ZeroFrom` from the `zerofrom` crate. + +<!-- cargo-rdme end --> + +## More Information + +For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). diff --git a/third_party/rust/zerofrom-derive/examples/zf_derive.rs b/third_party/rust/zerofrom-derive/examples/zf_derive.rs new file mode 100644 index 0000000000..a1bde83733 --- /dev/null +++ b/third_party/rust/zerofrom-derive/examples/zf_derive.rs @@ -0,0 +1,113 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +#![allow(unused)] + +use std::borrow::Cow; +use zerofrom::ZeroFrom; +use zerovec::{maps::ZeroMapKV, ule::AsULE, VarZeroVec, ZeroMap, ZeroVec}; + +#[derive(ZeroFrom, Copy, Clone)] +pub struct IntExample { + x: u32, +} + +#[derive(ZeroFrom, Copy, Clone)] +pub struct GenericsExample<T> { + x: u32, + y: T, +} + +#[derive(ZeroFrom, Copy, Clone)] +pub struct GenericsExampleWithDefault<T, U = usize> { + x: T, + y: U, +} + +#[derive(ZeroFrom)] +pub struct CowExample<'a> { + x: u8, + y: &'a str, + z: Cow<'a, str>, + w: Cow<'a, [u8]>, +} + +#[derive(ZeroFrom)] +pub struct ZeroVecExample<'a> { + var: VarZeroVec<'a, str>, + vec: ZeroVec<'a, u16>, +} + +#[derive(ZeroFrom)] +pub struct ZeroVecExampleWithGenerics<'a, T: AsULE> { + gen: ZeroVec<'a, T>, + vec: ZeroVec<'a, u16>, + bare: T, +} + +#[derive(ZeroFrom)] +pub struct HasTuples<'data> { + pub bar: (&'data str, &'data str), +} + +pub fn assert_zf_tuples<'b>(x: &'b HasTuples) -> HasTuples<'b> { + HasTuples::zero_from(x) +} +pub fn assert_zf_generics<'a, 'b>( + x: &'b ZeroVecExampleWithGenerics<'a, u8>, +) -> ZeroVecExampleWithGenerics<'b, u8> { + ZeroVecExampleWithGenerics::<'b, u8>::zero_from(x) +} + +#[derive(ZeroFrom)] +pub struct ZeroMapGenericExample<'a, T: for<'b> ZeroMapKV<'b> + ?Sized> { + map: ZeroMap<'a, str, T>, +} + +pub fn assert_zf_map<'b>(x: &'b ZeroMapGenericExample<str>) -> ZeroMapGenericExample<'b, str> { + ZeroMapGenericExample::zero_from(x) +} + +#[derive(Clone, ZeroFrom)] +pub struct CloningZF1 { + #[zerofrom(clone)] // Vec is not ZeroFrom, so it needs to be cloned + vec: Vec<u8>, +} + +#[derive(Clone, ZeroFrom)] +pub struct CloningZF2<'data> { + #[zerofrom(clone)] // Cow is ZeroFrom, but we force a clone + cow: Cow<'data, str>, +} + +#[derive(ZeroFrom)] +pub enum CloningZF3<'data> { + Cow(#[zerofrom(clone)] Cow<'data, str>), +} + +#[derive(ZeroFrom)] +#[zerofrom(may_borrow(T))] // instead of treating T as a copy type, we want to allow zerofromming T too +pub struct GenericsThatAreAlsoZf<T> { + x: T, + y: Option<T>, +} + +pub fn assert_zf_generics_may_borrow<'a, 'b>( + x: &'b GenericsThatAreAlsoZf<&'a str>, +) -> GenericsThatAreAlsoZf<&'b str> { + GenericsThatAreAlsoZf::<&'b str>::zero_from(x) +} + +#[derive(ZeroFrom)] +pub struct UsesGenericsThatAreAlsoZf<'a> { + x: GenericsThatAreAlsoZf<&'a str>, +} + +// Ensure it works with invariant types too +#[derive(ZeroFrom)] +pub struct UsesGenericsThatAreAlsoZfWithMap<'a> { + x: GenericsThatAreAlsoZf<ZeroMap<'a, str, str>>, +} + +fn main() {} diff --git a/third_party/rust/zerofrom-derive/src/lib.rs b/third_party/rust/zerofrom-derive/src/lib.rs new file mode 100644 index 0000000000..c6a3839fb5 --- /dev/null +++ b/third_party/rust/zerofrom-derive/src/lib.rs @@ -0,0 +1,294 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +//! Custom derives for `ZeroFrom` from the `zerofrom` crate. + +// https://github.com/unicode-org/icu4x/blob/main/docs/process/boilerplate.md#library-annotations +#![cfg_attr( + not(test), + deny( + clippy::indexing_slicing, + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::exhaustive_structs, + clippy::exhaustive_enums, + missing_debug_implementations, + ) +)] + +use core::mem; +use proc_macro::TokenStream; +use proc_macro2::{Span, TokenStream as TokenStream2}; +use quote::quote; +use std::collections::{HashMap, HashSet}; +use syn::fold::{self, Fold}; +use syn::punctuated::Punctuated; +use syn::spanned::Spanned; +use syn::{ + parse_macro_input, parse_quote, DeriveInput, Ident, Lifetime, MetaList, Token, Type, TypePath, + WherePredicate, +}; +use synstructure::Structure; +mod visitor; + +/// Custom derive for `zerofrom::ZeroFrom`, +/// +/// This implements `ZeroFrom<Ty> for Ty` for types +/// without a lifetime parameter, and `ZeroFrom<Ty<'data>> for Ty<'static>` +/// for types with a lifetime parameter. +/// +/// Apply the `#[zerofrom(clone)]` attribute to a field if it doesn't implement +/// Copy or ZeroFrom; this data will be cloned when the struct is zero_from'ed. +/// +/// Apply the `#[zerofrom(maybe_borrow(T, U, V))]` attribute to the struct to indicate +/// that certain type parameters may themselves contain borrows (by default +/// the derives assume that type parameters perform no borrows and can be copied or cloned). +/// +/// In rust versions where [this issue](https://github.com/rust-lang/rust/issues/114393) is fixed, +/// `#[zerofrom(may_borrow)]` can be applied directly to type parameters. +#[proc_macro_derive(ZeroFrom, attributes(zerofrom))] +pub fn zf_derive(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + TokenStream::from(zf_derive_impl(&input)) +} + +fn has_attr(attrs: &[syn::Attribute], name: &str) -> bool { + attrs.iter().any(|a| { + if let Ok(i) = a.parse_args::<Ident>() { + if i == name { + return true; + } + } + false + }) +} + +// Collects all idents from #[zerofrom(may_borrow(A, B, C, D))] +// needed since #[zerofrom(may_borrow)] doesn't work yet +// (https://github.com/rust-lang/rust/issues/114393) +fn get_may_borrow_attr(attrs: &[syn::Attribute]) -> Result<HashSet<Ident>, Span> { + let mut params = HashSet::new(); + for attr in attrs { + if let Ok(list) = attr.parse_args::<MetaList>() { + if list.path.is_ident("may_borrow") { + if let Ok(list) = + list.parse_args_with(Punctuated::<Ident, Token![,]>::parse_terminated) + { + params.extend(list) + } else { + return Err(attr.span()); + } + } + } + } + Ok(params) +} + +fn zf_derive_impl(input: &DeriveInput) -> TokenStream2 { + let mut tybounds = input + .generics + .type_params() + .map(|ty| { + // Strip out param defaults, we don't need them in the impl + let mut ty = ty.clone(); + ty.eq_token = None; + ty.default = None; + ty + }) + .collect::<Vec<_>>(); + let typarams = tybounds + .iter() + .map(|ty| ty.ident.clone()) + .collect::<Vec<_>>(); + let lts = input.generics.lifetimes().count(); + let name = &input.ident; + let structure = Structure::new(input); + + let may_borrow_attrs = match get_may_borrow_attr(&input.attrs) { + Ok(mb) => mb, + Err(span) => { + return syn::Error::new( + span, + "#[zerofrom(may_borrow)] on the struct takes in a comma separated list of type parameters, like so: `#[zerofrom(may_borrow(A, B, C, D)]`", + ).to_compile_error(); + } + }; + + // This contains every generic type introduced in this code. + // If the gneeric type is may_borrow, this additionally contains the identifier corresponding to + // a newly introduced mirror type parameter that we are borrowing from, similar to C in the original trait. + // For convenience, we are calling these "C types" + let generics_env: HashMap<Ident, Option<Ident>> = tybounds + .iter() + .map(|param| { + // First one doesn't work yet https://github.com/rust-lang/rust/issues/114393 + let maybe_new_param = if has_attr(¶m.attrs, "may_borrow") + || may_borrow_attrs.contains(¶m.ident) + { + Some(Ident::new( + &format!("{}ZFParamC", param.ident), + param.ident.span(), + )) + } else { + None + }; + (param.ident.clone(), maybe_new_param) + }) + .collect(); + + // Do any of the generics potentially borrow? + let generics_may_borrow = generics_env.values().any(|x| x.is_some()); + + if lts == 0 && !generics_may_borrow { + let has_clone = structure + .variants() + .iter() + .flat_map(|variant| variant.bindings().iter()) + .any(|binding| has_attr(&binding.ast().attrs, "clone")); + let (clone, clone_trait) = if has_clone { + (quote!(this.clone()), quote!(Clone)) + } else { + (quote!(*this), quote!(Copy)) + }; + let bounds: Vec<WherePredicate> = typarams + .iter() + .map(|ty| parse_quote!(#ty: #clone_trait + 'static)) + .collect(); + quote! { + impl<'zf, #(#tybounds),*> zerofrom::ZeroFrom<'zf, #name<#(#typarams),*>> for #name<#(#typarams),*> where #(#bounds),* { + fn zero_from(this: &'zf Self) -> Self { + #clone + } + } + } + } else { + if lts > 1 { + return syn::Error::new( + input.generics.span(), + "derive(ZeroFrom) cannot have multiple lifetime parameters", + ) + .to_compile_error(); + } + + let mut zf_bounds: Vec<WherePredicate> = vec![]; + let body = structure.each_variant(|vi| { + vi.construct(|f, i| { + let binding = format!("__binding_{i}"); + let field = Ident::new(&binding, Span::call_site()); + + if has_attr(&f.attrs, "clone") { + quote! { + #field.clone() + } + } else { + // the field type + let fty = replace_lifetime(&f.ty, custom_lt("'zf")); + // the corresponding lifetimey type we are borrowing from (effectively, the C type) + let lifetime_ty = + replace_lifetime_and_type(&f.ty, custom_lt("'zf_inner"), &generics_env); + + let (has_ty, has_lt) = visitor::check_type_for_parameters(&f.ty, &generics_env); + if has_ty { + // For types without type parameters, the compiler can figure out that the field implements + // ZeroFrom on its own. However, if there are type parameters, there may be complex preconditions + // to `FieldTy: ZeroFrom` that need to be satisfied. We get them to be satisfied by requiring + // `FieldTy<'zf>: ZeroFrom<'zf, FieldTy<'zf_inner>>` + if has_lt { + zf_bounds + .push(parse_quote!(#fty: zerofrom::ZeroFrom<'zf, #lifetime_ty>)); + } else { + zf_bounds.push(parse_quote!(#fty: zerofrom::ZeroFrom<'zf, #fty>)); + } + } + if has_ty || has_lt { + // By doing this we essentially require ZF to be implemented + // on all fields + quote! { + <#fty as zerofrom::ZeroFrom<'zf, #lifetime_ty>>::zero_from(#field) + } + } else { + // No lifetimes, so we can just copy + quote! { *#field } + } + } + }) + }); + // Due to the possibility of generics_may_borrow, we might reach here with no lifetimes on self, + // don't accidentally feed them to self later + let (maybe_zf_lifetime, maybe_zf_inner_lifetime) = if lts == 0 { + (quote!(), quote!()) + } else { + (quote!('zf,), quote!('zf_inner,)) + }; + + // Array of C types. Only different if generics are allowed to borrow + let mut typarams_c = typarams.clone(); + + if generics_may_borrow { + for typaram_c in &mut typarams_c { + if let Some(Some(replacement)) = generics_env.get(typaram_c) { + // we use mem::replace here so we can be really clear about the C vs the T type + let typaram_t = mem::replace(typaram_c, replacement.clone()); + zf_bounds + .push(parse_quote!(#typaram_c: zerofrom::ZeroFrom<'zf_inner, #typaram_t>)); + tybounds.push(parse_quote!(#typaram_c)); + } + } + } + + quote! { + impl<'zf, 'zf_inner, #(#tybounds),*> zerofrom::ZeroFrom<'zf, #name<#maybe_zf_inner_lifetime #(#typarams_c),*>> for #name<#maybe_zf_lifetime #(#typarams),*> + where + #(#zf_bounds,)* { + fn zero_from(this: &'zf #name<#maybe_zf_inner_lifetime #(#typarams_c),*>) -> Self { + match *this { #body } + } + } + } + } +} + +fn custom_lt(s: &str) -> Lifetime { + Lifetime::new(s, Span::call_site()) +} + +/// Replace all lifetimes in a type with a specified one +fn replace_lifetime(x: &Type, lt: Lifetime) -> Type { + struct ReplaceLifetime(Lifetime); + + impl Fold for ReplaceLifetime { + fn fold_lifetime(&mut self, _: Lifetime) -> Lifetime { + self.0.clone() + } + } + ReplaceLifetime(lt).fold_type(x.clone()) +} + +/// Replace all lifetimes in a type with a specified one, AND replace all types that have a corresponding C type +/// with the C type +fn replace_lifetime_and_type( + x: &Type, + lt: Lifetime, + generics_env: &HashMap<Ident, Option<Ident>>, +) -> Type { + struct ReplaceLifetimeAndTy<'a>(Lifetime, &'a HashMap<Ident, Option<Ident>>); + + impl Fold for ReplaceLifetimeAndTy<'_> { + fn fold_lifetime(&mut self, _: Lifetime) -> Lifetime { + self.0.clone() + } + fn fold_type_path(&mut self, i: TypePath) -> TypePath { + if i.qself.is_none() { + if let Some(ident) = i.path.get_ident() { + if let Some(Some(replacement)) = self.1.get(ident) { + return parse_quote!(#replacement); + } + } + } + fold::fold_type_path(self, i) + } + } + ReplaceLifetimeAndTy(lt, generics_env).fold_type(x.clone()) +} diff --git a/third_party/rust/zerofrom-derive/src/visitor.rs b/third_party/rust/zerofrom-derive/src/visitor.rs new file mode 100644 index 0000000000..4204b7c68d --- /dev/null +++ b/third_party/rust/zerofrom-derive/src/visitor.rs @@ -0,0 +1,120 @@ +// This file is part of ICU4X. For terms of use, please see the file +// called LICENSE at the top level of the ICU4X source tree +// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). + +//! Visitor for determining whether a type has type and non-static lifetime parameters +//! (duplicated in yoke/derive/src/visitor.rs) + +use std::collections::HashMap; +use syn::visit::{visit_lifetime, visit_type, visit_type_path, Visit}; +use syn::{Ident, Lifetime, Type, TypePath}; + +struct TypeVisitor<'a> { + /// The type parameters in scope + typarams: &'a HashMap<Ident, Option<Ident>>, + /// Whether we found a type parameter + found_typarams: bool, + /// Whether we found a non-'static lifetime parameter + found_lifetimes: bool, +} + +impl<'a, 'ast> Visit<'ast> for TypeVisitor<'a> { + fn visit_lifetime(&mut self, lt: &'ast Lifetime) { + if lt.ident != "static" { + self.found_lifetimes = true; + } + visit_lifetime(self, lt) + } + fn visit_type_path(&mut self, ty: &'ast TypePath) { + // We only need to check ty.path.get_ident() and not ty.qself or any + // generics in ty.path because the visitor will eventually visit those + // types on its own + if let Some(ident) = ty.path.get_ident() { + if let Some(maybe_borrowed) = self.typarams.get(ident) { + self.found_typarams = true; + if maybe_borrowed.is_some() { + self.found_lifetimes = true; + } + } + } + + visit_type_path(self, ty) + } +} + +/// Checks if a type has type or lifetime parameters, given the local context of +/// named type parameters. Returns (has_type_params, has_lifetime_params) +pub fn check_type_for_parameters( + ty: &Type, + typarams: &HashMap<Ident, Option<Ident>>, +) -> (bool, bool) { + let mut visit = TypeVisitor { + typarams, + found_typarams: false, + found_lifetimes: false, + }; + visit_type(&mut visit, ty); + + (visit.found_typarams, visit.found_lifetimes) +} + +#[cfg(test)] +mod tests { + use proc_macro2::Span; + use std::collections::HashMap; + use syn::{parse_quote, Ident}; + + use super::check_type_for_parameters; + fn make_typarams(params: &[&str]) -> HashMap<Ident, Option<Ident>> { + params + .iter() + .map(|x| (Ident::new(x, Span::call_site()), None)) + .collect() + } + + #[test] + fn test_simple_type() { + let environment = make_typarams(&["T", "U", "V"]); + + let ty = parse_quote!(Foo<'a, T>); + let check = check_type_for_parameters(&ty, &environment); + assert_eq!(check, (true, true)); + + let ty = parse_quote!(Foo<T>); + let check = check_type_for_parameters(&ty, &environment); + assert_eq!(check, (true, false)); + + let ty = parse_quote!(Foo<'static, T>); + let check = check_type_for_parameters(&ty, &environment); + assert_eq!(check, (true, false)); + + let ty = parse_quote!(Foo<'a>); + let check = check_type_for_parameters(&ty, &environment); + assert_eq!(check, (false, true)); + + let ty = parse_quote!(Foo<'a, Bar<U>, Baz<(V, u8)>>); + let check = check_type_for_parameters(&ty, &environment); + assert_eq!(check, (true, true)); + + let ty = parse_quote!(Foo<'a, W>); + let check = check_type_for_parameters(&ty, &environment); + assert_eq!(check, (false, true)); + } + + #[test] + fn test_assoc_types() { + let environment = make_typarams(&["T"]); + + let ty = parse_quote!(<Foo as SomeTrait<'a, T>>::Output); + let check = check_type_for_parameters(&ty, &environment); + assert_eq!(check, (true, true)); + + let ty = parse_quote!(<Foo as SomeTrait<'static, T>>::Output); + let check = check_type_for_parameters(&ty, &environment); + assert_eq!(check, (true, false)); + + let ty = parse_quote!(<T as SomeTrait<'static, Foo>>::Output); + let check = check_type_for_parameters(&ty, &environment); + assert_eq!(check, (true, false)); + } +} |