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

Class <>z__ReadOnlyArray<T> generated for literal collections is not tagged with CompilerGeneratedAttribute #72539

Closed
ndepend opened this issue Mar 14, 2024 · 11 comments · Fixed by #74683
Assignees
Labels
Area-Compilers Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Milestone

Comments

@ndepend
Copy link

ndepend commented Mar 14, 2024

Class <>z__ReadOnlyArray<T> generated for literal collections is not tagged with System.Runtime.CompilerServices.CompilerGeneratedAttribute. I think it should.

image

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 14, 2024
@jnm2
Copy link
Contributor

jnm2 commented Mar 15, 2024

@cston This wouldn't have spec implications, would it?

@cston
Copy link
Member

cston commented Mar 15, 2024

@jnm2, no spec implications come to mind. Was there some potential implication in particular that you were thinking of?

@jnm2
Copy link
Contributor

jnm2 commented Mar 15, 2024

@cston No, just asking in order to learn! Thanks. Could this be up-for-grabs then?

@jaredpar jaredpar added Bug and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Mar 19, 2024
@jaredpar jaredpar added this to the 17.10 milestone Mar 19, 2024
@jaredpar
Copy link
Member

@jnm2 will let @cston comment if he's okay with someone taking this over or not.

@cston
Copy link
Member

cston commented Mar 19, 2024

I'll mark the issue up for grabs.

For the implementation, it should be sufficient to override AddSynthesizedAttributes() in SynthesizedReadOnlyListTypeSymbol and add the attribute there. For existing examples in the codebase, search for TrySynthesizeAttribute(WellKnownMember.System_Runtime_CompilerServices_CompilerGeneratedAttribute__ctor).

I don't think we'll need to add CompilerGeneratedAttribute to any members within those generated types since the entire types will be marked as compiler generated. (For comparison, for anonymous types there is an attribute on the type, but not the members - see sharplab.io.)

@cston cston added the help wanted The issue is "up for grabs" - add a comment if you are interested in working on it label Mar 19, 2024
@waf
Copy link

waf commented Mar 22, 2024

It should possibly also be tagged with SerializableAttribute similar to List and Array.

@DoctorKrolic
Copy link
Contributor

It should possibly also be tagged with SerializableAttribute similar to List and Array.

[Serializable] is a legacy from the past. Compiler should not bloat IL with this attribute, as it is effectively useless

@FairPlayer4
Copy link

FairPlayer4 commented Jun 15, 2024

Found a small edge case where [Serializable] would be useful.
In ASP.NET (not Core) I used the collection expression with an IReadOnlyList{string} in a class that is stored in the session and got a serialization exception.
image
Easy fix was making the IReadOnlyList static.

@DoctorKrolic
Copy link
Contributor

In ASP.NET (not Core) I used the collection expression

Collection expressions are a C#12 feature. C#12 is officially supported on .NET 8+. Given that you use ASP.NET (legacy), you are on .NET Framework, therefore your case falls into unsupported territory. I understand, that migrating to modern .NET might be too expensive or in some other way impossible for your project, but that doesn't mean that we should poison new language features with all that many-years-old legacy attributes and so on. Starting with .NET 9 adding [Serializable] will have no impact other than a little bit of additional disk space needed to store the assembly

@jnm2
Copy link
Contributor

jnm2 commented Aug 8, 2024

@FairPlayer4 In cases where you can't make it static, but you don't want the minimal implementation that the compiler generates, you can also pick your own collection type with an explicit cast, such as IReadOnlyList<string> list = (string[])["..."].

@FairPlayer4
Copy link

Please don't change anything in the compiler, I actually wrote my comment so that the unlucky few who encounter this know why it's happening. More as documentation.

In my my opinion the behavior is correct and it should not be expected that an implementation of a Collection Interface has some attribute just because the most common Collection implementations have that attribute. Especially if the attribute is considered obsolete.

@jnm2 That's a good way to handle it, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug help wanted The issue is "up for grabs" - add a comment if you are interested in working on it
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants