alef 0.82.1

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
use super::*;

/// Regression test for a crate-local `type Result<T> = ...<T, E>;` alias where the alias
/// itself carries a generic parameter (`<T>`) — the normal, idiomatic shape of this pattern.
///
/// Functions that return the alias with a single type argument (`Result<Foo>`, relying on
/// the alias to supply the error type) must resolve `error_type` to the alias's real error
/// type (`ConversionError`), not fall back to a placeholder like `anyhow::Error` that gets
/// rendered downstream as a bare `Error` — a type the crate does not export. ~keep
#[test]
fn test_generic_result_alias_supplies_real_error_type() {
    let source = r#"
        pub struct ConversionError;

        pub struct ConversionResult;

        pub type Result<T> = std::result::Result<T, ConversionError>;

        pub fn convert(html: &str) -> Result<ConversionResult> {
            unimplemented!()
        }
    "#;

    let surface = extract_from_source(source);
    let convert = surface.functions.iter().find(|f| f.name == "convert").unwrap();
    assert_eq!(
        convert.error_type.as_deref(),
        Some("ConversionError"),
        "generic Result<T> alias must resolve error_type from its own definition, got: {:?}",
        convert.error_type
    );
}

/// Same as above, but for a method on an `impl` block rather than a free function, since
/// methods resolve their return type through a separate code path (`functions/methods.rs`). ~keep
#[test]
fn test_generic_result_alias_supplies_real_error_type_for_method() {
    let source = r#"
        pub struct ConversionError;

        pub struct ConversionResult;

        pub type Result<T> = std::result::Result<T, ConversionError>;

        pub struct Converter;

        impl Converter {
            pub fn convert(&self, html: &str) -> Result<ConversionResult> {
                unimplemented!()
            }
        }
    "#;

    let surface = extract_from_source(source);
    let converter = surface.types.iter().find(|t| t.name == "Converter").unwrap();
    let convert = converter.methods.iter().find(|m| m.name == "convert").unwrap();
    assert_eq!(
        convert.error_type.as_deref(),
        Some("ConversionError"),
        "generic Result<T> alias must resolve error_type from its own definition, got: {:?}",
        convert.error_type
    );
}

/// The alias and the function that returns it normally live in *different* modules
/// (`error.rs` declares `Result`, `convert_api.rs` returns it). Extraction walks one module at a
/// time, so a per-module hint map that replaces rather than accumulates loses the alias before the
/// function is resolved — the single-module cases above pass while every real crate still renders
/// the placeholder `Error`. ~keep
#[test]
fn test_result_alias_resolves_when_declared_in_a_different_module() {
    let source = r#"
        pub struct ConversionError;

        pub struct ConversionResult;

        pub type Result<T> = std::result::Result<T, ConversionError>;

        pub mod convert_api {
            use super::{ConversionResult, Result};

            pub fn convert(html: &str) -> Result<ConversionResult> {
                unimplemented!()
            }
        }
    "#;

    let surface = extract_from_source(source);
    let convert = surface.functions.iter().find(|f| f.name == "convert").unwrap();
    assert_eq!(
        convert.error_type.as_deref(),
        Some("ConversionError"),
        "alias declared in a sibling module must still supply the error type, got: {:?}",
        convert.error_type
    );
}

/// Source shape of a real crate: a canonical `Result` next to the crate error plus a private
/// `Result` inside a format subsystem, with the plugin traits declared in a third module that
/// imports `crate::Result`.
///
/// A hint map keyed by alias *name* makes the last alias walked win for the whole crate, so the
/// plugin traits pick up the subsystem's private error type — a type the crate does not re-export,
/// which then lands in generated bindings as an unresolvable import.
const CRATE_WITH_PRIVATE_SUBSYSTEM_ALIAS: &str = r#"
        pub mod error {
            pub struct SampleCrateError;
            pub type Result<T> = std::result::Result<T, SampleCrateError>;
        }

        pub mod extraction {
            pub mod binary {
                pub mod error {
                    pub struct BinaryFormatError;
                    pub type Result<T> = std::result::Result<T, BinaryFormatError>;
                }

                pub mod model {
                    use super::error::Result;

                    pub fn parse_header(bytes: &[u8]) -> Result<u32> {
                        unimplemented!()
                    }
                }
            }
        }

        pub mod plugins {
            use crate::Result;

            pub struct Embedding;

            pub trait EmbeddingBackend {
                fn embed(&self, texts: Vec<String>) -> Result<Embedding>;
            }
        }

        pub use error::{Result, SampleCrateError};
    "#;

#[test]
fn test_trait_method_uses_the_canonical_alias_not_a_module_private_one() {
    let surface = extract_from_source(CRATE_WITH_PRIVATE_SUBSYSTEM_ALIAS);
    let backend = surface
        .types
        .iter()
        .find(|t| t.name == "EmbeddingBackend")
        .expect("trait must be extracted");
    let embed = backend.methods.iter().find(|m| m.name == "embed").unwrap();
    assert_eq!(
        embed.error_type.as_deref(),
        Some("SampleCrateError"),
        "a trait importing crate::Result must resolve to the crate's exported error type, got: {:?}",
        embed.error_type
    );
}

#[test]
fn test_module_private_alias_still_applies_inside_its_own_subsystem() {
    let surface = extract_from_source(CRATE_WITH_PRIVATE_SUBSYSTEM_ALIAS);
    let parse_header = surface
        .functions
        .iter()
        .find(|f| f.name == "parse_header")
        .expect("subsystem function must be extracted");
    assert_eq!(
        parse_header.error_type.as_deref(),
        Some("BinaryFormatError"),
        "a module importing its own subsystem alias keeps that alias's error type, got: {:?}",
        parse_header.error_type
    );
}

/// A module that returns `anyhow::Result<T>` must keep `anyhow::Error`; substituting the crate's
/// own error type there would be the same defect in the opposite direction.
#[test]
fn test_foreign_result_alias_is_not_replaced_by_the_crate_error() {
    let source = r#"
        pub mod error {
            pub struct SampleCrateError;
            pub type Result<T> = std::result::Result<T, SampleCrateError>;
        }

        pub mod scripting {
            use anyhow::Result;

            pub struct Script;

            pub fn compile(source: &str) -> Result<Script> {
                unimplemented!()
            }
        }
    "#;

    let surface = extract_from_source(source);
    let compile = surface.functions.iter().find(|f| f.name == "compile").unwrap();
    assert_eq!(
        compile.error_type.as_deref(),
        Some("anyhow::Error"),
        "anyhow::Result must not be rewritten to the crate error type, got: {:?}",
        compile.error_type
    );
}

/// A module that imports `anyhow::Result` for its internal helpers, then writes the crate's own
/// alias fully qualified as `crate::Result<T>` on its public API — the reason to qualify at all is
/// that the bare name is already taken. The qualification is the whole signal, so resolution that
/// reads only the module's `use` statements answers `anyhow::Error` for a function whose source
/// plainly names the crate alias. ~keep
#[test]
fn test_crate_qualified_result_beats_a_foreign_result_import() {
    let source = r#"
        pub mod error {
            pub struct SampleCrateError;
        }

        pub type Result<T> = std::result::Result<T, error::SampleCrateError>;

        pub mod api {
            use anyhow::Result;

            pub struct Extraction;

            pub fn extract(input: &str) -> crate::Result<Extraction> {
                unimplemented!()
            }

            pub fn helper(input: &str) -> Result<u32> {
                unimplemented!()
            }
        }
    "#;

    let surface = extract_from_source(source);
    let extract = surface.functions.iter().find(|f| f.name == "extract").unwrap();
    assert_eq!(
        extract.error_type.as_deref(),
        Some("error::SampleCrateError"),
        "crate::Result<T> must resolve through the crate alias, got: {:?}",
        extract.error_type
    );
    let helper = surface.functions.iter().find(|f| f.name == "helper").unwrap();
    assert_eq!(
        helper.error_type.as_deref(),
        Some("anyhow::Error"),
        "the bare imported anyhow::Result must keep anyhow::Error, got: {:?}",
        helper.error_type
    );
}

/// The mirror image, and the negative control for the fix: a module that imports the crate alias
/// but writes `anyhow::Result<T>` fully qualified on one function must keep `anyhow::Error` there.
#[test]
fn test_foreign_qualified_result_is_not_rewritten_to_the_crate_error() {
    let source = r#"
        pub struct SampleCrateError;

        pub type Result<T> = std::result::Result<T, SampleCrateError>;

        pub mod api {
            use crate::Result;

            pub struct Script;

            pub fn compile(source: &str) -> anyhow::Result<Script> {
                unimplemented!()
            }

            pub fn run(source: &str) -> Result<u32> {
                unimplemented!()
            }
        }
    "#;

    let surface = extract_from_source(source);
    let compile = surface.functions.iter().find(|f| f.name == "compile").unwrap();
    assert_eq!(
        compile.error_type.as_deref(),
        Some("anyhow::Error"),
        "an inline anyhow::Result must keep anyhow::Error, got: {:?}",
        compile.error_type
    );
    let run = surface.functions.iter().find(|f| f.name == "run").unwrap();
    assert_eq!(
        run.error_type.as_deref(),
        Some("SampleCrateError"),
        "the imported crate alias must still supply the crate error, got: {:?}",
        run.error_type
    );
}

/// `super::error::Result<T>` written inline must pick the subsystem alias it names, not the
/// crate's canonical one.
#[test]
fn test_super_qualified_result_resolves_against_the_parent_module() {
    let source = r#"
        pub mod error {
            pub struct SampleCrateError;
            pub type Result<T> = std::result::Result<T, SampleCrateError>;
        }

        pub mod binary {
            pub mod error {
                pub struct BinaryFormatError;
                pub type Result<T> = std::result::Result<T, BinaryFormatError>;
            }

            pub mod model {
                pub fn parse_header(bytes: &str) -> super::error::Result<u32> {
                    unimplemented!()
                }
            }
        }

        pub use error::{Result, SampleCrateError};
    "#;

    let surface = extract_from_source(source);
    let parse_header = surface.functions.iter().find(|f| f.name == "parse_header").unwrap();
    assert_eq!(
        parse_header.error_type.as_deref(),
        Some("BinaryFormatError"),
        "super::error::Result must resolve to the parent module's alias, got: {:?}",
        parse_header.error_type
    );
}

/// An unqualified `Result<T>` in a module with no `Result` import keeps falling back to the
/// crate's canonical alias — the behavior the qualified-path handling must not disturb.
#[test]
fn test_unqualified_result_still_falls_back_to_the_canonical_alias() {
    let source = r#"
        pub mod error {
            pub struct SampleCrateError;
            pub type Result<T> = std::result::Result<T, SampleCrateError>;
        }

        pub mod api {
            pub fn load(path: &str) -> Result<u32> {
                unimplemented!()
            }
        }
    "#;

    let surface = extract_from_source(source);
    let load = surface.functions.iter().find(|f| f.name == "load").unwrap();
    assert_eq!(
        load.error_type.as_deref(),
        Some("SampleCrateError"),
        "an unqualified Result must still reach the canonical alias, got: {:?}",
        load.error_type
    );
}

/// A crate alias that genuinely resolves to `anyhow::Error` must keep answering `anyhow::Error`,
/// even when it is reached through a `crate::Result<T>` qualification.
#[test]
fn test_crate_alias_over_anyhow_error_stays_anyhow() {
    let source = r#"
        pub type Result<T> = std::result::Result<T, anyhow::Error>;

        pub mod api {
            pub struct Report;

            pub fn build() -> crate::Result<Report> {
                unimplemented!()
            }
        }
    "#;

    let surface = extract_from_source(source);
    let build = surface.functions.iter().find(|f| f.name == "build").unwrap();
    assert_eq!(
        build.error_type.as_deref(),
        Some("anyhow::Error"),
        "an alias whose error really is anyhow::Error must stay anyhow::Error, got: {:?}",
        build.error_type
    );
}

/// An inline `Result<T, E>` with both parameters spelled out never consults an alias at all.
#[test]
fn test_inline_two_parameter_result_ignores_the_alias() {
    let source = r#"
        pub struct SampleCrateError;
        pub struct ParseError;

        pub type Result<T> = std::result::Result<T, SampleCrateError>;

        pub mod api {
            use crate::{ParseError, Result};

            pub fn parse(input: &str) -> std::result::Result<u32, ParseError> {
                unimplemented!()
            }
        }
    "#;

    let surface = extract_from_source(source);
    let parse = surface.functions.iter().find(|f| f.name == "parse").unwrap();
    assert_eq!(
        parse.error_type.as_deref(),
        Some("ParseError"),
        "an explicit two-parameter Result must keep its own error type, got: {:?}",
        parse.error_type
    );
}

/// An alias generic over its *error* parameter has the concrete type only in the parameter's
/// default, so reading the right-hand side literally records the parameter name (`E`) as the error
/// type — a name no backend can resolve to anything. ~keep
#[test]
fn test_alias_generic_over_its_error_parameter_uses_the_default() {
    let source = r#"
        pub struct SampleCrateError;

        pub type Result<T, E = SampleCrateError> = std::result::Result<T, E>;

        pub mod api {
            use crate::Result;

            pub fn load(path: &str) -> Result<u32> {
                unimplemented!()
            }
        }
    "#;

    let surface = extract_from_source(source);
    let load = surface.functions.iter().find(|f| f.name == "load").unwrap();
    assert_eq!(
        load.error_type.as_deref(),
        Some("SampleCrateError"),
        "the error parameter's default is the alias's real error type, got: {:?}",
        load.error_type
    );
}

/// The same alias shape without a default has no concrete error type to offer, so it must record
/// no hint at all and let the fallback stand — never the bare parameter name.
#[test]
fn test_alias_generic_over_an_undefaulted_error_parameter_records_no_hint() {
    let source = r#"
        pub type Result<T, E> = std::result::Result<T, E>;

        pub mod api {
            use crate::Result;

            pub fn load(path: &str) -> Result<u32> {
                unimplemented!()
            }
        }
    "#;

    let surface = extract_from_source(source);
    let load = surface.functions.iter().find(|f| f.name == "load").unwrap();
    assert_eq!(
        load.error_type.as_deref(),
        Some("anyhow::Error"),
        "a bare generic parameter must never be recorded as an error type, got: {:?}",
        load.error_type
    );
}

/// Task #514 measurement: a crate declaring TWO error enums, with a function whose return type
/// spells out the second one explicitly (`Result<T, SecondError>`), must resolve `error_type` to
/// that exact enum -- never to whichever enum happens to be declared first. This is the
/// extraction-side half of the zig backend's `resolve_zig_error_type` name-mismatch fix
/// (5a2964785): that fix only matters if extraction hands it the right name to match against;
/// if extraction itself ever produced `FirstError` here, no backend-side fix could recover the
/// right answer, only a safe unknown one. ~keep
#[test]
fn test_error_type_matches_the_declared_enum_named_in_an_explicit_two_error_crate() {
    let source = r#"
        pub enum FirstError {
            Boom,
        }

        pub enum SecondError {
            Bang,
        }

        pub struct Widget;

        pub fn build(input: &str) -> std::result::Result<Widget, SecondError> {
            unimplemented!()
        }
    "#;

    let surface = extract_from_source(source);
    let build = surface.functions.iter().find(|f| f.name == "build").unwrap();
    assert_eq!(
        build.error_type.as_deref(),
        Some("SecondError"),
        "an explicit two-parameter Result naming the second declared enum must resolve to it \
         exactly, not the first-declared enum, got: {:?}",
        build.error_type
    );
}

/// Same measurement, but through a `Result<T>` alias whose hint is recorded in a *different*
/// module than the function that returns it, in a crate that declares two unrelated error enums.
/// `RESULT_ERROR_HINTS` is keyed by declaring module, which is the likeliest place a real defect
/// would hide once more than one error alias is in play. ~keep
#[test]
fn test_error_type_resolves_correctly_through_a_cross_module_alias_with_two_error_enums() {
    let source = r#"
        pub mod primary_error {
            pub enum FirstError {
                Boom,
            }

            pub type Result<T> = std::result::Result<T, FirstError>;
        }

        pub mod secondary {
            pub mod error {
                pub enum SecondError {
                    Bang,
                }

                pub type Result<T> = std::result::Result<T, SecondError>;
            }

            pub mod api {
                use super::error::Result;

                pub struct Widget;

                pub fn build(input: &str) -> Result<Widget> {
                    unimplemented!()
                }
            }
        }
    "#;

    let surface = extract_from_source(source);
    let build = surface.functions.iter().find(|f| f.name == "build").unwrap();
    assert_eq!(
        build.error_type.as_deref(),
        Some("SecondError"),
        "a Result<T> alias declared in a sibling module, in a crate with two error enums, must \
         resolve to its own module's error type, got: {:?}",
        build.error_type
    );
}