rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.06k stars 1.56k forks source link

Expression variant of `include!` always produces errors #10647

Open strohel opened 2 years ago

strohel commented 2 years ago

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

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

bjorn3 commented 2 years ago

text.in doesn't work as rust-analyzer only loads .rs files when populating the database. During macro expansion it isn't possible to read additional files. Using include!() as expression with files with the .rs extension not working should definitively be fixed though.

strohel commented 2 years ago

text.in doesn't work as rust-analyzer only loads .rs files when populating the database. During macro expansion it isn't possible to read additional files.

Got it, thanks for the info. Would it be possible to demote the error into a warning or info, saying something like "rust-analyzer cannot read non-.rs files during macro expansion"?

Using include!() as expression with files with the .rs extension not working should definitively be fixed though.

I've updated title, description and the code in example repository to directly showcase this case.

crumblingstatue commented 2 years ago

I'm running into the same problem (that rust-analyzer only loads .rs files) I named an include file .inc because it's in the cargo examples directory, and I don't want it to get picked up as an example.

NilsIrl commented 2 years ago

Duplicate of #10178

strohel commented 2 years ago

Duplicate of #10178

That's right, the problem is the same. #10178 has somewhat confusing title: the problem is in fact orthogonal to OUT_DIR and doesn't happen spuriously.