alef 0.62.11

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
use crate::core::ir::{ApiSurface, DefaultValue, MethodDef, TypeDef, UnsupportedPublicItem};
use ahash::AHashMap;

use super::super::SerdeDefaultsByType;
use super::super::defaults::{ConstructorIndex, extract_default_values};
use super::super::helpers::{build_rust_path, extract_binding_exclusion_reason, extract_cfg_condition, is_test_gated};
use super::super::postprocess::{enum_default_variant_names, warn_on_default_disagreement};
use super::extract_method;

fn has_non_lifetime_generics(generics: &syn::Generics) -> bool {
    generics
        .params
        .iter()
        .any(|param| !matches!(param, syn::GenericParam::Lifetime(_)))
}

fn record_unsupported_generic_impl_methods(
    item: &syn::ItemImpl,
    crate_name: &str,
    type_name: &str,
    surface: &mut ApiSurface,
    reason: &str,
    methods_are_public_by_trait: bool,
) {
    for impl_item in &item.items {
        let syn::ImplItem::Fn(method) = impl_item else {
            continue;
        };
        if (!methods_are_public_by_trait && !super::super::helpers::is_pub(&method.vis))
            || extract_binding_exclusion_reason(&method.attrs).is_some()
        {
            continue;
        }
        let method_name = method.sig.ident.to_string();
        if method_name.starts_with('_') {
            continue;
        }
        surface.unsupported_public_items.push(UnsupportedPublicItem {
            item_kind: "method".to_string(),
            item_path: format!("{crate_name}::{type_name}.{method_name}"),
            reason: reason.to_string(),
            suggested_fix:
                "exclude the method, configure an opaque/bridge policy, or provide explicit monomorphization metadata"
                    .to_string(),
        });
    }
}

/// Extract methods from an `impl` block and attach them to the corresponding `TypeDef`.
#[allow(clippy::too_many_arguments)]
pub(crate) fn extract_impl_block(
    item: &syn::ItemImpl,
    crate_name: &str,
    module_path: &str,
    surface: &mut ApiSurface,
    type_index: &AHashMap<String, usize>,
    binding_excluded_type_names: &ahash::AHashSet<String>,
    result_wrapping_aliases: &ahash::AHashSet<String>,
    literal_consts: &AHashMap<String, DefaultValue>,
    constructors: &ConstructorIndex<'_>,
    pending_serde_defaults: &SerdeDefaultsByType,
) {
    // Honor `#[cfg_attr(alef, alef(skip))]` (or bare `#[alef(skip)]`) on the impl block
    if extract_binding_exclusion_reason(&item.attrs).is_some() {
        return;
    }

    // The block's own gate applies to every method it contains; `#[cfg(test)]` blocks were
    // already dropped by the caller, so anything left here is a real binding-surface gate. ~keep
    let impl_cfg = extract_cfg_condition(&item.attrs);

    if item.trait_.is_some() {
        extract_trait_impl_methods(
            item,
            crate_name,
            surface,
            type_index,
            result_wrapping_aliases,
            literal_consts,
            impl_cfg.as_deref(),
            constructors,
            pending_serde_defaults,
        );
        return;
    }

    let type_name = match &*item.self_ty {
        syn::Type::Path(p) => p.path.segments.last().map(|s| s.ident.to_string()).unwrap_or_default(),
        _ => return,
    };

    if binding_excluded_type_names.contains(&type_name)
        || type_index
            .get(&type_name)
            .is_some_and(|&idx| surface.types[idx].binding_excluded)
    {
        return;
    }

    if has_non_lifetime_generics(&item.generics) {
        record_unsupported_generic_impl_methods(
            item,
            crate_name,
            &type_name,
            surface,
            "public methods on generic impl blocks cannot be represented without explicit monomorphization metadata",
            false,
        );
        return;
    }

    let type_is_opaque = item.generics.params.is_empty()
        && (type_index
            .get(&type_name)
            .map(|&idx| surface.types[idx].is_opaque)
            .unwrap_or(false)
            || surface.enums.iter().any(|e| e.name == type_name)
            || surface.errors.iter().any(|e| e.name == type_name)
            || !type_index.contains_key(&type_name));

    let methods: Vec<MethodDef> = item
        .items
        .iter()
        .filter_map(|impl_item| {
            if let syn::ImplItem::Fn(method) = impl_item
                && super::super::helpers::is_pub(&method.vis) {
                    // Skip `#[cfg(test)]` methods (e.g. test-only constructors like
                    if is_test_gated(&method.attrs) {
                        return None;
                    }
                    if !method.sig.generics.params.is_empty() {
                        if extract_binding_exclusion_reason(&method.attrs).is_none() {
                            surface.unsupported_public_items.push(UnsupportedPublicItem {
                                item_kind: "method".to_string(),
                                item_path: format!("{crate_name}::{type_name}.{}", method.sig.ident),
                                reason: "public generic inherent methods cannot be represented without explicit monomorphization metadata".to_string(),
                                suggested_fix: "exclude the method, configure an opaque/bridge policy, or provide explicit monomorphization metadata".to_string(),
                            });
                        }
                        return None;
                    }
                    let method_name = method.sig.ident.to_string();
                    if method_name.starts_with('_') {
                        return None;
                    }
                    if method_name == "new" && !type_is_opaque
                        && let syn::ReturnType::Type(_, ty) = &method.sig.output
                            && matches!(&**ty, syn::Type::Path(p) if p.path.is_ident("Self")) {
                                return None;
                            }
                    return Some(extract_method(
                        method,
                        crate_name,
                        &type_name,
                        None,
                        result_wrapping_aliases,
                        impl_cfg.as_deref(),
                    ));
                }
            None
        })
        .collect();

    if methods.is_empty() {
        return;
    }

    if let Some(&idx) = type_index.get(&type_name) {
        for method in methods {
            // First-wins by name, with no `cfg` merge: when the same method name is provided by
            // two blocks under disjoint gates (`#[cfg(feature = "x")]` / `#[cfg(not(...))]`), the
            // first block's gate is the one that survives onto the retained `MethodDef`. Free
            // functions have `codegen::fn_dedup` for exactly this; methods have no counterpart
            // yet. Merging the gates (OR of the group, mirroring `with_deduped_functions`) is
            // deliberately deferred — do it here and in the trait-impl loop below together. ~keep
            if !surface.types[idx].methods.iter().any(|m| m.name == method.name) {
                surface.types[idx].methods.push(method);
            }
        }
    } else if let Some(error_def) = surface.errors.iter_mut().find(|e| e.name == type_name) {
        const ERROR_METHOD_WHITELIST: &[&str] = &["status_code", "is_transient", "error_type"];
        for method in methods {
            let is_whitelisted = ERROR_METHOD_WHITELIST.contains(&method.name.as_str());
            let already_present = error_def.methods.iter().any(|m| m.name == method.name);
            if is_whitelisted && !already_present {
                error_def.methods.push(method);
            }
        }
    } else if let Some(enum_def) = surface.enums.iter_mut().find(|e| {
        if e.name != type_name {
            return false;
        }
        let crate_prefix = format!("{crate_name}::");
        let rel = e.rust_path.strip_prefix(&*crate_prefix).unwrap_or(e.rust_path.as_str());
        let enum_module_rel = rel.rfind("::").map(|i| &rel[..i]).unwrap_or("");
        if enum_module_rel.is_empty() {
            return true;
        }
        if module_path.is_empty() {
            return false;
        }
        enum_module_rel.starts_with(module_path) || module_path.starts_with(enum_module_rel)
    }) {
        for method in &methods {
            if method.is_static && !enum_def.methods.iter().any(|m| m.name == method.name) {
                enum_def.methods.push(method.clone());
            }
        }
    } else {
        let rust_path = build_rust_path(crate_name, module_path, &type_name);
        surface.types.push(TypeDef {
            name: type_name.clone(),
            rust_path,
            original_rust_path: String::new(),
            fields: vec![],
            methods,
            is_opaque: true,
            is_clone: false,
            is_copy: false,
            is_trait: false,
            has_default: false,
            has_stripped_cfg_fields: false,
            is_return_type: false,
            doc: String::new(),
            cfg: None,
            serde_rename_all: None,
            has_serde: false,
            serde_container_default: false,
            super_traits: vec![],
            binding_excluded: true,
            binding_exclusion_reason: Some(
                "synthetic-opaque-from-impl-block (source visibility unverified)".to_string(),
            ),
            is_variant_wrapper: false,
            has_lifetime_params: false,
            has_private_fields: false,
            version: Default::default(),
        });
    }
}

/// The unit variant a hand-written `impl Default for SomeEnum` returns, when the body is a bare
/// `Self::Variant` / `SomeEnum::Variant` tail expression.
///
/// `#[derive(Default)]` records its choice on the variant itself (`EnumVariant::is_default`), but a
/// manual impl carries the same fact only in its body, and every consumer of `is_default` — the Go,
/// Rustler, Dart, WASM, Kotlin, Magnus and PHP backends, plus the generated Rust mirror enum's
/// `#[default]` marker — would otherwise fall back to the *first declared* variant or to no default
/// at all. Both are guesses that silently disagree with the Rust core whenever the real default is
/// declared elsewhere in the enum. Reading it here turns that guess into a fact.
///
/// Deliberately narrow: only a bare path to a unit variant is recognised. A tuple/struct variant, a
/// `match`, or any computed body leaves `is_default` unset, so callers keep their existing honest
/// fallback rather than receiving a fabricated variant. ~keep
fn manual_default_unit_variant(item: &syn::ItemImpl) -> Option<String> {
    let default_fn = item.items.iter().find_map(|impl_item| match impl_item {
        syn::ImplItem::Fn(method) if method.sig.ident == "default" => Some(method),
        _ => None,
    })?;

    let tail = match default_fn.block.stmts.last()? {
        syn::Stmt::Expr(expr, _) => expr,
        _ => return None,
    };
    let expr = match tail {
        syn::Expr::Return(ret) => ret.expr.as_deref()?,
        other => other,
    };
    let syn::Expr::Path(path_expr) = expr else {
        return None;
    };

    let segments = &path_expr.path.segments;
    if segments.len() != 2 {
        return None;
    }
    let qualifier = segments.first()?.ident.to_string();
    let variant = segments.last()?;
    if !variant.arguments.is_none() {
        return None;
    }
    let self_type = match &*item.self_ty {
        syn::Type::Path(p) => p.path.segments.last().map(|s| s.ident.to_string()),
        _ => None,
    };
    (qualifier == "Self" || Some(&qualifier) == self_type.as_ref()).then(|| variant.ident.to_string())
}

/// Extract methods from a trait impl and attach them to an existing type in the surface.
#[allow(clippy::too_many_arguments)]
fn extract_trait_impl_methods(
    item: &syn::ItemImpl,
    crate_name: &str,
    surface: &mut ApiSurface,
    type_index: &AHashMap<String, usize>,
    result_wrapping_aliases: &ahash::AHashSet<String>,
    literal_consts: &AHashMap<String, DefaultValue>,
    impl_cfg: Option<&str>,
    constructors: &ConstructorIndex<'_>,
    pending_serde_defaults: &SerdeDefaultsByType,
) {
    let type_name = match &*item.self_ty {
        syn::Type::Path(p) => p.path.segments.last().map(|s| s.ident.to_string()),
        _ => None,
    };

    let Some(type_name) = type_name else { return };

    let Some(&idx) = type_index.get(&type_name) else {
        if let Some((path, _)) = &item.trait_
            && path.segments.last().is_some_and(|s| s.ident == "Default")
            && let Some(enum_def) = surface.enums.iter_mut().find(|e| e.name == type_name)
        {
            enum_def.has_default = true;
            if let Some(variant_name) = manual_default_unit_variant(item)
                && let Some(variant) = enum_def
                    .variants
                    .iter_mut()
                    .find(|v| v.name == variant_name && v.fields.is_empty() && !v.originally_had_data_fields)
            {
                variant.is_default = true;
            }
        }
        return;
    };

    if has_non_lifetime_generics(&item.generics) {
        record_unsupported_generic_impl_methods(
            item,
            crate_name,
            &type_name,
            surface,
            "public trait implementation methods on generic impl blocks cannot be represented without explicit monomorphization metadata",
            true,
        );
        return;
    }

    const STD_TRAITS: &[&str] = &[
        "Default",
        "Clone",
        "Copy",
        "Debug",
        "Display",
        "Drop",
        "PartialEq",
        "Eq",
        "PartialOrd",
        "Ord",
        "Hash",
        "From",
        "Into",
        "TryFrom",
        "TryInto",
        "Iterator",
        "IntoIterator",
        "Send",
        "Sync",
        "Sized",
        "Unpin",
        "Serialize",
        "Deserialize",
    ];
    let trait_source = item.trait_.as_ref().and_then(|(path, _)| {
        let segments: Vec<String> = path.segments.iter().map(|s| s.ident.to_string()).collect();
        let trait_name = segments.last().map(|s| s.as_str()).unwrap_or("");
        if STD_TRAITS.contains(&trait_name) {
            return None;
        }
        if segments.len() == 1 {
            let trait_name = &segments[0];
            surface
                .types
                .iter()
                .find(|t| t.is_trait && t.name == *trait_name)
                .map(|t| t.rust_path.replace('-', "_"))
        } else {
            Some(segments.join("::").replace('-', "_"))
        }
    });

    let type_def = &mut surface.types[idx];

    let is_default_trait_impl = item
        .trait_
        .as_ref()
        .is_some_and(|(path, _)| path.segments.last().is_some_and(|segment| segment.ident == "Default"));
    if is_default_trait_impl {
        // NOTE: this also sets `has_default` for a *manual* `impl Default`, so the flag does not
        // distinguish a derived (type-zero) default from a hand-written one. Telling those apart
        // is `DefaultValue::Unresolved`'s job, not this flag's. ~keep
        let self_type = type_def.name.clone();
        type_def.has_default = true;
        extract_default_values(item, &self_type, &mut type_def.fields, literal_consts, constructors);
        if let Some(serde_defaults) = pending_serde_defaults.get(&type_def.rust_path) {
            // Only the enums this crate has extracted so far (this file's own module, plus any
            // earlier file in `sources`) are visible here — a manual `impl Default` naming a
            // variant of an enum declared in a *later* source file cannot be proven to agree, so
            // it falls back to `warn_on_default_disagreement`'s ordinary (warn-on-mismatch)
            // behavior instead of guessing. See `agrees_via_enum_default`. ~keep
            let enum_default_variants = enum_default_variant_names(&surface.enums);
            warn_on_default_disagreement(
                &type_def.rust_path,
                &type_def.fields,
                serde_defaults,
                &enum_default_variants,
            );
        }
    }

    let is_conversion_trait = item.trait_.as_ref().is_some_and(|(path, _)| {
        path.segments
            .last()
            .is_some_and(|s| matches!(s.ident.to_string().as_str(), "From" | "Into" | "TryFrom" | "TryInto"))
    });
    if is_conversion_trait {
        return;
    }

    let is_std_trait_impl = item.trait_.as_ref().is_some_and(|(path, _)| {
        path.segments
            .last()
            .is_some_and(|s| STD_TRAITS.contains(&s.ident.to_string().as_str()))
    });
    if is_std_trait_impl && !is_default_trait_impl {
        return;
    }

    for impl_item in &item.items {
        if let syn::ImplItem::Fn(method) = impl_item {
            if !method.sig.generics.params.is_empty() {
                if extract_binding_exclusion_reason(&method.attrs).is_none() {
                    surface.unsupported_public_items.push(UnsupportedPublicItem {
                        item_kind: "method".to_string(),
                        item_path: format!("{crate_name}::{type_name}.{}", method.sig.ident),
                        reason: "public generic trait implementation methods cannot be represented without explicit monomorphization metadata".to_string(),
                        suggested_fix: "exclude the method, configure an opaque/bridge policy, or provide explicit monomorphization metadata".to_string(),
                    });
                }
                continue;
            }
            let method_def = extract_method(
                method,
                crate_name,
                &type_name,
                trait_source.clone(),
                result_wrapping_aliases,
                impl_cfg,
            );
            // First-wins by name, no `cfg` merge — see the note in `extract_impl_block`. ~keep
            if !type_def.methods.iter().any(|m| m.name == method_def.name) {
                type_def.methods.push(method_def);
            }
        }
    }
}