harn-hostlib 0.10.197

Opt-in code-intelligence and deterministic-tool host builtins for the Harn VM
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
//! `allow_visibility_widening` and the visibility checks around it.

use super::*;

#[test]
fn rust_nested_import_does_not_stand_in_for_the_sources_own_import() {
    // The tests module imports the moved struct itself, but the module body
    // uses it too and needs its own import from the new home.
    let fixture = Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\n"),
        (
            "src/a.rs",
            "pub struct Item {\n    pub id: u8,\n}\n\npub fn make() -> Item {\n    Item { id: 1 }\n}\n\n#[cfg(test)]\nmod tests {\n    use super::Item;\n\n    #[test]\n    fn builds() {\n        let _ = Item { id: 2 };\n    }\n}\n",
        ),
        ("src/b.rs", "pub fn other() {}\n"),
    ]);
    let result = fixture.mv("Item", "src/a.rs", "src/b.rs");
    assert_eq!(tag(&result), "applied", "{result:?}");
    let source = fixture.read("src/a.rs");
    assert!(
        source.starts_with("use crate::b::Item;\n\npub fn make() -> Item {"),
        "{source}"
    );
    assert!(source.contains("    use crate::b::Item;\n"), "{source}");
}

// === Visibility widening ===

const KIT: (&str, &str) = (
    "Cargo.toml",
    "[package]\nname = \"kit\"\nversion = \"0.1.0\"\n",
);

fn widened(result: &VmValue) -> Vec<(String, i64, String, String)> {
    list(field(result, "widened"))
        .iter()
        .map(|w| {
            (
                s(field(w, "path")),
                int(field(w, "line")),
                s(field(w, "from")),
                s(field(w, "to")),
            )
        })
        .collect()
}

fn widening_args<'a>(
    symbol: &'a str,
    path: &'a str,
    to_path: &'a str,
    allow: Option<&'a str>,
) -> Vec<(&'a str, VmValue)> {
    let mut args = vec![
        ("symbol", vm_string(symbol)),
        ("path", vm_string(path)),
        ("to_path", vm_string(to_path)),
    ];
    if let Some(level) = allow {
        args.push(("allow_visibility_widening", vm_string(level)));
    }
    args
}

/// `a` and `b` are siblings under the crate root, so `pub(super)` on an item
/// of `a` already reaches `b`.
fn sibling_modules_fixture() -> Fixture {
    Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\n"),
        (
            "src/a.rs",
            "fn helper() -> u8 {\n    1\n}\n\nfn untouched() -> u8 {\n    2\n}\n\npub fn caller() -> u8 {\n    helper()\n}\n",
        ),
        ("src/b.rs", "pub fn other() {}\n"),
    ])
}

#[test]
fn rust_widening_uses_the_narrowest_level_and_leaves_other_items_alone() {
    let fixture = sibling_modules_fixture();
    // The allowance is looser than needed: only `pub(super)` is applied.
    let result = fixture.move_symbol(&widening_args(
        "caller",
        "src/a.rs",
        "src/b.rs",
        Some("pub"),
    ));
    assert_eq!(tag(&result), "applied", "{result:?}");
    assert_eq!(
        widened(&result),
        [(
            "src/a.rs".to_string(),
            1,
            "private".to_string(),
            "pub(super)".to_string()
        )]
    );
    assert_eq!(
        fixture.read("src/a.rs"),
        "pub(super) fn helper() -> u8 {\n    1\n}\n\nfn untouched() -> u8 {\n    2\n}\n"
    );
    let moved = fixture.read("src/b.rs");
    assert!(moved.contains("use crate::a::helper;"), "{moved}");
    assert!(
        moved.contains("pub fn caller() -> u8 {\n    helper()\n}"),
        "{moved}"
    );
}

#[test]
fn rust_widening_defaults_to_refusing_and_names_the_level() {
    let fixture = sibling_modules_fixture();
    let result = fixture.refused(
        "visibility_required",
        &widening_args("caller", "src/a.rs", "src/b.rs", None),
    );
    assert_eq!(s(field(&result, "required_visibility")), "pub(super)");
    assert!(list(field(&result, "widened")).is_empty());
    let sites = list(field(&result, "sites"));
    assert_eq!(sites.len(), 1, "{sites:?}");
    assert_eq!(int(field(&sites[0], "line")), 1);
}

#[test]
fn rust_widening_beyond_the_allowance_refuses() {
    // `a::model` and `b::moved` share only the crate root, so `pub(super)`
    // on a `a::model` item cannot reach `b::moved`: `pub(crate)` is needed.
    let fixture = Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\n"),
        ("src/a/mod.rs", "pub mod model;\n"),
        (
            "src/a/model.rs",
            "fn helper() -> u8 {\n    1\n}\n\npub fn caller() -> u8 {\n    helper()\n}\n",
        ),
        ("src/b/mod.rs", "pub mod moved;\n"),
        ("src/b/moved.rs", "pub fn other() {}\n"),
    ]);
    let result = fixture.refused(
        "visibility_required",
        &widening_args(
            "caller",
            "src/a/model.rs",
            "src/b/moved.rs",
            Some("pub(super)"),
        ),
    );
    assert_eq!(s(field(&result, "required_visibility")), "pub(crate)");
    assert!(
        s(field(&result, "details")).contains("`pub(crate)`"),
        "{result:?}"
    );
    let applied = fixture.move_symbol(&widening_args(
        "caller",
        "src/a/model.rs",
        "src/b/moved.rs",
        Some("pub(crate)"),
    ));
    assert_eq!(tag(&applied), "applied", "{applied:?}");
    assert_eq!(
        widened(&applied),
        [(
            "src/a/model.rs".to_string(),
            1,
            "private".to_string(),
            "pub(crate)".to_string()
        )]
    );
}

#[test]
fn rust_widening_covers_private_fields_and_the_moved_item_itself() {
    let fields = Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\n"),
        (
            "src/a.rs",
            "pub struct Job {\n    level: u8,\n    note: u8,\n}\n\npub fn level(job: &Job) -> u8 {\n    job.level\n}\n",
        ),
        ("src/b.rs", "pub fn other() {}\n"),
    ]);
    let result = fields.move_symbol(&widening_args(
        "level",
        "src/a.rs",
        "src/b.rs",
        Some("pub(crate)"),
    ));
    assert_eq!(tag(&result), "applied", "{result:?}");
    assert_eq!(
        widened(&result),
        [(
            "src/a.rs".to_string(),
            2,
            "private".to_string(),
            "pub(super)".to_string()
        )]
    );
    assert_eq!(
        fields.read("src/a.rs"),
        "pub struct Job {\n    pub(super) level: u8,\n    note: u8,\n}\n"
    );

    // A private item the source keeps using is widened where it lands.
    let item = Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\n"),
        (
            "src/a.rs",
            "fn helper() -> u8 {\n    1\n}\n\npub fn caller() -> u8 {\n    helper()\n}\n",
        ),
        ("src/b.rs", "pub fn other() {}\n"),
    ]);
    let refused = item.refused(
        "visibility_required",
        &widening_args("helper", "src/a.rs", "src/b.rs", None),
    );
    assert_eq!(s(field(&refused, "required_visibility")), "pub(super)");
    let result = item.move_symbol(&widening_args(
        "helper",
        "src/a.rs",
        "src/b.rs",
        Some("pub(super)"),
    ));
    assert_eq!(tag(&result), "applied", "{result:?}");
    assert_eq!(
        widened(&result),
        [(
            "src/a.rs".to_string(),
            1,
            "private".to_string(),
            "pub(super)".to_string()
        )]
    );
    let moved = item.read("src/b.rs");
    assert!(moved.contains("pub(super) fn helper() -> u8 {"), "{moved}");
    assert!(item.read("src/a.rs").contains("use crate::b::helper;"));
}

#[test]
fn rust_widening_replaces_a_restricted_field_for_the_modules_that_read_it() {
    let fixture = restricted_fields_fixture(&[
        (
            "src/a/mod.rs",
            "pub mod model;\n\npub fn level(m: &model::Meta) -> u8 {\n    m.level\n}\n",
        ),
        ("src/a/model/child.rs", "pub fn noop() {}\n"),
    ]);
    let refused = fixture.refused(
        "visibility_required",
        &widening_args(
            "Meta",
            "src/a/model.rs",
            "src/b/moved.rs",
            Some("pub(super)"),
        ),
    );
    assert_eq!(s(field(&refused, "required_visibility")), "pub(crate)");
    let result = fixture.move_symbol(&widening_args(
        "Meta",
        "src/a/model.rs",
        "src/b/moved.rs",
        Some("pub(crate)"),
    ));
    assert_eq!(tag(&result), "applied", "{result:?}");
    // Only the field `a` reads is widened; the private `id` nobody reads stays.
    assert_eq!(
        widened(&result),
        [(
            "src/a/model.rs".to_string(),
            4,
            "pub(super)".to_string(),
            "pub(crate)".to_string()
        )]
    );
    let moved = fixture.read("src/b/moved.rs");
    assert!(
        moved.contains("pub(crate) level: u8,\n    pub(crate) target: u8,\n    id: u8,\n"),
        "{moved}"
    );
}

#[test]
fn rust_widening_never_covers_constructors_or_restricted_paths() {
    // A struct update copies every field, so widening one read is not enough.
    let fixture = restricted_fields_fixture(&[
        ("src/a/mod.rs", "pub mod model;\n"),
        (
            "src/a/model/child.rs",
            "use super::Meta;\n\npub fn copy(m: &Meta) -> Meta {\n    Meta { target: 2, ..*m }\n}\n",
        ),
    ]);
    fixture.refused(
        "visibility_required",
        &widening_args("Meta", "src/a/model.rs", "src/b/moved.rs", Some("pub")),
    );
}

#[test]
fn widening_levels_must_belong_to_the_language() {
    let rust = sibling_modules_fixture();
    let mismatched = run(
        &rust.capability.shared(),
        &[dict(&widening_args(
            "caller",
            "src/a.rs",
            "src/b.rs",
            Some("export"),
        ))],
    );
    assert!(mismatched.is_err(), "export is not a Rust level");
    let unknown = run(
        &rust.capability.shared(),
        &[dict(&widening_args(
            "caller",
            "src/a.rs",
            "src/b.rs",
            Some("pub(in x)"),
        ))],
    );
    assert!(unknown.is_err(), "unknown levels are rejected, not ignored");
    let ts = Fixture::new(&[
        (
            "src/a.ts",
            "function helper(): number {\n  return 1\n}\n\nexport function caller(): number {\n  return helper()\n}\n",
        ),
        ("src/b.ts", "export const B = 1\n"),
    ]);
    let crate_level = run(
        &ts.capability.shared(),
        &[dict(&widening_args(
            "caller",
            "src/a.ts",
            "src/b.ts",
            Some("pub(crate)"),
        ))],
    );
    assert!(crate_level.is_err(), "pub(crate) is not a TypeScript level");
}

#[test]
fn ts_widening_exports_exactly_the_blocking_declarations() {
    let fixture = Fixture::new(&[
        (
            "src/a.ts",
            "function helper(): number {\n  return 1\n}\n\nfunction untouched(): number {\n  return 2\n}\n\nexport function caller(): number {\n  return helper()\n}\n",
        ),
        ("src/b.ts", "export const B = 1\n"),
    ]);
    fixture.refused(
        "visibility_required",
        &widening_args("caller", "src/a.ts", "src/b.ts", None),
    );
    let result = fixture.move_symbol(&widening_args(
        "caller",
        "src/a.ts",
        "src/b.ts",
        Some("export"),
    ));
    assert_eq!(tag(&result), "applied", "{result:?}");
    assert_eq!(
        widened(&result),
        [(
            "src/a.ts".to_string(),
            1,
            "unexported".to_string(),
            "export".to_string()
        )]
    );
    assert_eq!(
        fixture.read("src/a.ts"),
        "export function helper(): number {\n  return 1\n}\n\nfunction untouched(): number {\n  return 2\n}\n"
    );
    assert!(fixture
        .read("src/b.ts")
        .contains("import { helper } from \"./a\""));
}

#[test]
fn ts_widening_exports_a_moved_item_its_source_still_uses() {
    let fixture = Fixture::new(&[
        (
            "src/a.ts",
            "function helper(): number {\n  return 1\n}\n\nexport function caller(): number {\n  return helper()\n}\n",
        ),
        ("src/b.ts", "export const B = 1\n"),
    ]);
    let result = fixture.move_symbol(&widening_args(
        "helper",
        "src/a.ts",
        "src/b.ts",
        Some("export"),
    ));
    assert_eq!(tag(&result), "applied", "{result:?}");
    assert_eq!(
        widened(&result),
        [(
            "src/a.ts".to_string(),
            1,
            "unexported".to_string(),
            "export".to_string()
        )]
    );
    assert!(fixture
        .read("src/b.ts")
        .contains("export function helper(): number {"));
}

#[test]
fn rust_widened_item_in_a_new_module_gets_only_the_module_visibility_it_needs() {
    // Users in the item's former sibling: `pub(super)` reaches them, and the
    // private `mod` line the new module gets is already visible there.
    let near = sibling_modules_fixture();
    let result = near.move_symbol(&widening_args(
        "helper",
        "src/a.rs",
        "src/c.rs",
        Some("pub(crate)"),
    ));
    assert_eq!(tag(&result), "applied", "{result:?}");
    assert_eq!(
        widened(&result),
        [(
            "src/a.rs".to_string(),
            1,
            "private".to_string(),
            "pub(super)".to_string()
        )]
    );
    assert_eq!(near.read("src/lib.rs"), "pub mod a;\npub mod b;\nmod c;\n");

    // Users two levels away need `pub(crate)`, and the module line carries it
    // and is reported.
    let far = Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\n"),
        ("src/a/mod.rs", "pub mod model;\n"),
        (
            "src/a/model.rs",
            "fn helper() -> u8 {\n    1\n}\n\npub fn caller() -> u8 {\n    helper()\n}\n",
        ),
        ("src/b/mod.rs", "pub fn other() {}\n"),
    ]);
    let refused = far.refused(
        "visibility_required",
        &widening_args(
            "helper",
            "src/a/model.rs",
            "src/b/moved.rs",
            Some("pub(super)"),
        ),
    );
    assert_eq!(s(field(&refused, "required_visibility")), "pub(crate)");
    let result = far.move_symbol(&widening_args(
        "helper",
        "src/a/model.rs",
        "src/b/moved.rs",
        Some("pub(crate)"),
    ));
    assert_eq!(tag(&result), "applied", "{result:?}");
    assert_eq!(
        widened(&result),
        [
            (
                "src/b/mod.rs".to_string(),
                1,
                "private".to_string(),
                "pub(crate)".to_string()
            ),
            (
                "src/a/model.rs".to_string(),
                1,
                "private".to_string(),
                "pub(crate)".to_string()
            ),
        ]
    );
    assert!(far.read("src/b/mod.rs").contains("pub(crate) mod moved;"));
}

#[test]
fn rust_restricted_visibility_is_not_crate_wide() {
    let files = |helper: &'static str| -> [(&'static str, String); 6] {
        [
            (
                "Cargo.toml",
                "[package]\nname = \"kit\"\nversion = \"0.1.0\"\n".to_string(),
            ),
            ("src/lib.rs", "pub mod a;\npub mod b;\n".to_string()),
            (
                "src/a/mod.rs",
                "pub mod model;\npub mod sibling;\n".to_string(),
            ),
            (
                "src/a/model.rs",
                format!(
                    "{helper} fn helper() -> u8 {{\n    1\n}}\n\npub fn caller() -> u8 {{\n    helper()\n}}\n"
                ),
            ),
            ("src/a/sibling.rs", "pub fn other() {}\n".to_string()),
            ("src/b/mod.rs", "pub mod moved;\n".to_string()),
        ]
    };
    let fixture = |helper: &'static str| {
        let mut all: Vec<(String, String)> = files(helper)
            .into_iter()
            .map(|(p, b)| (p.to_string(), b))
            .collect();
        all.push((
            "src/b/moved.rs".to_string(),
            "pub fn other() {}\n".to_string(),
        ));
        let refs: Vec<(&str, &str)> = all.iter().map(|(p, b)| (p.as_str(), b.as_str())).collect();
        Fixture::new(&refs)
    };

    // `pub(super)` reaches `a` and its children, not `b::moved`.
    let hidden = fixture("pub(super)");
    let refused = hidden.refused(
        "visibility_required",
        &widening_args("caller", "src/a/model.rs", "src/b/moved.rs", None),
    );
    assert_eq!(s(field(&refused, "required_visibility")), "pub(crate)");
    let result = hidden.move_symbol(&widening_args(
        "caller",
        "src/a/model.rs",
        "src/b/moved.rs",
        Some("pub(crate)"),
    ));
    assert_eq!(tag(&result), "applied", "{result:?}");
    assert_eq!(
        widened(&result),
        [(
            "src/a/model.rs".to_string(),
            1,
            "pub(super)".to_string(),
            "pub(crate)".to_string()
        )]
    );
    assert!(hidden
        .read("src/a/model.rs")
        .starts_with("pub(crate) fn helper()"));

    // The destination inside the region sees it already: no refusal, no change.
    let inside = fixture("pub(super)");
    let result = inside.mv("caller", "src/a/model.rs", "src/a/sibling.rs");
    assert_eq!(tag(&result), "applied", "{result:?}");
    assert!(inside
        .read("src/a/model.rs")
        .starts_with("pub(super) fn helper()"));

    // A path-restricted modifier is never rewritten, whatever the allowance.
    let pathed = fixture("pub(in crate::a)");
    pathed.refused(
        "visibility_required",
        &widening_args("caller", "src/a/model.rs", "src/b/moved.rs", Some("pub")),
    );
}

#[test]
fn rust_widening_leaves_a_same_named_member_of_another_type_alone() {
    // The moved function flushes a `Stdout`; the private `flush` belongs to
    // `Buffer`, which the function never names.
    let unrelated = Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\n"),
        (
            "src/a.rs",
            "pub struct Buffer;\n\nimpl Buffer {\n    fn flush(&self) {}\n}\n\npub fn run(out: &mut std::io::Stdout) {\n    use std::io::Write;\n    out.flush().unwrap();\n}\n",
        ),
        ("src/b.rs", "pub fn other() {}\n"),
    ]);
    unrelated.refused(
        "visibility_required",
        &widening_args("run", "src/a.rs", "src/b.rs", Some("pub")),
    );

    // Two owners declare a private `flush`: the name does not pick one.
    let twins = Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\n"),
        (
            "src/a.rs",
            "pub struct One;\npub struct Two;\n\nimpl One {\n    fn flush(&self) {}\n}\n\nimpl Two {\n    fn flush(&self) {}\n}\n\npub fn run(one: &One) {\n    one.flush();\n}\n",
        ),
        ("src/b.rs", "pub fn other() {}\n"),
    ]);
    twins.refused(
        "visibility_required",
        &widening_args("run", "src/a.rs", "src/b.rs", Some("pub")),
    );
}

#[test]
fn rust_widening_requires_the_receiver_to_be_the_owning_type() {
    // The function names `Buffer` and calls `flush`, but on a `Stdout`.
    let fixture = Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\n"),
        (
            "src/a.rs",
            "pub struct Buffer;\n\nimpl Buffer {\n    fn flush(&self) {}\n}\n\npub fn run(_buf: &Buffer, out: &mut std::io::Stdout) {\n    use std::io::Write;\n    out.flush().unwrap();\n}\n",
        ),
        ("src/b.rs", "pub fn other() {}\n"),
    ]);
    fixture.refused(
        "visibility_required",
        &widening_args("run", "src/a.rs", "src/b.rs", Some("pub")),
    );
}

#[test]
fn rust_widening_respects_receiver_lexical_scope() {
    for body in [
        "pub fn run(out: &mut std::io::Stdout) {\n    fn helper(out: &Buffer) {}\n    use std::io::Write;\n    out.flush().unwrap();\n}\n",
        "pub fn run(out: &Buffer) {\n    fn helper(out: &mut std::io::Stdout) {\n        use std::io::Write;\n        out.flush().unwrap();\n    }\n}\n",
        "pub fn run<Buffer: std::io::Write>(out: &mut Buffer) {\n    out.flush().unwrap();\n}\n",
        "pub fn run(out: &crate::other::Buffer) {\n    out.flush();\n}\n",
        "pub fn run() {\n    use crate::other::*;\n    let _ = Buffer { flush: 1 };\n}\n",
        "pub fn run(out: &Buffer, other: &crate::other::Buffer) {\n    let r#out = other;\n    out.flush();\n}\n",
        "pub fn run(out: &Buffer, other: &crate::other::Buffer) {\n    macro_rules! bind { ($name:ident, $value:expr) => { let $name = $value; }; }\n    bind!(out, other);\n    out.flush();\n}\n",
        "pub fn run(out: &Buffer, other: &crate::other::Buffer) {\n    macro_rules! assert { ($name:ident, $value:expr) => { let $name = $value; }; }\n    assert!(out, other);\n    out.flush();\n}\n",
        "macro_rules! install { ($d:tt) => { macro_rules! assert { ($d name:ident, $d value:expr) => { let $d name = $d value; }; } }; }\ninstall!($);\npub fn run(out: &Buffer, other: &crate::other::Buffer) {\n    assert!(out, other);\n    out.flush();\n}\n",
        "pub fn run() {\n    #[derive(binding_macro::InstallBuffer)]\n    struct Marker;\n    let _ = Buffer { flush: 1 };\n}\n",
        "pub fn run() {\n    #[cfg_attr(all(), binding_macro::install_buffer)]\n    struct Marker;\n    let _ = Buffer { flush: 1 };\n}\n",
        "pub fn run() {\n    #[cfg_attr(all(), derive(binding_macro::InstallBuffer))]\n    struct Marker;\n    let _ = Buffer { flush: 1 };\n}\n",
        "pub fn run() {\n    #[cfg_attr(all(), cfg_attr(all(), binding_macro::install_buffer))]\n    struct Marker;\n    let _ = Buffer { flush: 1 };\n}\n",
        "use binding_macro::install_buffer as test;\npub fn run() {\n    #[test]\n    struct Marker;\n    let _ = Buffer { flush: 1 };\n}\n",
        "use binding_macro::install_buffer as derive;\npub fn run() {\n    #[derive(Debug)]\n    struct Marker;\n    let _ = Buffer { flush: 1 };\n}\n",
        "use binding_macro::install_buffer as derive;\npub fn run() {\n    #[cfg_attr(all(), derive(Debug))]\n    struct Marker;\n    let _ = Buffer { flush: 1 };\n}\n",
    ] {
        let source =
            format!("pub struct Buffer;\n\nimpl Buffer {{\n    fn flush(&self) {{}}\n}}\n\n{body}");
        let fixture = Fixture::new(&[
            KIT,
            ("src/lib.rs", "pub mod a;\npub mod b;\npub mod other;\n"),
            ("src/a.rs", &source),
            ("src/b.rs", "pub fn other() {}\n"),
            (
                "src/other.rs",
                "pub struct Buffer { pub flush: u8 }\nimpl Buffer { pub fn flush(&self) {} }\n",
            ),
        ]);
        fixture.refused(
            "visibility_required",
            &widening_args("run", "src/a.rs", "src/b.rs", Some("pub")),
        );
    }
}

#[test]
fn rust_widening_never_confuses_inherent_impl_specializations() {
    let fixture = Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\npub mod other;\n"),
        (
            "src/a.rs",
            "pub struct Buffer<T>(pub T);\n\nimpl Buffer<String> {\n    fn flush(&self) {}\n}\n\nimpl std::ops::Deref for Buffer<u8> {\n    type Target = crate::other::Buffer;\n    fn deref(&self) -> &Self::Target { &crate::other::Buffer }\n}\n\npub fn run(out: &Buffer<u8>) {\n    out.flush();\n}\n",
        ),
        ("src/b.rs", "pub fn other() {}\n"),
        (
            "src/other.rs",
            "pub struct Buffer;\nimpl Buffer { pub fn flush(&self) {} }\n",
        ),
    ]);
    fixture.refused(
        "visibility_required",
        &widening_args("run", "src/a.rs", "src/b.rs", Some("pub")),
    );
}

#[test]
fn rust_widening_never_confuses_owned_and_borrowed_method_receivers() {
    let fixture = receiver_dispatch_fixture(
        "fn flush(&self) {}",
        "pub fn run(out: Buffer) { use crate::other::Flush; out.flush(); }",
        "pub trait Flush { fn flush(self); }\nimpl Flush for crate::a::Buffer { fn flush(self) {} }\n",
    );
    fixture.refused(
        "visibility_required",
        &widening_args("run", "src/a.rs", "src/b.rs", Some("pub")),
    );
}

#[test]
fn rust_widening_never_confuses_shared_and_mutable_method_receivers() {
    let fixture = receiver_dispatch_fixture(
        "fn flush(&mut self) {}",
        "pub fn run(out: &Buffer) { use crate::other::Flush; out.flush(); }",
        "pub trait Flush { fn flush(&self); }\nimpl Flush for crate::a::Buffer { fn flush(&self) {} }\n",
    );
    fixture.refused(
        "visibility_required",
        &widening_args("run", "src/a.rs", "src/b.rs", Some("pub")),
    );
}

#[test]
fn rust_widening_never_assumes_a_conditional_member_is_present() {
    for attribute in [
        "#[cfg(feature = \"hidden_inherent\")]",
        "#[cfg_attr(all(), cfg(feature = \"hidden_inherent\"))]",
        "#[cfg_attr(all(), cfg_attr(all(), cfg(feature = \"hidden_inherent\")))]",
    ] {
        let method = format!("{attribute} fn flush(&self) {{}}");
        let fixture = receiver_dispatch_fixture(
            &method,
            "pub fn run(out: &Buffer) { #[cfg(not(feature = \"hidden_inherent\"))] { use crate::other::Flush; out.flush(); } }",
            "pub trait Flush { fn flush(&self); }\nimpl Flush for crate::a::Buffer { fn flush(&self) {} }\n",
        );
        fixture.refused(
            "visibility_required",
            &widening_args("run", "src/a.rs", "src/b.rs", Some("pub")),
        );
    }
    let inner = receiver_dispatch_fixture(
        "fn flush(&self) { #![cfg(feature = \"hidden_inherent\")] }",
        "pub fn run(out: &Buffer) { #[cfg(not(feature = \"hidden_inherent\"))] { use crate::other::Flush; out.flush(); } }",
        "pub trait Flush { fn flush(&self); }\nimpl Flush for crate::a::Buffer { fn flush(&self) {} }\n",
    );
    inner.refused(
        "visibility_required",
        &widening_args("run", "src/a.rs", "src/b.rs", Some("pub")),
    );
    let conditional_impl = receiver_dispatch_fixture(
        "",
        "#[cfg(feature = \"hidden_inherent\")] impl Buffer { fn flush(&self) {} }\npub fn run(out: &Buffer) { #[cfg(not(feature = \"hidden_inherent\"))] { use crate::other::Flush; out.flush(); } }",
        "pub trait Flush { fn flush(&self); }\nimpl Flush for crate::a::Buffer { fn flush(&self) {} }\n",
    );
    conditional_impl.refused(
        "visibility_required",
        &widening_args("run", "src/a.rs", "src/b.rs", Some("pub")),
    );
}

#[test]
fn rust_widening_keeps_exact_method_receivers_and_generic_fields() {
    for (method, caller, trait_code) in [
        (
            "#[inline] fn flush(&self) {}",
            "pub fn run(out: &Buffer) { use crate::other::Flush; out.flush(); }",
            "pub trait Flush { fn flush(self); }\nimpl Flush for &crate::a::Buffer { fn flush(self) {} }\n",
        ),
        (
            "#[cfg_attr(all(), inline)] fn flush(&mut self) {}",
            "pub fn run(out: &mut Buffer) { use crate::other::Flush; out.flush(); }",
            "pub trait Flush { fn flush(self); }\nimpl Flush for &mut crate::a::Buffer { fn flush(self) {} }\n",
        ),
        (
            "fn flush(self) {}",
            "pub fn run(out: Buffer) { use crate::other::Flush; out.flush(); }",
            "pub trait Flush { fn flush(&self); }\nimpl Flush for crate::a::Buffer { fn flush(&self) {} }\n",
        ),
    ] {
        let fixture = receiver_dispatch_fixture(method, caller, trait_code);
        let result = fixture.move_symbol(&widening_args(
            "run", "src/a.rs", "src/b.rs", Some("pub(super)"),
        ));
        assert_eq!(tag(&result), "applied", "{result:?}");
        assert_eq!(
            widened(&result),
            [("src/a.rs".to_string(), 3, "private".to_string(), "pub(super)".to_string())],
        );
    }
    let field = Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\n"),
        ("src/a.rs", "pub struct Buffer<T> {\n    flush: T,\n}\n\npub fn run(out: &Buffer<u8>) -> u8 { out.flush }\n"),
        ("src/b.rs", "pub fn other() {}\n"),
    ]);
    let result = field.move_symbol(&widening_args(
        "run",
        "src/a.rs",
        "src/b.rs",
        Some("pub(super)"),
    ));
    assert_eq!(tag(&result), "applied", "{result:?}");
    assert_eq!(
        widened(&result),
        [(
            "src/a.rs".to_string(),
            2,
            "private".to_string(),
            "pub(super)".to_string()
        )]
    );
}

fn receiver_dispatch_fixture(method: &str, caller: &str, trait_code: &str) -> Fixture {
    let source = format!("pub struct Buffer;\nimpl Buffer {{\n    {method}\n}}\n\n{caller}\n");
    Fixture::new(&[
        KIT,
        ("src/lib.rs", "pub mod a;\npub mod b;\npub mod other;\n"),
        ("src/a.rs", &source),
        ("src/b.rs", "pub fn other() {}\n"),
        ("src/other.rs", trait_code),
    ])
}

#[test]
fn rust_widening_keeps_proven_receivers_in_their_own_scope() {
    for body in [
        "pub fn run(out: &Buffer) {\n    fn helper(out: &mut std::io::Stdout) {}\n    out.flush();\n}\n",
        "pub fn run() {\n    fn helper(out: &Buffer) { out.flush(); }\n}\n",
        "pub fn run(out: &Buffer) {\n    { let out = 3; let _ = out; }\n    out.flush();\n}\n",
        "pub fn run(out: &Buffer) {\n    assert!(true);\n    out.flush();\n}\n",
        "pub fn run(out: &Buffer) {\n    use std::io::Write;\n    std::assert!(true);\n    out.flush();\n}\n",
        "pub fn run(out: &Buffer) {\n    #[derive(Debug, Clone)]\n    struct Marker;\n    out.flush();\n}\n",
        "pub fn run(out: &Buffer) {\n    #[cfg_attr(all(), derive(Debug, Clone))]\n    struct Marker;\n    out.flush();\n}\n",
    ] {
        let source =
            format!("pub struct Buffer;\n\nimpl Buffer {{\n    fn flush(&self) {{}}\n}}\n\n{body}");
        let fixture = Fixture::new(&[
            KIT,
            ("src/lib.rs", "pub mod a;\npub mod b;\n"),
            ("src/a.rs", &source),
            ("src/b.rs", "pub fn other() {}\n"),
        ]);
        let result =
            fixture.move_symbol(&widening_args("run", "src/a.rs", "src/b.rs", Some("pub")));
        assert_eq!(tag(&result), "applied", "{result:?}");
        assert_eq!(
            widened(&result),
            [(
                "src/a.rs".to_string(),
                4,
                "private".to_string(),
                "pub(super)".to_string()
            )],
        );
    }
}