aver-lang 0.26.0

VM and transpiler for Aver, a statically-typed language designed for AI-assisted development
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
//! Single-binary multi-module flattening for the wasm-gc backend.
//!
//! The wasm-gc backend emits one standalone module today. Dependent Aver
//! modules are therefore inlined into the entry module by prefixing function
//! names with their module path and rewriting cross-module calls to those
//! flat function names.
//!
//! Type identity follows two rules:
//!
//! - **Non-colliding types** (a bare name declared by exactly one dep
//!   module): the dep `TypeDef.name` stays bare. Entry-side qualified
//!   references like `record Wrapper { status: TmpReviewB.Status }` get
//!   their module prefix stripped to the bare form, matching the registry's
//!   bare key. Stamped expression types from the pre-flatten typechecker
//!   also use bare names and resolve correctly.
//! - **Colliding types** (same bare name declared by two or more dep
//!   modules — see #180 Phase 6 PR 3): dep `TypeDef.name` gets renamed to
//!   the canonical `"Prefix.Name"` form (`Left.Box`, `Right.Box`) so the
//!   wasm-gc `TypeRegistry` keys them into distinct slots. Dep-internal
//!   bare references to the colliding own type (in `TypeDef` field types,
//!   `FnDef` signatures, body `Constructor` / `RecordCreate` / `RecordUpdate`
//!   / pattern heads, let-binding annotations) get rewritten to canonical
//!   so the post-flatten resolver + registry agree. Entry-side qualified
//!   references stay verbatim (no strip) for the same reason.
//!
//! Treating collision as the trigger keeps the migration narrow: the legacy
//! strip path and bare-name lookups continue to work for every existing
//! single-declarer dep type, and only the genuinely-ambiguous slot pair gets
//! the canonical-key routing.

use std::collections::{HashMap, HashSet};

use crate::ast::{Expr, FnBody, Pattern, Spanned, Stmt, TopLevel, TypeDef};
use crate::codegen::ModuleInfo;
use crate::codegen::common::type_def_name;

/// Walks each loaded `ModuleInfo`, prefixes every fn name with
/// `{module_prefix}_`, rewrites bare same-module call sites to use the
/// prefixed name, strips non-colliding module-qualified type refs to their
/// bare form, canonicalises colliding own-type refs to `Prefix.Name`, then
/// appends the dep's renamed `TypeDef`s + prefixed `FnDef`s onto the entry
/// items.
///
/// Component Model is a future separate mode; this single-binary path is the
/// bench-friendly and playground-friendly default.
pub fn flatten_multimodule(items: &mut Vec<TopLevel>, dep_modules: &[ModuleInfo]) {
    if dep_modules.is_empty() {
        return;
    }

    let prefixes: HashSet<String> = dep_modules.iter().map(|m| m.prefix.clone()).collect();

    // Bare-name → owning dep prefix(es) across all dep modules. A bare
    // name that appears in two+ dep entries is "colliding"; the wasm-gc
    // `TypeRegistry` keys those canonically (`Left.Box` / `Right.Box`)
    // so the two slots don't merge under one bare key.
    let mut bare_owners: HashMap<String, Vec<String>> = HashMap::new();
    for dep in dep_modules {
        for td in &dep.type_defs {
            bare_owners
                .entry(type_def_name(td).to_string())
                .or_default()
                .push(dep.prefix.clone());
        }
    }
    let colliding_bare_names: HashSet<String> = bare_owners
        .iter()
        .filter(|(_, owners)| owners.len() > 1)
        .map(|(bare, _)| bare.clone())
        .collect();

    let qualified_type_names: HashSet<String> = dep_modules
        .iter()
        .flat_map(|dep| {
            dep.type_defs.iter().map(|td| {
                let name = match td {
                    TypeDef::Sum { name, .. } | TypeDef::Product { name, .. } => name.clone(),
                };
                format!("{}.{}", dep.prefix, name)
            })
        })
        .collect();

    let empty_own_colliding: HashSet<String> = HashSet::new();
    let empty_set: HashSet<String> = HashSet::new();

    for item in items.iter_mut() {
        match item {
            TopLevel::FnDef(fd) => {
                rewrite_fn_signature(fd, &qualified_type_names, &colliding_bare_names);
                let body_arc = std::sync::Arc::make_mut(&mut fd.body);
                let FnBody::Block(stmts) = body_arc;
                rewrite_stmts(stmts, &prefixes, None, &empty_set, &empty_own_colliding, "");
            }
            TopLevel::TypeDef(td) => {
                rewrite_type_def(td, &qualified_type_names, &colliding_bare_names);
            }
            _ => {}
        }
    }

    for dep in dep_modules {
        let same_module_fns: HashSet<String> =
            dep.fn_defs.iter().map(|fd| fd.name.clone()).collect();
        // Own colliding type names — the canonicalisation passes use
        // this set to rewrite dep-internal bare refs to `Prefix.Name`
        // (only colliding ones need the rewrite; non-colliding bare
        // names continue to resolve against the registry's bare key).
        let own_colliding: HashSet<String> = dep
            .type_defs
            .iter()
            .filter_map(|td| {
                let bare = type_def_name(td).to_string();
                if colliding_bare_names.contains(&bare) {
                    Some(bare)
                } else {
                    None
                }
            })
            .collect();

        for td in &dep.type_defs {
            let mut new_td = td.clone();
            rewrite_type_def(&mut new_td, &qualified_type_names, &colliding_bare_names);
            // Rename the typedef itself to `Prefix.Name` if it collides
            // with another dep's bare name.
            rename_typedef_if_colliding(&mut new_td, &dep.prefix, &colliding_bare_names);
            items.push(TopLevel::TypeDef(new_td));
        }

        for fd in &dep.fn_defs {
            let mut new_fd = fd.clone();
            rewrite_fn_signature(&mut new_fd, &qualified_type_names, &colliding_bare_names);
            canonicalise_fn_signature_for_own_colliding(&mut new_fd, &dep.prefix, &own_colliding);

            let body_arc = std::sync::Arc::make_mut(&mut new_fd.body);
            let FnBody::Block(stmts) = body_arc;
            rewrite_stmts(
                stmts,
                &prefixes,
                Some(&dep.prefix),
                &same_module_fns,
                &own_colliding,
                &dep.prefix,
            );

            new_fd.name = prefixed(&dep.prefix, &fd.name);
            items.push(TopLevel::FnDef(new_fd));
        }
    }
}

fn prefixed(prefix: &str, name: &str) -> String {
    format!("{}_{}", prefix.replace('.', "_"), name)
}

fn rewrite_fn_signature(
    fd: &mut crate::ast::FnDef,
    qualified_type_names: &HashSet<String>,
    colliding_bare_names: &HashSet<String>,
) {
    for (_, ty) in fd.params.iter_mut() {
        *ty = strip_non_colliding_prefixes(ty, qualified_type_names, colliding_bare_names);
    }
    fd.return_type =
        strip_non_colliding_prefixes(&fd.return_type, qualified_type_names, colliding_bare_names);
}

fn rewrite_type_def(
    td: &mut TypeDef,
    qualified_type_names: &HashSet<String>,
    colliding_bare_names: &HashSet<String>,
) {
    match td {
        TypeDef::Sum { variants, .. } => {
            for variant in variants {
                for ty in variant.fields.iter_mut() {
                    *ty = strip_non_colliding_prefixes(
                        ty,
                        qualified_type_names,
                        colliding_bare_names,
                    );
                }
            }
        }
        TypeDef::Product { fields, .. } => {
            for (_, ty) in fields.iter_mut() {
                *ty = strip_non_colliding_prefixes(ty, qualified_type_names, colliding_bare_names);
            }
        }
    }
}

fn rename_typedef_if_colliding(
    td: &mut TypeDef,
    dep_prefix: &str,
    colliding_bare_names: &HashSet<String>,
) {
    match td {
        TypeDef::Sum { name, .. } | TypeDef::Product { name, .. } => {
            if colliding_bare_names.contains(name.as_str()) {
                *name = format!("{dep_prefix}.{name}");
            }
        }
    }
}

fn canonicalise_fn_signature_for_own_colliding(
    fd: &mut crate::ast::FnDef,
    dep_prefix: &str,
    own_colliding: &HashSet<String>,
) {
    if own_colliding.is_empty() {
        return;
    }
    for (_, ty) in fd.params.iter_mut() {
        *ty = canonicalise_own_colliding(ty, dep_prefix, own_colliding);
    }
    fd.return_type = canonicalise_own_colliding(&fd.return_type, dep_prefix, own_colliding);
}

/// Strip module prefixes from qualified type references in `type_str`,
/// but leave colliding refs canonical. `Vector<TmpReviewB.Status>` becomes
/// `Vector<Status>`; `Vector<Left.Box>` stays as `Vector<Left.Box>` so
/// downstream `TypeRegistry` lookups land on the right slot.
fn strip_non_colliding_prefixes(
    type_str: &str,
    qualified_type_names: &HashSet<String>,
    colliding_bare_names: &HashSet<String>,
) -> String {
    if qualified_type_names.is_empty() {
        return type_str.to_string();
    }
    let mut out = type_str.to_string();
    for qualified in qualified_type_names {
        let Some((_, bare)) = qualified.rsplit_once('.') else {
            continue;
        };
        if colliding_bare_names.contains(bare) {
            // Two+ deps declare this bare name — keep the qualified form
            // so the wasm-gc registry can disambiguate by canonical key.
            continue;
        }
        out = replace_qualified_type(&out, qualified, bare);
    }
    out
}

/// Rewrite bare own-type references in `type_str` to `Prefix.Name` when
/// the bare name is in `own_colliding`. Run after the strip pass so the
/// two transforms compose cleanly.
fn canonicalise_own_colliding(
    type_str: &str,
    dep_prefix: &str,
    own_colliding: &HashSet<String>,
) -> String {
    if own_colliding.is_empty() {
        return type_str.to_string();
    }
    let mut out = String::with_capacity(type_str.len());
    let bytes = type_str.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        let b = bytes[i];
        if b.is_ascii_alphabetic() || b == b'_' {
            let start = i;
            while i < bytes.len()
                && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_' || bytes[i] == b'.')
            {
                i += 1;
            }
            let token = &type_str[start..i];
            if !token.contains('.') && own_colliding.contains(token) {
                out.push_str(dep_prefix);
                out.push('.');
                out.push_str(token);
            } else {
                out.push_str(token);
            }
        } else {
            out.push(b as char);
            i += 1;
        }
    }
    out
}

fn replace_qualified_type(input: &str, qualified: &str, bare: &str) -> String {
    let mut out = String::with_capacity(input.len());
    let mut i = 0;
    let bytes = input.as_bytes();
    while i < bytes.len() {
        let rest = &input[i..];
        if rest.starts_with(qualified) {
            let before_ok = i == 0 || is_type_boundary(bytes[i - 1]);
            let after = i + qualified.len();
            let after_ok = after >= bytes.len() || is_type_boundary(bytes[after]);
            if before_ok && after_ok {
                out.push_str(bare);
                i = after;
                continue;
            }
        }
        out.push(bytes[i] as char);
        i += 1;
    }
    out
}

fn is_type_boundary(byte: u8) -> bool {
    matches!(
        byte,
        b'<' | b'>' | b',' | b' ' | b'\t' | b'\n' | b'\r' | b'(' | b')'
    )
}

/// Flatten a chained `Attr` expression into its dotted form. Returns `None`
/// for any non-Attr or any Attr whose root is not an Ident.
fn attr_chain_to_dotted(expr: &Expr) -> Option<String> {
    match expr {
        Expr::Ident(name) => Some(name.clone()),
        Expr::Attr(parent, member) => {
            let head = attr_chain_to_dotted(&parent.node)?;
            Some(format!("{head}.{member}"))
        }
        _ => None,
    }
}

fn rewrite_expr(
    expr: &mut Expr,
    prefixes: &HashSet<String>,
    same_module_prefix: Option<&str>,
    same_module_fns: &HashSet<String>,
    own_colliding: &HashSet<String>,
    dep_prefix: &str,
) {
    match expr {
        Expr::FnCall(callee, args) => {
            let mut new_callee: Option<Expr> = None;
            if let Expr::Attr(parent, member) = &callee.node {
                if let Expr::Ident(p) = &parent.node
                    && prefixes.contains(p)
                {
                    new_callee = Some(Expr::Ident(prefixed(p, member)));
                } else if let Some(dotted) = attr_chain_to_dotted(&callee.node) {
                    new_callee = rewrite_dotted_module_ref(&dotted, prefixes);
                }
            }
            if new_callee.is_none()
                && let Expr::Ident(name) = &callee.node
                && let Some(prefix) = same_module_prefix
                && same_module_fns.contains(name)
            {
                new_callee = Some(Expr::Ident(prefixed(prefix, name)));
            }
            if let Some(rep) = new_callee {
                callee.node = rep;
            }
            rewrite_expr(
                &mut callee.node,
                prefixes,
                same_module_prefix,
                same_module_fns,
                own_colliding,
                dep_prefix,
            );
            for arg in args.iter_mut() {
                rewrite_expr(
                    &mut arg.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
        }
        Expr::TailCall(boxed) => {
            if let Some(prefix) = same_module_prefix
                && same_module_fns.contains(&boxed.target)
            {
                boxed.target = prefixed(prefix, &boxed.target);
            }
            for arg in boxed.args.iter_mut() {
                rewrite_expr(
                    &mut arg.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
        }
        Expr::BinOp(_, left, right) => {
            rewrite_expr(
                &mut left.node,
                prefixes,
                same_module_prefix,
                same_module_fns,
                own_colliding,
                dep_prefix,
            );
            rewrite_expr(
                &mut right.node,
                prefixes,
                same_module_prefix,
                same_module_fns,
                own_colliding,
                dep_prefix,
            );
        }
        Expr::Neg(inner) => {
            rewrite_expr(
                &mut inner.node,
                prefixes,
                same_module_prefix,
                same_module_fns,
                own_colliding,
                dep_prefix,
            );
        }
        Expr::Match { subject, arms } => {
            rewrite_expr(
                &mut subject.node,
                prefixes,
                same_module_prefix,
                same_module_fns,
                own_colliding,
                dep_prefix,
            );
            for arm in arms.iter_mut() {
                if !dep_prefix.is_empty() {
                    canonicalise_pattern(&mut arm.pattern, dep_prefix, own_colliding);
                }
                rewrite_expr(
                    &mut arm.body.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
        }
        Expr::Attr(_, _) => {
            // Cross-module call shape (`Worker.helper`) first — produces
            // an `Ident("Worker_helper")` for FnCall callees. Doing the
            // own-colliding canonicalisation before this would let the
            // canonicalised form be mis-matched as a cross-module access
            // and unrewritten back to bare.
            let rewrite = attr_chain_to_dotted(expr)
                .and_then(|dotted| rewrite_dotted_module_ref(&dotted, prefixes));
            if let Some(new_node) = rewrite {
                *expr = new_node;
                return;
            }
            if !dep_prefix.is_empty() {
                canonicalise_attr_head_for_own_colliding(expr, dep_prefix, own_colliding);
            }
            if let Expr::Attr(obj, _) = expr {
                rewrite_expr(
                    &mut obj.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
        }
        Expr::Constructor(name, payload) => {
            if !dep_prefix.is_empty() {
                canonicalise_constructor_name(name, dep_prefix, own_colliding);
            }
            if let Some(payload) = payload.as_deref_mut() {
                rewrite_expr(
                    &mut payload.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
        }
        Expr::RecordCreate { type_name, fields } => {
            if !dep_prefix.is_empty() {
                canonicalise_bare_type_name(type_name, dep_prefix, own_colliding);
            }
            for (_, expr) in fields.iter_mut() {
                rewrite_expr(
                    &mut expr.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
        }
        Expr::RecordUpdate {
            type_name,
            base,
            updates,
        } => {
            if !dep_prefix.is_empty() {
                canonicalise_bare_type_name(type_name, dep_prefix, own_colliding);
            }
            rewrite_expr(
                &mut base.node,
                prefixes,
                same_module_prefix,
                same_module_fns,
                own_colliding,
                dep_prefix,
            );
            for (_, expr) in updates.iter_mut() {
                rewrite_expr(
                    &mut expr.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
        }
        Expr::List(items) | Expr::Tuple(items) | Expr::IndependentProduct(items, _) => {
            for item in items.iter_mut() {
                rewrite_expr(
                    &mut item.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
        }
        Expr::MapLiteral(entries) => {
            for (key, value) in entries.iter_mut() {
                rewrite_expr(
                    &mut key.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
                rewrite_expr(
                    &mut value.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
        }
        Expr::ErrorProp(inner) => {
            rewrite_expr(
                &mut inner.node,
                prefixes,
                same_module_prefix,
                same_module_fns,
                own_colliding,
                dep_prefix,
            );
        }
        Expr::InterpolatedStr(parts) => {
            for part in parts.iter_mut() {
                if let crate::ast::StrPart::Parsed(inner) = part {
                    rewrite_expr(
                        &mut inner.node,
                        prefixes,
                        same_module_prefix,
                        same_module_fns,
                        own_colliding,
                        dep_prefix,
                    );
                }
            }
        }
        _ => {}
    }
}

fn canonicalise_bare_type_name(
    type_name: &mut String,
    dep_prefix: &str,
    own_colliding: &HashSet<String>,
) {
    if !type_name.contains('.') && own_colliding.contains(type_name.as_str()) {
        *type_name = format!("{dep_prefix}.{type_name}");
    }
}

fn canonicalise_constructor_name(
    name: &mut String,
    dep_prefix: &str,
    own_colliding: &HashSet<String>,
) {
    let head = name.split('.').next().unwrap_or(name);
    if own_colliding.contains(head) {
        let rest = &name[head.len()..];
        *name = format!("{dep_prefix}.{head}{rest}");
    }
}

fn canonicalise_attr_head_for_own_colliding(
    expr: &mut Expr,
    dep_prefix: &str,
    own_colliding: &HashSet<String>,
) {
    fn walk(e: &mut Expr, dep_prefix: &str, own_colliding: &HashSet<String>, depth: u32) {
        if depth > 32 {
            return;
        }
        match e {
            Expr::Attr(inner, _) => walk(&mut inner.node, dep_prefix, own_colliding, depth + 1),
            Expr::Ident(name) if !name.contains('.') && own_colliding.contains(name.as_str()) => {
                *name = format!("{dep_prefix}.{name}");
            }
            _ => {}
        }
    }
    walk(expr, dep_prefix, own_colliding, 0);
}

fn canonicalise_pattern(pat: &mut Pattern, dep_prefix: &str, own_colliding: &HashSet<String>) {
    if let Pattern::Constructor(name, _bindings) = pat {
        let head = name.split('.').next().unwrap_or(name);
        if own_colliding.contains(head) {
            let rest = &name[head.len()..];
            *name = format!("{dep_prefix}.{head}{rest}");
        }
    }
}

fn rewrite_dotted_module_ref(dotted: &str, prefixes: &HashSet<String>) -> Option<Expr> {
    let segments: Vec<&str> = dotted.split('.').collect();
    if segments.len() < 2 {
        return None;
    }

    let mut best: Option<usize> = None;
    for split in (1..segments.len()).rev() {
        let candidate = segments[..split].join(".");
        if prefixes.contains(&candidate) {
            best = Some(split);
            break;
        }
    }
    let split = best?;
    let prefix_dotted = segments[..split].join(".");

    Some(match segments.len() - split {
        0 => return None,
        1 => Expr::Ident(prefixed(&prefix_dotted, segments[split])),
        _ => {
            let type_name = segments[split..segments.len() - 1].join("_");
            let last = segments[segments.len() - 1].to_string();
            Expr::Attr(Box::new(Spanned::bare(Expr::Ident(type_name))), last)
        }
    })
}

fn rewrite_stmts(
    stmts: &mut [Stmt],
    prefixes: &HashSet<String>,
    same_module_prefix: Option<&str>,
    same_module_fns: &HashSet<String>,
    own_colliding: &HashSet<String>,
    dep_prefix: &str,
) {
    for stmt in stmts.iter_mut() {
        match stmt {
            Stmt::Binding(_, type_ann, expr) => {
                if !dep_prefix.is_empty()
                    && let Some(ty) = type_ann.as_mut()
                {
                    *ty = canonicalise_own_colliding(ty, dep_prefix, own_colliding);
                }
                rewrite_expr(
                    &mut expr.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
            Stmt::Expr(expr) => {
                rewrite_expr(
                    &mut expr.node,
                    prefixes,
                    same_module_prefix,
                    same_module_fns,
                    own_colliding,
                    dep_prefix,
                );
            }
        }
    }
}