intuicio-derive 0.53.0

Procedural macro module for Intuicio scripting platform
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! Expansion of the `intuicio_methods` attribute.
//!
//! The `impl` block is emitted unchanged and a second one is added beside
//! it. For every method carrying `#[intuicio_method]` that block holds
//! three items named after the method:
//!
//! - `<name>__intuicio_function` - the `fn(&mut Context, &Registry)` shim.
//! - `<name>__define_signature` - builds the `FunctionSignature`, with the
//!   type of the `impl` attached so the method stays grouped under it.
//! - `<name>__define_function` - pairs the two into a `Function`.
//!
//! A `self` receiver becomes the first parameter, named `this`. Methods
//! without the marker attribute are skipped entirely.
use proc_macro::{Span, TokenStream};
use quote::{ToTokens, quote};
use std::collections::HashMap;
use syn::{
    AttributeArgs, FnArg, Ident, ImplItem, ItemImpl, Lit, Meta, NestedMeta, Pat, ReturnType, Type,
    Visibility, parse_macro_input,
};

/// Everything the attribute can carry on the `impl`.
#[derive(Default)]
struct ImplAttributes {
    /// Module the methods are registered under.
    pub module_name: Option<String>,
    /// `ValueTransformer` used by every method that does not name its own.
    pub transformer: Option<Ident>,
}

/// Everything `#[intuicio_method(...)]` can carry.
#[derive(Default)]
struct MethodAttributes {
    /// Registered name, [`None`] keeps the Rust one.
    pub name: Option<String>,
    /// Whether the argument named `registry` comes from the caller rather than
    /// the stack.
    pub use_registry: bool,
    /// Whether the argument named `context` comes from the caller rather than
    /// the stack.
    pub use_context: bool,
    /// Whether to print the expansion while compiling.
    pub debug: bool,
    /// `ValueTransformer` for this method, overriding the one on the `impl`.
    pub transformer: Option<Ident>,
    /// Argument that a returned reference borrows from, usually `this`.
    pub dependency: Option<Ident>,
    /// `Meta` source attached to the method.
    pub meta: Option<String>,
    /// `Meta` source per argument name.
    pub args_meta: HashMap<String, String>,
}

/// Reads the attribute list on the `impl` into [`ImplAttributes`].
///
/// Returns from the surrounding function on a parse error, so it only works
/// inside one that returns [`TokenStream`].
macro_rules! parse_impl_attributes {
    ($attributes:ident) => {{
        let mut result = ImplAttributes::default();
        let attributes = parse_macro_input!($attributes as AttributeArgs);
        for attribute in attributes {
            match attribute {
                NestedMeta::Meta(Meta::NameValue(name_value)) => {
                    if name_value.path.is_ident("module_name") {
                        match name_value.lit {
                            Lit::Str(content) => result.module_name = Some(content.value()),
                            _ => {}
                        }
                    } else if name_value.path.is_ident("transformer") {
                        match &name_value.lit {
                            Lit::Str(content) => {
                                result.transformer =
                                    Some(Ident::new(&content.value(), Span::call_site().into()))
                            }
                            _ => {}
                        }
                    }
                }
                _ => {}
            }
        }
        result
    }};
}

/// Reads `#[intuicio_method(...)]` on one method into [`MethodAttributes`].
///
/// Yields the attributes together with whether the marker was present at
/// all, since methods without it are left alone.
macro_rules! parse_method_attributes {
    ($attributes:expr) => {{
        let mut found = false;
        let mut result = MethodAttributes::default();
        for attribute in $attributes {
            let attribute = match attribute.parse_meta() {
                Ok(attribute) => attribute,
                Err(err) => return TokenStream::from(err.to_compile_error()),
            };
            match attribute {
                Meta::List(list) if list.path.is_ident("intuicio_method") => {
                    found = true;
                    for meta in list.nested.iter() {
                        match meta {
                            NestedMeta::Meta(meta) => match meta {
                                Meta::Path(path) => {
                                    if path.is_ident("use_registry") {
                                        result.use_registry = true;
                                    } else if path.is_ident("use_context") {
                                        result.use_context = true;
                                    } else if path.is_ident("debug") {
                                        result.debug = true;
                                    }
                                }
                                Meta::List(list) => {
                                    if list.path.is_ident("args_meta") {
                                        for meta in list.nested.iter() {
                                            if let NestedMeta::Meta(Meta::NameValue(name_value)) =
                                                meta
                                            {
                                                match &name_value.lit {
                                                    Lit::Str(content) => {
                                                        result.args_meta.insert(
                                                            name_value
                                                                .path
                                                                .get_ident()
                                                                .unwrap()
                                                                .to_string(),
                                                            content.value(),
                                                        );
                                                    }
                                                    _ => {}
                                                }
                                            }
                                        }
                                    }
                                }
                                Meta::NameValue(name_value) => {
                                    if name_value.path.is_ident("name") {
                                        match &name_value.lit {
                                            Lit::Str(content) => {
                                                result.name = Some(content.value())
                                            }
                                            _ => {}
                                        }
                                    } else if name_value.path.is_ident("transformer") {
                                        match &name_value.lit {
                                            Lit::Str(content) => {
                                                result.transformer = Some(Ident::new(
                                                    &content.value(),
                                                    Span::call_site().into(),
                                                ))
                                            }
                                            _ => {}
                                        }
                                    } else if name_value.path.is_ident("dependency") {
                                        match &name_value.lit {
                                            Lit::Str(content) => {
                                                result.dependency = Some(Ident::new(
                                                    &content.value(),
                                                    Span::call_site().into(),
                                                ))
                                            }
                                            _ => {}
                                        }
                                    } else if name_value.path.is_ident("meta") {
                                        match &name_value.lit {
                                            Lit::Str(content) => {
                                                result.meta = Some(content.value());
                                            }
                                            _ => {}
                                        }
                                    }
                                }
                            },
                            _ => {}
                        }
                    }
                }
                _ => {}
            }
        }
        (result, found)
    }};
}

/// Expands the attribute. See the [module docs](self) for the shape of the
/// output.
///
/// # Panics
///
/// Panics on a trait `impl`, since only inherent ones can be exposed, and on
/// an argument whose pattern is not a plain identifier.
pub fn intuicio_methods(attributes: TokenStream, input: TokenStream) -> TokenStream {
    let ImplAttributes {
        module_name,
        transformer,
    } = parse_impl_attributes!(attributes);
    let impl_transformer = transformer;
    let item = parse_macro_input!(input as ItemImpl);
    if item.trait_.is_some() {
        panic!("Intuicio methods must be applied only for non-trait implementations!");
    }
    let module_name = if let Some(module_name) = module_name {
        quote! { result.module_name = Some(#module_name.to_owned()); }
    } else {
        quote! {}
    };
    let type_path = &item.self_ty;
    let type_handle = quote! {
        result.type_handle = Some(
            registry
                .find_type(intuicio_core::types::TypeQuery::of_type_name::<#type_path>())
                .unwrap_or_else(|| panic!("Could not find type: `{}`", std::any::type_name::<#type_path>()))
        );
    };
    let items = item
        .items
        .iter()
        .filter_map(|item| match item {
            ImplItem::Method(method) => Some(method),
            _ => None,
        })
        .collect::<Vec<_>>();
    let mut methods = Vec::with_capacity(items.len());
    for item in items {
        let (
            MethodAttributes {
                name,
                use_registry,
                use_context,
                debug,
                mut transformer,
                dependency,
                meta,
                args_meta,
            },
            found,
        ) = parse_method_attributes!(&item.attrs);
        if transformer.is_none() {
            transformer = impl_transformer.clone();
        }
        if !found {
            continue;
        }
        let intuicio_function_ident = Ident::new(
            &format!("{}__intuicio_function", item.sig.ident),
            Span::call_site().into(),
        );
        let define_signature_ident = Ident::new(
            &format!("{}__define_signature", item.sig.ident),
            Span::call_site().into(),
        );
        let define_function_ident = Ident::new(
            &format!("{}__define_function", item.sig.ident),
            Span::call_site().into(),
        );
        let vis = item.vis.clone();
        let ident = item.sig.ident.clone();
        let name = if let Some(name) = name {
            quote! { result.name = #name.to_owned(); }
        } else {
            quote! {}
        };
        let visibility = match vis {
            Visibility::Inherited => {
                quote! { result.visibility = intuicio_core::Visibility::Private; }
            }
            Visibility::Restricted(_) | Visibility::Crate(_) => {
                quote! { result.visibility = intuicio_core::Visibility::Module; }
            }
            Visibility::Public(_) => quote! {},
        };
        let meta = if let Some(meta) = meta {
            quote! { result.meta = intuicio_core::meta::Meta::parse(#meta).ok(); }
        } else {
            quote! {}
        };
        let args_meta = item
            .sig
            .inputs
            .iter()
            .map(|arg| {
                let name = match arg {
                    FnArg::Receiver(_) => "self".to_owned(),
                    FnArg::Typed(item) => match &*item.pat {
                        Pat::Ident(ident) => ident.ident.to_string(),
                        _ => panic!("Only identifiers are accepted as argument names!"),
                    },
                };
                if let Some(meta) = args_meta.get(&name) {
                    quote! { arg.meta = intuicio_core::meta::Meta::parse(#meta).ok(); }
                } else {
                    quote! {}
                }
            })
            .collect::<Vec<_>>();
        let arg_idents = item
            .sig
            .inputs
            .iter()
            .filter_map(|arg| match arg {
                FnArg::Receiver(_) => Some(Ident::new("this", Span::call_site().into())),
                FnArg::Typed(meta) => match &*meta.pat {
                    Pat::Ident(ident) => {
                        if (use_registry && ident.ident == "registry")
                            || (use_context && ident.ident == "context")
                        {
                            None
                        } else {
                            Some(ident.ident.clone())
                        }
                    }
                    _ => panic!("Only identifiers are accepted as argument names!"),
                },
            })
            .collect::<Vec<_>>();
        let call_arg_idents = item
            .sig
            .inputs
            .iter()
            .map(|arg| match arg {
                FnArg::Receiver(_) => Ident::new("this", Span::call_site().into()),
                FnArg::Typed(meta) => match &*meta.pat {
                    Pat::Ident(ident) => ident.ident.clone(),
                    _ => panic!("Only identifiers are accepted as argument names!"),
                },
            })
            .collect::<Vec<_>>();
        let arg_types: Vec<_> = item
            .sig
            .inputs
            .iter()
            .filter_map(|arg| match arg {
                FnArg::Receiver(meta) => {
                    Some(transformer
                        .as_ref()
                        .map(|transformer| if meta.reference.is_some() {
                            if meta.mutability.is_some() {
                                syn::parse2::<Type>(quote!{
                                    <#transformer<#type_path> as intuicio_core::transformer::ValueTransformer>::RefMut
                                }).unwrap()
                            }else {
                                syn::parse2::<Type>(quote!{
                                    <#transformer<#type_path> as intuicio_core::transformer::ValueTransformer>::Ref
                                }).unwrap()
                            }
                        } else {
                            syn::parse2::<Type>(quote!{
                                <#transformer<#type_path> as intuicio_core::transformer::ValueTransformer>::Owned
                            }).unwrap()
                        })
                        .unwrap_or_else(|| if meta.reference.is_some() {
                            if meta.mutability.is_some() {
                                syn::parse2::<Type>(quote!{&mut #type_path}).unwrap()
                            }else {
                                syn::parse2::<Type>(quote!{& #type_path}).unwrap()
                            }
                        } else {
                            *type_path.clone()
                        }))
                },
                FnArg::Typed(meta) => {
                    let ident = match &*meta.pat {
                        Pat::Ident(ident) => &ident.ident,
                        _ => panic!("Only identifiers are accepted as argument names!"),
                    };
                    if (use_registry && ident == "registry") || (use_context && ident == "context")
                    {
                        None
                    } else {
                        Some(transformer
                            .as_ref()
                            .map(|transformer| match unpack_type(&meta.ty) {
                                UnpackedType::Owned(ty) => {
                                    syn::parse2::<Type>(quote!{
                                        <#transformer<#ty> as intuicio_core::transformer::ValueTransformer>::Owned
                                    }).unwrap()
                                }
                                UnpackedType::Ref(ty) => {
                                    syn::parse2::<Type>(quote!{
                                        <#transformer<#ty> as intuicio_core::transformer::ValueTransformer>::Ref
                                    }).unwrap()
                                }
                                UnpackedType::RefMut(ty) => {
                                    syn::parse2::<Type>(quote!{
                                        <#transformer<#ty> as intuicio_core::transformer::ValueTransformer>::RefMut
                                    }).unwrap()
                                }
                            })
                            .unwrap_or_else(|| *meta.ty.clone()))
                    }
                }
            })
            .collect();
        let (transform_arg_idents, arg_transforms): (Vec<_>, Vec<_>) =
            if let Some(transformer) = transformer.as_ref() {
                item.sig
                    .inputs
                    .iter()
                    .filter_map(|arg| match arg {
                        FnArg::Receiver(meta) => Some((
                            Ident::new("this", Span::call_site().into()),
                            if meta.reference.is_some() {
                                if meta.mutability.is_some() {
                                    quote! {#transformer::into_ref_mut(&mut this)}
                                } else {
                                    quote! {#transformer::into_ref(&this)}
                                }
                            } else {
                                quote! {#transformer::into_owned(this)}
                            },
                        )),
                        FnArg::Typed(meta) => {
                            let ident = match &*meta.pat {
                                Pat::Ident(ident) => &ident.ident,
                                _ => panic!("Only identifiers are accepted as argument names!"),
                            };
                            if (use_registry && ident == "registry")
                                || (use_context && ident == "context")
                            {
                                None
                            } else {
                                Some((
                                    ident.clone(),
                                    match unpack_type(&meta.ty) {
                                        UnpackedType::Owned(_) => {
                                            quote! {#transformer::into_owned(#ident)}
                                        }
                                        UnpackedType::Ref(_) => {
                                            quote! {#transformer::into_ref(&#ident)}
                                        }
                                        UnpackedType::RefMut(_) => {
                                            quote! {#transformer::into_ref_mut(&mut #ident)}
                                        }
                                    },
                                ))
                            }
                        }
                    })
                    .unzip()
            } else {
                (vec![], vec![])
            };
        let transform_arg_deref = if transformer.is_some() {
            item.sig
                .inputs
                .iter()
                .filter_map(|arg| match arg {
                    FnArg::Receiver(meta) => {
                        if meta.reference.is_some() {
                            if meta.mutability.is_some() {
                                Some(quote! {let this = &mut this;})
                            } else {
                                Some(quote! {let this = &this;})
                            }
                        } else {
                            None
                        }
                    }
                    FnArg::Typed(meta) => {
                        let ident = match &*meta.pat {
                            Pat::Ident(ident) => &ident.ident,
                            _ => panic!("Only identifiers are accepted as argument names!"),
                        };
                        if (use_registry && ident == "registry")
                            || (use_context && ident == "context")
                        {
                            None
                        } else {
                            match unpack_type(&meta.ty) {
                                UnpackedType::Owned(_) => None,
                                UnpackedType::Ref(_) => Some(quote! {let #ident = &#ident;}),
                                UnpackedType::RefMut(_) => Some(quote! {let #ident = &mut #ident;}),
                            }
                        }
                    }
                })
                .collect::<Vec<_>>()
        } else {
            vec![]
        };
        let return_idents = match item.sig.output {
            ReturnType::Default => vec![],
            ReturnType::Type(_, _) => vec!["result"],
        };
        let return_types = match item.sig.output {
            ReturnType::Default => vec![],
            ReturnType::Type(_, ref ty) => vec![
                transformer
                    .as_ref()
                    .map(|_| {
                        match unpack_type(ty) {
                    UnpackedType::Owned(ty) => syn::parse2::<Type>(quote! {
                        <#transformer<#ty> as intuicio_core::transformer::ValueTransformer>::Owned
                    })
                    .unwrap(),
                    UnpackedType::Ref(ty) => syn::parse2::<Type>(quote! {
                        <#transformer<#ty> as intuicio_core::transformer::ValueTransformer>::Ref
                    })
                    .unwrap(),
                    UnpackedType::RefMut(ty) => syn::parse2::<Type>(quote! {
                        <#transformer<#ty> as intuicio_core::transformer::ValueTransformer>::RefMut
                    })
                    .unwrap(),
                }
                    })
                    .unwrap_or_else(|| *ty.clone()),
            ],
        };
        let (dependency, return_transform) = if let Some(transformer) = transformer.as_ref() {
            match item.sig.output {
                ReturnType::Default => (vec![], vec![]),
                ReturnType::Type(_, ref ty) => match unpack_type(ty) {
                    UnpackedType::Owned(_) => (
                        vec![],
                        vec![quote! {let result = #transformer::from_owned(registry, result);}],
                    ),
                    UnpackedType::Ref(ty) => (
                        vec![dependency.as_ref().map(|dependency|{
                            quote! {
                                let __dependency__ = Some(
                                    <#transformer<#ty> as intuicio_core::transformer::ValueTransformer>::Dependency::as_ref(&#dependency)
                                );
                            }
                        }).unwrap_or_else(|| quote!{let __dependency__ = None;})],
                        vec![quote! {let result = #transformer::from_ref(registry, result, __dependency__);}],
                    ),
                    UnpackedType::RefMut(ty) => (
                        vec![dependency.as_ref().map(|dependency|{
                            quote! {
                                let __dependency__ = Some(
                                    <#transformer<#ty> as intuicio_core::transformer::ValueTransformer>::Dependency::as_ref_mut(&mut #dependency)
                                );
                            }
                        }).unwrap_or_else(|| quote!{let __dependency__ = None;})],
                        vec![quote! {let result = #transformer::from_ref_mut(registry, result, __dependency__);}],
                    ),
                },
            }
        } else {
            (vec![], vec![])
        };
        let result = if return_types.is_empty() {
            quote! {
                {
                    #(#transform_arg_deref)*
                    #type_path::#ident(#(#call_arg_idents,)*)
                }
            }
        } else {
            quote! {
                let result = {
                    #(#transform_arg_deref)*
                    #type_path::#ident(#(#call_arg_idents,)*)
                };
                #(#return_transform)*
                (result,).stack_push_reversed(context.stack());
            }
        };
        let result = quote! {
            #[allow(dead_code)]
            #[allow(non_snake_case)]
            pub fn #intuicio_function_ident(
                context: &mut intuicio_core::context::Context,
                registry: &intuicio_core::registry::Registry,
            ) {
                use intuicio_data::data_stack::DataStackPack;
                #[allow(unused_mut)]
                let (#(mut #arg_idents,)*) = <(#(#arg_types,)*)>::stack_pop(context.stack());
                #(#dependency)*
                let (#(mut #transform_arg_idents,)*) = (#(#arg_transforms,)*);
                #result
            }

            #[allow(dead_code)]
            #[allow(non_snake_case)]
            pub fn #define_signature_ident(
                registry: &intuicio_core::registry::Registry
            ) -> intuicio_core::function::FunctionSignature {
                let mut result = intuicio_core::function::FunctionSignature::new(stringify!(#ident));
                #visibility
                #name
                #module_name
                #type_handle
                #meta
                #(
                    {
                        #[allow(unused_mut)]
                        let mut arg = intuicio_core::function::FunctionParameter::new(
                            stringify!(#arg_idents),
                            registry
                                .find_type(intuicio_core::types::TypeQuery::of_type_name::<#arg_types>())
                                .unwrap_or_else(|| panic!(
                                    "Could not find type: `{}` for argument: `{}` for function: `{}`",
                                    std::any::type_name::<#arg_types>(),
                                    stringify!(#arg_idents),
                                    stringify!(#ident),
                                ))
                        );
                        #args_meta
                        result.inputs.push(arg);
                    }
                )*
                #(
                    result.outputs.push(
                        intuicio_core::function::FunctionParameter::new(
                            #return_idents,
                            registry
                                .find_type(intuicio_core::types::TypeQuery::of_type_name::<#return_types>())
                                .unwrap_or_else(|| panic!(
                                    "Could not find type: `{}` for result: `{}` for function: `{}`",
                                    std::any::type_name::<#return_types>(),
                                    stringify!(#return_idents),
                                    stringify!(#ident),
                                ))
                        )
                    );
                )*
                result
            }

            #[allow(dead_code)]
            #[allow(non_snake_case)]
            pub fn #define_function_ident(
                registry: &intuicio_core::registry::Registry
            ) -> intuicio_core::function::Function {
                intuicio_core::function::Function::new(
                    #type_path::#define_signature_ident(registry),
                    intuicio_core::function::FunctionBody::pointer(#type_path::#intuicio_function_ident),
                )
            }
        };
        if debug {
            println!(
                "* Debug of `intuicio_method` attribute macro\n- Input: {}\n- Result: {}",
                item.to_token_stream(),
                result
            );
        }
        methods.push(result);
    }
    quote! {
        impl #type_path {
            #(#methods)*
        }

        #item
    }
    .into()
}

/// A type split into the value it names and how that value is passed.
enum UnpackedType {
    /// A plain `T`, holding `T`.
    Owned(Type),
    /// A `&T`, holding `T`.
    Ref(Type),
    /// A `&mut T`, holding `T`.
    RefMut(Type),
}

/// Splits a type into an [`UnpackedType`].
///
/// # Panics
///
/// Panics on anything that is neither a path nor a reference, since a
/// transformer has no rule for it.
fn unpack_type(ty: &Type) -> UnpackedType {
    match ty {
        Type::Path(_) => UnpackedType::Owned(ty.clone()),
        Type::Reference(reference) => {
            if reference.mutability.is_some() {
                UnpackedType::RefMut(*reference.elem.clone())
            } else {
                UnpackedType::Ref(*reference.elem.clone())
            }
        }
        _ => panic!("Unsupported kind of type to unpack: {ty:#?}"),
    }
}