alef 0.69.0

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
use super::{classify_param_type, emit_param_conversion};
use crate::core::ir::TypeRef;

/// Sync PyO3 free functions must release the GIL across the blocking core call so a
/// trait callback re-entering Python from a worker thread cannot deadlock. The generated
/// sync free function must (1) take an injected `py: Python<'_>` handle and (2) wrap the
/// core call in `py.detach(|| ...)`. This is a regression test for issue #136.
#[test]
fn sync_pyo3_free_fn_releases_gil_around_core_call() {
    use crate::codegen::generators::gen_function;
    use crate::core::ir::{FunctionDef, ParamDef};

    let func = FunctionDef {
        name: "count_words".to_owned(),
        rust_path: "sample_core::count_words".to_owned(),
        params: vec![ParamDef {
            name: "text".to_owned(),
            ty: TypeRef::String,
            optional: false,
            default: None,
            ..ParamDef::default()
        }],
        return_type: TypeRef::Primitive(crate::core::ir::PrimitiveType::U64),
        is_async: false,
        error_type: None,
        ..FunctionDef::default()
    };

    let mapper = crate::backends::pyo3::type_map::Pyo3Mapper::new();
    let cfg = crate::backends::pyo3::gen_bindings::config::binding_config("sample_core", true);
    let adapter_bodies = ahash::AHashMap::new();
    let opaque_types = ahash::AHashSet::new();

    let output = gen_function(&func, &mapper, &cfg, &adapter_bodies, &opaque_types);

    assert!(
        output.contains("py: Python<'_>"),
        "expected injected `py: Python<'_>` handle on sync free function:\n{output}"
    );
    assert!(
        output.contains("py.detach(|| sample_core::count_words("),
        "expected core call wrapped in `py.detach(|| ...)`:\n{output}"
    );
}

/// classify_param_type returns Plain for a bare Named type.
#[test]
fn classify_param_type_returns_plain_for_named() {
    let ty = TypeRef::Named("Foo".to_string());
    let result = classify_param_type(&ty);
    assert!(result.is_some());
    let (name, _) = result.unwrap();
    assert_eq!(name, "Foo");
}

/// classify_param_type returns None for a primitive type.
#[test]
fn classify_param_type_returns_none_for_primitive() {
    let ty = TypeRef::Primitive(crate::core::ir::PrimitiveType::Bool);
    assert!(classify_param_type(&ty).is_none());
}

/// emit_param_conversion emits a guarded None check when optional.
#[test]
fn emit_param_conversion_guards_optional() {
    let mut out = String::new();
    emit_param_conversion(&mut out, "_rust_x", "x", "convert(x)", true);
    assert!(out.contains("if x is not None else None"));
}

/// emit_param_conversion emits a direct assignment when not optional.
#[test]
fn emit_param_conversion_direct_when_required() {
    let mut out = String::new();
    emit_param_conversion(&mut out, "_rust_x", "x", "convert(x)", false);
    assert!(!out.contains("if x is not None"));
    assert!(out.contains("_rust_x = convert(x)"));
}

/// Async Pyo3 functions with let_bindings that create temporary borrows
/// (e.g., Vec<&str> from Vec<String>) must place the bindings INSIDE the
/// `async move` block, not before it. This ensures the temporary lifetimes
/// extend to when the future executes, not just when the function returns.
///
/// This is a regression test for the fix that moves ref_let_bindings inside
/// the async block for AsyncPattern::Pyo3FutureIntoPy functions.
#[test]
fn async_pyo3_functions_place_bindings_inside_async_block() {}

/// Regression guard for issue #145: the wide-integer return cast is gated on the extendr-only
/// cast flags (`cast_large_ints_to_f64` / `cast_uints_to_i32`). pyo3 does not set them, so a
/// `usize` return must stay `usize` with no `as f64` cast leaking into the body.
#[test]
fn pyo3_usize_return_is_not_cast_to_f64() {
    use crate::codegen::generators::gen_function;
    use crate::core::ir::{FunctionDef, PrimitiveType};

    let func = FunctionDef {
        name: "wide_value".to_owned(),
        rust_path: "sample_core::wide_value".to_owned(),
        params: vec![],
        return_type: TypeRef::Primitive(PrimitiveType::Usize),
        is_async: false,
        error_type: None,
        ..FunctionDef::default()
    };

    let mapper = crate::backends::pyo3::type_map::Pyo3Mapper::new();
    let cfg = crate::backends::pyo3::gen_bindings::config::binding_config("sample_core", true);
    let adapter_bodies = ahash::AHashMap::new();
    let opaque_types = ahash::AHashSet::new();

    let output = gen_function(&func, &mapper, &cfg, &adapter_bodies, &opaque_types);

    assert!(
        !output.contains("as f64"),
        "pyo3 usize return must not be cast to f64:\n{output}"
    );
    assert!(
        output.contains("-> usize"),
        "pyo3 signature should keep usize:\n{output}"
    );
}

/// Regression test for issue #380: a `&mut T` DTO parameter on a unit-returning sync function
/// previously rendered as `pub fn tag_record(record: Record) -> ()`, mutating a dropped `_core`
/// intermediate and leaving the caller's value untouched with no diagnostic. The binding must
/// instead return the mutated intermediate so the caller can observe the update.
#[test]
fn pyo3_mut_dto_param_returns_the_updated_value() {
    use crate::codegen::generators::gen_function;
    use crate::core::ir::{FunctionDef, ParamDef};

    let func = FunctionDef {
        name: "tag_record".to_owned(),
        rust_path: "sample_core::tag_record".to_owned(),
        params: vec![ParamDef {
            name: "record".to_owned(),
            ty: TypeRef::Named("Record".to_owned()),
            is_ref: true,
            is_mut: true,
            ..ParamDef::default()
        }],
        return_type: TypeRef::Unit,
        is_async: false,
        error_type: None,
        ..FunctionDef::default()
    };

    let mapper = crate::backends::pyo3::type_map::Pyo3Mapper::new();
    let cfg = crate::backends::pyo3::gen_bindings::config::binding_config("sample_core", true);
    let adapter_bodies = ahash::AHashMap::new();
    let opaque_types = ahash::AHashSet::new();

    let output = gen_function(&func, &mapper, &cfg, &adapter_bodies, &opaque_types);

    assert!(
        output.contains("-> Record"),
        "expected the binding to return the mutated DTO type instead of `()`:\n{output}"
    );
    assert!(
        !output.contains("-> ()"),
        "must not still advertise a unit return:\n{output}"
    );
    // Load-bearing round-trip: the core call must still pass `&mut record_core` (the mutation
    // actually happens) AND the tail must hand back `record_core.into()` (the mutation is
    // actually observable). Asserting only the signature changed would pass on a binding that
    // mutates nothing, or on one that returns a fresh default value instead of the mutated one.
    assert!(
        output.contains("py.detach(|| sample_core::tag_record(&mut record_core))"),
        "expected the core call to still pass `&mut record_core`:\n{output}"
    );
    assert!(
        output.contains("record_core.into()"),
        "expected the mutated intermediate to be returned:\n{output}"
    );
}

/// Negative control for issue #380: an immutable `&T` DTO param must not gain write-back
/// semantics -- the return type must stay `()`.
#[test]
fn pyo3_immutable_dto_param_keeps_unit_return() {
    use crate::codegen::generators::gen_function;
    use crate::core::ir::{FunctionDef, ParamDef};

    let func = FunctionDef {
        name: "read_record".to_owned(),
        rust_path: "sample_core::read_record".to_owned(),
        params: vec![ParamDef {
            name: "record".to_owned(),
            ty: TypeRef::Named("Record".to_owned()),
            is_ref: true,
            is_mut: false,
            ..ParamDef::default()
        }],
        return_type: TypeRef::Unit,
        is_async: false,
        error_type: None,
        ..FunctionDef::default()
    };

    let mapper = crate::backends::pyo3::type_map::Pyo3Mapper::new();
    let cfg = crate::backends::pyo3::gen_bindings::config::binding_config("sample_core", true);
    let adapter_bodies = ahash::AHashMap::new();
    let opaque_types = ahash::AHashSet::new();

    let output = gen_function(&func, &mapper, &cfg, &adapter_bodies, &opaque_types);

    assert!(
        output.contains("-> ()"),
        "immutable borrow must keep unit return:\n{output}"
    );
    assert!(
        !output.contains("record_core.into()"),
        "immutable borrow must not gain a write-back tail:\n{output}"
    );
}

/// Negative control for issue #380: an owned `T` DTO param (no `&mut`, no write-back candidate)
/// must render byte-for-byte the same as before the fix.
#[test]
fn pyo3_owned_dto_param_unaffected_by_writeback() {
    use crate::codegen::generators::gen_function;
    use crate::core::ir::{FunctionDef, ParamDef};

    let func = FunctionDef {
        name: "consume_record".to_owned(),
        rust_path: "sample_core::consume_record".to_owned(),
        params: vec![ParamDef {
            name: "record".to_owned(),
            ty: TypeRef::Named("Record".to_owned()),
            is_ref: false,
            is_mut: false,
            ..ParamDef::default()
        }],
        return_type: TypeRef::Unit,
        is_async: false,
        error_type: None,
        ..FunctionDef::default()
    };

    let mapper = crate::backends::pyo3::type_map::Pyo3Mapper::new();
    let cfg = crate::backends::pyo3::gen_bindings::config::binding_config("sample_core", true);
    let adapter_bodies = ahash::AHashMap::new();
    let opaque_types = ahash::AHashSet::new();

    let output = gen_function(&func, &mapper, &cfg, &adapter_bodies, &opaque_types);

    assert!(output.contains("-> ()"), "owned param must keep unit return:\n{output}");
    assert!(
        output.contains("py.detach(|| sample_core::consume_record(record_core))"),
        "owned param call must be unaffected:\n{output}"
    );
    assert!(
        !output.contains("record_core.into()"),
        "owned param must not gain a write-back tail:\n{output}"
    );
}

/// Regression test for issue #380 (async path): a `&mut T` DTO parameter on a unit-returning
/// `async fn` previously rendered as `pub fn tag_record_async<'py>(py: Python<'py>, record:
/// Record) -> PyResult<Bound<'py, PyAny>>` whose future body mutated a dropped `_core`
/// intermediate and then resolved to `Ok(())` -- the caller's value was left untouched with no
/// diagnostic. The binding must instead resolve to the mutated intermediate.
#[test]
fn pyo3_async_mut_dto_param_returns_the_updated_value() {
    use crate::codegen::generators::gen_function;
    use crate::core::ir::{FunctionDef, ParamDef};

    let func = FunctionDef {
        name: "tag_record_async".to_owned(),
        rust_path: "sample_core::tag_record_async".to_owned(),
        params: vec![ParamDef {
            name: "record".to_owned(),
            ty: TypeRef::Named("Record".to_owned()),
            is_ref: true,
            is_mut: true,
            ..ParamDef::default()
        }],
        return_type: TypeRef::Unit,
        is_async: true,
        error_type: None,
        ..FunctionDef::default()
    };

    let mapper = crate::backends::pyo3::type_map::Pyo3Mapper::new();
    let cfg = crate::backends::pyo3::gen_bindings::config::binding_config("sample_core", true);
    let adapter_bodies = ahash::AHashMap::new();
    let opaque_types = ahash::AHashSet::new();

    let output = gen_function(&func, &mapper, &cfg, &adapter_bodies, &opaque_types);

    // Load-bearing round-trip: the core call must still `.await` while passing
    // `&mut record_core`, AND the future must resolve to `Ok(record_core.into())`.
    assert!(
        output.contains("sample_core::tag_record_async(&mut record_core).await"),
        "expected the core call to still be awaited with `&mut record_core`:\n{output}"
    );
    assert!(
        output.contains("Ok(record_core.into())"),
        "expected the future to resolve to the mutated intermediate:\n{output}"
    );
    assert!(
        !output.contains("Ok(())"),
        "must not resolve the future to unit and drop the mutated value:\n{output}"
    );
}

/// Negative control: an async immutable `&T` DTO param must not gain write-back semantics.
#[test]
fn pyo3_async_immutable_dto_param_unaffected_by_writeback() {
    use crate::codegen::generators::gen_function;
    use crate::core::ir::{FunctionDef, ParamDef};

    let func = FunctionDef {
        name: "read_record_async".to_owned(),
        rust_path: "sample_core::read_record_async".to_owned(),
        params: vec![ParamDef {
            name: "record".to_owned(),
            ty: TypeRef::Named("Record".to_owned()),
            is_ref: true,
            is_mut: false,
            ..ParamDef::default()
        }],
        return_type: TypeRef::Unit,
        is_async: true,
        error_type: None,
        ..FunctionDef::default()
    };

    let mapper = crate::backends::pyo3::type_map::Pyo3Mapper::new();
    let cfg = crate::backends::pyo3::gen_bindings::config::binding_config("sample_core", true);
    let adapter_bodies = ahash::AHashMap::new();
    let opaque_types = ahash::AHashSet::new();

    let output = gen_function(&func, &mapper, &cfg, &adapter_bodies, &opaque_types);

    assert!(
        !output.contains("record_core.into()"),
        "immutable borrow must not gain a write-back tail:\n{output}"
    );
}

/// Negative control: an async owned `T` DTO param must render unaffected by write-back.
#[test]
fn pyo3_async_owned_dto_param_unaffected_by_writeback() {
    use crate::codegen::generators::gen_function;
    use crate::core::ir::{FunctionDef, ParamDef};

    let func = FunctionDef {
        name: "consume_record_async".to_owned(),
        rust_path: "sample_core::consume_record_async".to_owned(),
        params: vec![ParamDef {
            name: "record".to_owned(),
            ty: TypeRef::Named("Record".to_owned()),
            is_ref: false,
            is_mut: false,
            ..ParamDef::default()
        }],
        return_type: TypeRef::Unit,
        is_async: true,
        error_type: None,
        ..FunctionDef::default()
    };

    let mapper = crate::backends::pyo3::type_map::Pyo3Mapper::new();
    let cfg = crate::backends::pyo3::gen_bindings::config::binding_config("sample_core", true);
    let adapter_bodies = ahash::AHashMap::new();
    let opaque_types = ahash::AHashSet::new();

    let output = gen_function(&func, &mapper, &cfg, &adapter_bodies, &opaque_types);

    assert!(
        !output.contains("record_core.into()"),
        "owned param must not gain a write-back tail:\n{output}"
    );
}

/// Async write-back must also work when the core function returns `Result<(), E>`: the future
/// must map the `Ok(())` to `Ok(record_core.into())` rather than discarding it.
#[test]
fn pyo3_async_mut_dto_param_with_error_returns_the_updated_value() {
    use crate::codegen::generators::gen_function;
    use crate::core::ir::{FunctionDef, ParamDef};

    let func = FunctionDef {
        name: "tag_record_async".to_owned(),
        rust_path: "sample_core::tag_record_async".to_owned(),
        params: vec![ParamDef {
            name: "record".to_owned(),
            ty: TypeRef::Named("Record".to_owned()),
            is_ref: true,
            is_mut: true,
            ..ParamDef::default()
        }],
        return_type: TypeRef::Unit,
        is_async: true,
        error_type: Some("ProbeError".to_owned()),
        ..FunctionDef::default()
    };

    let mapper = crate::backends::pyo3::type_map::Pyo3Mapper::new();
    let cfg = crate::backends::pyo3::gen_bindings::config::binding_config("sample_core", true);
    let adapter_bodies = ahash::AHashMap::new();
    let opaque_types = ahash::AHashSet::new();

    let output = gen_function(&func, &mapper, &cfg, &adapter_bodies, &opaque_types);

    assert!(
        output.contains("-> PyResult<Bound<'py, PyAny>>"),
        "pyo3 async functions always declare a PyResult<Bound> outer signature:\n{output}"
    );
    assert!(
        output.contains("sample_core::tag_record_async(&mut record_core).await"),
        "expected the core call to still be awaited with `&mut record_core`:\n{output}"
    );
    assert!(
        output.contains("Ok(record_core.into())"),
        "expected the fallible future to resolve to the mutated intermediate on success:\n{output}"
    );
}