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

Add a polyfill to enable newer compiler features #10315

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

jimmylewis
Copy link

New versions of C# offer many features that improve quality of life: nullable annotations, record types, init and required properties, etc. These can be enabled in projects targeting older framework versions by adding the right attributes and types to the compilation.

PolySharp is a source generator which automatically does this. For each project, it will generate all of the types/attributes which are missing. In the case of an InternalsVisibleTo reference, it is smart enough to only define types in the second assembly that are not being consumed from the first assembly (avoiding duplicate type definitions).

New versions of C# offer many features that improve quality of life:
nullable annotations, record types, init and required properties, etc.
These can be enabled in projects targeting older framework versions by
adding the right attributes and types to the compilation.

PolySharp is a source generator which automatically does this.  For each
project, it will generate all of the types/attributes which are missing.
In the case of an InternalsVisibleTo reference, it is smart enough to
only define types in the second assembly that are not being consumed
from the first assembly (avoiding duplicate type definitions).
@jimmylewis jimmylewis changed the base branch from main to dev January 7, 2025 19:30
@jimmylewis jimmylewis force-pushed the jamlew-compilerpolyfill branch from 3185208 to d6b5c7e Compare January 7, 2025 19:31
@jimmylewis jimmylewis marked this pull request as ready for review January 7, 2025 19:57
@jimmylewis jimmylewis requested a review from a team as a code owner January 7, 2025 19:57
@@ -19,6 +19,44 @@ public void ShouldOnlyHaveInterfacesAndEnums()
// included in the assembly by newer language versions
"Microsoft.CodeAnalysis.EmbeddedAttribute",
"System.Runtime.CompilerServices.RefSafetyRulesAttribute",
// generated polyfills from PolySharp
"System.Runtime.CompilerServices.NullableAttribute",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I wonder if we can filter out internal types for this test. This test appears to be a "public contract changes should be acknowledged by this test".

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could easily check only types where type.IsVisible is true

true if the current Type is a public type or a public nested type such that all the enclosing types are public; otherwise, false.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.

joelverhagen
joelverhagen previously approved these changes Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants