-
Notifications
You must be signed in to change notification settings - Fork 1k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Proposal: Attributes everywhere #2478
Comments
So really these are just a fancy form of a comment that gets some type checking. It looks like they should be permitted anywhere |
The whole contents would be transformed into a syntax tree and get the same level of checking as any other syntax tree.
That's what I was suggesting: |
I meant a new kind of trivia. If it derives from If you have trivia referencing |
Ah, yeah, that's what I had in mind.
Yes, it would. Similar to how refactoring code today needs to look inside XML comment trivia when doing renames. |
@stephentoub |
Same idea. Instead of |
@stephentoub |
|
VB already has this feature, its the tick-bracket token. Try it... '[ attribute ]' It already works! |
@mattwar, cute 😉 In any event, I appreciate the enthusiasm around choosing syntax, but I'm not particularly concerned with the exact choice of tokens, in either language. I'm confident that if we like the general idea and decided to implement it that we could find reasonable characters to use. |
I bet @KathleenDollard has something to say about this proposal 😄 |
I like the idea, but if it's not an attribute let's find some other name and preferably a quite different syntax from attributes. What about ambiguities? How do we know that the "attribute" in the following cases applies to the lambda instead of to parameter [[NoCapture]] i => i * this.Value;
[[Allocs.AllowCapture(this)]] i => i * this.Value; |
@bondsbw |
@alrz That is still questionable, what if |
@bondsbw |
Why can't it be a type of usage of the attribute? Or another property, like I guess you're trying to keep this a language-only feature without any dependencies from CLR changes. |
The intent is that you can use these in places where you can't even represent attributes in IL, and as a result you can do things with them that you can't do with attributes.
Yes.
I personally very much like the [[...]] syntax, but I'm of course open to other suggestions... my main goal is the functionality, and if it's exposed in a different way, that's fine. As for name, again, that was just a moniker I threw on it as I thought it helped to introduce the concept. If it's confusing, it can of course be called something else, for example "expression comments". |
Smooth. |
To state clearly some reasons between the lines for this...
Also: The known spaces (again between the lines in the description above:
I am most interested in applicability to Analyzers and Code Expansion/generation. The code expansion space is remarkably broad, including things like declaring validation lambdas in place and letting your templates/expanders determine how to code that up. And I think it will make it easier to uptake both Semantic Logging/EventSource and the new Diagnostic Source. Kathleen |
One possible elegant usage would be hints to JIT compiler [[Unlikely]] //branch prediction hint, also [[Likely]]
if(x < 0)
throw new ArgumentOutOfRangeException(nameof(x));
else
{
//following code
} var p = [[NoInline]] Foo(x); //inlining hint, also [[Inline]] [[Hot]] //spend more time here on code optimization - this function will run a lot
void Foo()
{
//complicated code to follow
} |
@ghord True. Unfortunately, the JIT team does not seem to agree that it's optimizer will always lack the information to make certain choices. It took 4 .NET versions to get the Another important hint would be to specify that code is hot. This would raise the inliner limit, cause loops to be unrolled and trigger all other optimizations that are not usually done for code size concerns. The JIT can't statically know what code is hot and what code is cold. Therefore, hints are helpful. |
@ghord I think the intention is that this data is lost after compilation, so it couldn't work for JIT hinting. |
Just an idea but C# uses the at (@) symbol to allow us to name things that are otherwise reserved maybe we can use this idea and use the exclamation mark (!) to hint the compiler that this isn't going to emit anything? or it absolutely needs to be verbose? :) What I mean is something like this:
Or/and this:
The idea is that they blend in to the code like normal attributes. |
While I'm in agreement that this addresses many shortcomings such as performance issues with JIT by providing "hints" (but I still fear that "hints" are workarounds for improper characteristics of the JIT), I hope that it doesn't make C# become Attribute-Driven Language where code is littered with Attributes. I disagree using Attributes where actual coding can do the job since some attributes may ever live as "hints" which is non-deterministic. |
If I understood your comment about |
So why relate this to "attributes"? It could be a structured comment, e.g. markdown syntax works just fine, // `SomeAttribute`
class X Aside: I think there was also a proposal to support markdown for doc comments. Alternatively, some comments could have meaning (heh). {
// no capture
object F() => null;
} This is already used in, for example, R# to disable code fixes etc. I think the real problem here is associating comments to syntax nodes and a better API around it. Perhaps we could use triple-slash or {
/// no capture
object F() => null;
} AFAIK we already have this for doc comments but the API is not optimized for these use cases. |
@alrz The only issue with comments is it doesn't matter what style you would use to annotate the code it's one that might be used today by some people and this might have some implications in particular IDE experience:
R# provides an alternative way to annotate the code in the form of comments only because there's no other proper way to do it and unfortunately this is a hack not a solution. The only thing that really bothers me is the syntax it looks pretty verbose whereas comments seems terser. So how about having a new character for annotations something like Annotation as
Annotation as text:
The benefit of this is that a tool like R# can then offer specialized intellisense for available annotations when typing |
It's up the tool to treat a particular comment differently. You can't stop that. But the main issue that this proposal is addressing is that it associates these annotations to a particular syntax node. You may use
My point is that we have already a similar mechanism to do this but it just should be extended to work anywhere else and an API optimized for these use cases i.e. for analyzers to consume. So I'd call this "structured trivia everywhere" rather than attributes everywhere or the like. :) |
Don't get me wrong, I'm not against source-only attributes, i.e. attributes that do not emitted to the IL. I like the idea of |
Comments should be used to comment out code whereas xml comments are used for documentation purposes, I think it would be inappropriate to add yet another kind of comments that are considered as annotations or reusing doc comments to denote annotations at this time.
Vendors are abusing or exploiting comments today to enhance productivity because there's no other way to do it. I'd expect a productivity tool to give me an intellisense for xml comments when I'm using Today Visual Studio colour xml comment with grey this is the opposite of what some people might expect from annotations and they may want to choose a different colour for it. One more thing, Visual Studio automatically adds the xml summary element when you type Sorry for nagging about colours and intellisense but I think that it is a vital part to this proposal.
I think that annotations mean one thing and comments mean another thing therefor we need to treat them differently and the syntax for them should be different. If it was C# 1.0 then I'd agree that it was cool if they formalized annotations as part of comments the way they did with xml comments but it would be wrong to introduce annotations as comments today because you might end up with cases where comments are interpreted as annotations from a tooling perspective but not from the standpoint of developers.
Yes but like I've pointed out above I don't think that reusing the syntax for comments is the right approach to take here.
Indeed, I like it. 👍
I agree, I think that they need to stand out in the code like attributes and yet be as plain as comments. p.s. Completely forgot that Just as an example for how R# language injection might look like with this:
Alternatively:
|
There could be use cases for a start and end tag. Maybe something like this? [#Issue(123)]
// multiple methods which are related to this issue
[#/Issue] |
@eyalsk |
@alrz Yeah I generalized because you and other mentioned comments so I said okay, let's have anything in there and let the tool decide what it is but yeah either way |
@alrz Agreed, VB and C# should utilize their respective expression/attribute syntax, and the trivia be constructed such that semantically-equivalent tags look identical to an analyzer. |
Assuming these tags have such a well-defined language-based structure, perhaps they should also include analyzer-provided constraints (e.g. C#-only, can only be applied within a statement list, the parameter can only be a property name, etc.). This would imply that the end user cannot just make up ad hoc tags without writing the associated analyzer. |
I really like the notion of "structured trivia everywhere" as a way to think of the alternative to "attributes everywhere." It's a better description than "comments." I think it allows a better format for a relatively strict syntax. I believe there is a set of simple rules that allow syntax checking, colorization and IntelliSense, strong typing against the code and safe use of lambdas enforced by a simple (read that as fast) "structured trivia compiler" It would be most interesting if this could also provide a better experience for XML comments as a more-or-less-free tag-along features |
And more strict means more room to open up the syntax in a later version. Better that, than making it wide open today and wanting to restrict it later (breaking BC). |
@gafter Something to move to csharplang? |
This issue has been around for a while, but I wanted to remark that we would have a compelling use case for trimming. Right now you can annotate |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Background
Today, attributes have a few restrictions relevant to this conversation:
Proposal
[…]
syntax for attributes, I propose we introduce an additional[[…]]
syntax.[…]
remains exactly as it is today, whereas[[…]]
represents attribution that is source-only and will never be emitted to IL. Thus you can write:and the first attribute will end up in metadata whereas the latter won’t.
[…]
continues to be limited by AttributeUsage, whereas[[…]]
doesn’t.[[…]]
can be used anywhere in source where we can make it syntactically unambiguous (which should be most, if not all, locations). The purpose of this is to have it represented as a special kind of syntax node in a syntax tree available to analyzers, likely as another form of trivia.[[…]]
would also not be limited to types derived from Attribute. Arbitrary expressions could be used in the…
, with those expressions showing up as children of the special syntax node in the syntax tree. For example, you could do things like:or:
or:
The text was updated successfully, but these errors were encountered: