-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Ref assemblies should not drop [Pure] attribute from APIs #28488
Comments
Do the reference assemblies produced by the C# compiler include these types of attributes. CC. @jaredpar? |
@tannergooding I just confirmed that csc does emit But the |
Can we get that changed? I don't see any reason to strip away the PureAttribute without warning the developer that they need to add a hidden compiler flag. That is just going to lead to confusion. |
Yes we do keep (as @AArnott confirmed). Consider that if we did not then it would significantly hamper analyzers. Any analyzer based on attributes would essentially be un-usable in the face of reference assemblies. |
Given that the C# ref assembly feature does preserve the attributes (where applicable), this should probably be moved to dotnet/arcade so we can update I think the removal of CC. @ericstj, @ViktorHofer |
Is it Or we could resolve this issue by removing the |
The ref assemblies in CoreFX are special and are not generated by the C# compiler, instead they are (outside a couple exceptions) generated by some special tooling in the arcade repo. The root issue here is that the tooling in arcade is doing something different than the C# compiler (specifically, it isn't counting attributes as part of the public API). There have been other cases of this in the past, and we have had to fix those cases as well (such as counting private fields of structs as part of this public API). So, ultimately we likely need two (possibly three issues):
|
That is not true. We use tools to generate source code and that source code is checked in and directly compiled by CSC. Did you mean GenAPI? GenAPI will persist the pure attribute if it exists. That's controlled by an exclusion list and PureAttribute isn't in it. https://github.com/dotnet/corefx/blob/master/eng/DefaultGenApiDocIds.txt If the PureAttribute is present in the source you can run GenAPI and it will add it to the checked in source file. If the PureAttribute is in the checked in source file it will be preserved in the compiled reference assembly. |
I assume you mean after the |
@AArnott you are correct. I was missing that aspect of this issue. PureAttribute would need to be persisted into the assembly that GenAPI reads, so you'd need to build with CONTRACTS_FULL once before running GenAPI. |
I meant that it isn't generated by the C# ref assembly feature (which is how most people should be generating reference assemblies). |
This worked for me:
|
@ericstj what should we do here? I know that we made changes to GenAPI and our other tools, so this might already be fixed? |
@ViktorHofer There is still no |
Yeah, this is not fixed. I think we should just consider adding CONTRACTS_FULL as a default define in the dotnet/runtime build then updating the reference assemblies that are impacted. Any objections? |
So Immutable.Collections is the only assembly that actually needs this, so I'll just constrain the Define to that assembly. |
@ericstj If no other assembly has |
I'm not going to debate on the value of Contracts here.
Yes. The only other place its used is internally in System.Runtime.WindowsRuntime, so it has no impact on that reference assembly.
Yes, and this is actually consistent with what you'd need to do in a customer project if you wanted to ship the usage of the Pure attribute. If a create a new project with "[Pure]" on APIs and compile normally the attribute doesn't perist in the assembly. If I add a define of CONTRACTS_FULL then it does. |
Per discussion we're actually planning to remove the Pure attribute usage: #35118 (comment) |
@ericstj, should this be closed now, or is it still tracking work? |
Triage: @ericstj will take a look |
We agreed to stop using Pure and removed its usage from the codebase. We could now remove the exception from our attribute lists; @Anipik feel free to clean that up. Closing this as the OP is addressed |
The Pure attribute is checked by Rule CA1806 in Microsoft.CodeQuality.Analyzers. The
[Pure]
attribute helps prevent many bugs, particular around APIs such as the immutable collections where folks frequently make such mistakes as this:If
ImmutableList<T>.Add(T)
had the[Pure]
attribute on it, the CA1806 analyzer mentioned above would produce an error in the above snippet, thereby helping the developer avoid a bug.In fact the
Add
method does have a[Pure]
attribute on it:https://github.com/dotnet/corefx/blob/abc7e38a7a6bb0eed6a972127438685d7e9cbc98/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableList_1.cs#L222-L223
But the ref assembly drops these attributes, rendering all the effort we went to in tagging these pure methods to no effect.
In the below example, the call to
ImmutableList<int>.Add
does not produce a warning, but the next call to the sample's ownAdd
method does produce a warning, because the[Pure]
attribute is actually there.Can whatever build process that generates these ref assemblies be fixed to preserve this attribute?
Originally posted by @AArnott in dotnet/corefx#31071
The text was updated successfully, but these errors were encountered: