diplomat_core 0.15.0

Shared utilities between Diplomat macros and code generation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! Lifetime information for types.
#![allow(dead_code)]

use super::IdentBuf;
use crate::ast;
use core::fmt::Debug;
use core::hash::Hash;

use smallvec::{smallvec, SmallVec};
use std::borrow::{Borrow, Cow};

/// Convenience const representing the number of lifetimes a [`LifetimeEnv`]
/// can hold inline before needing to dynamically allocate.
pub(crate) const INLINE_NUM_LIFETIMES: usize = 4;

/// The lifetimes and bounds found on a method or type definition
#[derive(Debug)]
pub struct LifetimeEnv {
    /// List of named lifetimes in scope of the method, and their bounds
    nodes: SmallVec<[BoundedLifetime; INLINE_NUM_LIFETIMES]>,

    /// Only relevant for method LifetimeEnvs (otherwise this is nodes.len())
    ///
    /// The number of named _and_ anonymous lifetimes in the method.
    /// We store the sum since it represents the upper bound on what indices
    /// are in range of the graph. If we make a [`Lifetimes`] with
    /// `num_lifetimes` entries, then `Lifetime`s that convert into
    /// `Lifetime`s will fall into this range, and we'll know that it's
    /// a named lifetime if it's < `nodes.len()`, or that it's an anonymous
    /// lifetime if it's < `num_lifetimes`. Otherwise, we'd have to make a
    /// distinction in `Lifetime` about which context it's in.
    num_lifetimes: usize,
}

impl LifetimeEnv {
    /// Format a lifetime indexing this env for use in code
    pub fn fmt_lifetime(&self, lt: impl Borrow<Lifetime>) -> Cow<'_, str> {
        // we use Borrow here so that this can be used in templates where there's autoborrowing
        let lt = *lt.borrow();
        if let Some(lt) = self.nodes.get(lt.0) {
            Cow::from(lt.ident.as_str())
        } else if lt.0 < self.num_lifetimes {
            format!("anon_{}", lt.0 - self.nodes.len()).into()
        } else {
            panic!("Found out of range lifetime: Got {lt:?} for env with {} nodes and {} total lifetimes", self.nodes.len(), self.num_lifetimes);
        }
    }

    /// Get an iterator of all lifetimes that this must live as long as (including itself)
    /// with the first lifetime always being returned first
    pub fn all_shorter_lifetimes(
        &self,
        lt: impl Borrow<Lifetime>,
    ) -> impl Iterator<Item = Lifetime> + '_ {
        // we use Borrow here so that this can be used in templates where there's autoborrowing
        let lt = *lt.borrow();
        // longer = true, since we are looking for lifetimes this is longer than
        LifetimeTransitivityIterator::new(self, lt.0, false)
    }

    /// Same as all_shorter_lifetimes but the other way
    pub fn all_longer_lifetimes(
        &self,
        lt: impl Borrow<Lifetime>,
    ) -> impl Iterator<Item = Lifetime> + '_ {
        // we use Borrow here so that this can be used in templates where there's autoborrowing
        let lt = *lt.borrow();
        LifetimeTransitivityIterator::new(self, lt.0, true)
    }

    // List all named and unnamed lifetimes
    pub fn num_lifetimes(&self) -> usize {
        self.num_lifetimes
    }

    pub fn all_lifetimes(&self) -> impl ExactSizeIterator<Item = Lifetime> {
        (0..self.num_lifetimes()).map(Lifetime::new)
    }

    /// Get the bounds for a named lifetime (none for unnamed lifetimes)
    pub(super) fn get_bounds(&self, named_lifetime: Lifetime) -> Option<&BoundedLifetime> {
        self.nodes.get(named_lifetime.0)
    }

    /// Returns a new [`LifetimeEnv`].
    pub(super) fn new(
        nodes: SmallVec<[BoundedLifetime; INLINE_NUM_LIFETIMES]>,
        num_lifetimes: usize,
    ) -> Self {
        Self {
            nodes,
            num_lifetimes,
        }
    }

    /// Returns a fresh [`Lifetimes`] corresponding to `self`.
    pub fn lifetimes(&self) -> Lifetimes {
        let indices = (0..self.num_lifetimes)
            .map(|index| MaybeStatic::NonStatic(Lifetime::new(index)))
            .collect();

        Lifetimes { indices }
    }

    /// Returns a new [`SubtypeLifetimeVisitor`], which can visit all reachable
    /// lifetimes
    pub fn subtype_lifetimes_visitor<F>(&self, visit_fn: F) -> SubtypeLifetimeVisitor<'_, F>
    where
        F: FnMut(Lifetime),
    {
        SubtypeLifetimeVisitor::new(self, visit_fn)
    }
}

