Skip to content

Parse error for some valid procedural attribute macros, derive macro helper attributes #92

Closed
@ninevra

Description

@ninevra

tree-sitter-rust parses attributes using the meta-item syntax, which is the syntax used by most of rust's built-in attributes. The meta-item syntax is a subset of the attr syntax. Procedural attribute macros, however, use a larger subset of the attr syntax allowing arbitrary DelimTokenTrees. Derive macro helper attributes use the full attr syntax. As a result, tree-sitter-rust incorrectly parses many valid attributes.

As an example, the following is one of the usage examples for the thiserror crate. thiserror defines a derive macro helper attribute, error:

#[derive(Error, Debug)]
pub enum Error {
    #[error("first letter must be lowercase but was {:?}", first_char(.0))]
    WrongCase(String),
    #[error("invalid index {idx}, expected at least {} and at most {}", .limits.lo, .limits.hi)]
    OutOfBounds { idx: usize, limits: Limits },
} 

The phrases .limits.lo, .limits.hi, and first_char(.0) are legal under the attr syntax but not under meta-item, and so these attributes are parsed incorrectly.

This is likely the cause of atom/atom#20897. Any attribute not conforming to meta-item currently breaks atom's rust syntax highlighting.

Attributes should instead be parsed using the more permissive attr syntax.

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