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

Macros emitted by other macros aren't found #8120

Closed
nox opened this issue Mar 20, 2021 · 5 comments · Fixed by #8128
Closed

Macros emitted by other macros aren't found #8120

nox opened this issue Mar 20, 2021 · 5 comments · Fixed by #8128
Labels
A-macro macro expansion S-actionable Someone could pick this issue up and work on it right now

Comments

@nox
Copy link

nox commented Mar 20, 2021

macro_rules! item {
    ($item:item) => { $item }
}

item! {
    macro_rules! indirect_macro { () => {} }
}

indirect_macro!(); // rust-analyzer sees an error here, because it doesn't know of indirect_macro.
{
	"resource": "/Users/nox/src/ra-bug/src/lib.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 call",
	"source": "rust-analyzer",
	"startLineNumber": 9,
	"startColumn": 1,
	"endLineNumber": 9,
	"endColumn": 15
}

I don't think this was ever reported, so here we are.

@lnicola lnicola added A-macro macro expansion S-actionable Someone could pick this issue up and work on it right now labels Mar 20, 2021
@edwin0cheng edwin0cheng self-assigned this Mar 20, 2021
@jonas-schievink
Copy link
Contributor

This might be because we don't do this correctly during name resolution, not necessarily a problem with the macro expansion engine

@edwin0cheng
Copy link
Member

Yeah, and I remembered we handled this case in our fixed-point loop, so maybe a regression ?

@edwin0cheng
Copy link
Member

edwin0cheng commented Mar 20, 2021

Um.. I think I know the problem :

  1. In first collecting phase, we collected item!() in legacy scope and indirect_macro!() in module scope.
  2. When expanding item!(), we defined a new macro rules indirect_macro! in legacy scope.
  3. When resolving indirect_macro, we only search for module scope. (Because we assumed it cannot be resolved in legacy scope in (1).

But I don't know what's the correct way to fix it ? @jonas-schievink any idea?

@edwin0cheng edwin0cheng removed their assignment Mar 20, 2021
@jonas-schievink
Copy link
Contributor

I think we should expand item! during collection phase, I believe that's closer to how legacy macros work in rustc anyways. This should always work for legacy macros since they don't need the fixed-point loop.

Not sure how difficult this is to implement, I might look into it later.

@jonas-schievink
Copy link
Contributor

Turns out this was really easy, unless I missed something #8128

@bors bors bot closed this as completed in 5bb65bb Mar 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro macro expansion S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants