Skip to content

Conversation

@tamasfe
Copy link
Contributor

@tamasfe tamasfe commented Jun 23, 2023

This PR aims to alter how we handle ignored proc macros so the syntax tree is not altered by them in any way. This way lsp features should keep working inside macros that seemingly do nothing (e.g. async_trait, tracing::instrument, tokio::main).

Note that this approach is still not perfect, as we miss out on any changes by macros, e.g. by ignoring pin_project we get IDE assists working on the struct but RA will not know anything about the methods it added. To handle this properly I'd imagine we could still expand the macro for completions but still ignore expanded code at the invocation site for anything else, doing this is way out of scope for me however.

Changes

  • fixed incorrectly expanding associated item macros despite attr macros being disabled in config
  • instead of using identity expanders, we mark the macros as ignored when we collect proc macros
  • we lookup the macros during macro (call) resolution and skip the ignored macros altogether
  • ignored crate names are insensitive to - and _ (as not remembering which one a crate used is a source of frustrations for me)

TODO

We could alternatively keep macro resolution as it was before but throw away expansions at a later point, which would probably be better but it's deeper in the rabbit hole.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 23, 2023
@tamasfe tamasfe marked this pull request as ready for review June 27, 2023 20:51
@tamasfe
Copy link
Contributor Author

tamasfe commented Jun 27, 2023

After spending a few days writing/refactoring an async-trait-heavy codebase, this is definitely a net improvement for my workflow, so from a user standpoint I believe merging this even in its current state would be beneficial.

@Veykril
Copy link
Member

Veykril commented Jun 28, 2023

Note that this approach is still not perfect, as we miss out on any changes by macros, e.g. by ignoring pin_project we get IDE assists working on the struct but RA will not know anything about the methods it added. To handle this properly I'd imagine we could still expand the macro for completions but still ignore expanded code at the invocation site for anything else, doing this is way out of scope for me however.

That's not the idea behind this setting. Ignoring means we ignore the macros, that's it. Ideally this setting wouldn't exist but r-a is currently not good enough with macros so we need this escape hatch.

attr,
) {
Ok(ResolvedAttr::Macro(call_id)) => {
self.attr_calls.push((ast_id, call_id));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh ... this was the reason things broke down for tracing and the like ...

Comment on lines +273 to +276
/// Ignored proc macros should only be expanded
/// when we are explicitly asked to do so by the user.
pub ignored: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, let's do things a bit differently. Instead of encoding ignoredness as a flag here, let's just add a second special ProcMacroExpander for ignored ones, samy way we hadnle the dummy expander basically. Then we don't need to fetch the loaded proc-macros everytime to check and can instead immediately check the expander at hand

// disabled. This is analogous to the handling in
// `DefCollector::collect_macros`.
if exp.is_dummy() {
continue 'attrs;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
continue 'attrs;
self.diagnostics.push(
DefDiagnostic::unresolved_proc_macro(
self.module_id.local_id,
loc.kind,
loc.def.krate,
),
);
continue 'attrs;

@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 3, 2023
@bors
Copy link
Contributor

bors commented Jul 3, 2023

☔ The latest upstream changes (presumably #15118) made this pull request unmergeable. Please resolve the merge conflicts.

@Veykril
Copy link
Member

Veykril commented Aug 15, 2023

Friendly bump, I presume you just haven't had the time for this but I wanted to ask for a status update nevertheless :)

@tamasfe
Copy link
Contributor Author

tamasfe commented Aug 15, 2023

Sorry, I made no progress on this since. I assume there is not a lot of work to do here just requires a context switch on my part.

I'll try to get back to it next week. If anyone wants to take over in the meantime, that's also fine by me.

@tamasfe tamasfe force-pushed the feat/better-ignored-macros branch from ed2e2a5 to 6699133 Compare November 17, 2023 10:17
@tamasfe tamasfe closed this Nov 17, 2023
bors added a commit that referenced this pull request Feb 12, 2024
feat: ignored and disabled macro expansion

Supersedes #15117, I was having some conflicts after a rebase and since I didn't remember much of it I started clean instead.

The end result is pretty much the same as the linked PR, but instead of proc macro lookups, I marked the expanders that explicitly cannot be expanded and we shouldn't even attempt to do so.

## Unresolved questions

- [ ] I introduced a `DISABLED_ID` next to `DUMMY_ID` in `hir-expand`'s `ProcMacroExpander`, that is effectively exactly the same thing with slightly different semantics, dummy macros are not (yet) expanded probably due to errors, while not expanding disabled macros is part of the usual flow. I'm not sure if it's the right way to handle this, I also thought of just adding a flag instead of replacing the macro ID, so that the disabled macro can still be expanded for any reason if needed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants