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

Attributes: Redundant syntax, unnecessary complication #31001

Closed
prasannavl opened this issue Jan 18, 2016 · 6 comments
Closed

Attributes: Redundant syntax, unnecessary complication #31001

prasannavl opened this issue Jan 18, 2016 · 6 comments
Labels
rust-2-breakage-wishlist In the wishlist of breaking changes that requires rust 2.0 T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@prasannavl
Copy link

Well, I'm quite new to rust, so its entirely possible I'm missing something important. But I'm curious to know why the team chose to have two different type of attributes, and comments as well.

One is of the form #[attr], while the other is #![attr].

The former is straightforward, but the latter is for the item that its enclosing.

Taking the example right out of the docs

#[foo]
struct Foo;

mod bar {
    #![bar]
}

Is there a necessity for this redundant complication? I see no reason why the same #[foo] can't be applied right outside the module.

i.e,

#[foo]
mod bar {
    ...
}

Now, foo will apply into the scope. From what I can surmise, the only reason I can come up is, for scenarios like these:

let x = |x| {
 #[foo for this block]
}

But this can also be quite easily achieved with the compiler, this way,

let x =  #[foo] |x| {
 ...
}

Similar to how attributes are handled in C# for example. I see it from the perspective that a little more complication in the compiler to be able to resolve these would definitely be better than complicating the whole language by adding a language syntax that seems entirely redundant. And when it comes to readability, the one is actually quite better than a whole new syntax for achieving the same thing, because the intent is more consistent.

Same goes for comments, for the module as well. Can't this just be simplified? Rust already has a complicated enough learning curve, and these seem completely unnecessary. I'm hoping that I completely missed something, and I'm wrong :) .. or this is not the most stellar design choice that should be rectified as soon as possible while the rust ecosystem is still in its infancy.

@durka
Copy link
Contributor

durka commented Jan 18, 2016

The #![] form is used almost exclusively for crate attributes and module documentation. For the example defining a mod inside a file, it is indeed redundant. But when you have mod foo; in one file and its implementation in foo.rs, it is much more convenient to put the module documentation in foo.rs, where you use //! comments (which is the same as #![doc="comments"]). Furthermore, there is no file "outside of the crate" where you could put crate-level attributes such as #![crate_type = "lib"] or #![no_std] or #![feature(box_syntax)], so inner attributes are the only choice for things like that.

@mitaa
Copy link
Contributor

mitaa commented Jan 18, 2016

A similar suggestion has already been made here regarding doc-comments.

To attach attributes to the local crate #![attr] is needed.

@brson brson added the rust-2-breakage-wishlist In the wishlist of breaking changes that requires rust 2.0 label Jan 20, 2016
@brson
Copy link
Contributor

brson commented Jan 20, 2016

These syntax forms are all stable, and while their design has drawbacks, we can't remove any of them. I believe the previous responses have answered your questions about why they are like they are.

I did tag this as something to consider for Rust 2.0.

@brson brson closed this as completed Jan 20, 2016
@prasannavl
Copy link
Author

Thanks! Guess that makes sense.

@bstrie
Copy link
Contributor

bstrie commented Apr 3, 2016

The real redundancy wrt attributes is that we allow both foo(bar) and foo=bar forms. If we were going to deprecate anything, I'd suggest the latter.

@audreyality
Copy link

@mitaa

To attach attributes to the local crate #![attr] is needed.

Now that crate is used for visibility and paths in mod, perhaps there's a way it could be used for crate-level attributes as well? (Similar to .Net's [assembly:Attribute] syntax.)

@dtolnay dtolnay added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rust-2-breakage-wishlist In the wishlist of breaking changes that requires rust 2.0 T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants