Discussion: source-only attributes #1452
Replies: 3 comments
-
I am a big fan of this proposal, especially since source-only attributes should allow us to write attributes in places where none was permitted before. I'm thinking that being able to 'tag' local variables etc with attributes will be incredible helpful for those of us writing Roslyn analyzers. |
Beta Was this translation helpful? Give feedback.
-
Why not be more flexible, and just allow these attributes anywhere where trivia is allowed? in essence they would become a new form of structured trivia as far as the lexer/parser was concerned. |
Beta Was this translation helpful? Give feedback.
-
Your two grammar rules have an ambigiuty that would need to be resolved. Specifically: [[Blah]]
Foo(); Is that a source attribute on the primary invocation-expression? Or is it on the expression statement? |
Beta Was this translation helpful? Give feedback.
-
https://github.com/dotnet/roslyn/issues/6671 was never moved to csharplang and the discussion seems to have died down but I think this could be a great addition to the language and has known use cases (in analyzers, source rewriters, etc).
The original proposal suggests a new syntax for source-only attributes,
Perhaps it might not be needed because
[ConditionAttribute("id")]
on attributes would do the exact same thing.However, you may not be able to do this to external attributes because that should be done at the declaration-site (which IMO doesn't limit the feature, maybe those attributes are intended to be emitted).
As an alternative to a new syntax, we could permit types that do not inherit from
Attribute
but not emit to the IL.On the expression and statement level, attributes can never be emitted to the IL so I guess we don't even need a special syntax here (if not ambiguous).
Attributes on expressions could be defined with a syntax similar to the cast-expression:
And attributes on statements could be defined with a syntax similar to the labeled-statement:
We can have a
SourceAttributeUsageAttribute
to specify specific targets to be permitted.As noted in the original thread, we probably want this as structured trivia to not interfere the actual ast with noop nodes.
This could cover local functions and possibly lambdas as well but we may want attributes to be a part of the declaration itself and allow other syntactic forms which are not permitted with above syntax.
Plus, in those two cases it actually makes sense to emit the attribute and make it available at runtime.
Relates to https://github.com/dotnet/csharplang/blob/master/proposals/lambda-attributes.md
Beta Was this translation helpful? Give feedback.
All reactions