Stream: t-compiler/wg-rls-2.0

Topic: Find all possible derive macro values?


Kirill Bulatov (May 01 2020 at 12:12, on Zulip):

Is there a way to get all proc_macro_derive declarations in the current project?

Edwin Cheng (May 01 2020 at 12:14, on Zulip):

You means in RA ? or means there is some APIs to do that ?

Kirill Bulatov (May 01 2020 at 12:16, on Zulip):

Yep, I want to use something in RA to provide completion for #[derive(<|>)] cases.
I'm currently hardcoding the standard ones, but wonder if there's a better way to do that.

Edwin Cheng (May 01 2020 at 12:22, on Zulip):

In theory , the following step should works:

  1. Find all MacroDefID in scope (I think we may have this API but I am not sure)
  2. Check which MacroDefKind it is.
Kirill Bulatov (May 01 2020 at 12:23, on Zulip):

I'll give it a shot, thanks.

Florian Diebold (May 01 2020 at 12:29, on Zulip):

except built-in derives are currently hard-coded and would have to be added separately, right?

Edwin Cheng (May 01 2020 at 12:33, on Zulip):

Oh , it reminded me I still haven't looked for what changed in Macro 2.0...

Kirill Bulatov (May 01 2020 at 12:47, on Zulip):

I wonder, should https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/ra_syntax/src/ast/extensions.rs#L466 be T!['}'] | T![')'] | T![']'] => true, instead?

Edwin Cheng (May 01 2020 at 12:50, on Zulip):

I think so, it is introduce in https://github.com/rust-analyzer/rust-analyzer/pull/3926 and these functions are not in used :)

Kirill Bulatov (May 01 2020 at 22:24, on Zulip):

Apparently, I'm either doing something wrong, or we fail to detect the derive macros.

I have the following silly code to check if there any results:
image.png

For a project with Serde, I get plenty of macros printed: https://gist.github.com/SomeoneToIgnore/cef5dc151f59107989a721fab27b8c22
Yet there are no builtin_derive or custom_derive ones, none at all.

Edwin Cheng (May 02 2020 at 02:55, on Zulip):

Have you imported the path? I means :

import serde::Deserialize;
Kirill Bulatov (May 02 2020 at 05:20, on Zulip):

Yes, the project compiles.

Edwin Cheng (May 02 2020 at 05:28, on Zulip):

Um... May I have the branch I could try ?

Edwin Cheng (May 02 2020 at 05:36, on Zulip):

you might add some dbg in [here][1] , to make sure the collector collect proc-macro correctly.

Kirill Bulatov (May 02 2020 at 05:54, on Zulip):

Sure, here's the branch with the dbg prints I've mentioned above:

https://github.com/SomeoneToIgnore/rust-analyzer/tree/derive-detection-tests

add some dbg in [here][1]

Guess the links are missing, so I could no add anything yet :)

Kirill Bulatov (May 02 2020 at 05:58, on Zulip):

Now you can open the same RA project with that branch installed, open json_project.rs and try co complete something in the attribute with Deserialize there:
image.png

dbg prints show that there are 3 types of macros detected with my code: BuiltInEager, Declarative, BuiltIn.

Kirill Bulatov (May 02 2020 at 05:59, on Zulip):

Ooooh, actually, I've just realized that I don't have proc macros enabled, maybe that's the caveat, sorry for the noise.

Kirill Bulatov (May 02 2020 at 06:02, on Zulip):

Lol, indeed that was the case.
Sorry for the noize and thank you a lot anyway :)

Edwin Cheng (May 02 2020 at 06:16, on Zulip):

So it works now ?

Kirill Bulatov (May 02 2020 at 06:20, on Zulip):

Yes, I can see derives in the debug output.

Kirill Bulatov (May 02 2020 at 06:23, on Zulip):

I still have to extract the derive attribute value from them, but that should be simple if I'm able to switch to ast somehow from those definitions, I'll dig more.

Edwin Cheng (May 02 2020 at 06:28, on Zulip):

There is no ast for proc-macro ...

Edwin Cheng (May 02 2020 at 06:29, on Zulip):

btw, why you need to extract the derive attribue ?

Edwin Cheng (May 02 2020 at 06:31, on Zulip):

By design, we get the proc-macro from its dll directly, and do not parse the [proc-macro_derive]

Kirill Bulatov (May 02 2020 at 06:36, on Zulip):

Maybe I worded it wrong: I need somehow to get all those Serialize, Deserialize, etc. names into the completion options.

By reading the https://doc.rust-lang.org/reference/procedural-macros.html , I've got an impression that I can do that by somehow getting the values out of #[proc_macro_derive(...)] attributes and that's what I try to achieve.

Kirill Bulatov (May 02 2020 at 06:39, on Zulip):

I see that name of the macro item matches the derive string I have to input. Can I rely on that instead?

[crates/ra_ide/src/completion/complete_attribute.rs:19] name = Name(
    Text(
        "Deserialize",
    ),
)
[crates/ra_ide/src/completion/complete_attribute.rs:20] mac = MacroDef {
    id: MacroDefId {
        krate: Some(
            CrateId(
                63,
            ),
        ),
        ast_id: None,
        kind: CustomDerive(
            ProcMacroExpander {
                krate: CrateId(
                    63,
                ),
                proc_macro_id: ProcMacroId(
                    1,
                ),
            },
        ),
    },
}
Edwin Cheng (May 02 2020 at 06:48, on Zulip):

Yes, it should be the name to use, it is extract from DLL directly.

Kirill Bulatov (May 02 2020 at 06:52, on Zulip):

Perfecto, even better for me, thanks.

Last update: Oct 28 2020 at 18:15UTC