rustc-dev-guide icon indicating copy to clipboard operation
rustc-dev-guide copied to clipboard

More information on attributes

Open phansch opened this issue 7 years ago • 10 comments

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.

phansch avatar Mar 27 '18 19:03 phansch

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?

mark-i-m avatar Mar 27 '18 23:03 mark-i-m

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.

petrochenkov avatar Mar 27 '18 23:03 petrochenkov

Do you think that libsyntax2.0 will replace all of this choas (if it happens)?

mark-i-m avatar Mar 28 '18 17:03 mark-i-m

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 avatar Mar 28 '18 20:03 nagisa

@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?

matklad avatar Mar 29 '18 00:03 matklad

@phansch Did you still want to take a stab at this?

mark-i-m avatar Apr 10 '18 02:04 mark-i-m

@mark-i-m Sorry I'm currently not in the position to dig into this :/

phansch avatar Apr 10 '18 05:04 phansch

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.

jieyouxu avatar Nov 04 '24 11:11 jieyouxu

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

jdonszelmann avatar Nov 04 '24 11:11 jdonszelmann

@rustbot claim

jdonszelmann avatar Nov 04 '24 11:11 jdonszelmann