Skip to content

Support doc comments and attributes on nested items #6932

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

Closed
ironcev opened this issue Feb 14, 2025 · 4 comments · Fixed by #7146
Closed

Support doc comments and attributes on nested items #6932

ironcev opened this issue Feb 14, 2025 · 4 comments · Fixed by #7146
Assignees
Labels
compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler: parser Everything to do with the parser compiler General compiler. Should eventually become more specific as the issue is triaged good first issue Good for newcomers may be rewarded ODHack12 ODHack13 P: low team:compiler Compiler Team

Comments

@ironcev
Copy link
Member

ironcev commented Feb 14, 2025

E.g., while this compiles:

library;

fn fun() {
    struct S {
        field: u8,
    }

    const CONST: u8 = 0;
}

trying to attribute or comment nested items will result in "Expected an expression." error on every doc comment or attribute:

library;

fn fun() {
    /// Comment.             // <<<--- ERROR: Expected an expression.
    #[allow(dead_code)]      // <<<--- ERROR: Expected an expression.
    struct S {
        /// Comment.         // <<<--- ERROR: Expected an expression.
        #[allow(dead_code)]  // <<<--- ERROR: Expected an expression.
        field: u8,
    }

    /// Comment.             // <<<--- ERROR: Expected an expression.
    #[allow(dead_code)]      // <<<--- ERROR: Expected an expression.
    const CONST: u8 = 0;
}
@ironcev ironcev added compiler General compiler. Should eventually become more specific as the issue is triaged compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler: parser Everything to do with the parser P: low team:compiler Compiler Team labels Feb 14, 2025
@mexes20
Copy link

mexes20 commented Mar 20, 2025

May I handle this issue?

@truthixify
Copy link

truthixify commented Apr 24, 2025

To resolve this issue, I will investigate why Rust’s parser incorrectly emits “Expected an expression” errors when documentation comments or attributes are used on nested items inside a function body. This behavior deviates from typical expectations, where attributes and doc comments should be allowed on items like structs, constants, or fields—even when scoped within a function.

My approach will be:
• First, reproduce the issue with a minimal test case using the current compiler version.
• Examine the Rust grammar for item definitions within function bodies to understand if this is a limitation by design or a parsing bug.
• Look into how the parser differentiates between expressions and item declarations, and whether it’s mistakenly trying to parse an attribute or doc comment as an expression.
• If it’s a known issue or has been previously discussed (e.g., in a Rust GitHub issue), I’ll review prior efforts and determine where contributions are needed.
• Otherwise, I’ll narrow the source of the error and propose a fix that updates the parser to properly handle attributes and comments in inner item contexts.

I will also write regression tests to ensure that this fix doesn’t break attribute or comment handling elsewhere. Depending on the complexity of the parser modification, this task should take 2 business days. Once implemented and validated, I’ll submit a pull request that references and closes the issue.

@saimeunt
Copy link
Contributor

As a returning contributor from the last ODHack I'd like to try this one.

@blurbeast
Copy link

hi! i would be glad to jump on this . thank you .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler: parser Everything to do with the parser compiler General compiler. Should eventually become more specific as the issue is triaged good first issue Good for newcomers may be rewarded ODHack12 ODHack13 P: low team:compiler Compiler Team
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants