-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Change inner attribute syntax for clarity and consistency #2569
Change inner attribute syntax for clarity and consistency #2569
Comments
This should make attribute parsing significantly simpler since there's a fixed amount of lookahead needed to know if you are parsing an inner attribute. |
I'm ok with this but as mentioned in the other bug on doc comments, think angle-bracket might not be the most delicious choice. It's not really visually suggestive of what it applies to; the semi rule had that going for it (with the drawback being, I guess, both subtlety and no obvious connection to any doc-comment syntax). Maybe something involving exclamation points or vertical bars? :) |
(only half joking; we adopted shebang comments already and intend to have those around for tagging language-version. maybe you can dangle this function on a relative of that syntax) |
I guess it has much the same issues, but how about |
@Dretch. I think |
I'm surprised that graydon suggested the shebang notation, because I was going to suggest
...but I thought people would think it was too silly. :) I still like it, though! |
Yeah. I've poked at this long enough in my own emacs buffer now to feel positive about the bang-mark. It goes ok in comments too. Though to ensure we don't confuse any shell interpreters trying to run attributes, I think it should differ in first-two chars from shebang itself. I.e. move the bang inside the attribute. So we wind up with:
Is this too ghastly? It looks ... ok to me. I mean, it's a stretch but we have a bunch of layers of functionality to get to hang together here and it'd be good to have it "work" nice. (In theory we could also wedge-in a pre-parse conditional-compilation mode, via the plain-sharp coments, as an escape hatch for conditionally-compiling things that differ at only a lexeme-by-lexeme level. Sometimes the resolution of the |
The bang does look good. The main reason I didn't want to use it is because in doxygen We should change the attribute grammer to only have one meta item per. This is something I've wanted to do anyway as a simplification, but I really don't want to be able to write The bang does strongly suggest logical negation though. What does One thing that might impact attribute syntax is that pcwalton wants to change the extension grammer to be
Of course then you have to worry about distinguishing between attributes and a shebang. |
I'm not sure that we need to resort to Iff you are swayed by these arguments, I think a clearer rule for the docstrings and attributes would be "docstrings and attributes apply to the following item, unless their opening token is followed by a
(New personal resolution: submit one unrelated-to-syntax patch for every bikeshedding comment I make.) |
re: brson's comment (which wasn't here when I started typing the above): I really adore the bracketless notation, and also signifying macros with a trailing (patch_debt++) |
I just got assigned this for the triage. I actually feel that the current syntax is more consistent than the suggested alternatives. Does anyone have strong feelings about this still? |
@z0w0 I still have strong feelings that the existing trailing-semicolon-inner-attribute syntax is very bad, I just think there's been no real discussion of what to replace it with. Would be nice to bring this up on the mailing list and fish for comments. |
I can live with any of the options, including present form. I think @brson still dislikes it though, and people do stumble on it regularly. |
My first choice is bracketless, followed by |
Perhaps inline could be exclamation mark in front of the statement like |
@metajack bracketless is also my favorite option, and I think brson has expressed some fondness for that form as well. The "lightness" of the bracketless syntax is very refreshing. And maybe we could then use bracketed syntax for inner attributes, as a visual shorthand to represent that those attributes are "enclosed".
|
@bstrie at first glance +1 |
If we go with @bstrie's suggestion of bracketless vs brackets then we also need to consider how that relates to doc comments
This solution hasn't particularly improved the syntactic relationship between attributes and doc comments. I might feel happiest making inner attributes |
I don't think there's necessarily a need to draw parallels between doc comments and attributes, even if the former happens to be implemented in terms of the latter. Having two forms of doc comments has always just seemed silly. :) Inner doc comments can do everything that outer ones can, yeah? At the risk of going out-of-scope, I'd propose getting rid of the bizarre More generally speaking, I feel like having two different syntaxes for doc comments is needlessly decadent. Better to pick just one form and enforce it. |
Inner for modules. We keep revisiting this :) |
@bstrie the reasons for needing both doc comment forms are identical to the reason for needing both attribute forms. Doc comments are attributes. I am though still open to figuring out a reliable way to make doc comments work with only a single syntax. Even I regularly use the wrong form of comments in places, and emacs still doesn't know how to word wrap |
Nominating. |
What about two hashes? |
I prefer the form with the delimiters. I'd like to see them go away altogether or be restricted to module-top-level, but it makes me sad when a perfectly serviceable delimiter notation gets axed. |
@jbclements "I prefer the form with the delimiters" seems to contradict "I'd like to see them go away altogether"; is there a typo there? Update: Okay, I now infer that you meant "I'd like to see inner attributes themselves go away entirely, or be restricted in where they can be used, but if one is going to keep them as a feature, then I'd prefer a bracketed syntax." |
If we go with the shebang notation, I would prefer to also keep the trailing semi-colon. I want it to be clear to the human reader that this form is a significant independent entity and does not apply to the item that follows it. For example: This way, when one is scanning backwards, one can distinguish the two cases without having to look all the way at the beginning of the attribute declaration. |
This will require a snapshot to finish, but these commits update the parser to parse attributes of the form `#![...]` Thanks to @thehydroimpulse for all the initial work! cc #2569
Applied fixes: *logging macros need the log crate *attribute fix (cf rust-lang/rust#2569) *priv attribute removal *Rename Chan to Sender (cf rust-lang/rust@7858065) *New vec library (cf rust-lang/rust#12771) *update the channel constructor (cf rust-lang/rust@7858065) *Rename Port to Receiver (cf rust-lang/rust@7858065)
Applied fixes: *attribute fix (cf rust-lang/rust#2569) *New vec library (cf rust-lang/rust#12771)
Applied fixes: *priv attribute removal *attribute fix (cf rust-lang/rust#2569)
Applied fixes: *priv attribute removal *attribute fix (cf rust-lang/rust#2569) *Rename Port to Receiver (cf rust-lang/rust@7858065) *logging macros need the log crate
Applied fixes: *attribute fix (cf rust-lang/rust#2569) *priv attribute removal (cf rust-lang/rust@f2a5c7a)
Applied fixes: *extern mod is obsolete *attribute fix (cf rust-lang/rust#2569) *priv attribute removal (cf rust-lang/rust@f2a5c7a)
Applied fixes: *attribute fix (cf rust-lang/rust#2569) *Rename Chan to Sender (cf rust-lang/rust@7858065) *New vec library (cf rust-lang/rust#12771) *priv attribute removal (cf rust-lang/rust@f2a5c7a) *Replace Freeze by Share (cf rust-lang/rust@12ecafb)
Applied fixes: *attribute fix (cf rust-lang/rust#2569) *crate extra was removed *priv attribute removal (cf rust-lang/rust@f2a5c7a) *update the channel constructor (cf rust-lang/rust@7858065)
Applied fixes: *attribute fix (cf rust-lang/rust#2569) *New vec library (cf rust-lang/rust#12771) *priv attribute removal (cf rust-lang/rust@f2a5c7a)
Applied fixes: *attribute fix (cf rust-lang/rust#2569) *priv attribute removal (cf rust-lang/rust@f2a5c7a)
As discussed in #2568, the trailing semicolon to differentiate 'inner' from 'outer' attributes is not intuitive. Also, #2498 will add doc comments as a special form of attribute, which will differentiate 'inner' doc comments using doxygen's
/**< */
syntax. To make inner attribute syntax more consistent with doc comments and more visually distinct I would like to change the inner attribute syntax to:#<attribute>
The meaning would still be the same - it's an inner attribute - but in practice you would probably think of them as 'crate attributes' or 'module attributes' since they typically would appear at the top of files and apply to whatever the file represents.
No attribute is followed by a semicolon.
Added clarification for people visiting this bug in the future (e.g. for triage), mostly to explain the "inner" vs "outer" distinction, because that terminology is not used in the Rust manual: Currently attributes in Rust can either be terminated by a semi-colon, or they can leave out the semi-colon; the former denotes an "inner attribute", which applies to the (entire) entity that the attribute is declared within, and the latter denotes an "outer attribute", which must be followed by an entity (and applies solely to that entity).
This bug is discussing a number of alternatives to that syntax. Lets see if we can enumerate the proposals and how much interest they have garnered. (Most of the proposals keep the existing outer attribute syntax unchanged, but there is one exception, so I include it in my examples below.)
mod foo { #[outer(attr)] mod bar { #[inner(attr)]; } }
mod foo { #[outer(attr)] mod bar { #<inner(attr)> } }
mod foo { #[outer(attr)] mod bar { #![inner(attr)] } }
mod foo { #[outer(attr)] mod bar { #[!inner(attr)] } }
mod foo { #outer(attr) mod bar { #! inner(attr) } }
mod foo { #outer(attr) mod bar { #[inner(attr)] } }
mod foo { #[outer(attr)] mod bar { #inner(attr); } }
Note also of course that we have indeed now switched to denoting macros with a trailing
!
as discussed in the comments, which paves the way for the bracket-less forms.The text was updated successfully, but these errors were encountered: