diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:25 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:18:25 +0000 |
commit | 5363f350887b1e5b5dd21a86f88c8af9d7fea6da (patch) | |
tree | 35ca005eb6e0e9a1ba3bb5dbc033209ad445dc17 /vendor/yoke-derive | |
parent | Adding debian version 1.66.0+dfsg1-1. (diff) | |
download | rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.tar.xz rustc-5363f350887b1e5b5dd21a86f88c8af9d7fea6da.zip |
Merging upstream version 1.67.1+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/yoke-derive')
-rw-r--r-- | vendor/yoke-derive/.cargo-checksum.json | 1 | ||||
-rw-r--r-- | vendor/yoke-derive/Cargo.lock | 135 | ||||
-rw-r--r-- | vendor/yoke-derive/Cargo.toml | 61 | ||||
-rw-r--r-- | vendor/yoke-derive/LICENSE | 51 | ||||
-rw-r--r-- | vendor/yoke-derive/README.md | 7 | ||||
-rw-r--r-- | vendor/yoke-derive/examples/yoke_derive.rs | 86 | ||||
-rw-r--r-- | vendor/yoke-derive/src/lib.rs | 244 | ||||
-rw-r--r-- | vendor/yoke-derive/src/visitor.rs | 113 |
8 files changed, 698 insertions, 0 deletions
diff --git a/vendor/yoke-derive/.cargo-checksum.json b/vendor/yoke-derive/.cargo-checksum.json new file mode 100644 index 000000000..00b384ec1 --- /dev/null +++ b/vendor/yoke-derive/.cargo-checksum.json @@ -0,0 +1 @@ +{"files":{"Cargo.lock":"749614197321ed7a12f39f677ac7ff5f47b38bd0b610b0448d31eb0acbbfad2d","Cargo.toml":"efb71924dd343a2bc1c174a5068b9178f96d63967d32b0668d27cdc5c3e53332","LICENSE":"4ad7541d66a407234e2c84902124cef325c29f3e966353efdb800bedb8b8da21","README.md":"a386dfc08e98106071c34adf2cdfea6a60c5a1d71ca9c9ed383a4922148d14d7","examples/yoke_derive.rs":"787ad9872040733c243ec81e67e0b9651937d4e01670b6f050c13e82f1c24a4e","src/lib.rs":"f2fd1a01134d82d34c67539929ad7119f29795b719b534309ef39cbb0b2bb4de","src/visitor.rs":"24545c1e81fd35c1d2bd38a1c8d1e684dd08faed4d10d75b103c371df4446c21"},"package":"1346e4cd025ae818b88566eac7eb65ab33a994ea55f355c86889af2e7e56b14e"}
\ No newline at end of file diff --git a/vendor/yoke-derive/Cargo.lock b/vendor/yoke-derive/Cargo.lock new file mode 100644 index 000000000..2822aee8f --- /dev/null +++ b/vendor/yoke-derive/Cargo.lock @@ -0,0 +1,135 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "proc-macro2" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" + +[[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.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a864042229133ada95abf3b54fdc62ef5ccabe9515b64717bcb9a1919e59445d" +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", + "unicode-xid", +] + +[[package]] +name = "unicode-ident" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" + +[[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.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fe1d55ca72c32d573bfbd5cb2f0ca65a497854c44762957a6d3da96041a5184" +dependencies = [ + "serde", + "stable_deref_trait", + "yoke-derive 0.6.0", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58c2c5bb7c929b85c1b9ec69091b0d835f0878b4fd9eb67973b25936e06c4374" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "yoke-derive" +version = "0.6.1" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", + "yoke", + "zerovec", +] + +[[package]] +name = "zerofrom" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79e9355fccf72b04b7deaa99ce7a0f6630530acf34045391b74460fcd714de54" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8785f47d6062c1932866147f91297286a9f350b3070e9d9f0b6078e37d623c1a" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerovec" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9d919a74c17749ccb17beaf6405562e413cd94e98ba52ca1e64bbe7eefbd8b8" +dependencies = [ + "yoke", + "zerofrom", +] diff --git a/vendor/yoke-derive/Cargo.toml b/vendor/yoke-derive/Cargo.toml new file mode 100644 index 000000000..4d1eedac9 --- /dev/null +++ b/vendor/yoke-derive/Cargo.toml @@ -0,0 +1,61 @@ +# 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 = "2018" +name = "yoke-derive" +version = "0.6.1" +authors = ["Manish Goregaokar <manishsmail@gmail.com>"] +description = "Custom derive for the yoke crate" +keywords = [ + "zerocopy", + "serialization", + "lifetime", + "borrow", + "self-referential", +] +categories = [ + "data-structures", + "memory-management", + "caching", + "no-std", +] +license = "Unicode-DFS-2016" +repository = "https://github.com/unicode-org/icu4x" +resolver = "2" + +[lib] +path = "src/lib.rs" +proc_macro = true + +[dependencies.proc-macro2] +version = "1.0.27" + +[dependencies.quote] +version = "1.0.9" + +[dependencies.syn] +version = "1.0.73" +features = [ + "derive", + "fold", +] + +[dependencies.synstructure] +version = "0.12.4" + +[dev-dependencies.yoke] +version = "0.6.0" +features = ["derive"] + +[dev-dependencies.zerovec] +version = "0.9" +features = ["yoke"] diff --git a/vendor/yoke-derive/LICENSE b/vendor/yoke-derive/LICENSE new file mode 100644 index 000000000..9858d01ab --- /dev/null +++ b/vendor/yoke-derive/LICENSE @@ -0,0 +1,51 @@ +UNICODE, INC. LICENSE AGREEMENT - DATA FILES AND SOFTWARE + +See Terms of Use <https://www.unicode.org/copyright.html> +for definitions of Unicode Inc.’s Data Files and Software. + +NOTICE TO USER: Carefully read the following legal agreement. +BY DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING UNICODE INC.'S +DATA FILES ("DATA FILES"), AND/OR SOFTWARE ("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. + +COPYRIGHT AND PERMISSION NOTICE + +Copyright © 1991-2022 Unicode, Inc. All rights reserved. +Distributed under the Terms of Use in https://www.unicode.org/copyright.html. + +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Unicode data files and any associated documentation +(the "Data Files") or Unicode 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/vendor/yoke-derive/README.md b/vendor/yoke-derive/README.md new file mode 100644 index 000000000..00bb7fb3e --- /dev/null +++ b/vendor/yoke-derive/README.md @@ -0,0 +1,7 @@ +# yoke-derive [![crates.io](https://img.shields.io/crates/v/yoke-derive)](https://crates.io/crates/yoke-derive) + +Custom derives for `Yokeable` from the `yoke` crate. + +## More Information + +For more information on development, authorship, contributing etc. please visit [`ICU4X home page`](https://github.com/unicode-org/icu4x). diff --git a/vendor/yoke-derive/examples/yoke_derive.rs b/vendor/yoke-derive/examples/yoke_derive.rs new file mode 100644 index 000000000..a011d5c24 --- /dev/null +++ b/vendor/yoke-derive/examples/yoke_derive.rs @@ -0,0 +1,86 @@ +// 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 yoke::{Yoke, Yokeable}; +use zerovec::{maps::ZeroMapKV, ule::AsULE, VarZeroVec, ZeroMap, ZeroVec}; + +#[derive(Yokeable)] +pub struct StringExample { + x: String, +} + +#[derive(Yokeable, Copy, Clone)] +pub struct IntExample { + x: u32, +} + +#[derive(Yokeable, Copy, Clone)] +pub struct GenericsExample<T> { + x: u32, + y: T, +} + +#[derive(Yokeable, Copy, Clone)] +pub struct GenericsExampleWithDefault<T, U = usize> { + x: T, + y: U, +} + +#[derive(Yokeable)] +pub struct CowExample<'a> { + x: u8, + y: &'a str, + z: Cow<'a, str>, + w: Cow<'a, [u8]>, +} + +#[derive(Yokeable)] +pub struct ZeroVecExample<'a> { + var: VarZeroVec<'a, str>, + vec: ZeroVec<'a, u16>, +} + +#[derive(Yokeable)] +pub struct ZeroVecExampleWithGenerics<'a, T: AsULE> { + gen: ZeroVec<'a, T>, + vec: ZeroVec<'a, u16>, + bare: T, +} + +// Since ZeroMap has generic parameters, the Rust compiler cannot +// prove the covariance of the lifetimes. To use derive(Yokeable) +// with a type such as ZeroMap, you just add the attribute +// yoke(prove_covariance_manually) +#[derive(Yokeable)] +#[yoke(prove_covariance_manually)] +pub struct ZeroMapExample<'a> { + map: ZeroMap<'a, str, u16>, +} + +#[derive(Yokeable)] +#[yoke(prove_covariance_manually)] +pub struct ZeroMapGenericExample<'a, T: for<'b> ZeroMapKV<'b> + ?Sized> { + map: ZeroMap<'a, str, T>, +} + +pub struct AssertYokeable { + string: Yoke<StringExample, Box<[u8]>>, + int: Yoke<IntExample, Box<[u8]>>, + gen1: Yoke<GenericsExample<u32>, Box<[u8]>>, + gen2: Yoke<GenericsExample<String>, Box<[u8]>>, + gen_default1: Yoke<GenericsExampleWithDefault<String>, Box<[u8]>>, + gen_default2: Yoke<GenericsExampleWithDefault<String, u8>, Box<[u8]>>, + cow: Yoke<CowExample<'static>, Box<[u8]>>, + zv: Yoke<ZeroVecExample<'static>, Box<[u8]>>, + zv_gen1: Yoke<ZeroVecExampleWithGenerics<'static, u8>, Box<[u8]>>, + zv_gen2: Yoke<ZeroVecExampleWithGenerics<'static, char>, Box<[u8]>>, + map: Yoke<ZeroMapExample<'static>, Box<[u8]>>, + map_gen1: Yoke<ZeroMapGenericExample<'static, u32>, Box<[u8]>>, + map_gen2: Yoke<ZeroMapGenericExample<'static, str>, Box<[u8]>>, +} + +fn main() {} diff --git a/vendor/yoke-derive/src/lib.rs b/vendor/yoke-derive/src/lib.rs new file mode 100644 index 000000000..615669d84 --- /dev/null +++ b/vendor/yoke-derive/src/lib.rs @@ -0,0 +1,244 @@ +// 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 `Yokeable` from the `yoke` crate. + +use proc_macro::TokenStream; +use proc_macro2::{Span, TokenStream as TokenStream2}; +use quote::quote; +use syn::spanned::Spanned; +use syn::{parse_macro_input, parse_quote, DeriveInput, Ident, Lifetime, Type, WherePredicate}; +use synstructure::Structure; + +mod visitor; + +/// Custom derive for `yoke::Yokeable`, +/// +/// If your struct contains `zerovec::ZeroMap`, then the compiler will not +/// be able to guarantee the lifetime covariance due to the generic types on +/// the `ZeroMap` itself. You must add the following attribute in order for +/// the custom derive to work with `ZeroMap`. +/// +/// ```rust,ignore +/// #[derive(Yokeable)] +/// #[yoke(prove_covariance_manually)] +/// ``` +/// +/// Beyond this case, if the derive fails to compile due to lifetime issues, it +/// means that the lifetime is not covariant and `Yokeable` is not safe to implement. +#[proc_macro_derive(Yokeable, attributes(yoke))] +pub fn yokeable_derive(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as DeriveInput); + TokenStream::from(yokeable_derive_impl(&input)) +} + +fn yokeable_derive_impl(input: &DeriveInput) -> TokenStream2 { + let 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<_>>(); + // We require all type parameters be 'static, otherwise + // the Yokeable impl becomes really unweildy to generate safely + let static_bounds: Vec<WherePredicate> = typarams + .iter() + .map(|ty| parse_quote!(#ty: 'static)) + .collect(); + let lts = input.generics.lifetimes().count(); + if lts == 0 { + let name = &input.ident; + quote! { + // This is safe because there are no lifetime parameters. + unsafe impl<'a, #(#tybounds),*> yoke::Yokeable<'a> for #name<#(#typarams),*> where #(#static_bounds),* { + type Output = Self; + #[inline] + fn transform(&self) -> &Self::Output { + self + } + #[inline] + fn transform_owned(self) -> Self::Output { + self + } + #[inline] + unsafe fn make(this: Self::Output) -> Self { + this + } + #[inline] + fn transform_mut<F>(&'a mut self, f: F) + where + F: 'static + for<'b> FnOnce(&'b mut Self::Output) { + f(self) + } + } + // This is safe because there are no lifetime parameters. + unsafe impl<'a, #(#tybounds),*> yoke::IsCovariant<'a> for #name<#(#typarams),*> where #(#static_bounds),* {} + } + } else { + if lts != 1 { + return syn::Error::new( + input.generics.span(), + "derive(Yokeable) cannot have multiple lifetime parameters", + ) + .to_compile_error(); + } + let name = &input.ident; + let manual_covariance = input.attrs.iter().any(|a| { + if let Ok(i) = a.parse_args::<Ident>() { + if i == "prove_covariance_manually" { + return true; + } + } + false + }); + if manual_covariance { + let mut structure = Structure::new(input); + let generics_env = typarams.iter().cloned().collect(); + let static_bounds: Vec<WherePredicate> = typarams + .iter() + .map(|ty| parse_quote!(#ty: 'static)) + .collect(); + let mut yoke_bounds: Vec<WherePredicate> = vec![]; + structure.bind_with(|_| synstructure::BindStyle::Move); + let body = structure.each_variant(|vi| { + vi.construct(|f, i| { + let binding = format!("__binding_{}", i); + let field = Ident::new(&binding, Span::call_site()); + let fty = replace_lifetime(&f.ty, static_lt()); + + 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 + // Yokeable on its own. However, if there are type parameters, there may be complex preconditions + // to `FieldTy: Yokeable` that need to be satisfied. We get them to be satisfied by requiring + // `FieldTy<'static>: Yokeable<FieldTy<'a>>` + if has_lt { + let a_ty = replace_lifetime(&f.ty, custom_lt("'a")); + yoke_bounds + .push(parse_quote!(#fty: yoke::Yokeable<'a, Output = #a_ty>)); + } else { + yoke_bounds.push(parse_quote!(#fty: yoke::Yokeable<'a, Output = #fty>)); + } + } + if has_ty || has_lt { + // By calling transform_owned on all fields, we manually prove + // that the lifetimes are covariant, since this requirement + // must already be true for the type that implements transform_owned(). + quote! { + <#fty as yoke::Yokeable<'a>>::transform_owned(#field) + } + } else { + // No nested lifetimes, so nothing to be done + quote! { #field } + } + }) + }); + return quote! { + unsafe impl<'a, #(#tybounds),*> yoke::Yokeable<'a> for #name<'static, #(#typarams),*> + where #(#static_bounds,)* + #(#yoke_bounds,)* { + type Output = #name<'a, #(#typarams),*>; + #[inline] + fn transform(&'a self) -> &'a Self::Output { + unsafe { + // safety: we have asserted covariance in + // transform_owned + ::core::mem::transmute(self) + } + } + #[inline] + fn transform_owned(self) -> Self::Output { + match self { #body } + } + #[inline] + unsafe fn make(this: Self::Output) -> Self { + use core::{mem, ptr}; + // unfortunately Rust doesn't think `mem::transmute` is possible since it's not sure the sizes + // are the same + debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); + let ptr: *const Self = (&this as *const Self::Output).cast(); + #[allow(clippy::forget_copy)] // This is a noop if the struct is copy, which Clippy doesn't like + mem::forget(this); + ptr::read(ptr) + } + #[inline] + fn transform_mut<F>(&'a mut self, f: F) + where + F: 'static + for<'b> FnOnce(&'b mut Self::Output) { + unsafe { f(core::mem::transmute::<&'a mut Self, &'a mut Self::Output>(self)) } + } + } + }; + } + quote! { + // This is safe because as long as `transform()` compiles, + // we can be sure that `'a` is a covariant lifetime on `Self` + // + // This will not work for structs involving ZeroMap since + // the compiler does not know that ZeroMap is covariant. + // + // This custom derive can be improved to handle this case when + // necessary + unsafe impl<'a, #(#tybounds),*> yoke::Yokeable<'a> for #name<'static, #(#typarams),*> where #(#static_bounds),* { + type Output = #name<'a, #(#typarams),*>; + #[inline] + fn transform(&'a self) -> &'a Self::Output { + self + } + #[inline] + fn transform_owned(self) -> Self::Output { + self + } + #[inline] + unsafe fn make(this: Self::Output) -> Self { + use core::{mem, ptr}; + // unfortunately Rust doesn't think `mem::transmute` is possible since it's not sure the sizes + // are the same + debug_assert!(mem::size_of::<Self::Output>() == mem::size_of::<Self>()); + let ptr: *const Self = (&this as *const Self::Output).cast(); + mem::forget(this); + ptr::read(ptr) + } + #[inline] + fn transform_mut<F>(&'a mut self, f: F) + where + F: 'static + for<'b> FnOnce(&'b mut Self::Output) { + unsafe { f(core::mem::transmute::<&'a mut Self, &'a mut Self::Output>(self)) } + } + } + // This is safe because it is in the same block as the above impl, which only compiles + // if 'a is a covariant lifetime. + unsafe impl<'a, #(#tybounds),*> yoke::IsCovariant<'a> for #name<'a, #(#typarams),*> where #(#static_bounds),* {} + } + } +} + +fn static_lt() -> Lifetime { + Lifetime::new("'static", Span::call_site()) +} + +fn custom_lt(s: &str) -> Lifetime { + Lifetime::new(s, Span::call_site()) +} + +fn replace_lifetime(x: &Type, lt: Lifetime) -> Type { + use syn::fold::Fold; + struct ReplaceLifetime(Lifetime); + + impl Fold for ReplaceLifetime { + fn fold_lifetime(&mut self, _: Lifetime) -> Lifetime { + self.0.clone() + } + } + ReplaceLifetime(lt).fold_type(x.clone()) +} diff --git a/vendor/yoke-derive/src/visitor.rs b/vendor/yoke-derive/src/visitor.rs new file mode 100644 index 000000000..daca1da13 --- /dev/null +++ b/vendor/yoke-derive/src/visitor.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 ). + +//! Visitor for determining whether a type has type and non-static lifetime parameters + +use std::collections::HashSet; +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 HashSet<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 self.typarams.contains(ident) { + self.found_typarams = 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: &HashSet<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::HashSet; + use syn::{parse_quote, Ident}; + + use super::check_type_for_parameters; + fn make_typarams(params: &[&str]) -> HashSet<Ident> { + params + .iter() + .map(|x| Ident::new(x, Span::call_site())) + .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)); + } +} |