-
Notifications
You must be signed in to change notification settings - Fork 789
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
Don't emit IsReadOnlyAttribute if not available. #14276
Conversation
@nojaf I don't think this is the right way to go. Roslyn instead synthesizes an internal attribute if the tfm does not have it. This allows compilers to still apply the same optimizations for apps that have dependencies compiled for high compatibility tfms like ns2.0 |
I don't think we do that. We usually check for runtime presence instead. |
Sure you'd only add the internal attribute if the presence check fails. I don't see why a ns2.0 library should have its consumers lose out on perf benefits just because its tfm lacks the attributes to convey the actual applicable semantics. In the end it's just my opinion, do with the knowledge what you wish, but I at least wanted to mention the approach roslyn takes here. |
@dsyme @KevinRansom wdyt about the codegen missing attributes here? |
To add a bit of extra context, Roslyn does this only in specific cases. It doesn't do so for features that are explicitly not supported on older tfms like non nullable reference types. There you have to manually add the attributes if you turn on the feature and use it, as it will produce a compiler error otherwise. C# 11 |
Ok, I vote we merge this one as is, since it's breaking a bunch of things, and have a follow-up item for generating a stub for it. @NinoFloris @dotnet/fsharp-team-msft what do you say? |
Sounds good to me! |
/backport to release/dev17.4 |
Started backporting to release/dev17.4: https://github.com/dotnet/fsharp/actions/runs/3430274253 |
We cannot upgrade to .NET 7 because of emitted IsReadOnlyAttribute in netstandard2.0 projects, tried with latest VS preview containing SDK 7.0.200-preview.22628.1 and 7.0.300-preview.23106.2 The funny thing is: the attribute is only emitted when the package System.Collections.Immutable is referenced inside the project. An updated version of @nojaf repro having the System.Collections.Immutable package referenced https://github.com/SamuelBerger/serialize-struct @vzarytovskii is this fixed included in 7.0.200-preview.22628.1 and/or 7.0.300-preview.23106.2? |
Hm, this should be included in the 7.0.200 release and all previews release after Nov 14, 2022. |
But can you reproduce the issue? (System.TypeLoadException : Could not load type 'System.Runtime.CompilerServices.IsReadOnlyAttribute' from assembly 'System.Collections.Immutable, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') Or is it a weirdness on my side? |
A workaround would be to have it in your project (create a namespace S.R.CS with IsReadOnlyAttribute inside). |
@0101 fyi, I will be reverting it alltogether |
When adding:
to the project containing the struct, it still picks up the one from System.Collections.Immutable:
But never mind, we will simply wait with the .NET 7 upgrade. |
Ugh, that's unexpected, it probably starts searching from the "top" and references are going first. |
Fixes #14275
I'm not sure how to add a test for this fix.
I believe IsReadOnlyAttribute is available for both
net472
andnet6.0
. Only when targetingnetstandard2.0
or lower can you come across the situation where it is not present.I'm not sure how to add a test for that exact condition.
I was able to compile my [repro]
(https://github.com/nojaf/serialize-struct) with the local compiler and the test now passes.