harn-hostlib 0.10.171

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
//! Fail-closed binding proof for rename targets, using the existing module model.
//!
//! The symbol graph identifies the seed, not every lexical binding. A spelling
//! match is writable only in a known reference position with an unambiguous
//! file-level binding. Unknown syntax, local bindings and receiver members stop
//! the whole plan before persistence.

use tree_sitter::{Node, Tree};

use crate::ast::name_resolution::{
    binding_defeaters, has_dynamic_class_lookup, rust_itemless_std_macro, Defeater,
};
use crate::ast::Language;

use super::move_symbol::{Family, Lang, Shape};
use super::refactor_core::IdentifierSpan;
use super::state::IndexState;
use super::symbol_graph::{Node as SymbolNode, NodeKind};

pub(super) struct Seed<'a> {
    pub state: &'a IndexState,
    pub node: &'a SymbolNode,
    pub name: &'a str,
}

pub(super) fn check(
    seed: Seed<'_>,
    path: &str,
    language: Language,
    source: &str,
    tree: &Tree,
    targets: &[IdentifierSpan],
) -> Result<(), String> {
    let Seed {
        state,
        node: seed_node,
        name,
    } = seed;
    let seed_path = seed_node.path.as_ref();
    let seed_line = seed_node.line;
    let seed_kind = seed_node.kind;
    let seed_language = Language::detect(std::path::Path::new(seed_path), None);
    if seed_language.is_none_or(|seed_language| seed_language != language) {
        return Err("cross-language bindings require a binding-aware LSP rename".into());
    }
    if path == seed_path
        && !targets.iter().any(|span| {
            tree.root_node()
                .named_descendant_for_byte_range(span.start_byte, span.end_byte)
                .is_some_and(|node| declaration(node, seed_line, seed_kind))
        })
    {
        return Err("the indexed seed is no longer a supported top-level declaration".into());
    }
    if !matches!(seed_kind, NodeKind::Function | NodeKind::Type) {
        return Err(
            "receiver and non-declaration bindings require a binding-aware LSP rename".into(),
        );
    }
    let defeaters = binding_defeaters(tree, source, language);
    if !defeaters.is_empty() {
        return Err(format!(
            "AST binding proof for {path} is incomplete ({})",
            defeaters
                .into_iter()
                .map(Defeater::as_str)
                .collect::<Vec<_>>()
                .join(", ")
        ));
    }
    let mut lang = Family::of(language).map(|family| Lang::new(family, state, &[]));
    let module = lang.as_mut().and_then(|lang| lang.module_of(seed_path));
    let model = lang.as_mut().map(|lang| lang.model(path, tree, source));
    let mut bound = path == seed_path;
    if let Some(model) = &model {
        for binding in &model.bindings {
            if binding.shape == Shape::Glob {
                return Err("wildcard imports do not establish a unique lexical binding".into());
            }
            if binding.local == name || binding.imported == name {
                if binding.shape != Shape::Named
                    || binding.imported != name
                    || Some(&binding.target) != module.as_ref()
                {
                    return Err("a same-spelled import does not resolve to the seed module".into());
                }
                bound |= binding.local == name && binding.top_level;
            }
        }
    }
    for span in targets {
        let node = tree
            .root_node()
            .named_descendant_for_byte_range(span.start_byte, span.end_byte)
            .ok_or_else(|| "identifier has no syntax node".to_string())?;
        if has_dynamic_class_lookup(node, language) {
            return Err(format!(
                "Python class namespace lookup at {path}:{}:{} is unresolved",
                span.start_row + 1,
                span.start_col + 1
            ));
        }
        if let Some(model) = &model {
            if model.in_import(span.start_byte) {
                let proven = model.bindings.iter().any(|binding| {
                    binding.statement.contains(&span.start_byte)
                        && binding.shape == Shape::Named
                        && binding.imported == name
                        && Some(&binding.target) == module.as_ref()
                });
                if proven && import_name(node) {
                    continue;
                }
                return Err("import occurrence has no resolved seed binding".into());
            }
        }
        if path == seed_path && declaration(node, seed_line, seed_kind) {
            continue;
        }
        if !bound || !reference(node, language, source, name) {
            return Err(format!(
                "unresolved binding at {path}:{}:{} ({})",
                span.start_row + 1,
                span.start_col + 1,
                node.parent().map_or("root", |parent| parent.kind())
            ));
        }
    }
    Ok(())
}

fn import_name(node: Node<'_>) -> bool {
    let at = node.start_byte();
    let mut outer = node.parent();
    while let Some(parent) = outer {
        let module_field = match parent.kind() {
            "import_from_statement" => Some("module_name"),
            "scoped_identifier" | "scoped_type_identifier" | "scoped_use_list" => Some("path"),
            _ => None,
        };
        if module_field
            .and_then(|field| parent.child_by_field_name(field))
            .is_some_and(|module| module.byte_range().contains(&at))
        {
            return false;
        }
        outer = parent.parent();
    }
    true
}

fn field(node: Node<'_>, parent: Node<'_>, name: &str) -> bool {
    parent
        .child_by_field_name(name)
        .is_some_and(|child| child.id() == node.id())
}

fn declaration(node: Node<'_>, line: u32, kind: NodeKind) -> bool {
    let Some(parent) = node.parent() else {
        return false;
    };
    if node.start_position().row + 1 != line as usize || !field(node, parent, "name") {
        return false;
    }
    let correct_kind = match kind {
        NodeKind::Function => matches!(
            parent.kind(),
            "function_item" | "function_definition" | "function_declaration"
        ),
        NodeKind::Type => matches!(
            parent.kind(),
            "struct_item"
                | "enum_item"
                | "trait_item"
                | "type_item"
                | "class_definition"
                | "class_declaration"
                | "interface_declaration"
                | "type_alias_declaration"
                | "type_spec"
                | "struct_declaration"
        ),
        _ => false,
    };
    if !correct_kind {
        return false;
    }
    let mut outer = parent.parent();
    while let Some(ancestor) = outer {
        if !matches!(
            ancestor.kind(),
            "source_file"
                | "program"
                | "module"
                | "export_statement"
                | "decorated_definition"
                | "type_declaration"
        ) {
            return false;
        }
        outer = ancestor.parent();
    }
    true
}

/// Only syntax positions with no binding or receiver semantics are accepted.
/// Keep this allowlist conservative: a new grammar production must be proved
/// before it can become a mutation target.
fn reference(node: Node<'_>, language: Language, source: &str, name: &str) -> bool {
    let mut outer = node.parent();
    while let Some(ancestor) = outer {
        if matches!(
            ancestor.kind(),
            "mod_item" | "internal_module" | "module_declaration"
        ) {
            return false;
        }
        if declares_type_parameter(ancestor, source, name) {
            return false;
        }
        outer = ancestor.parent();
    }
    let Some(parent) = node.parent() else {
        return false;
    };
    if parent.kind() == "token_tree" {
        return macro_argument_reference(node, source);
    }
    if type_position(node, parent, language) {
        return true;
    }
    if matches!(
        node.kind(),
        "field_identifier"
            | "property_identifier"
            | "shorthand_property_identifier"
            | "shorthand_property_identifier_pattern"
            | "shorthand_field_identifier"
            | "package_identifier"
    ) {
        return false;
    }
    match parent.kind() {
        "call_expression" => {
            field(node, parent, "function")
                || field(node, parent, "constructor")
                || language == Language::Swift
        }
        "call" => field(node, parent, "function"),
        "new_expression" => field(node, parent, "constructor"),
        "scoped_identifier" | "scoped_type_identifier" => {
            // `Widget::new` reads Widget; `other::Widget` has another owner.
            field(node, parent, "path") && node.kind() != "field_identifier"
        }
        "let_declaration" | "variable_declarator" | "const_spec" | "var_spec" => {
            field(node, parent, "value") || field(node, parent, "type")
        }
        "assignment" | "assignment_expression" => field(node, parent, "right"),
        "field_initializer" | "keyword_argument" => field(node, parent, "value"),
        "parameter"
        | "required_parameter"
        | "optional_parameter"
        | "typed_parameter"
        | "typed_default_parameter"
        | "default_parameter" => field(node, parent, "type") || field(node, parent, "value"),
        "field_expression" => field(node, parent, "value"),
        "attribute" => field(node, parent, "object"),
        "member_expression" => field(node, parent, "object"),
        "struct_expression" | "composite_literal" => {
            field(node, parent, "name") || field(node, parent, "type")
        }
        "field_declaration"
        | "struct_field"
        | "property_signature"
        | "property_declaration"
        | "type_item"
        | "type_alias_declaration" => field(node, parent, "type") || field(node, parent, "value"),
        "impl_item" | "impl_block" => {
            field(node, parent, "type")
                || field(node, parent, "trait")
                || field(node, parent, "type_name")
        }
        "arguments"
        | "argument_list"
        | "value_arguments"
        | "return_expression"
        | "return_statement"
        | "expression_statement"
        | "binary_expression"
        | "binary_operator"
        | "boolean_operator"
        | "comparison_operator"
        | "unary_expression"
        | "unary_operator"
        | "await_expression"
        | "try_expression"
        | "parenthesized_expression"
        | "tuple_expression"
        | "list"
        | "list_expression"
        | "array"
        | "array_expression"
        | "type_annotation"
        | "type_arguments"
        | "generic_type"
        | "reference_type"
        | "pointer_type"
        | "nullable_type"
        | "optional_type"
        | "user_type"
        | "return_type" => true,
        "function_item" | "function_definition" | "function_declaration" => {
            field(node, parent, "return_type")
        }
        _ => false,
    }
}

/// A generic parameter of `scope` spelled `name`, which shadows the seed for
/// everything inside that item.
fn declares_type_parameter(scope: Node<'_>, source: &str, name: &str) -> bool {
    let Some(parameters) = scope.child_by_field_name("type_parameters") else {
        return false;
    };
    let mut cursor = parameters.walk();
    let declared = parameters.named_children(&mut cursor).any(|parameter| {
        let declared = match parameter.kind() {
            "type_identifier" | "identifier" => Some(parameter),
            "constrained_type_parameter" => parameter.child_by_field_name("left"),
            "type_parameter" | "optional_type_parameter" => parameter
                .child_by_field_name("name")
                .or_else(|| parameter.named_child(0)),
            _ => None,
        };
        declared.is_some_and(|declared| source.get(declared.byte_range()) == Some(name))
    });
    declared
}

/// A type name used in type syntax. Type syntax cannot bind a name except in a
/// declaring slot (an item's name, a generic parameter, `infer`), and generic
/// shadowing is ruled out by [`declares_type_parameter`] first.
fn type_position(node: Node<'_>, parent: Node<'_>, language: Language) -> bool {
    match language {
        Language::Rust | Language::TypeScript | Language::Tsx => {
            node.kind() == "type_identifier"
                && !field(node, parent, "name")
                && !matches!(
                    parent.kind(),
                    "scoped_type_identifier"
                        | "type_parameters"
                        | "constrained_type_parameter"
                        | "type_parameter"
                        | "optional_type_parameter"
                        | "infer_type"
                        | "mapped_type_clause"
                )
        }
        // An annotation is evaluated as an expression; a plain name or the
        // head of a dotted name in it reads the module binding.
        Language::Python => {
            if parent.kind() == "attribute" && !field(node, parent, "object") {
                return false;
            }
            let mut outer = Some(parent);
            while let Some(ancestor) = outer {
                match ancestor.kind() {
                    "type" => return true,
                    "function_definition" | "class_definition" | "lambda" | "block" | "module" => {
                        return false
                    }
                    _ => outer = ancestor.parent(),
                }
            }
            false
        }
        _ => false,
    }
}

/// An identifier token inside a macro's arguments. Only an itemless `std`
/// macro is known to treat its arguments as expressions, where a bare name or
/// a path head reads the file binding. A token after `.` is a member, after
/// `::` a path tail with its own owner, and before a single `=` a named
/// format argument.
fn macro_argument_reference(node: Node<'_>, source: &str) -> bool {
    let mut invocation = node.parent();
    while let Some(ancestor) = invocation {
        if ancestor.kind() == "macro_invocation" {
            break;
        }
        if ancestor.kind() != "token_tree" {
            return false;
        }
        invocation = ancestor.parent();
    }
    let Some(invocation) = invocation else {
        return false;
    };
    if !rust_itemless_std_macro(invocation, source) {
        return false;
    }
    let before = node
        .prev_sibling()
        .and_then(|token| source.get(token.byte_range()));
    let after = node
        .next_sibling()
        .and_then(|token| source.get(token.byte_range()));
    !matches!(before, Some("." | "::" | "$")) && after != Some("=")
}