/// A lifetime in a [`LifetimeEnv`], which keeps track of which lifetimes it's
/// longer and shorter than.
///
/// Invariant: for a BoundedLifetime found inside a LifetimeEnv, all short/long connections
/// should be bidirectional.
#[derive(Debug)]
pub(super) struct BoundedLifetime {
    pub(super) ident: IdentBuf,
    /// Lifetimes longer than this (not transitive)
    ///
    /// These are the inverse graph edges compared to `shorter`
    pub(super) longer: SmallVec<[Lifetime; 2]>,
    /// Lifetimes this is shorter than (not transitive)
    ///
    /// These match `'a: 'b + 'c` bounds
    pub(super) shorter: SmallVec<[Lifetime; 2]>,
}

impl BoundedLifetime {
    /// Returns a new [`BoundedLifetime`].
    pub(super) fn new(
        ident: IdentBuf,
        longer: SmallVec<[Lifetime; 2]>,
        shorter: SmallVec<[Lifetime; 2]>,
    ) -> Self {
        Self {
            ident,
            longer,
            shorter,
        }
    }
}

/// Visit subtype lifetimes recursively, keeping track of which have already
/// been visited.
pub struct SubtypeLifetimeVisitor<'lt, F> {
    lifetime_env: &'lt LifetimeEnv,
    visited: SmallVec<[bool; INLINE_NUM_LIFETIMES]>,
    visit_fn: F,
}

impl<'lt, F> SubtypeLifetimeVisitor<'lt, F>
where
    F: FnMut(Lifetime),
{
    fn new(lifetime_env: &'lt LifetimeEnv, visit_fn: F) -> Self {
        Self {
            lifetime_env,
            visited: smallvec![false; lifetime_env.nodes.len()],
            visit_fn,
        }
    }

    /// Visit more sublifetimes. This method tracks which lifetimes have already
    /// been visited, and uses this to not visit the same lifetime twice.
    pub fn visit_subtypes(&mut self, method_lifetime: Lifetime) {
        if let Some(visited @ false) = self.visited.get_mut(method_lifetime.0) {
            *visited = true;

            (self.visit_fn)(method_lifetime);

            for longer in self.lifetime_env.nodes[method_lifetime.0].longer.iter() {
                self.visit_subtypes(*longer)
            }
        } else {
            debug_assert!(
                method_lifetime.0 > self.lifetime_env.num_lifetimes,
                "method lifetime has an internal index that's not in range of the lifetime env"
            );
        }
    }
}

/// Wrapper type for `Lifetime` and `Lifetime`, indicating that it may
/// be the `'static` lifetime.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[allow(clippy::exhaustive_enums)] // this will only ever have two variants
pub enum MaybeStatic<T> {
    Static,
    NonStatic(T),
}

impl<T> MaybeStatic<T> {
    /// Maps the lifetime, if it's not the `'static` lifetime, to another
    /// non-static lifetime.
    pub(super) fn map_nonstatic<F, R>(self, f: F) -> MaybeStatic<R>
    where
        F: FnOnce(T) -> R,
    {
        match self {
            MaybeStatic::Static => MaybeStatic::Static,
            MaybeStatic::NonStatic(lifetime) => MaybeStatic::NonStatic(f(lifetime)),
        }
    }

    /// Maps the lifetime, if it's not the `'static` lifetime, to a potentially
    /// static lifetime.
    pub(super) fn flat_map_nonstatic<R, F>(self, f: F) -> MaybeStatic<R>
    where
        F: FnOnce(T) -> MaybeStatic<R>,
    {
        match self {
            MaybeStatic::Static => MaybeStatic::Static,
            MaybeStatic::NonStatic(lifetime) => f(lifetime),
        }
    }
}

/// A lifetime that exists as part of a type name, struct signature, or method signature.
///
/// This index only makes sense in the context of a surrounding type or method; since
/// this is essentially an index into that type/method's lifetime list.
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct Lifetime(usize);

/// A set of lifetimes found on a type name, struct signature, or method signature
#[derive(Clone, Debug)]
pub struct Lifetimes {
    indices: SmallVec<[MaybeStatic<Lifetime>; 2]>,
}

impl Lifetime {
    pub(super) const fn new(index: usize) -> Self {
        Self(index)
    }
}

impl Lifetimes {
    /// Returns an iterator over the contained [`Lifetime`]s.
    pub fn lifetimes(&self) -> impl ExactSizeIterator<Item = MaybeStatic<Lifetime>> + '_ {
        self.indices.iter().copied()
    }

    pub(super) fn as_slice(&self) -> &[MaybeStatic<Lifetime>] {
        self.indices.as_slice()
    }
}

impl Lifetime {
    /// Returns a [`Lifetime`] from its AST counterparts.
    pub(super) fn from_ast(named: &ast::NamedLifetime, lifetime_env: &ast::LifetimeEnv) -> Self {
        let index = lifetime_env
            .id(named)
            .unwrap_or_else(|| panic!("lifetime `{named}` not found in lifetime env"));
        Self::new(index)
    }

    /// Returns a new [`MaybeStatic<Lifetime>`] representing `self` in the
    /// scope of the method that it appears in.
    ///
    /// For example, if we have some `Foo<'a>` type with a field `&'a Bar`, then
    /// we can call this on the `'a` on the field. If `Foo` was `Foo<'static>`
    /// in the method, then this will return `MaybeStatic::Static`. But if it
    /// was `Foo<'b>`, then this will return `MaybeStatic::NonStatic` containing
    /// the `Lifetime` corresponding to `'b`.
    pub fn as_method_lifetime(self, method_lifetimes: &Lifetimes) -> MaybeStatic<Lifetime> {
        method_lifetimes.indices[self.0]
    }
}

impl Lifetimes {
    pub(super) fn from_fn<F>(lifetimes: &[ast::Lifetime], lower_fn: F) -> Self
    where
        F: FnMut(&ast::Lifetime) -> MaybeStatic<Lifetime>,
    {
        Self {
            indices: lifetimes.iter().map(lower_fn).collect(),
        }
    }

    /// Append an additional lifetime. Used to tack on anon lifetimes
    pub(super) fn append_lifetime(&mut self, lifetime: MaybeStatic<Lifetime>) {
        self.indices.push(lifetime)
    }

    /// Returns a new [`Lifetimes`] representing the lifetimes in the scope
    /// of the method this type appears in.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # struct Alice<'a>(&'a ());
    /// # struct Bob<'b>(&'b ());
    /// struct Foo<'a, 'b> {
    ///     alice: Alice<'a>,
    ///     bob: Bob<'b>,
    /// }
    ///
    /// fn bar<'x, 'y>(arg: Foo<'x, 'y>) {}
    /// ```
    /// Here, `Foo` will have a [`Lifetimes`] containing `['a, 'b]`,
    /// and `bar` will have a [`Lifetimes`] containing `{'x: 'x, 'y: 'y}`.
    /// When we enter the scope of `Foo` as a type, we use this method to combine
    /// the two to get a new [`Lifetimes`] representing the mapping from
    /// lifetimes in `Foo`'s scope to lifetimes in `bar`s scope: `{'a: 'x, 'b: 'y}`.
    ///
    /// This tells us that `arg.alice` has lifetime `'x` in the method, and
    /// that `arg.bob` has lifetime `'y`.
    pub fn as_method_lifetimes(&self, method_lifetimes: &Lifetimes) -> Lifetimes {
        let indices = self
            .indices
            .iter()
            .map(|maybe_static_lt| {
                maybe_static_lt.flat_map_nonstatic(|lt| lt.as_method_lifetime(method_lifetimes))
            })
            .collect();

        Lifetimes { indices }
    }
}

struct LifetimeTransitivityIterator<'env> {
    env: &'env LifetimeEnv,
    visited: Vec<bool>,
    queue: Vec<usize>,
    longer: bool,
}

impl<'env> LifetimeTransitivityIterator<'env> {
    // Longer is whether we are looking for lifetimes longer or shorter than this
    fn new(env: &'env LifetimeEnv, starting: usize, longer: bool) -> Self {
        Self {
            env,
            visited: vec![false; env.num_lifetimes()],
            queue: vec![starting],
            longer,
        }
    }
}

impl<'env> Iterator for LifetimeTransitivityIterator<'env> {
    type Item = Lifetime;

    fn next(&mut self) -> Option<Lifetime> {
        while let Some(next) = self.queue.pop() {
            if self.visited[next] {
                continue;
            }
            self.visited[next] = true;

            if let Some(named) = self.env.nodes.get(next) {
                let edge_dir = if self.longer {
                    &named.longer
                } else {
                    &named.shorter
                };
                self.queue.extend(edge_dir.iter().map(|i| i.0));
            }

            return Some(Lifetime::new(next));
        }
        None
    }
}

/// Convenience type for linking the lifetimes found at a type *use* site (e.g. `&'c Foo<'a, 'b>`)
/// with the lifetimes found at its *def* site (e.g. `struct Foo<'x, 'y>`).
///
/// Construct this by calling `.linked_lifetimes()` on a StructPath or OpaquePath
pub struct LinkedLifetimes<'def, 'tcx> {
    env: &'tcx LifetimeEnv,
    self_lt: Option<MaybeStatic<Lifetime>>,
    lifetimes: &'def Lifetimes,
}

impl<'def, 'tcx> LinkedLifetimes<'def, 'tcx> {
    pub(crate) fn new(
        env: &'tcx LifetimeEnv,
        self_lt: Option<MaybeStatic<Lifetime>>,
        lifetimes: &'def Lifetimes,
    ) -> Self {
        debug_assert_eq!(
            lifetimes.lifetimes().len(),
            env.all_lifetimes().len(),
            "Should only link lifetimes between a type and its def"
        );
        Self {
            env,
            self_lt,
            lifetimes,
        }
    }

    /// Takes a lifetime at the def site and produces one at the use site
    pub fn def_to_use(&self, def_lt: Lifetime) -> MaybeStatic<Lifetime> {
        *self
            .lifetimes
            .as_slice()
            .get(def_lt.0)
            .expect("All def site lifetimes must be used!")
    }

    /// The lifetime env at the def site. Def lifetimes should be resolved
    /// against this.
    pub fn def_env(&self) -> &'tcx LifetimeEnv {
        self.env
    }

    /// Link lifetimes from the use site to lifetimes from the def site, only including
    /// lifetimes found at the def site.
    ///
    /// This will *not* include the self-lifetime, i.e. for an opaque use site `&'c Foo<'a, 'b>`
    /// this will not include `'c` (but you can obtain it from [`Self::self_lifetime()`]))
    ///
    /// The return iterator returns pairs of (use_lt, def_lt), in order.
    ///
    /// This behaves identically to [`Self::lifetimes_all()`] for `LinkedLifetimes` constructed
    /// from anything other than a borrowing opaque.
    pub fn lifetimes_def_only(
        &self,
    ) -> impl Iterator<Item = (MaybeStatic<Lifetime>, Lifetime)> + '_ {
        self.lifetimes.lifetimes().zip(self.env.all_lifetimes())
    }

    /// If there is a self-lifetime (e.g. `'c` on `&'c Foo<'a, 'b>`), return it. This lifetime
    /// isn't found at the def site.
    pub fn self_lifetime(&self) -> Option<MaybeStatic<Lifetime>> {
        self.self_lt
    }

    /// Link lifetimes from the use site to lifetimes from the def site, including self lifetimes.
    ///
    /// This returns Options since self-lifetimes do not map to anything at the def site.
    ///
    /// The return iterator returns pairs of (use_lt, def_lt), in order, with the first entry potentially being
    /// the self lifetime (which has a def_lt of None).
    ///
    /// This behaves identically to [`Self::lifetimes_all()`] for `LinkedLifetimes` constructed
    /// from anything other than a borrowing opaque.
    pub fn lifetimes_all(
        &self,
    ) -> impl Iterator<Item = (MaybeStatic<Lifetime>, Option<Lifetime>)> + '_ {
        self.self_lt.iter().map(|i| (*i, None)).chain(
            self.lifetimes
                .lifetimes()
                .zip(self.env.all_lifetimes().map(Some)),
        )
    }
}