rust-lang / rust-lang/rust-analyzer

Expression variant of `include!` always produces errors

Open
#10,647 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-macro A-vfs S-actionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

std::include! has 2 variants depending on how it is used:

  • Item variant include!("bindings.rs") often used in -sys crates.
  • Expression variant let value = include!("file.in") used likely much less.

The item variant works well, but the expression variant always causes rust-analyzer to emit an error. The only negative effect of the error on rust-analyzer is that it doesn't infer the type of the variable. Specifying the type explicitly works.

Minimal reproduction:

fn main() {
    let text_in = include!("text.in");
    let text_rs = include!("text.rs");
    println!("{} {}", text_in, text_rs);
}

With both text.in and text.rs being:

"Hello, world!"

Gives the following rust-analyzer errors:

main.rs
    failed to load file `text.in` rust-analyzer(macro-error) [2, 16]
text.rs
    Syntax Error: expected an item rust-analyzer(syntax-error) [1, 1]
    file not included in module tree rust-analyzer(unlinked-file)

Also available at https://github.com/strohel/include-test-case as a complete Cargo crate.

Note that if the included file has .rs extension (text.rs), the errors are slightly different.

VSCodium 1.61.2
rust-analyzer v0.2.784 from https://open-vsx.org/extension/matklad/rust-analyzer/0.2.784

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

Start with the complete Cargo reproduction at strohel/include-test-case and the minimal main.rs example, then run rust-analyzer to reproduce the diagnostics for expression-form include!. Trace the handling of include! and verify that valid included expressions no longer produce erroneous diagnostics or prevent type inference.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.