Skip to content

False positive in macro expansion (Did not find struct MyJsClass parsed before expand #[napi] for impl) #17429

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
theamniel opened this issue Jun 15, 2024 · 2 comments
Labels
A-nameres name, path and module resolution A-proc-macro proc macro C-bug Category: bug

Comments

@theamniel
Copy link

Hi, I'm a bit new to using rust but I like to do a lot of research on what I use and I've had problems with the rust-analyzer as it's a false positive because the code works, disabling macro expansion doesn't mark it as an error.

I was exploring and testing a little bit the possible solutions that I was seeing in the issues of this repository, like this #17231.
But my solution is this comment.

I'm sorry for my bad English

rust-analyzer version: 0.4.1999-standalone (6b8b8ff 2024-06-14)
rustc version: rustc 1.79.0 (129f3b996 2024-06-10)

editor or extension: VSCode Insiders (only rust-analyzer enable)

relevant settings:

  • CARGO_HOME: D:\Programs\Rust\.cargo
  • RUSTUP_HOME: D:\Programs\Rust\.rustup

repository link (if public, optional): repo for testing

code snippet to reproduce:

#[macro_use]
extern crate napi_derive;

#[napi]
pub struct MyJsClass {
  pub input: String,
}

#[napi]
impl MyJsClass {
  // Js constructor
  #[napi(constructor)] // when add this: Did not find struct `MyJsClass` parsed before expand #[napi] for impl 
  pub fn new(input: Option<String>) -> Self {
    MyJsClass {
      input: input.unwrap_or("none".to_string()),
    }
  }

  // class getter
  #[napi(getter)]
  pub fn val(&self) -> &String {
    &self.input
  }

  // class setter
  #[napi(setter)]
  pub fn set_val(&mut self, input: String) {
    self.input = input;
  }

  // class method
  #[napi]
  pub fn get_val(&self) -> String {
    self.input.clone()
  }
}


// But this work
#[napi(constructor)]
pub struct MyJsClass2 {
  // new MyJsClass2("input param")
  pub input: String,
}

#[napi]
impl MyJsClass2 {
  // class getter
  #[napi(getter)]
  pub fn val(&self) -> &String {
    &self.input
  }
  
  // class setter
  #[napi(setter)]
  pub fn set_val(&mut self, input: String) {
    self.input = input;
  }

  // class method
  #[napi]
  pub fn get_val(&self) -> String {
    self.input.clone()
  }
}
@theamniel theamniel added the C-bug Category: bug label Jun 15, 2024
@Veykril Veykril added A-nameres name, path and module resolution A-proc-macro proc macro labels Jun 16, 2024
@flodiebold
Copy link
Member

flodiebold commented Jun 22, 2024

napi appears to keep state between invocations of the macro and rely on the order the macro is invoked in. This is questionable at best in rustc, and never going to work in rust-analyzer which can lazily expand individual macro calls as needed. The solution in the linked napi issue, setting rust-analyzer.procMacro.ignored to ignore the napi macro, is the best you're going to get. You could open an issue in napi about this.

@flodiebold flodiebold closed this as not planned Won't fix, can't repro, duplicate, stale Jun 22, 2024
@Sunny-117
Copy link

Sunny-117 commented May 22, 2025

// .vscode/settings.json
{
"rust-analyzer.procMacro.ignored": { "napi-derive": ["napi"] }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-nameres name, path and module resolution A-proc-macro proc macro C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

4 participants