More information on attributes
I recently wrote a clippy lint that deals with attributes (mostly outer attributes) and found very little information on how they are implemented. Most of the information I gathered through PR comments and issues in the rust repo. The reference only explains how to use the different attributes.
Do you think it would make sense to have a chapter on attributes in the guide?
Some weird things and questions I came across:
- The AST seems to contain attributes that are not in the actual source code (Is this because of macro expansion?) (https://github.com/rust-lang-nursery/rust-clippy/pull/2340#issuecomment-361055252)
- some expanded attributes seem to have an empty TokenStream and some not
- cfg_attr is special somehow
- In general lots of inconsistencies in the data from the syntax crate?
I would be happy to start something myself, but would need some pointers as to where the attribute stuff happens.
I think it would be great to add this! I personally have often wondered how attributes work.
Do you happen to know how they interact with custom drive and proc macros?
The implementation history for libsyntax(_ext) looked roughly like "let's urgently patch things up for 1.0" -> "let's urgently implement mostly working macros 1.1" -> "let's attract new contributors to tick some boxes for the proc macros rfc here and there", and soon "let's urgently implement mostly working macros 1.2 and also attributes for tools" all while keeping the old infrastructure for legacy syntax plugins. You can imagine the amount of technical debt. jseyfried did a lot of work to reduce this chaos, but not enough to fully conquer it.
Do you think that libsyntax2.0 will replace all of this choas (if it happens)?
There will be discussion about libsyntax 2.0 on friday @ all hands. I pre-ask this question :)
cc @matklad I hear you organise discussion for that?
@nagisa yep! And this attribute processing actually a nice illustration for he lossy nature of the current libsyntax. However libsyntax 2 is a hypothetical far away future, so it seems to me that the more actionable item here is to try to refactor the current attribute handling?
@phansch Did you still want to take a stab at this?
@mark-i-m Sorry I'm currently not in the position to dig into this :/
Triage: attribute rework is happening in https://github.com/rust-lang/rust/pull/131808 thanks to @jdonszelmann. cc tracking issue https://github.com/rust-lang/rust/issues/131229.
hi! that's right, and I'll also write some things down about it. In general, the new rustc_attr crate already contains copious amounts of docs on my own system. Will try to push things soon
@rustbot claim