Skip to content

Proc macro is panicking in rust-analyzer when it does not in a rustc compilation [Nightly Rust] #17193

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
d3rpp opened this issue May 6, 2024 · 2 comments
Labels
C-bug Category: bug

Comments

@d3rpp
Copy link

d3rpp commented May 6, 2024

rust-analyzer version: 0.4.1948-standalone

rustc version:

  • rustc 1.80.0-nightly (9c9b56879 2024-05-05)
  • rustc 1.78.0-nightly (9b00956e5 2024-04-29)

editor or extension: VSCode

relevant settings:

  • Cargo Check command set to use clippy
  • Proc Macro support enabled

repository link (if public, optional): d3rpp/proc_macros_path_panicking_incorrectly

code snippet to reproduce:

The repository link is a reproduced example, it is able to replicate the issue exactly as it is occurring in the main project (which is unfortunately closed source)

// look above

Note: This is using unstable features in order to provide the information that enables this bug to be triggered.

#![feature(proc_macro_span)]
@d3rpp d3rpp added the C-bug Category: bug label May 6, 2024
@Veykril
Copy link
Member

Veykril commented May 6, 2024

The unstable proc-macro apis are currently unimplemented in r-a #15950

@Veykril Veykril closed this as completed May 6, 2024
@d3rpp
Copy link
Author

d3rpp commented May 6, 2024

For those that come across this and want their squiggly lines to go away, I've got 2 work arounds for you:

Note

I'm yet to test either of these, and they've been written from memory.

Detect when rust-analyzer is running the macro and act accordingly

in my reproducible example there's a line showing that the path simply shows up as being empty, since you'll either get a path or a compiler error in regular rust, we can use this to detect if the macro is being analyzed and just make it return an empty token stream.

  • easy to add
  • works for macros that have users outside its original workspace
use proc_macro::Span;
use proc_macro2::TokenStream;

#[proc_macro]
pub fn proc_macro(_input: TokenStream) -> TokenStream {
    let is_rust_analyzer = Span::call_site()
        .source_file()
        .path()
        .into_os_string()
        .is_empty();

    if is_rust_analyzer {
        TokenStream::default()
    } else {
        // ...
    }
}

Just, ignore the macro.

this is more useful to macros that are reside in, and are only used by, one project (like in my use case), they rust-analyzer devs are cool enough to allow you to add a config that just gets it to completely ignore specific macros.

Set the option

rust-analyzer.procMacro.ignored

to an object shaped like something below (below taken from VSCode)

"rust-analyzer.procMacro.ignored": {
    "crate_name": [
        "macro_name"
    ]
}

I'd recommend applying this to workspace settings as opposed to global settings, that way other users of the repo have the setting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

2 participants