Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: Incorrect parsing of attributes #244

Open
2 tasks done
wetneb opened this issue Nov 14, 2024 · 0 comments · May be fixed by #245
Open
2 tasks done

bug: Incorrect parsing of attributes #244

wetneb opened this issue Nov 14, 2024 · 0 comments · May be fixed by #245
Labels

Comments

@wetneb
Copy link

wetneb commented Nov 14, 2024

Did you check existing issues?

  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues of tree-sitter-rust

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

tree-sitter 0.23.0

Describe the bug

The grammar does not attach attributes to the syntactic elements they annotate. Instead, attributes are treated as siblings of the elements they annotate. For instance, when parsing the following example

mod tests {
    #[cfg(feature = "tracing")]
    pub fn execute() {}
}

Both #[cfg(feature = "tracing")] and pub fn execute() {} are direct children of the module body, whereas I would expect them to be gathered in a node representing the function declaration.

Another symptom of this problem is that the grammar successfully parses the following code:

mod tests {
    pub fn execute() {}
    #[cfg(feature = "tracing")]
}

This code is incorrect because #[cfg(feature = "tracing")] is an outer attribute, which needs to be followed by the element it is applied to. This is indeed rejected by rustc:

error: expected item after attributes
 --> test.rs:3:5
  |
3 |     #[cfg(feature = "tracing")]
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error

Steps To Reproduce/Bad Parse Tree

Parsing the first sample above:

(source_file [0, 0] - [4, 0]
  (mod_item [0, 0] - [3, 1]
    name: (identifier [0, 4] - [0, 9])
    body: (declaration_list [0, 10] - [3, 1]
      (attribute_item [1, 4] - [1, 31]
        (attribute [1, 6] - [1, 30]
          (identifier [1, 6] - [1, 9])
          arguments: (token_tree [1, 9] - [1, 30]
            (identifier [1, 10] - [1, 17])
            (string_literal [1, 20] - [1, 29]
              (string_content [1, 21] - [1, 28])))))
      (function_item [2, 4] - [2, 23]
        (visibility_modifier [2, 4] - [2, 7])
        name: (identifier [2, 11] - [2, 18])
        parameters: (parameters [2, 18] - [2, 20])
        body: (block [2, 21] - [2, 23])))))

Expected Behavior/Parse Tree

Correctly bundling outer attributes with the syntactic element they apply to:

(source_file [0, 0] - [4, 0]
  (mod_item [0, 0] - [3, 1]
    name: (identifier [0, 4] - [0, 9])
    body: (declaration_list [0, 10] - [3, 1]
      (function_item [1, 4] - [2, 23]
        (attribute_item [1, 4] - [1, 31]
          (attribute [1, 6] - [1, 30]
            (identifier [1, 6] - [1, 9])
            arguments: (token_tree [1, 9] - [1, 30]
              (identifier [1, 10] - [1, 17])
              (string_literal [1, 20] - [1, 29]
                (string_content [1, 21] - [1, 28])))))
        (visibility_modifier [2, 4] - [2, 7])
        name: (identifier [2, 11] - [2, 18])
        parameters: (parameters [2, 18] - [2, 20])
        body: (block [2, 21] - [2, 23])))))

Repro

mod tests {
    pub fn execute() {}
    #[cfg(feature = "tracing")]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant