Skip to content
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

Expression variant of include! always produces errors #10647

Open
strohel opened this issue Oct 27, 2021 · 5 comments
Open

Expression variant of include! always produces errors #10647

strohel opened this issue Oct 27, 2021 · 5 comments
Labels
A-macro macro expansion A-vfs S-actionable Someone could pick this issue up and work on it right now

Comments

@strohel
Copy link

strohel commented Oct 27, 2021

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

@bjorn3
Copy link
Member

bjorn3 commented Oct 27, 2021

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.

@bjorn3 bjorn3 added A-macro macro expansion S-actionable Someone could pick this issue up and work on it right now labels Oct 27, 2021
@strohel strohel changed the title Expression variant of include! always yields failed to load file (macro-error) Expression variant of include! always produces errors Oct 27, 2021
@strohel
Copy link
Author

strohel commented Oct 27, 2021

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
Copy link

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
Copy link

NilsIrl commented Dec 20, 2021

Duplicate of #10178

@strohel
Copy link
Author

strohel commented Dec 20, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro macro expansion A-vfs S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

No branches or pull requests

5 participants