dart-lang / macros

A Dart mono-repo for macro development.
BSD 3-Clause "New" or "Revised" License
93 stars 5 forks source link

Macro API design discussion #91

Open jakemac53 opened 1 month ago

jakemac53 commented 1 month ago

I wanted to discuss the overall design of the macro APIs that people are coding against. Ultimately, my goal is to move them much closer to the existing APIs with the exception of the introspection APIs.

Some specific changes I want to make:

cc @davidmorgan maybe we should discuss tomorrow am?

jakemac53 commented 1 month ago

Another area we need to evaluate is the Query API and how we want to make that slot into the phases, but I don't have a proposal for it.

davidmorgan commented 1 month ago

Thanks for pushing on this Jake ... there is a surprisingly large amount to unpack here :)


Re: "description field".

With Macro application annotation identification we haven't fleshed out how the host finds out implementation detail of the macro, such as phases.

What we currently have in v2 is that macro implementation detail is sent by the macro on startup in MacroStartedRequest.

So to support fine-grained interfaces per the v1 API we would have:

and then we have a few more questions about how this whole thing ties together:

Metadata and Declaration are mostly interesting for the question of how they interact with queries and the Model. Possibly what a v2 macro gets is a default initial query and it immediately gets passed a Model which is the result of that query. But then the macro hasn't had a chance to specify the details of that query, e.g. whether to fetch all the types mentioned. So that's not 100% clear.

Another consideration here is that I think the Macro interface is tied to the major protocol version: when the major protocol version changes the Macro interface becomes Macro2 and all the types change. So the macro_client dispatch to macro methods has to be able to do this. (Should be easy enough, particularly since we fully control the generation of the bootstrap code).

I think a good path is to push for one e2e example that works like a v1 macro, it's not clear to me that we're headed for something exactly like v1 macros, but if there are going to be any differences then we can figure out what+why along the way.


Builder APIs: yes, I think we want these, and yes to flushing out AugmentationResponse correspondingly.

Just one note here. If I understood correctly, builders are code running on the client that helps to create Dart code. That's fine, but we should think about forwards compatibility: what happens when a new language version requires something different? We might need a way to introduce new code paths for new language versions.

Again I think probably some simple e2e example to prove out the new ground here is probably a good way to go.


Indeed, let's discuss :) thanks!

jakemac53 commented 1 month ago

Just one note here. If I understood correctly, builders are code running on the client that helps to create Dart code. That's fine, but we should think about forwards compatibility: what happens when a new language version requires something different? We might need a way to introduce new code paths for new language versions.

It is a combination of client + server side right now. The client side only ever generates a single declaration, and not any surrounding declarations (but will have a reference to which surrounding declaration it is generating it for). The server side then merges all those declarations together from all the macros, under a single augmentation of the surrounding declaration.

We could move literally all of it server side though, if that would be better. It might even be a requirement, because if the queries are not complete, the client might not have the required information to generate the full declaration.

jakemac53 commented 1 month ago

Copying a comment from https://github.com/dart-lang/macros/pull/104, we decided on the following additional things regarding builder objects: