yoke_derive/lib.rs
1// This file is part of ICU4X. For terms of use, please see the file
2// called LICENSE at the top level of the ICU4X source tree
3// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).
4
5// https://github.com/unicode-org/icu4x/blob/main/documents/process/boilerplate.md#library-annotations
6// #![cfg_attr(not(any(test, doc)), no_std)]
7#![cfg_attr(
8 not(test),
9 deny(
10 clippy::indexing_slicing,
11 clippy::unwrap_used,
12 clippy::expect_used,
13 clippy::panic,
14 )
15)]
16#![warn(missing_docs)]
17
18//! Custom derives for `Yokeable` from the `yoke` crate.
19
20mod lifetimes;
21mod visitor;
22
23use proc_macro::TokenStream;
24use proc_macro2::TokenStream as TokenStream2;
25use quote::quote;
26use syn::ext::IdentExt as _;
27use syn::spanned::Spanned;
28use syn::{parse_macro_input, parse_quote, DeriveInput, GenericParam, Ident, WherePredicate};
29use synstructure::Structure;
30
31use self::lifetimes::{custom_lt, ignored_lifetime_ident, replace_lifetime, static_lt};
32use self::visitor::{
33 check_parameter_for_bound_lts, check_type_for_parameters, check_where_clause_for_bound_lts,
34 CheckResult,
35};
36
37/// Custom derive for `yoke::Yokeable`.
38///
39/// If your struct contains `zerovec::ZeroMap`, then the compiler will not
40/// be able to guarantee the lifetime covariance due to the generic types on
41/// the `ZeroMap` itself. You must add the following attribute in order for
42/// the custom derive to work with `ZeroMap`.
43///
44/// ```rust,ignore
45/// #[derive(Yokeable)]
46/// #[yoke(prove_covariance_manually)]
47/// ```
48///
49/// Beyond this case, if the derive fails to compile due to lifetime issues, it likely
50/// means that the lifetime is not covariant and `Yokeable` is not safe to implement.
51#[proc_macro_derive(Yokeable, attributes(yoke))]
52pub fn yokeable_derive(input: TokenStream) -> TokenStream {
53 let input = parse_macro_input!(input as DeriveInput);
54 TokenStream::from(yokeable_derive_impl(&input))
55}
56
57/// A small amount of metadata about a field of the yokeable type
58struct FieldParamUsage {
59 uses_lt: bool,
60 uses_ty: bool,
61}
62
63impl From<CheckResult> for FieldParamUsage {
64 fn from(value: CheckResult) -> Self {
65 Self {
66 uses_lt: value.uses_lifetime_param,
67 uses_ty: value.uses_type_params,
68 }
69 }
70}
71
72fn yokeable_derive_impl(input: &DeriveInput) -> TokenStream2 {
73 let name = &input.ident;
74 let tybounds = input
75 .generics
76 .params
77 .iter()
78 .filter_map(|param| {
79 match param {
80 GenericParam::Lifetime(_) => None,
81 GenericParam::Type(ty) => {
82 // Strip out param defaults, we don't need them in the impl
83 let mut ty = ty.clone();
84 ty.default = None;
85 Some(GenericParam::Type(ty))
86 }
87 // TODO: support const-generics in a future PR
88 // GenericParam::Const(const_param) => {
89 // // Strip out param defaults, we don't need them in the impl
90 // let mut const_param = const_param.clone();
91 // const_param.eq_token = None;
92 // const_param.default = None;
93 // Some(GenericParam::Const(const_param))
94 // }
95 GenericParam::Const(_) => None,
96 }
97 })
98 .collect::<Vec<_>>();
99 let typarams = tybounds
100 .iter()
101 .map(|param| match param {
102 // We filtered out lifetime parameters
103 GenericParam::Lifetime(_) => unreachable!(),
104 GenericParam::Type(ty) => ty.ident.clone(),
105 // TODO: support const-generics in a future PR
106 // GenericParam::Const(const_param) => const_param.ident.clone(),
107 GenericParam::Const(_) => unreachable!(),
108 })
109 .collect::<Vec<_>>();
110 let wherebounds = input
111 .generics
112 .where_clause
113 .iter()
114 .flat_map(|wc| wc.predicates.iter())
115 // If some future version of Rust adds more than just lifetime and type where-bound
116 // predicates, we may want to match more predicates.
117 .filter(|p| matches!(p, WherePredicate::Type(_)))
118 .collect::<Vec<_>>();
119 // We require all type parameters be 'static, otherwise
120 // the Yokeable impl becomes really unwieldy to generate safely.
121 let static_bounds: Vec<WherePredicate> = tybounds
122 .iter()
123 .filter_map(|param| {
124 if let GenericParam::Type(ty) = param {
125 let ty = &ty.ident;
126 Some(parse_quote!(#ty: 'static))
127 } else {
128 None
129 }
130 })
131 .collect();
132 // Above idents are *not* `unraw`d, because they may be emitted by the derive
133 // (so they might actually need to be raw).
134
135 // Either the `unraw`d first lifetime parameter of the yokeable, or some ignored ident.
136 // This parameter affects `uses_lifetime_param` values of `CheckResult`s and `uses_lt`
137 // values of `FieldParamUsage`, but those values only impact the generated code if there
138 // is at least one lifetime parameter; therefore, the random ident doesn't matter.
139 let lt_param = input
140 .generics
141 .lifetimes()
142 .next()
143 .map_or_else(ignored_lifetime_ident, |lt| lt.lifetime.ident.unraw());
144 let typarams_env = tybounds
145 .iter()
146 .filter_map(|param| {
147 if let GenericParam::Type(ty) = param {
148 Some(ty.ident.unraw())
149 } else {
150 None
151 }
152 })
153 .collect();
154 let mut underscores_for_lt = 0;
155
156 // We need to do this analysis even before the case where there are zero lifetime parameters
157 // in order to choose a `'__[underscores]__yoke` lifetime.
158 // We need to check:
159 // - trait bounds on generic type parameters
160 // - default types for generic type parameters
161 // - type bounds on const generic parameters
162 // - where-bounds
163 // - field types
164 // Checking lifetime parameters and default values for const generic parameters isn't
165 // particularly useful, but does no harm, so the simplest approach to knock out the first three
166 // is to just check every parameter.
167 for param in &input.generics.params {
168 underscores_for_lt = underscores_for_lt.max(check_parameter_for_bound_lts(param));
169 }
170 if let Some(where_clause) = &input.generics.where_clause {
171 underscores_for_lt = underscores_for_lt.max(check_where_clause_for_bound_lts(where_clause));
172 }
173
174 let structure = {
175 let mut structure = Structure::new(input);
176 structure.bind_with(|_| synstructure::BindStyle::Move);
177 structure
178 };
179
180 // Information from `synstructure::Structure`, whose ordering of fields is deterministic.
181 // Note that it's crucial that we don't filter out any variants or fields from the `Structure`.
182 let mut field_info: Vec<FieldParamUsage> = Vec::new();
183 // This code creating `field_info` should not be carelessly modified, else it could cause
184 // a panic in a below `expect`.
185 for variant_info in structure.variants() {
186 for field_binding_info in variant_info.bindings() {
187 let field = field_binding_info.ast();
188 // Note: `lt_param` and everything in `typarams_env` were `unraw`d
189 let check_result = check_type_for_parameters(<_param, &typarams_env, &field.ty);
190
191 underscores_for_lt = underscores_for_lt.max(check_result.min_underscores_for_yoke_lt);
192 field_info.push(check_result.into());
193 }
194 }
195 let field_info = field_info;
196
197 // All usages of the `check_*` functions are above this point,
198 // in order to ensure that `yoke_lt` is correct.
199 let (yoke_lt, bound_lt) = {
200 let underscores = vec![b'_'; underscores_for_lt];
201 #[expect(clippy::expect_used, reason = "invariant is ensured immediately above")]
202 let underscores = str::from_utf8(&underscores).expect("_ is ASCII and thus UTF-8");
203 (
204 format!("'{underscores}yoke"),
205 format!("'_{underscores}yoke"),
206 )
207 };
208 // This is used where the `Yokeable<'a>` trait uses `'a` by default
209 let yoke_lt = custom_lt(&yoke_lt);
210 // This is used where the `Yokeable<'a>` trait uses `'b` by default
211 let bound_lt = custom_lt(&bound_lt);
212
213 let mut lts = input.generics.lifetimes();
214
215 if lts.next().is_none() {
216 // There are 0 lifetime parameters.
217
218 return quote! {
219 // This is safe because there are no lifetime parameters, and `type Output = Self`.
220 unsafe impl<#yoke_lt, #(#tybounds),*> yoke::Yokeable<#yoke_lt>
221 for #name<#(#typarams),*>
222 where
223 #(#static_bounds,)*
224 #(#wherebounds,)*
225 Self: Sized
226 {
227 type Output = Self;
228 #[inline]
229 fn transform(&self) -> &Self::Output {
230 self
231 }
232 #[inline]
233 fn transform_owned(self) -> Self::Output {
234 self
235 }
236 #[inline]
237 unsafe fn make(this: Self::Output) -> Self {
238 this
239 }
240 #[inline]
241 fn transform_mut<F>(&#yoke_lt mut self, f: F)
242 where
243 F: 'static + for<#bound_lt> FnOnce(&#bound_lt mut Self::Output) {
244 f(self)
245 }
246 }
247 };
248 };
249
250 if lts.next().is_some() {
251 // We already extracted one lifetime into `source_lt`, so this means there are
252 // multiple lifetimes.
253 return syn::Error::new(
254 input.generics.span(),
255 "derive(Yokeable) cannot have multiple lifetime parameters",
256 )
257 .to_compile_error();
258 }
259
260 let manual_covariance = input.attrs.iter().any(|a| {
261 if a.path().is_ident("yoke") {
262 if let Ok(i) = a.parse_args::<Ident>() {
263 if i == "prove_covariance_manually" {
264 return true;
265 }
266 }
267 }
268 false
269 });
270
271 if !manual_covariance {
272 // This is safe because as long as `transform()` compiles,
273 // we can be sure that `'a` is a covariant lifetime on `Self`.
274 // (Using `'a` as shorthand for `#yoke_lt`.)
275 //
276 // In particular, the operand of `&raw const` is not a location where implicit
277 // type coercion can occur, so the type of `&raw const self` is `*const &'a Self`.
278 // The RHS of a `let` with an explicit type annotation allows type coercion, so
279 // `transform` checks that `*const &'a Self` can coerce to `*const &'a Self::Output`.
280 // Most of the possible type coercions
281 // (listed at https://doc.rust-lang.org/reference/type-coercions.html)
282 // do not apply, other than subtyping coercions and transitive coercions (which do
283 // not add anything beyond subtyping coercions). In particular, there's nothing
284 // like a `DerefRaw` on `*const T`, and `&T` does not implement `Unsize`, so
285 // there cannot be an unsizing coercion from `*const &'a Self` to
286 // `*const &'a Self::Output`. Therefore, `transform` compiles if and only if
287 // a subtyping coercion is possible; this requires that `Self` must be a subtype
288 // of `Self::Output`, just as `&'static T` is a subtype of `&'a T` (for `T: 'static`).
289 // This ensures covariance.
290 //
291 // This will not work for structs involving ZeroMap since
292 // the compiler does not know that ZeroMap is covariant.
293 //
294 // This custom derive can be improved to handle this case when necessary,
295 // with `prove_covariance_manually`.
296 return quote! {
297 unsafe impl<#yoke_lt, #(#tybounds),*> yoke::Yokeable<#yoke_lt>
298 for #name<'static, #(#typarams),*>
299 where
300 #(#static_bounds,)*
301 #(#wherebounds,)*
302 // Adding `Self: Sized` here doesn't work.
303 // `for<#bound_lt> #name<#bound_lt, #(#typarams),*>: Sized`
304 // might work, though. Since these trait bounds are very finicky, it's best to just
305 // not try unless necessary.
306 {
307 type Output = #name<#yoke_lt, #(#typarams),*>;
308 #[inline]
309 fn transform(&#yoke_lt self) -> &#yoke_lt Self::Output {
310 if false {
311 let _: *const &#yoke_lt Self::Output = &raw const self;
312 }
313 self
314 }
315 #[inline]
316 fn transform_owned(self) -> Self::Output {
317 self
318 }
319 #[inline]
320 unsafe fn make(from: Self::Output) -> Self {
321 ::core::mem::transmute::<Self::Output, Self>(from)
322 }
323 #[inline]
324 fn transform_mut<F>(&#yoke_lt mut self, f: F)
325 where
326 F: 'static + for<#bound_lt> FnOnce(&#bound_lt mut Self::Output) {
327 let y = unsafe { &mut *(self as *mut Self as *mut Self::Output) };
328 f(y)
329 }
330 }
331 };
332 }
333
334 // `prove_covariance_manually` requires additional bounds
335 let mut manual_proof_bounds: Vec<WherePredicate> = Vec::new();
336 let mut yokeable_checks = TokenStream2::new();
337 let mut output_checks = TokenStream2::new();
338 let mut field_info = field_info.into_iter();
339
340 // See `synstructure::Structure::each` and `synstructure::VariantInfo::each`
341 // for the setup of the two `*_checks` token streams. We can elide some brackets compared
342 // to `synstructure`, since we know that each check defines no local variables, items, etc.
343
344 // We iterate over the fields of `structure` in the same way that `field_info` was created.
345 for variant_info in structure.variants() {
346 let mut yokeable_check_body = TokenStream2::new();
347 let mut output_check_body = TokenStream2::new();
348
349 for field_binding_info in variant_info.bindings() {
350 let field = field_binding_info.ast();
351 let field_binding = &field_binding_info.binding;
352
353 // This invariant is somewhat complicated, but immutable variables, iteration order,
354 // and creating/using one `FieldParamUsage per iteration ensure that `field_info`
355 // has an entry for this field (and it refers to the expected field).
356 #[expect(
357 clippy::expect_used,
358 reason = "See above comment; this should never panic"
359 )]
360 let FieldParamUsage { uses_lt, uses_ty } = field_info
361 .next()
362 .expect("fields of an unmutated synstructure::Structure should remain the same");
363
364 // Note that this type could be a weird non-pure macro type. However, even though
365 // we evaluate it once or twice in where-bounds, we evaluate it exactly once
366 // in the soundness-critical checks, so it can't cause UB by unexpectedly evaluating
367 // to a different type. That can only cause a compile error at worst.
368 let fty_static = replace_lifetime(<_param, &field.ty, static_lt());
369
370 // For field types that don't use type or lifetime parameters, we don't add `Yokeable`
371 // or `'static` where-bounds, and the field is required to unconditionally meet a
372 // `'static` requirement (in its output form).
373 //
374 // For field types that use the lifetime parameter but no type parameters, we also don't
375 // add any where-bounds, and the field is required to unconditionally meet a
376 // `Yokeable` requirement (in its static yokeable form).
377 // (The compiler should be able to figure out whether that requirement is satisfied.
378 // A where-bound is intentionally avoided, to avoid letting `derive(Yokeable)`
379 // compile on a struct when it's statically known that the where-bound is never
380 // satisfied.)
381 //
382 // For field types that use a type parameter but not the lifetime parameter, the field
383 // is assumed not to borrow from the cart and is therefore required to be `'static`
384 // (in its output form), and a where-bound is added for this field being `'static`.
385 //
386 // For field types that use both the lifetime parameter and type parameters, the
387 // field is required to be `Yokeable` (in its static form). Since there may be complex
388 // preconditions to `FieldTy: Yokeable` that need to be satisfied, a where-bound
389 // requires that `FieldTy<'static>: Yokeable<#yoke_lt, Output = FieldTy<#yoke_lt>>`.
390 // This requirement is also tested on the field's static yokeable form.
391
392 // Note: if `field.ty` involves a non-pure macro type, each time it's evaluated, it
393 // could be a different type. The where-bounds are relied on to make the impl compile
394 // in sane cases, *not* for soundness. Our `transform()` impl does not blindly assume
395 // that the fields' types implement `Yokeable` or `'static`, regardless of these bounds.
396 if uses_ty {
397 if uses_lt {
398 let fty_output = replace_lifetime(<_param, &field.ty, yoke_lt.clone());
399
400 manual_proof_bounds.push(
401 parse_quote!(#fty_static: yoke::Yokeable<#yoke_lt, Output = #fty_output>),
402 );
403 } else {
404 manual_proof_bounds.push(parse_quote!(#fty_static: 'static));
405 }
406 }
407 if uses_lt {
408 // This confirms that this `FieldTy` is a subtype of something which implements
409 // `Yokeable<'a>`, and since only `'static` types can be subtypes of a `'static`
410 // type (and all `Yokeable` implementors are `'static`), we have that either:
411 // - `FieldTy` is some `'static` type which does NOT implement `Yokeable`, but via
412 // function pointer subtyping or something similar, is a subtype of something
413 // implementing `Yokeable`, or
414 // - `FieldTy` is some type which does itself implement `Yokeable`.
415 // In either of those cases, it is sound to treat `FieldTy` as covariant in the `'a`
416 // parameter. (Using `'a` as shorthand for `#yoke_lt`.)
417 //
418 // Now, to justify that `FieldTy` (the field's actual type,
419 // not just `field.ty`, which may have a non-pure macro type)
420 // is a subtype of something which implements `Yokeable<'a>`:
421 //
422 // `#field_binding` has type `&'a FieldTy` (since it's a field of `&'a Self` matched
423 // as `self`). The operand of `&raw const` is not a location where implicit type
424 // coercion can occur. Therefore, `&raw const #field_binding` is guaranteed to be
425 // type `*const &'a FieldTy`. The argument to `__yoke_derive_require_yokeable`
426 // does allow type coercion.
427 // Looking at <https://doc.rust-lang.org/reference/type-coercions.html>,
428 // there are only three types of coercions that could plausibly apply:
429 // - subtyping coercions,
430 // - transitive coercions, and
431 // - unsizing coercions.
432 // (If some sort of `DerefRaw` trait gets added for `*const`, there could plausibly
433 // be problems with that. But there's no reason to think that such a trait will be
434 // added, since it'd mess with `unsafe` code, and Rust devs should recognize that.)
435 //
436 // Since `&'a _` does not implement `Unsize`, we have that `*const &'a _` does not
437 // allow an unsizing coercion to occur. Therefore, there are only subtyping
438 // coercions, since transitive coercions add nothing on top of subtyping coercions.
439 // Therefore, if this compiles, `*const &'a FieldTy` must be a subtype of
440 // `*const &'a T` where `T = #fty_static` is the generic parameter of
441 // `__yoke_derive_require_yokeable`.
442 // Looking at the signature of that function generated below, we have that
443 // `T: Yokeable<'a>` (if it compiles). Note that if `#fty_static` is incorrect,
444 // even if there is some other `T` which would work, this will just fail to compile.
445 // Since `*const _` and `&'a _` are covariant over their type parameters, we have
446 // that `FieldTy` must be a subtype of `T` in order for a subtyping coercion from
447 // `*const &'a FieldTy` to `*const &'a T` to occur.
448 //
449 // Therefore, `FieldTy` must be a subtype of something which implements
450 // `Yokeable<'a>` in order for this to compile. (Though that is not a _sufficient_
451 // condition to compile, as some weird macro type could break stuff.)
452 yokeable_check_body.extend(quote! {
453 __yoke_derive_require_yokeable::<#yoke_lt, #fty_static>(&raw const #field_binding);
454 });
455 } else {
456 // No visible nested lifetimes, so there should be nothing to be done in sane cases.
457 // However, in case a macro type does something strange and accesses the available
458 // `#yoke_lt` lifetime, we still need to check that the field's actual type is
459 // `'static` regardless of `#yoke_lt` (which we can check by ensuring that it
460 // be a subtype of a `'static` type).
461 // See reasoning in the `if` branch for why this works. The difference is that
462 // `FieldTy` is guaranteed to be a subtype of `T = #fty_static` where `T: 'static`
463 // (if this compiles). Since the field's type is a subtype of something which is
464 // `'static`, it must itself be `'static`, and therefore did not manage to use
465 // `#yoke_lt` via a macro.
466 // Note that creating and using `#fty_output` is not necessary here, since
467 // `field.ty == fty_static == fty_output` (no lifetime was visibly present which
468 // could be replaced).
469 output_check_body.extend(quote! {
470 __yoke_derive_require_static::<#yoke_lt, #fty_static>(&raw const #field_binding);
471 });
472 }
473 }
474
475 let pat = variant_info.pat();
476 yokeable_checks.extend(quote! { #pat => { #yokeable_check_body }});
477 output_checks.extend(quote! { #pat => { #output_check_body }});
478 }
479
480 quote! {
481 // SAFETY: we assert covariance in `borrowed_checks`
482 unsafe impl<#yoke_lt, #(#tybounds),*> yoke::Yokeable<#yoke_lt>
483 for #name<'static, #(#typarams),*>
484 where
485 #(#static_bounds,)*
486 #(#wherebounds,)*
487 #(#manual_proof_bounds,)*
488 // Adding `Self: Sized` here doesn't work.
489 // `for<#bound_lt> #name<#bound_lt, #(#typarams),*>: Sized`
490 // might work, though. Since these trait bounds are very finicky, it's best to just
491 // not try unless necessary.
492 {
493 type Output = #name<#yoke_lt, #(#typarams),*>;
494 #[inline]
495 fn transform(&#yoke_lt self) -> &#yoke_lt Self::Output {
496 // These are just type asserts, we don't need to run them
497 if false {
498 // This could, hypothetically, conflict with the name of one of the `FieldTy`s
499 // we read (and cause a compilation error). However, such a conflict cannot
500 // cause unsoundness, since this function is in scope no matter what.
501 // (The problem is that attempting to refer to a type named
502 // `__yoke_derive_require_yokeable` would instead refer to this function item
503 // and therefore fail.)
504 #[allow(dead_code)]
505 fn __yoke_derive_require_yokeable<
506 #yoke_lt: #yoke_lt,
507 T: yoke::Yokeable<#yoke_lt>,
508 >(_t: *const &#yoke_lt T) {}
509
510 match self {
511 #yokeable_checks
512 }
513 }
514 let output = unsafe { ::core::mem::transmute::<&#yoke_lt Self, &#yoke_lt Self::Output>(self) };
515 if false {
516 // Same deal as above.
517 #[allow(dead_code)]
518 fn __yoke_derive_require_static<
519 #yoke_lt: #yoke_lt,
520 T: 'static,
521 >(_t: *const &#yoke_lt T) {}
522
523 match output {
524 #output_checks
525 }
526 }
527 output
528 }
529 #[inline]
530 fn transform_owned(self) -> Self::Output {
531 unsafe { ::core::mem::transmute::<Self, Self::Output>(self) }
532 }
533 #[inline]
534 unsafe fn make(from: Self::Output) -> Self {
535 unsafe { ::core::mem::transmute::<Self::Output, Self>(from) }
536 }
537 #[inline]
538 fn transform_mut<F>(&#yoke_lt mut self, f: F)
539 where
540 F: 'static + for<#bound_lt> FnOnce(&#bound_lt mut Self::Output) {
541 let y = unsafe { &mut *(self as *mut Self as *mut Self::Output) };
542 f(y)
543 }
544 }
545 }
546}