rust-lang / rust-lang/rust-analyzer

rust-analyzer can falsely shows errors in `include!`'d file that is a single expression

Open
#14,696 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

After a little more digging after erroneously opening #14695, I've pinned down that rust-analyzer works properly when calling the include! macro on any file, except when it's an expression and it isn't wrapped in a macro. For, some reason.

Example

main.rs:

// works fine, is even highlighted with the same color as a mod
include!("a.notrs");

// wrapping the expression around this works fine.
macro_rules! a {
    ($tokens:tt) => {
        $tokens
    }
}

fn main() {
    // works fine, is also highlighted with the same color as a macro invocation
    let x: u32 = include!("b.notrs");
    dbg!(x);
    // doesn't. has the same color as any literal and has an error in c.notrs
    let y: u32 = include!("c.notrs");
    dbg!(y);
}

a.notrs:

mod a {}

b.notrs:

a! { 1u32 }

c.notrs:

2u32

Compiling this works fine, but rust-analyzer shows "Syntax Error: expected an item" in c.notrs

rust-analyzer version: 0.3.1489-standalone

rustc version: rustc 1.71.0-nightly (5cdb7886a 2023-04-15)

relevant settings: none(?)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Recreate the reported example using main.rs, a.notrs, b.notrs, and c.notrs, then compare rust-analyzer's diagnostics with rustc's successful compilation. Start by tracing how include! content is parsed in expression position, especially when it is not wrapped in another macro. Done means c.notrs no longer reports "Syntax Error: expected an item" while the existing item and macro cases remain correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.