harn-hostlib 0.10.201

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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//! How far a single-file name analysis can be trusted.
//!
//! `ast.undefined_names` subtracts the names a file defines from the names it
//! references. That subtraction is only evidence of a defect when the analysis
//! could actually see every way a name might arrive. Two things break it, and
//! they break it for different reasons:
//!
//! - **The language.** In Python, JavaScript, TypeScript and Rust every name
//!   must be imported or bound in the same file, so a single file is a
//!   complete unit (Rust paths such as `a::b` are never counted as references).
//!   In Go a sibling file in the same package contributes names with no import
//!   at all. In Ruby, names are routinely created at runtime by the framework.
//! - **The file.** A wildcard import, an `eval`, a `setattr`, or a
//!   `method_missing` defeats resolution even in a language that is otherwise a
//!   complete unit.
//!
//! Both halves are required. Reporting only the language would call a Python
//! star-import file trustworthy; reporting only the file would call a Ruby file
//! trustworthy because it happens to contain no dynamic construct.
//!
//! The output is a fact about the analysis, not a verdict about the code.
//! Callers decide what to do with a low-confidence finding; this module only
//! refuses to overstate one.

use std::collections::HashSet;

use harn_vm::VmValue;
use tree_sitter::{Node, Tree};

use crate::tools::args::{build_dict, str_value};

use super::language::Language;
use super::undefined_names::{bind_rust_use_tree, node_text};

/// How far single-file name resolution can be trusted for a language.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum ResolutionCeiling {
    /// Every name must be imported or bound in this file, so one file is a
    /// complete unit and a clean file yields confident findings.
    SingleFileComplete,
    /// Sibling files in the same package or namespace contribute names without
    /// an import, so one file can never see the whole symbol space.
    PackageScoped,
    /// Names are routinely created at runtime, so an unresolved name is not
    /// evidence of a defect.
    RuntimeResolved,
}

impl ResolutionCeiling {
    fn as_str(self) -> &'static str {
        match self {
            Self::SingleFileComplete => "single_file_complete",
            Self::PackageScoped => "package_scoped",
            Self::RuntimeResolved => "runtime_resolved",
        }
    }
}

/// The declared ceiling per language. Data, not a branch at a call site.
pub(super) fn ceiling(language: Language) -> ResolutionCeiling {
    match language {
        Language::Python
        | Language::JavaScript
        | Language::Jsx
        | Language::TypeScript
        | Language::Tsx
        | Language::Rust => ResolutionCeiling::SingleFileComplete,
        Language::Go => ResolutionCeiling::PackageScoped,
        Language::Ruby => ResolutionCeiling::RuntimeResolved,
        _ => ResolutionCeiling::RuntimeResolved,
    }
}

/// A construct in the file that defeats resolution, named so a caller can say
/// why a finding was demoted rather than just that it was.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Defeater {
    WildcardImport,
    DynamicNameAccess,
    DynamicMethodDefinition,
    Eval,
    WithScope,
    IdentifierNormalization,
    SyntaxError,
}

impl Defeater {
    pub(crate) fn as_str(self) -> &'static str {
        match self {
            Self::WildcardImport => "wildcard_import",
            Self::DynamicNameAccess => "dynamic_name_access",
            Self::DynamicMethodDefinition => "dynamic_method_definition",
            Self::Eval => "eval",
            Self::WithScope => "with_scope",
            Self::IdentifierNormalization => "identifier_normalization",
            Self::SyntaxError => "syntax_error",
        }
    }
}

/// The assessed resolvability of one file.
#[derive(Debug, Clone)]
pub(super) struct Resolution {
    ceiling: ResolutionCeiling,
    defeaters: Vec<Defeater>,
    /// False when no analysis ran at all — an unsupported language, or a file
    /// that did not parse. Kept separate from `defeaters` so "we looked and
    /// found nothing" can never be confused with "we did not look".
    analysed: bool,
}

impl Resolution {
    /// A finding is confident only when the analysis ran, the language allows
    /// it, and the file did not defeat it.
    pub(super) fn is_complete(&self) -> bool {
        self.analysed
            && self.ceiling == ResolutionCeiling::SingleFileComplete
            && self.defeaters.is_empty()
    }

    pub(super) fn to_vm_value(&self) -> VmValue {
        let defeaters: Vec<VmValue> = self
            .defeaters
            .iter()
            .map(|d| str_value(d.as_str()))
            .collect();
        build_dict([
            ("complete", VmValue::Bool(self.is_complete())),
            ("analysed", VmValue::Bool(self.analysed)),
            ("ceiling", str_value(self.ceiling.as_str())),
            ("defeaters", VmValue::list(defeaters)),
        ])
    }

    /// The reading for a file we could not analyse at all. It is never
    /// complete: no analysis ran, so nothing was ruled out.
    pub(super) fn unanalysed(language: Language) -> Self {
        Self {
            ceiling: ceiling(language),
            defeaters: Vec::new(),
            analysed: false,
        }
    }
}

/// Assess one parsed file.
pub(super) fn assess(tree: &Tree, source: &str, language: Language) -> Resolution {
    Resolution {
        ceiling: ceiling(language),
        defeaters: binding_defeaters(tree, source, language),
        analysed: true,
    }
}

/// Constructs that can supply bindings absent from the parsed source. Reads
/// demote confidence; mutations must refuse before persisting their plan.
pub(crate) fn binding_defeaters(tree: &Tree, source: &str, language: Language) -> Vec<Defeater> {
    binding_defeaters_at(tree.root_node(), source, language)
}

/// Bindings supplied outside a subtree still affect the names it uses.
pub(crate) fn binding_defeaters_at(
    node: Node<'_>,
    source: &str,
    language: Language,
) -> Vec<Defeater> {
    let root = source_root(node);
    let mut defeaters: Vec<Defeater> = Vec::new();
    let mut note = |d: Defeater| {
        if !defeaters.contains(&d) {
            defeaters.push(d);
        }
    };
    // tree-sitter recovers from syntax errors rather than refusing to parse, so
    // a broken file still yields a tree. Definitions inside the damaged region
    // may never have been collected, which turns every name they would have
    // bound into a spurious undefined-name finding. A file that does not parse
    // cleanly cannot support a confident reading.
    if root.has_error() {
        note(Defeater::SyntaxError);
    }
    walk_nodes(root, &mut |node| {
        scan_node(node, source, language, &mut note);
    });
    defeaters
}

/// A Python class body consults a namespace that its metaclass can populate.
/// Method bodies skip that namespace, while method defaults and annotations
/// are still evaluated in it. Base expressions precede namespace creation.
pub(crate) fn has_dynamic_class_lookup(node: Node<'_>, language: Language) -> bool {
    if language != Language::Python {
        return false;
    }
    let at = node.start_byte();
    let mut in_function_body = false;
    let mut outer = node.parent();
    while let Some(parent) = outer {
        let in_body = parent
            .child_by_field_name("body")
            .is_some_and(|body| body.byte_range().contains(&at));
        if matches!(parent.kind(), "function_definition" | "lambda") && in_body {
            in_function_body = true;
        }
        if parent.kind() == "class_definition" && in_body && !in_function_body {
            return true;
        }
        outer = parent.parent();
    }
    false
}

/// Names whose call means the file can create or read bindings the parse cannot
/// see. `getattr` is deliberately absent: reading an attribute off an object
/// does not introduce a module-level name.
const PYTHON_DYNAMIC_CALLS: &[&str] = &[
    "eval", "exec", "globals", "locals", "setattr", "delattr", "vars", "compile",
];

const RUBY_DYNAMIC_CALLS: &[&str] = &[
    "method_missing",
    "define_method",
    "const_missing",
    "instance_variable_set",
    "const_set",
    "class_eval",
    "instance_eval",
    "attr_accessor",
    "attr_reader",
    "attr_writer",
];

fn source_root(mut node: Node<'_>) -> Node<'_> {
    while let Some(parent) = node.parent() {
        node = parent;
    }
    node
}

fn walk_nodes(root: Node<'_>, visit: &mut impl FnMut(Node<'_>)) {
    let mut cursor = root.walk();
    loop {
        visit(cursor.node());
        if cursor.goto_first_child() {
            continue;
        }
        while !cursor.goto_next_sibling() {
            if !cursor.goto_parent() {
                return;
            }
        }
    }
}

fn scan_node(node: Node<'_>, source: &str, language: Language, note: &mut impl FnMut(Defeater)) {
    if language
        .rename_identifier_kinds()
        .is_some_and(|kinds| kinds.contains(&node.kind()))
    {
        // The index records raw spellings. Python/Rust Unicode normalization
        // and escaped identifiers can hide a same-named binding from it.
        let spelling = node_text(node, source);
        let opaque = match language {
            Language::Python => !spelling.is_ascii(),
            Language::Rust => !spelling.is_ascii() || spelling.starts_with("r#"),
            Language::JavaScript | Language::Jsx | Language::TypeScript | Language::Tsx => {
                spelling.contains('\\')
            }
            Language::Swift => spelling.starts_with('`'),
            _ => false,
        };
        if opaque {
            note(Defeater::IdentifierNormalization);
        }
    }
    match (language, node.kind()) {
        (Language::Python, "wildcard_import") => note(Defeater::WildcardImport),
        (Language::Python, "call") => {
            if let Some(name) = called_name(node, source) {
                if PYTHON_DYNAMIC_CALLS.contains(&name) {
                    note(if name == "eval" || name == "exec" || name == "compile" {
                        Defeater::Eval
                    } else {
                        Defeater::DynamicNameAccess
                    });
                }
            }
        }
        (Language::Python, "function_definition") => {
            if let Some(name) = node.child_by_field_name("name") {
                if matches!(node_text(name, source), "__getattr__" | "__getattribute__") {
                    note(Defeater::DynamicNameAccess);
                }
            }
        }
        (Language::Ruby, "call") => {
            if let Some(name) = called_name(node, source) {
                if RUBY_DYNAMIC_CALLS.contains(&name) {
                    note(Defeater::DynamicMethodDefinition);
                }
            }
        }
        (Language::Ruby, "method") => {
            if let Some(name) = node.child_by_field_name("name") {
                if matches!(node_text(name, source), "method_missing" | "const_missing") {
                    note(Defeater::DynamicMethodDefinition);
                }
            }
        }
        (
            Language::JavaScript | Language::Jsx | Language::TypeScript | Language::Tsx,
            "call_expression",
        ) => {
            if let Some(name) = called_name(node, source) {
                if name == "eval" {
                    note(Defeater::Eval);
                }
            }
        }
        (
            Language::JavaScript | Language::Jsx | Language::TypeScript | Language::Tsx,
            "with_statement",
        ) => note(Defeater::WithScope),
        (Language::Rust, "use_wildcard") => note(Defeater::WildcardImport),
        (Language::Rust, "macro_invocation" | "attribute_item" | "inner_attribute_item")
            if rust_expansion_may_bind(node, source) =>
        {
            note(Defeater::DynamicNameAccess);
        }
        (Language::Swift, "macro_invocation" | "attribute") => note(Defeater::DynamicNameAccess),
        _ => {}
    }
}

/// `std` macros that expand to an expression or statement and declare no item.
/// `include!` and `thread_local!` are absent because they do declare items.
const RUST_ITEMLESS_STD_MACROS: &[&str] = &[
    "assert",
    "assert_eq",
    "assert_ne",
    "cfg",
    "column",
    "compile_error",
    "concat",
    "dbg",
    "debug_assert",
    "debug_assert_eq",
    "debug_assert_ne",
    "env",
    "eprint",
    "eprintln",
    "file",
    "format",
    "format_args",
    "include_bytes",
    "include_str",
    "line",
    "matches",
    "module_path",
    "option_env",
    "panic",
    "print",
    "println",
    "stringify",
    "todo",
    "unimplemented",
    "unreachable",
    "vec",
    "write",
    "writeln",
];

/// Built-in attributes that preserve the decorated declaration. Expansion
/// wrappers (`derive` and `cfg_attr`) inspect their arguments separately.
/// `path` is absent because it changes which file a module loads.
const RUST_INERT_ATTRIBUTES: &[&str] = &[
    "allow",
    "automatically_derived",
    "cold",
    "deny",
    "deprecated",
    "doc",
    "expect",
    "export_name",
    "forbid",
    "ignore",
    "inline",
    "link_section",
    "macro_export",
    "must_use",
    "no_mangle",
    "non_exhaustive",
    "repr",
    "should_panic",
    "track_caller",
    "used",
    "warn",
];

#[derive(Clone, Copy, PartialEq, Eq)]
enum RustAttributeEffect {
    Inert,
    Conditional,
    Opaque,
}

/// Constructs that can supply a binding to a name declared inside `scope` and
/// invisible outside it. Only code within the scope, or an expansion that
/// rewrites an item enclosing it, can shadow such a binding.
pub(crate) fn binding_defeaters_within(
    scope: Node<'_>,
    source: &str,
    language: Language,
) -> Vec<Defeater> {
    let mut defeaters: Vec<Defeater> = Vec::new();
    let mut note = |d: Defeater| {
        if !defeaters.contains(&d) {
            defeaters.push(d);
        }
    };
    if scope.has_error() {
        note(Defeater::SyntaxError);
    }
    walk_nodes(scope, &mut |node| {
        scan_node(node, source, language, &mut note);
    });
    if language == Language::Rust
        && !rust_enclosing_attributes_all(scope, source, |effect| {
            effect != RustAttributeEffect::Opaque
        })
    {
        note(Defeater::DynamicNameAccess);
    }
    defeaters
}

/// A declaration behind an unknown configuration cannot authorize a write.
pub(crate) fn rust_declaration_is_unconditional(node: Node<'_>, source: &str) -> bool {
    rust_enclosing_attributes_all(node, source, |effect| effect == RustAttributeEffect::Inert)
}

/// Whether every attribute on `node` and on each item enclosing it passes `allowed`.
fn rust_enclosing_attributes_all(
    node: Node<'_>,
    source: &str,
    allowed: impl Fn(RustAttributeEffect) -> bool,
) -> bool {
    let mut up = Some(node);
    while let Some(scope) = up {
        let mut previous = scope.prev_named_sibling();
        while let Some(prefix) = previous {
            match prefix.kind() {
                "attribute_item" if !allowed(rust_attribute_effect_at(prefix, source)) => {
                    return false;
                }
                "attribute_item" | "line_comment" | "block_comment" => {}
                _ => break,
            }
            previous = prefix.prev_named_sibling();
        }
        for container in [Some(scope), scope.child_by_field_name("body")]
            .into_iter()
            .flatten()
        {
            let mut cursor = container.walk();
            if container.named_children(&mut cursor).any(|child| {
                child.kind() == "inner_attribute_item"
                    && !allowed(rust_attribute_effect_at(child, source))
            }) {
                return false;
            }
        }
        up = scope.parent();
    }
    true
}

/// Whether a Rust macro invocation or attribute can expand into bindings the
/// parse cannot see.
///
/// Only an invocation in item or statement position can declare an item or a
/// call-site `let` that sibling code sees; one nested in an expression expands
/// to that expression. Opaque attributes may rewrite their item; conditional
/// attributes separately constrain whether a declaration is present.
///
/// Known gap: an expression-position user macro, possibly from a dependency,
/// can still name a binding at its call site; body-local renames refuse it.
fn rust_expansion_may_bind(node: Node<'_>, source: &str) -> bool {
    match node.kind() {
        "macro_invocation" => {
            let in_statement_position = node.parent().is_some_and(|parent| {
                matches!(
                    parent.kind(),
                    "source_file" | "declaration_list" | "block" | "expression_statement"
                )
            });
            in_statement_position && !rust_itemless_std_macro(node, source)
        }
        "attribute_item" | "inner_attribute_item" => {
            rust_attribute_effect_at(node, source) == RustAttributeEffect::Opaque
        }
        _ => false,
    }
}

fn rust_attribute_effect_at(node: Node<'_>, source: &str) -> RustAttributeEffect {
    let Some(attribute) = node.named_child(0) else {
        return RustAttributeEffect::Opaque;
    };
    let Some(path) = attribute.named_child(0) else {
        return RustAttributeEffect::Opaque;
    };
    rust_attribute_effect(
        node,
        source,
        node_text(path, source),
        attribute.child_by_field_name("arguments"),
    )
}

fn rust_attribute_effect(
    site: Node<'_>,
    source: &str,
    path: &str,
    arguments: Option<Node<'_>>,
) -> RustAttributeEffect {
    // An imported procedural attribute can replace even `derive` or `test`.
    // Prove the binding before trusting either its spelling or arguments.
    let (prefix, name) = path.split_once("::").unwrap_or(("", path));
    if !rust_macro_path_is_unshadowed(site, source, prefix, name) {
        return RustAttributeEffect::Opaque;
    }
    match path {
        "cfg" | "test" => RustAttributeEffect::Conditional,
        "derive" => {
            // Custom derives may append arbitrary items, including a local
            // type that shadows the receiver owner. Only unshadowed builtins
            // establish the narrower trait-implementation-only guarantee.
            const BUILTIN_DERIVES: &[&str] = &[
                "Clone",
                "Copy",
                "Debug",
                "Default",
                "Eq",
                "Hash",
                "Ord",
                "PartialEq",
                "PartialOrd",
            ];
            let Some(parts) = rust_attribute_arguments(arguments) else {
                return RustAttributeEffect::Opaque;
            };
            let mut expect_name = true;
            for part in parts {
                if expect_name && part.kind() == "identifier" {
                    let name = node_text(part, source);
                    if !BUILTIN_DERIVES.contains(&name)
                        || !rust_macro_path_is_unshadowed(site, source, "", name)
                    {
                        return RustAttributeEffect::Opaque;
                    }
                    expect_name = false;
                } else if !expect_name && part.kind() == "," {
                    expect_name = true;
                } else {
                    return RustAttributeEffect::Opaque;
                }
            }
            RustAttributeEffect::Inert
        }
        "cfg_attr" => {
            let Some(parts) = rust_attribute_arguments(arguments) else {
                return RustAttributeEffect::Opaque;
            };
            let mut groups = parts.split(|part| part.kind() == ",");
            // Do not guess a project's active configuration. Every possible
            // generated attribute must preserve the same binding guarantee.
            if groups.next().is_none_or(|predicate| predicate.is_empty()) {
                return RustAttributeEffect::Opaque;
            }
            let mut seen = false;
            let mut effect = RustAttributeEffect::Inert;
            for group in groups {
                if group.is_empty() {
                    continue;
                }
                seen = true;
                let path = node_text(group[0], source);
                let arguments = match group.get(1) {
                    Some(node) if node.kind() == "token_tree" && group.len() == 2 => Some(*node),
                    _ if matches!(path, "derive" | "cfg_attr") => {
                        return RustAttributeEffect::Opaque;
                    }
                    _ => None,
                };
                if group[0].kind() != "identifier"
                    || group.get(1).is_some_and(|node| node.kind() == "::")
                {
                    return RustAttributeEffect::Opaque;
                }
                match rust_attribute_effect(site, source, path, arguments) {
                    RustAttributeEffect::Opaque => return RustAttributeEffect::Opaque,
                    RustAttributeEffect::Conditional => effect = RustAttributeEffect::Conditional,
                    RustAttributeEffect::Inert => {}
                }
            }
            if seen {
                effect
            } else {
                RustAttributeEffect::Opaque
            }
        }
        _ => {
            if RUST_INERT_ATTRIBUTES.contains(&path)
                || path.split_once("::").is_some_and(|(prefix, _)| {
                    matches!(prefix, "clippy" | "rustfmt" | "diagnostic")
                })
            {
                RustAttributeEffect::Inert
            } else {
                RustAttributeEffect::Opaque
            }
        }
    }
}

fn rust_attribute_arguments(arguments: Option<Node<'_>>) -> Option<Vec<Node<'_>>> {
    let arguments = arguments?;
    let count = arguments.child_count();
    if count < 2 || arguments.child(0)?.kind() != "(" || arguments.child(count - 1)?.kind() != ")" {
        return None;
    }
    Some(
        (1..count - 1)
            .filter_map(|index| arguments.child(index))
            .filter(|node| !matches!(node.kind(), "line_comment" | "block_comment"))
            .collect(),
    )
}

/// Whether a Rust `macro_invocation` names an itemless `std` macro, bare or
/// through `std`, `core` or `alloc`.
pub(crate) fn rust_itemless_std_macro(invocation: Node<'_>, source: &str) -> bool {
    let Some(path) = invocation.child_by_field_name("macro") else {
        return false;
    };
    let path = node_text(path, source);
    let (prefix, name) = path.rsplit_once("::").unwrap_or(("", path));
    let prefix = prefix.trim_start_matches("::");
    matches!(prefix, "" | "std" | "core" | "alloc")
        && RUST_ITEMLESS_STD_MACROS.contains(&name)
        && rust_macro_path_is_unshadowed(invocation, source, prefix, name)
}

fn rust_macro_path_is_unshadowed(
    invocation: Node<'_>,
    source: &str,
    prefix: &str,
    name: &str,
) -> bool {
    let root = source_root(invocation);
    let binding = if prefix.is_empty() { name } else { prefix };
    let mut shadowed = false;
    // Without macro resolution, a competing binding anywhere in the file
    // defeats the standard-library spelling proof.
    walk_nodes(root, &mut |node| match node.kind() {
        "macro_definition" if prefix.is_empty() => {
            shadowed |= node
                .child_by_field_name("name")
                .is_some_and(|name| node_text(name, source) == binding);
        }
        "mod_item" | "extern_crate_declaration" if !prefix.is_empty() => {
            shadowed |= ["name", "alias"].into_iter().any(|field| {
                node.child_by_field_name(field)
                    .is_some_and(|name| node_text(name, source) == binding)
            });
        }
        "use_declaration" => {
            let mut bindings = HashSet::new();
            if let Some(argument) = node.child_by_field_name("argument") {
                bind_rust_use_tree(argument, source, &mut bindings);
            }
            shadowed |= bindings.contains(binding);
            walk_nodes(node, &mut |part| shadowed |= part.kind() == "use_wildcard");
        }
        "attribute_item" | "inner_attribute_item" if prefix.is_empty() => {
            shadowed |= node
                .named_child(0)
                .and_then(|attribute| attribute.named_child(0))
                .is_some_and(|path| node_text(path, source) == "macro_use");
        }
        _ => {}
    });
    !shadowed
}

/// The bare callee name of a call node, or `None` when the callee is anything
/// other than a plain identifier. A method call such as `obj.setattr(...)` is
/// not the builtin and must not be treated as one.
fn called_name<'a>(node: Node<'_>, source: &'a str) -> Option<&'a str> {
    let mut callee = node
        .child_by_field_name("function")
        .or_else(|| node.child_by_field_name("method"))?;
    while callee.kind() == "parenthesized_expression" {
        let mut cursor = callee.walk();
        callee = callee
            .named_children(&mut cursor)
            .find(|child| !child.is_extra())?;
    }
    match callee.kind() {
        "identifier" => Some(node_text(callee, source)),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::parse::parse_source;

    fn assess_source(source: &str, language: Language) -> Resolution {
        let tree = parse_source(source, language).expect("parses");
        assess(&tree, source, language)
    }

    fn defeater_names(resolution: &Resolution) -> Vec<&'static str> {
        resolution.defeaters.iter().map(|d| d.as_str()).collect()
    }

    #[test]
    fn python_dynamic_name_calls_defeat_resolution() {
        let resolution = assess_source("def f(t):\n    setattr(t, 'a', 1)\n", Language::Python);
        assert!(!resolution.is_complete());
        assert_eq!(defeater_names(&resolution), vec!["dynamic_name_access"]);
    }

    #[test]
    fn python_module_getattr_defeats_resolution() {
        let resolution = assess_source(
            "def __getattr__(name):\n    return name\n",
            Language::Python,
        );
        assert!(!resolution.is_complete());
        assert_eq!(defeater_names(&resolution), vec!["dynamic_name_access"]);
    }

    #[test]
    fn python_method_named_like_a_builtin_is_not_a_defeater() {
        // `obj.setattr(...)` is a method call, not the builtin.
        let resolution = assess_source("def f(o):\n    o.setattr('a', 1)\n", Language::Python);
        assert!(resolution.is_complete());
    }

    #[test]
    fn ruby_is_never_complete_even_when_plainly_written() {
        let resolution = assess_source("def total(a)\n  a + 1\nend\n", Language::Ruby);
        assert!(!resolution.is_complete());
        assert_eq!(resolution.ceiling.as_str(), "runtime_resolved");
        assert!(defeater_names(&resolution).is_empty());
    }

    #[test]
    fn ruby_dynamic_definition_is_named_as_a_defeater() {
        let resolution = assess_source(
            "class A\n  def method_missing(n)\n    n\n  end\nend\n",
            Language::Ruby,
        );
        assert!(!resolution.is_complete());
        assert_eq!(
            defeater_names(&resolution),
            vec!["dynamic_method_definition"]
        );
    }

    #[test]
    fn go_is_package_scoped_because_sibling_files_contribute_names() {
        let resolution = assess_source("package main\n\nfunc f() int { return 1 }\n", Language::Go);
        assert!(!resolution.is_complete());
        assert_eq!(resolution.ceiling.as_str(), "package_scoped");
    }

    #[test]
    fn javascript_eval_defeats_resolution() {
        let resolution = assess_source("function f(s) { return eval(s); }\n", Language::JavaScript);
        assert!(!resolution.is_complete());
        assert_eq!(defeater_names(&resolution), vec!["eval"]);
    }

    #[test]
    fn parenthesized_eval_still_defeats_resolution() {
        for source in [
            "function f(s) { return ((eval))(s); }\n",
            "function f(s) { return ((/* comment */ eval))(s); }\n",
        ] {
            let resolution = assess_source(source, Language::JavaScript);
            assert!(!resolution.is_complete());
            assert_eq!(defeater_names(&resolution), vec!["eval"]);
        }
    }

    #[test]
    fn identifier_spellings_that_require_normalization_are_not_complete() {
        for (language, source) in [
            (
                Language::Python,
                "def probe(ℝeading):\n    return Reading()\n",
            ),
            (
                Language::Rust,
                "fn probe() { let r#Reading = || 7; Reading(); }\n",
            ),
            (
                Language::JavaScript,
                "function probe() { const Read\\u0069ng = () => 7; return Reading(); }\n",
            ),
        ] {
            let resolution = assess_source(source, language);
            assert!(!resolution.is_complete());
            assert_eq!(
                defeater_names(&resolution),
                vec!["identifier_normalization"]
            );
        }
    }

    #[test]
    fn rust_macro_expansion_is_not_a_complete_binding_analysis() {
        for source in [
            "fn f() { binding_macro::bind!(); Reading() }\n",
            "#[binding_macro::bind]\nfn f() { Reading() }\n",
            "fn f() { macro_rules! assert { () => { let Reading = 1; }; } assert!(); Reading }\n",
            "use other::assert; fn f() { assert!(); Reading() }\n",
            "use other::{bind as assert}; fn f() { assert!(); Reading() }\n",
            "use other::std::{self}; fn f() { std::assert!(); Reading() }\n",
            "mod std {} fn f() { std::assert!(); Reading() }\n",
            "#[macro_use] extern crate other; fn f() { assert!(); Reading() }\n",
            "#[derive(binding_macro::Install)] struct Marker; fn f() { Reading() }\n",
            "use binding_macro::Debug; #[derive(Debug)] struct Marker; fn f() { Reading() }\n",
            "#[derive(Clone::Debug)] struct Marker; fn f() { Reading() }\n",
            "#[allow::install] fn f() { Reading() }\n",
            "#[cfg_attr(all(), binding_macro::install)] fn f() { Reading() }\n",
            "#[cfg_attr(all(), derive(binding_macro::Install))] struct Marker; fn f() { Reading() }\n",
            "#[cfg_attr(all(), cfg_attr(all(), binding_macro::install))] fn f() { Reading() }\n",
            "use binding_macro::install as test; #[test] fn f() { Reading() }\n",
            "use binding_macro::install as derive; #[derive(Debug)] struct Marker; fn f() { Reading() }\n",
            "use binding_macro::install as derive; #[cfg_attr(all(), derive(Debug))] struct Marker; fn f() { Reading() }\n",
        ] {
            let resolution = assess_source(source, Language::Rust);
            assert!(!resolution.is_complete());
            assert_eq!(defeater_names(&resolution), vec!["dynamic_name_access"]);
        }
    }

    #[test]
    fn itemless_rust_macros_and_inert_attributes_keep_resolution_complete() {
        let source = "#![allow(dead_code)]\nuse std::{io::{self}, fmt as fmt_alias};\nuse std::assert_eq as check;\n#[derive(Debug, Clone)]\npub struct Reading;\n\
            #[cfg_attr(all(), allow(dead_code), cfg_attr(all(), derive(Debug, Clone)))]\nstruct Marker;\n\
            #[cfg(test)]\nmod tests {\n    #[test]\n    fn t() { assert_eq!(1, 1); println!(\"{}\", 2); }\n}\n\
            fn f() -> String { let v = vec![1]; std::format!(\"{v:?}\") }\n\
            fn g() -> usize { my_crate::count!(a, b) + 1 }\n";
        let resolution = assess_source(source, Language::Rust);
        assert!(
            defeater_names(&resolution).is_empty(),
            "{:?}",
            defeater_names(&resolution)
        );
        for source in [
            "fn f() { my_crate::bind!(); }\n",
            "fn f() { other::println!(); }\n",
            "include!(\"generated.rs\");\n",
            "#[tokio::main]\nasync fn main() {}\n",
        ] {
            assert_eq!(
                defeater_names(&assess_source(source, Language::Rust)),
                vec!["dynamic_name_access"],
                "{source}"
            );
        }
    }

    #[test]
    fn swift_expansion_is_not_a_complete_binding_analysis() {
        for source in [
            "func probe() { #declareReading(); Reading() }\n",
            "@GenerateBindings func probe() { Reading() }\n",
        ] {
            let resolution = assess_source(source, Language::Swift);
            assert_eq!(defeater_names(&resolution), vec!["dynamic_name_access"]);
        }
    }

    #[test]
    fn plain_javascript_file_resolves_completely() {
        let resolution = assess_source(
            "export function f(a) { return a + 1; }\n",
            Language::JavaScript,
        );
        assert!(resolution.is_complete());
    }

    #[test]
    fn defeaters_are_reported_once_each() {
        let resolution = assess_source(
            "from a import *\nfrom b import *\n\n\ndef f(t):\n    setattr(t, 'x', 1)\n    setattr(t, 'y', 2)\n",
            Language::Python,
        );
        assert_eq!(
            defeater_names(&resolution),
            vec!["wildcard_import", "dynamic_name_access"]
        );
    }

    #[test]
    fn unanalysed_file_is_never_complete() {
        assert!(!Resolution::unanalysed(Language::Python).is_complete());
    }

    #[test]
    fn a_file_that_does_not_parse_cleanly_is_not_complete() {
        // tree-sitter recovers rather than failing, so this still produces a
        // tree. Definitions in the damaged region were never collected, so the
        // reading must not claim confidence.
        let resolution = assess_source("def f(:\n    return zzz\n", Language::Python);
        assert!(!resolution.is_complete());
        assert!(defeater_names(&resolution).contains(&"syntax_error"));
    }
}