Relax restrictions on attribute arguments if the attribute is used only by the compiler and analyzers #8429
Unanswered
dlebansais
asked this question in
Language Ideas
Replies: 1 comment
-
Think the best path forward for this is revisiting ideas like source only attributes. Essentially attributes that are only visible in source by the compiler and are never emitted to metadata. That's come up a few times ask an ask by source generator authors. If that were a feature then we could consider expanding the set of types used by them. That is unlikely to be a small ask though. There are likely lots of places that have an assumption about what types are allowed here. Would likely need a strong motivating example. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Relevant existing discussions:
#1452
https://github.com/dotnet/roslyn/issues/6671
Currently attribute arguments are restricted to a subset of types (basically integral, strings and arrays) as explained in this section of the documentation on attributes.
My understanding is that the CLR is limited to certain types, and so the restriction is enforced on C# attributes.
It seems to me that for attributes only used by the compiler and analyzers, if it makes sense to not put them in the resulting binary, then the restriction on types could be lifted.
Is that even possible? I do not know. Perhaps people who know more about the various steps involved in analysis and compilation can tell me more.
Is there a use case? Well, there is definitely one for me. I wrote a few analyzers, and one of them uses several attributes to tag methods and generate code. Arguments that really should be C# keywords are written as strings, then the analyzer parses the strings to do its work. I don't want to elaborate more, because my guess is that this has already been discussed somewhere (sorry, after a search all I could find are the two references above), and nobody was sufficiently interested to make a proposal for it or give consideration to the idea.
Or maybe it's just not possible, or doesn't make sense.
Beta Was this translation helpful? Give feedback.
All reactions