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

First pass at a guide to compiler plugins #16995

Merged
merged 8 commits into from
Oct 3, 2014

Conversation

kmcallister
Copy link
Contributor

@steveklabnik, are you interested in looking this over?

@steveklabnik
Copy link
Member

I will check it out soon. I'm in Europe so there may be some lag.

is the procedural macro. These are invoked the same way as [ordinary
macros](guide-macros.html), but the expansion is performed by arbitrary Rust
code that manipulates [syntax trees](http://doc.rust-lang.org/syntax/ast/) at
compile time.
Copy link
Contributor

Choose a reason for hiding this comment

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

Plugins manipulate ASTs while macros manipulate tokens, right? Might be worth clarifying this distinction.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Eh, in both cases the base input is a sequence of token trees. A syntax extension plugin can parse to AST types using syntax::parse, but a MBE macro can do something similar with a metavariable like $x:expr. The result of a MBE macro is a token tree, while the result of a syntax extension is a thing that maybe knows how to make an expression, maybe knows how to make a pattern, and so forth.

So it's all a bit murky and I don't think there's a clear distinction worth highlighting.

Copy link
Member

Choose a reason for hiding this comment

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

A procedural macro really just manipulates token trees to possibly output some AST (e.g. https://github.com/huonw/compile_msg expands to nothing meaningful); there's no particular need for it to be interpreting any part of the input as an actual Rust AST.

(I'm happy with this phrasing though.)

@steveklabnik
Copy link
Member

I think as a first step, writing wise, this is just fine. It needs some more refinement, but before we get into that, I have a slightly bigger concern.

Is this a guide we actually really want in-tree right now? I'm glad to have this documentation exist, but these APIs, as you say, are very unstable, and are not likely to be stable any time soon. I'm not sure that giving them an official "guide" right now is the right call.

Maybe could this work as module docs in libsyntax or something? Not totally sure.

@huonw
Copy link
Member

huonw commented Sep 5, 2014

That seems like a pretty good idea; the macro part could be a docs in syntax::ext, the plugin stuff in rustc::plugin and the lint-specific stuff in rustc::lint. (We could have guide or doc submodules, like other places in the compiler.)

@kmcallister
Copy link
Contributor Author

@steveklabnik, @huonw: Hm, after your comments I gave some thought to moving all this into API docs. But I think basically nobody will find it on their own, without a more inviting starting place.

Basically I see this list as a key reference for "How do I do X in Rust?" for X = testing, FFI, macros, and other core areas where any language should have a good up-front answer. I think "procedural macros" should be on that list. Witness the number of systems projects with home-grown code generators; this is a capability many systems programmers need, even when it's a tremendous pain. We have a much better story here and we shouldn't bury it in internal compiler API docs.

@huonw
Copy link
Member

huonw commented Sep 14, 2014

How about linking to the internal doc page in that list?

@kmcallister
Copy link
Contributor Author

That seems okay, but at that point how is it meaningfully different from a first-class Guide? There are plenty of compiler implementation details in the other guides (e.g. here). Even though plugins involve compiler internals, the audience for a "How to write plugins" document is users of the compiler who don't necessarily plan to contribute to rustc itself. So I'd like to style this as "user-facing" documentation, similar to the other Guides.

I suppose that eventually we will have documentation for the compiler itself, separate from the language reference. This would cover things like rustc command-like syntax, and might be a natural place to describe plugins as well.

@steveklabnik
Copy link
Member

how is it meaningfully different from a first-class Guide?

Guides are intended (at least, by me, as of now) to be a sort of cross-cutting documentation. If something only involves one module, it should really be module-level documentation.

Furthermore, I could see an argument for maybe making this a guide, but given that 1: this is really about rustc and not about Rust and 2) compiler plugins are not stable and will not be soon, now may not be the appropriate time anyway.

(also, this needs a rebase regardless)

kmcallister added a commit to kmcallister/rust that referenced this pull request Sep 16, 2014
I will document this in the macros guide, after the conflicting rust-lang#16995 lands.
@kmcallister
Copy link
Contributor Author

the macro part could be a docs in syntax::ext

This involves putting rustc-specific docs in libsyntax, for example the stuff about --pretty.

Guides are intended (at least, by me, as of now) to be a sort of cross-cutting documentation. If something only involves one module, it should really be module-level documentation.

I do think this is cross-cutting — the stuff about syntax extensions covers syntax::ext, syntax::ast, rustc::plugin, and the rustc command-line interface, as well as feature-gated language syntax (quasiquotes).

@kmcallister
Copy link
Contributor Author

Thanks for the review everyone! I rebased with changes addressing most of the above comments.

I'm still trying to think of a more compelling example of a procedural macro. It's hard to strike the right balance of interesting, short, illustrative, and useful.

@kmcallister
Copy link
Contributor Author

Rebased, with a new syntax plugin example, and red-box warnings as suggested by @steveklabnik.

@mdinger
Copy link
Contributor

mdinger commented Sep 27, 2014

Macro guide line 105

rules of tokenization apply,
// extra comma             ^

@steveklabnik
Copy link
Member

Looks good to me overall :)

@kmcallister
Copy link
Contributor Author

Rebased.

@steveklabnik
Copy link
Member

I'm happy with this after that one last nit regarding relative references, and with a squash.

@kmcallister
Copy link
Contributor Author

Squashed, with a fix for check-docs in the macro guide.

@kmcallister
Copy link
Contributor Author

NB: it's this commit that needs the r+, GitHub is weird.

bors added a commit that referenced this pull request Oct 3, 2014
@bors bors closed this Oct 3, 2014
@bors bors merged commit 3f0c483 into rust-lang:master Oct 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants