rust-lang / rust-lang/rust-analyzer

Inconsistent unresolved-macro-call errors for macro_rules macro generated via proc-macro.

Open
#10,644 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I was using the usdt crate and noticed some weird behaviour about which macro_rules macros rust-analyzer would recognize.

The background here is that with the usdt crate, you have a separate file which defines some providers and probes:

provider test {
	probe count(uint8_t);
};

This defines a single probe count which we can use to instrument our rust code. The dtrace_provider proc macro from the usdt crate generates macro_rules macros that will insert your probes:

usdt::dtrace_provider!("test.d");

fn main() {
    usdt::register_probes().unwrap();
    
    let mut counter: u8 = 0;
    loop {
        test_count!(|| (counter));

        counter = counter.wrapping_add(1);
    }
}

test_count here is the generated macro and all is fine. But, try to call said macro from a different module and Rust-Analzyer just throws unresolved-macro-call errors:

usdt::dtrace_provider!("test.d");

mod foo {
    pub fn do_work(c: u8) {
        test_count!(|| (c));
    }
}

fn main() {
    usdt::register_probes().unwrap();
    
    let mut counter: u8 = 0;
    loop {
        test_count!(|| (counter));
        foo::do_work(counter);

        counter = counter.wrapping_add(1);
    }
}
{
	"resource": "/tmp/dust/src/main.rs",
	"owner": "rustc",
	"code": {
		"value": "unresolved-macro-call",
		"target": {
			"$mid": 1,
			"external": "https://rust-analyzer.github.io/manual.html#unresolved-macro-call",
			"path": "/manual.html",
			"scheme": "https",
			"authority": "rust-analyzer.github.io",
			"fragment": "unresolved-macro-call"
		}
	},
	"severity": 8,
	"message": "unresolved macro `test_count!`",
	"source": "rust-analyzer",
	"startLineNumber": 5,
	"startColumn": 9,
	"endLineNumber": 5,
	"endColumn": 19
}

But cargo check/build work just fine.

rust-analyzer version: ed39b45e8 2021-10-26 nightly

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

Reproduce the supplied usdt::dtrace_provider! example, comparing a macro call in the defining module with one inside mod foo. Start by checking how rust-analyzer resolves proc-macro-generated macro_rules macros across modules, and compare its diagnostics with cargo check/build. Done means valid cross-module calls no longer produce unresolved-macro-call errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.