alef 0.25.16

Opinionated polyglot binding generator for Rust libraries
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
use super::*;

// ---------------------------------------------------------------------------
// Bug-regression tests: one per fixed bug so regressions are caught immediately.
// ---------------------------------------------------------------------------

/// Bug 1: Bare excluded-type references.
///
/// When a trait method references a type that was excluded from the binding surface
/// (present in `api.excluded_type_paths`), the generated trait impl must use the
/// fully-qualified Rust path, not the bare type name.
///
/// Example: an excluded `HiddenDoc` argument must emit the fully-qualified
/// Rust path, not the bare type name.
#[test]
fn bug1_excluded_type_is_fully_qualified_in_trait_impl() {
    let internal_doc_method = MethodDef {
        name: "render".to_string(),
        params: vec![crate::core::ir::ParamDef {
            name: "doc".to_string(),
            ty: TypeRef::Named("HiddenDoc".to_string()),
            optional: false,
            default: None,
            sanitized: false,
            typed_default: None,
            is_ref: true,
            is_mut: false,
            newtype_wrapper: None,
            original_type: None,
            map_is_ahash: false,
            map_key_is_cow: false,
            vec_inner_is_ref: false,
            map_is_btree: false,
            core_wrapper: crate::core::ir::CoreWrapper::None,
        }],
        return_type: TypeRef::String,
        is_async: false,
        is_static: false,
        error_type: Some("Box<dyn std::error::Error + Send + Sync>".to_string()),
        doc: String::new(),
        receiver: Some(ReceiverKind::Ref),
        sanitized: false,
        trait_source: None,
        returns_ref: false,
        returns_cow: false,
        return_newtype_wrapper: None,
        has_default_impl: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        version: Default::default(),
    };
    let trait_def = make_trait_def("Renderer", vec![internal_doc_method]);
    let bridge_cfg = sample_bridge_cfg("Renderer");

    // Include HiddenDoc as an excluded type path.
    let api = ApiSurface {
        crate_name: "my-lib".to_string(),
        version: "1.0.0".to_string(),
        types: vec![],
        functions: vec![],
        enums: vec![],
        errors: vec![],
        excluded_type_paths: {
            let mut m = ::std::collections::HashMap::new();
            m.insert("HiddenDoc".to_string(), "my_lib::internal::HiddenDoc".to_string());
            m
        },
        excluded_trait_names: ::std::collections::HashSet::new(),
        services: vec![],
        handler_contracts: vec![],
        unsupported_public_items: Vec::new(),
    };

    let code = gen_trait_bridge(
        &trait_def,
        &bridge_cfg,
        "ml",
        "my_lib",
        "MyError",
        "MyError::from({msg})",
        None,
        &api,
    );

    assert!(
        code.contains("&my_lib::internal::HiddenDoc"),
        "excluded type must be fully-qualified, not bare;\n\
         actual code:\n{code}"
    );
    assert!(
        !code.contains("&HiddenDoc"),
        "bare type reference must not appear in generated trait impl;\n\
         actual code:\n{code}"
    );
}

/// Bug 2: Sync method bodies must use the trait's error type, not `Box::from`.
///
/// `gen_vtable_call_body(inside_closure=false)` is used for synchronous trait method
/// bodies.  Those methods return `Result<T, SampleCrateError>`, so error construction
/// must call `spec.make_error(...)` (e.g. `MyError::from(...)`), not `Box::from(...)`.
/// `Box::from` is correct only inside the async `_SendFn` closure where the return type
/// is `Box<dyn Error + Send + Sync>`.
#[test]
fn bug2_sync_method_body_uses_trait_error_type_not_box_from() {
    use crate::codegen::generators::trait_bridge::TraitBridgeSpec;

    let method = MethodDef {
        name: "run".to_string(),
        params: vec![],
        return_type: TypeRef::String,
        is_async: false,
        is_static: false,
        error_type: Some("MyError".to_string()),
        doc: String::new(),
        receiver: Some(ReceiverKind::Ref),
        sanitized: false,
        trait_source: None,
        returns_ref: false,
        returns_cow: false,
        return_newtype_wrapper: None,
        has_default_impl: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        version: Default::default(),
    };
    let trait_def = make_trait_def("Backend", vec![method.clone()]);
    let bridge_cfg = sample_bridge_cfg("Backend");

    let spec = TraitBridgeSpec {
        trait_def: &trait_def,
        bridge_config: &bridge_cfg,
        core_import: "my_lib",
        wrapper_prefix: "Ml",
        type_paths: ::std::collections::HashMap::new(),
        lifetime_type_names: std::collections::HashSet::new(),
        error_type: "MyError".to_string(),
        error_constructor: "MyError::from({msg})".to_string(),
    };

    let generator = FfiBridgeGenerator {
        prefix: "ml".to_string(),
        core_import: "my_lib".to_string(),
        type_paths: ::std::collections::HashMap::new(),
        error_type: "MyError".to_string(),
        plugin_error_constructor: None,
        lifetime_type_names: ::std::collections::HashSet::new(),
    };

    // Sync body (inside_closure = false): must use MyError::from, not Box::from
    let sync_body = generator.gen_vtable_call_body(&method, &spec, false);
    assert!(
        sync_body.contains("MyError::from("),
        "sync method body must use the trait's error constructor;\n\
         actual body:\n{sync_body}"
    );
    assert!(
        !sync_body.contains("Err(Box::from("),
        "sync method body must NOT use Box::from (that's for the async closure);\n\
         actual body:\n{sync_body}"
    );

    // Closure body (inside_closure = true): must use Box::from, not MyError::from
    let closure_body = generator.gen_vtable_call_body(&method, &spec, true);
    assert!(
        closure_body.contains("Err(Box::from("),
        "async closure body must use Box::from;\n\
         actual body:\n{closure_body}"
    );
}

/// Bug 3: `Vec<String> + returns_ref` methods must emit `&[&str]` in the trait impl,
/// and the bridge struct must gain a `{method_name}_strs: &'static [&'static str]` field
/// populated at construction time.
#[test]
fn bug3_returns_ref_vec_string_emits_slice_ref_and_cache_field() {
    let method = MethodDef {
        name: "supported_mime_types".to_string(),
        params: vec![],
        return_type: TypeRef::Vec(Box::new(TypeRef::String)),
        is_async: false,
        is_static: false,
        error_type: None,
        doc: String::new(),
        receiver: Some(ReceiverKind::Ref),
        sanitized: false,
        trait_source: None,
        returns_ref: true, // `fn supported_mime_types(&self) -> &[&str]`
        returns_cow: false,
        return_newtype_wrapper: None,
        has_default_impl: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        version: Default::default(),
    };
    let trait_def = make_trait_def("DocumentExtractor", vec![method]);
    let bridge_cfg = sample_bridge_cfg("DocumentExtractor");
    let api = sample_api();

    let code = gen_trait_bridge(
        &trait_def,
        &bridge_cfg,
        "kr",
        "sample_crate",
        "SampleCrateError",
        "SampleCrateError::from({msg})",
        None,
        &api,
    );

    // The trait impl return type must be `&[&str]`, not `Vec<String>`
    assert!(
        code.contains("fn supported_mime_types(&self) -> &[&str]"),
        "returns_ref Vec<String> must produce &[&str] in trait impl;\n\
         actual code:\n{code}"
    );

    // The bridge struct must have the cache field
    assert!(
        code.contains("supported_mime_types_strs: &'static [&'static str]"),
        "bridge struct must have supported_mime_types_strs cache field;\n\
         actual code:\n{code}"
    );

    // The trait impl body must return from the cache field
    assert!(
        code.contains("self.supported_mime_types_strs"),
        "trait impl body must return from the cached field;\n\
         actual code:\n{code}"
    );

    // The constructor must populate the cache field by calling the vtable
    assert!(
        code.contains("Box::leak"),
        "constructor must use Box::leak to build &'static [&'static str];\n\
         actual code:\n{code}"
    );
}

/// Bug 4 (FFI variant): `ffi_skip_methods` opts a method out of the FFI trait impl.
///
/// FFI's vtable bridge intentionally emits every trait method (including those with
/// `has_default_impl = true`) so the vtable can forward them — this is required for
/// visitor-style traits where every method has a default. The only way
/// to opt out of vtable forwarding (and fall back to the trait's own default) is to
/// list the method in `ffi_skip_methods`.
#[test]
fn bug4_ffi_skip_methods_opts_out_of_trait_impl() {
    let required = make_method("run", TypeRef::String, true, false);
    let optional = make_method("shutdown", TypeRef::Unit, false, true);
    let trait_def = make_trait_def("Backend", vec![required, optional]);
    let mut bridge_cfg = sample_bridge_cfg("Backend");
    bridge_cfg.ffi_skip_methods = vec!["shutdown".to_string()];
    let api = sample_api();

    let code = gen_trait_bridge(
        &trait_def,
        &bridge_cfg,
        "ml",
        "my_lib",
        "MyError",
        "MyError::from({msg})",
        None,
        &api,
    );

    // Required method must appear in the trait impl
    assert!(
        code.contains("fn run("),
        "required method must appear in trait impl;\n\
         actual code:\n{code}"
    );

    // Method in ffi_skip_methods must NOT appear — let the trait's own default take effect
    assert!(
        !code.contains("fn shutdown("),
        "method listed in ffi_skip_methods must NOT get a generated body;\n\
         actual code:\n{code}"
    );
}

/// Bug 5: Async method with a `&str` param must clone the param with `.to_string()`
/// before moving it into the `spawn_blocking` closure, not with `.clone()`.
///
/// `.clone()` on `&str` returns `&str` — the original borrow escapes into the closure,
/// triggering E0521 ("borrowed data escapes outside of method").  `.to_string()`
/// produces an owned `String` that is `'static` and safe to move into the closure.
#[test]
fn bug5_async_str_param_uses_to_string_not_clone() {
    let method = MethodDef {
        name: "process".to_string(),
        params: vec![ParamDef {
            name: "mime_type".to_string(),
            ty: TypeRef::String,
            optional: false,
            default: None,
            sanitized: false,
            typed_default: None,
            is_ref: true, // &str — the borrow that escapes without .to_string()
            is_mut: false,
            newtype_wrapper: None,
            original_type: None,
            map_is_ahash: false,
            map_key_is_cow: false,
            vec_inner_is_ref: false,
            map_is_btree: false,
            core_wrapper: crate::core::ir::CoreWrapper::None,
        }],
        return_type: TypeRef::Unit,
        is_async: true, // async method — closure must own all captured data
        is_static: false,
        error_type: Some("Box<dyn std::error::Error + Send + Sync>".to_string()),
        doc: String::new(),
        receiver: Some(ReceiverKind::Ref),
        sanitized: false,
        trait_source: None,
        returns_ref: false,
        returns_cow: false,
        return_newtype_wrapper: None,
        has_default_impl: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        version: Default::default(),
    };
    let trait_def = make_trait_def("Backend", vec![method]);
    let bridge_cfg = sample_bridge_cfg("Backend");
    let api = sample_api();

    let code = gen_trait_bridge(
        &trait_def,
        &bridge_cfg,
        "ml",
        "my_lib",
        "MyError",
        "MyError::from({msg})",
        None,
        &api,
    );

    // The closure capture must convert &str to String, not clone the borrow.
    assert!(
        code.contains("let mime_type = mime_type.to_string()"),
        "async &str param must be captured via .to_string() to avoid E0521;\n\
         actual code:\n{code}"
    );
    assert!(
        !code.contains("let mime_type = mime_type.clone()"),
        "async &str param must NOT use .clone() (returns &str, still borrows);\n\
         actual code:\n{code}"
    );
}

/// Bug 6: Async method whose trait return type is an excluded Named type must:
///   (a) emit the fully-qualified path in the method SIGNATURE, and
///   (b) deserialize JSON from the C ABI back to that type in the closure BODY.
///
/// Before the fix the generator emitted `Result<String, _>` in the signature and
/// `Ok(cs.to_string_lossy().into_owned())` in the body — both wrong for Named returns.
#[test]
fn bug6_async_excluded_type_return_signature_and_deserialization() {
    let method = MethodDef {
        name: "extract_bytes".to_string(),
        params: vec![ParamDef {
            name: "content".to_string(),
            ty: TypeRef::Bytes,
            optional: false,
            default: None,
            sanitized: false,
            typed_default: None,
            is_ref: true,
            is_mut: false,
            newtype_wrapper: None,
            original_type: None,
            map_is_ahash: false,
            map_key_is_cow: false,
            vec_inner_is_ref: false,
            map_is_btree: false,
            core_wrapper: crate::core::ir::CoreWrapper::None,
        }],
        return_type: TypeRef::Named("HiddenDoc".to_string()),
        is_async: true,
        is_static: false,
        error_type: Some("Box<dyn std::error::Error + Send + Sync>".to_string()),
        doc: String::new(),
        receiver: Some(ReceiverKind::Ref),
        sanitized: false,
        trait_source: None,
        returns_ref: false,
        returns_cow: false,
        return_newtype_wrapper: None,
        has_default_impl: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        version: Default::default(),
    };
    let trait_def = make_trait_def("Extractor", vec![method]);
    let bridge_cfg = sample_bridge_cfg("Extractor");

    let api = ApiSurface {
        crate_name: "my-lib".to_string(),
        version: "1.0.0".to_string(),
        types: vec![],
        functions: vec![],
        enums: vec![],
        errors: vec![],
        excluded_type_paths: {
            let mut m = ::std::collections::HashMap::new();
            m.insert("HiddenDoc".to_string(), "my_lib::internal::HiddenDoc".to_string());
            m
        },
        excluded_trait_names: ::std::collections::HashSet::new(),
        services: vec![],
        handler_contracts: vec![],
        unsupported_public_items: Vec::new(),
    };

    let code = gen_trait_bridge(
        &trait_def,
        &bridge_cfg,
        "ml",
        "my_lib",
        "MyError",
        "MyError::from({msg})",
        None,
        &api,
    );

    // Signature must use the fully-qualified path, not String.
    assert!(
        code.contains("-> std::result::Result<my_lib::internal::HiddenDoc,"),
        "async method return type must be qualified excluded type in signature;\n\
         actual code:\n{code}"
    );
    assert!(
        !code.contains("-> std::result::Result<String,"),
        "async method return type must NOT be String for Named return types;\n\
         actual code:\n{code}"
    );

    // Closure body must deserialize JSON back to the excluded core type, not pass String through.
    assert!(
        code.contains("serde_json::from_str::<my_lib::internal::HiddenDoc>"),
        "async closure body must deserialize JSON to HiddenDoc;\n\
         actual code:\n{code}"
    );
    assert!(
        !code.contains("Ok(cs.to_string_lossy().into_owned())"),
        "async closure body must NOT return raw String for Named return types;\n\
         actual code:\n{code}"
    );
}

/// Regression: `gen_ffi_trait_impl` was calling `format_type_ref` which ignores
/// `is_ref`/`is_mut`, causing `&[u8]` → `Vec<u8>`, `&str` → `String`, `&Path` →
/// `PathBuf`, and excluded named references in the trait impl method
/// signatures.  The fix uses `format_param_type` which respects those flags.
#[test]
fn bug_ffi1_trait_impl_param_types_respect_is_ref() {
    let method = MethodDef {
        name: "process".to_string(),
        params: vec![
            ParamDef {
                name: "content".to_string(),
                ty: TypeRef::Bytes,
                optional: false,
                default: None,
                sanitized: false,
                typed_default: None,
                is_ref: true, // &[u8]
                is_mut: false,
                newtype_wrapper: None,
                original_type: None,
                map_is_ahash: false,
                map_key_is_cow: false,
                vec_inner_is_ref: false,
                map_is_btree: false,
                core_wrapper: crate::core::ir::CoreWrapper::None,
            },
            ParamDef {
                name: "mime_type".to_string(),
                ty: TypeRef::String,
                optional: false,
                default: None,
                sanitized: false,
                typed_default: None,
                is_ref: true, // &str
                is_mut: false,
                newtype_wrapper: None,
                original_type: None,
                map_is_ahash: false,
                map_key_is_cow: false,
                vec_inner_is_ref: false,
                map_is_btree: false,
                core_wrapper: crate::core::ir::CoreWrapper::None,
            },
            ParamDef {
                name: "path".to_string(),
                ty: TypeRef::Path,
                optional: false,
                default: None,
                sanitized: false,
                typed_default: None,
                is_ref: true, // &Path
                is_mut: false,
                newtype_wrapper: None,
                original_type: None,
                map_is_ahash: false,
                map_key_is_cow: false,
                vec_inner_is_ref: false,
                map_is_btree: false,
                core_wrapper: crate::core::ir::CoreWrapper::None,
            },
        ],
        return_type: TypeRef::Unit,
        is_async: false,
        is_static: false,
        error_type: Some("Box<dyn std::error::Error + Send + Sync>".to_string()),
        doc: String::new(),
        receiver: Some(ReceiverKind::Ref),
        sanitized: false,
        trait_source: None,
        returns_ref: false,
        returns_cow: false,
        return_newtype_wrapper: None,
        has_default_impl: false,
        binding_excluded: false,
        binding_exclusion_reason: None,
        version: Default::default(),
    };
    let trait_def = make_trait_def("Extractor", vec![method]);
    let bridge_cfg = sample_bridge_cfg("Extractor");
    let api = sample_api();

    let code = gen_trait_bridge(
        &trait_def,
        &bridge_cfg,
        "ml",
        "my_lib",
        "MyError",
        "MyError::from({msg})",
        None,
        &api,
    );

    // trait impl must emit the reference types, not the owned equivalents
    assert!(
        code.contains("content: &[u8]"),
        "is_ref Bytes param must be &[u8] in trait impl, not Vec<u8>;\n\
         actual code:\n{code}"
    );
    assert!(
        code.contains("mime_type: &str"),
        "is_ref String param must be &str in trait impl, not String;\n\
         actual code:\n{code}"
    );
    assert!(
        code.contains("path: &std::path::Path"),
        "is_ref Path param must be &std::path::Path in trait impl, not PathBuf;\n\
         actual code:\n{code}"
    );
}

/// Named types with `has_lifetime_params = true` (e.g. `SyntaxContext<'a>`) must be
/// emitted as `&Type<'_>` in the FFI trait impl method signature so it matches the
/// trait definition exactly. Without `<'_>`, rustc rejects the impl because the
/// concrete struct is `SyntaxContext<'_>` but the generated method omits the lifetime.
#[test]
fn lifetime_param_named_type_emits_angle_lifetime_placeholder() {
    use crate::core::ir::{ParamDef, TypeDef};

    let method = MethodDef {
        name: "visit_element".to_string(),
        params: vec![ParamDef {
            name: "context".to_string(),
            ty: TypeRef::Named("NodeContext".to_string()),
            optional: false,
            default: None,
            sanitized: false,
            typed_default: None,
            is_ref: true, // &NodeContext<'_> in the trait definition
            is_mut: false,
            newtype_wrapper: None,
            original_type: None,
            map_is_ahash: false,
            map_key_is_cow: false,
            vec_inner_is_ref: false,
            map_is_btree: false,
            core_wrapper: crate::core::ir::CoreWrapper::None,
        }],
        return_type: TypeRef::Unit,
        is_async: false,
        is_static: false,
        error_type: None,
        doc: String::new(),
        receiver: Some(ReceiverKind::Ref),
        sanitized: false,
        trait_source: None,
        returns_ref: false,
        returns_cow: false,
        return_newtype_wrapper: None,
        has_default_impl: true, // visitor method — has default but FFI still emits it
        binding_excluded: false,
        binding_exclusion_reason: None,
        version: Default::default(),
    };
    let trait_def = make_trait_def("HtmlVisitor", vec![method]);
    let bridge_cfg = sample_bridge_cfg("HtmlVisitor");

    // Include the context type as a type with lifetime params.
    let api = ApiSurface {
        crate_name: "my-lib".to_string(),
        version: "1.0.0".to_string(),
        types: vec![TypeDef {
            name: "NodeContext".to_string(),
            rust_path: "my_lib::NodeContext".to_string(),
            original_rust_path: String::new(),
            fields: vec![],
            methods: vec![],
            is_opaque: false,
            is_clone: false,
            is_copy: false,
            is_trait: false,
            has_default: false,
            has_stripped_cfg_fields: false,
            is_return_type: false,
            serde_rename_all: None,
            has_serde: false,
            super_traits: vec![],
            doc: String::new(),
            cfg: None,
            binding_excluded: false,
            binding_exclusion_reason: None,
            is_variant_wrapper: false,
            has_lifetime_params: true, // <-- the key flag
            version: Default::default(),
        }],
        functions: vec![],
        enums: vec![],
        errors: vec![],
        excluded_type_paths: ::std::collections::HashMap::new(),
        excluded_trait_names: ::std::collections::HashSet::new(),
        services: vec![],
        handler_contracts: vec![],
        unsupported_public_items: Vec::new(),
    };

    let code = gen_trait_bridge(
        &trait_def,
        &bridge_cfg,
        "htm",
        "my_lib",
        "MyError",
        "MyError::from({msg})",
        None,
        &api,
    );

    // The trait impl method signature must include a lifetime placeholder, not a bare type.
    assert!(
        code.contains("context: &my_lib::NodeContext<'_>"),
        "lifetime-parameterized Named type must be &Type<'_> in trait impl signature;\n\
         actual code:\n{code}"
    );
    assert!(
        !code.contains("context: &my_lib::NodeContext,") && !code.contains("context: &my_lib::NodeContext\n"),
        "bare &NodeContext without lifetime placeholder must NOT appear in trait impl;\n\
         actual code:\n{code}"
    );
}

#[test]
fn vtable_registration_signature_takes_const_pointer() {
    // Regression test for C9: Go cgo vtable cimport unification.
    // FFI registration function must take `vtable: *const VTableName` (pointer),
    // not `vtable: VTableName` (value), so that Go can consistently pass `&vtable`
    // without cgo type unification issues.

    let trait_def = make_trait_def(
        "TestBackend",
        vec![make_method("process", TypeRef::String, true, false)],
    );

    let bridge_cfg = crate::core::config::TraitBridgeConfig {
        trait_name: "TestBackend".to_string(),
        register_fn: Some("register_backend".to_string()),
        registry_getter: Some("get_registry".to_string()),
        ..Default::default()
    };

    let api = ApiSurface {
        crate_name: "test_lib".to_string(),
        version: "0.1.0".to_string(),
        types: vec![trait_def.clone()],
        functions: vec![],
        enums: vec![],
        errors: vec![],
        excluded_type_paths: ::std::collections::HashMap::new(),
        excluded_trait_names: ::std::collections::HashSet::new(),
        services: vec![],
        handler_contracts: vec![],
        unsupported_public_items: Vec::new(),
    };

    let code = gen_trait_bridge(
        &trait_def,
        &bridge_cfg,
        "test",
        "test_lib",
        "TestError",
        "TestError::from({msg})",
        None,
        &api,
    );

    // The registration function signature must use `*const` for the vtable parameter.
    assert!(
        code.contains("vtable: *const TestTestBackendVTable"),
        "FFI registration function must take vtable as `*const VTableName` pointer;\n\
         actual code:\n{code}"
    );

    // Should not use value-type vtable parameter.
    assert!(
        !code.contains(
            "pub unsafe extern \"C\" fn test_register_backend(\n    name: *const std::ffi::c_char,\n    vtable: TestTestBackendVTable,"
        ),
        "FFI registration function must NOT take vtable as value type (bare struct);\n\
         actual code:\n{code}"
    );

    // Should dereference the pointer in the function body.
    assert!(
        code.contains("let vtable_ref = &*vtable;"),
        "FFI registration function body must dereference vtable pointer;\n\
         actual code:\n{code}"
    );
}