Skip to content

Rust parser with set ranges can't correctly capture query after edit #3962

@casouri

Description

@casouri

Problem

I observed a strange phenomenon in Emacs and can reproduce it in C. Given the following rust source file:

fn foo() -> i32 {
    4
}

fn main() {
    println!("{:?}", foo());
}

We create a rust parser and set its ranges so it only contains ("{:?}", foo()), ie, the part after the println! macro but before the semicolon.

At this point, querying the parser works fine.

Now, insert a whitespace at the end of the line, ie, after the semicolon. Now the code looks like this (<space> represents a whitespace):

fn foo() -> i32 {
    4
}

fn main() {
    println!("{:?}", foo());<space>
}

Now the parser doesn't return any matches for any query. Note that we didn't change the ranges for the parser. All that we did is to append a whitespace at the end of the line, and re-parse.

Steps to reproduce

I created a reproduce repo here: https://github.com/casouri/tree-sitter-rust-bug-repo

As always, running ./run.sh should download latest tree-sitter and tree-sitter-rust, and compile the reproduce recipe program. If everything works right, the console output should look like this:

First parse root node:
(source_file (expression_statement (tuple_expression (string_literal (string_content)) (call_expression function: (identifier) arguments: (arguments))) (MISSING ";")))
First parse query: has match
Second parse root node:
(source_file (expression_statement (tuple_expression (string_literal (string_content)) (call_expression function: (identifier) arguments: (arguments))) (MISSING ";")))
Second parse query: no match

Note that the node string from two parses are the same. After the first parse, we can find matches for the query; after the second parse, we no long get matches for the same query.

The query is a simple query: (call_expression) @cap

Expected behavior

The console should print Second parse query: has match instead of Second parse query: no match

Tree-sitter version (tree-sitter --version)

tree-sitter master (325b320)

Operating system/version

macOS 14.6.1 (23G93)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions