Skip to content

Should all the AST node types with attributes be using AttrVec? #77662

Closed
@Ryan1729

Description

@Ryan1729

I've been working on a PR to clippy that involved looking at the different AST node types in rustc_ast::ast. I noticed that while most of the subset of those types that can contain attributes use AttrVec, (an alias of a ThinVec of Attributes) some node types use a plain Vec<Attribute>.

In particular, as of this writing, Variant, StructField, Item, Crate, and Arm all use Vec<Attribute>. (I found these via a search in rustc_ast for "attr" and checking each field in the results, so hopefully that list is complete.)

ThinVec's documentation says:

A vector type optimized for cases where this size is usually 0 (cf. SmallVector). The Option<Box<..>> wrapping allows us to represent a zero sized vector with None, which uses only a single (null) pointer.

I would suspect that for most of the types that use a plain Vec<Attribute> the Vec is empty almost all of the time, with the possible exception of Items, since #[derive(Debug)] etc. is pretty common.

So should more of the types that use a plain Vec<Attribute> be using an AttrVec instead? Are these fields plain Vecs just because no one changed them, or was it somehow determined that those types aren't copied enough for this optimization to matter?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-parserArea: The lexing & parsing of Rust source code to an ASTC-cleanupCategory: PRs that clean code up or issues documenting cleanup.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions