Skip to content

bug: Incorrect parsing of attributes #244

Open
@wetneb

Description

@wetneb

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")]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions