From 107789e7abb3837ddf9b12db16c7718b076fcb8d Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Thu, 27 Aug 2026 04:29:21 +0300 Subject: [PATCH 1/3] Copy .NET 11 attribute sources from `dotnet/runtime`. --- ...cs.CodeAnalysis.RequiresUnsafeAttribute.cs | 25 +++++++++++++++ ....CompilerServices.IsClosedTypeAttribute.cs | 31 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Diagnostics.CodeAnalysis.RequiresUnsafeAttribute.cs create mode 100644 src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Runtime.CompilerServices.IsClosedTypeAttribute.cs diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Diagnostics.CodeAnalysis.RequiresUnsafeAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Diagnostics.CodeAnalysis.RequiresUnsafeAttribute.cs new file mode 100644 index 0000000..1e7b707 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Diagnostics.CodeAnalysis.RequiresUnsafeAttribute.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Diagnostics.CodeAnalysis +{ + /// + /// Indicates that the specified member requires the caller to be in an unsafe context. + /// + [AttributeUsage( + AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Property, + Inherited = false, + AllowMultiple = false)] +#if SYSTEM_PRIVATE_CORELIB + public +#else + internal +#endif + sealed class RequiresUnsafeAttribute : Attribute + { + /// + /// Initializes a new instance of the class. + /// + public RequiresUnsafeAttribute() { } + } +} \ No newline at end of file diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Runtime.CompilerServices.IsClosedTypeAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Runtime.CompilerServices.IsClosedTypeAttribute.cs new file mode 100644 index 0000000..eb17320 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Runtime.CompilerServices.IsClosedTypeAttribute.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.ComponentModel; + +namespace System.Runtime.CompilerServices +{ + /// + /// Reserved for use by a compiler for tracking metadata. + /// This attribute should not be used by developers in source code. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + [AttributeUsage(AttributeTargets.Class, Inherited = false)] + public sealed class IsClosedTypeAttribute : Attribute + { + private Type[] _derivedTypes = Type.EmptyTypes; + + /// Initializes the attribute. + public IsClosedTypeAttribute() + { + } + + /// Gets or sets the derived types of the closed type. + /// An array of the derived types of the closed type. A value is normalized to an empty array. + public Type[] DerivedTypes + { + get => _derivedTypes; + set => _derivedTypes = value ?? Type.EmptyTypes; + } + } +} \ No newline at end of file From 78a029b8bf2c6e51b510dcbdd0e29e32a6c250fc Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Thu, 27 Aug 2026 04:34:00 +0300 Subject: [PATCH 2/3] Adapt the attributes to the codebase's standards. --- ...cs.CodeAnalysis.RequiresUnsafeAttribute.cs | 20 +++++++++++-------- ....CompilerServices.IsClosedTypeAttribute.cs | 20 +++++++++++-------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Diagnostics.CodeAnalysis.RequiresUnsafeAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Diagnostics.CodeAnalysis.RequiresUnsafeAttribute.cs index 1e7b707..f415f47 100644 --- a/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Diagnostics.CodeAnalysis.RequiresUnsafeAttribute.cs +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Diagnostics.CodeAnalysis.RequiresUnsafeAttribute.cs @@ -1,3 +1,7 @@ +// +#pragma warning disable +#nullable enable annotations + // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. @@ -6,16 +10,16 @@ namespace System.Diagnostics.CodeAnalysis /// /// Indicates that the specified member requires the caller to be in an unsafe context. /// - [AttributeUsage( - AttributeTargets.Constructor | AttributeTargets.Event | AttributeTargets.Method | AttributeTargets.Property, + [global::System.AttributeUsage( + global::System.AttributeTargets.Constructor | + global::System.AttributeTargets.Event | + global::System.AttributeTargets.Method | + global::System.AttributeTargets.Property, Inherited = false, AllowMultiple = false)] -#if SYSTEM_PRIVATE_CORELIB - public -#else - internal -#endif - sealed class RequiresUnsafeAttribute : Attribute + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + [global::Microsoft.CodeAnalysis.Embedded] + internal sealed class RequiresUnsafeAttribute : global::System.Attribute { /// /// Initializes a new instance of the class. diff --git a/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Runtime.CompilerServices.IsClosedTypeAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Runtime.CompilerServices.IsClosedTypeAttribute.cs index eb17320..954ab37 100644 --- a/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Runtime.CompilerServices.IsClosedTypeAttribute.cs +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/LanguageSupport/System.Runtime.CompilerServices.IsClosedTypeAttribute.cs @@ -1,19 +1,23 @@ +// +#pragma warning disable +#nullable enable annotations + // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -using System.ComponentModel; - namespace System.Runtime.CompilerServices { /// /// Reserved for use by a compiler for tracking metadata. /// This attribute should not be used by developers in source code. /// - [EditorBrowsable(EditorBrowsableState.Never)] - [AttributeUsage(AttributeTargets.Class, Inherited = false)] - public sealed class IsClosedTypeAttribute : Attribute + [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)] + [global::System.AttributeUsage(AttributeTargets.Class, Inherited = false)] + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + [global::Microsoft.CodeAnalysis.Embedded] + public sealed class IsClosedTypeAttribute : global::System.Attribute { - private Type[] _derivedTypes = Type.EmptyTypes; + private global::System.Type[] _derivedTypes = global::System.Type.EmptyTypes; /// Initializes the attribute. public IsClosedTypeAttribute() @@ -22,10 +26,10 @@ public IsClosedTypeAttribute() /// Gets or sets the derived types of the closed type. /// An array of the derived types of the closed type. A value is normalized to an empty array. - public Type[] DerivedTypes + public global::System.Type[] DerivedTypes { get => _derivedTypes; - set => _derivedTypes = value ?? Type.EmptyTypes; + set => _derivedTypes = value ?? global::System.Type.EmptyTypes; } } } \ No newline at end of file From 9763f1a42abd132facecd0d9ec2f23fbafa5f538 Mon Sep 17 00:00:00 2001 From: Theodore Tsirpanis Date: Thu, 27 Aug 2026 04:39:41 +0300 Subject: [PATCH 3/3] Update README.md files. --- README.md | 2 ++ src/PolySharp.Package/README.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/README.md b/README.md index 6f3dba4..2c3f034 100644 --- a/README.md +++ b/README.md @@ -57,6 +57,8 @@ Here's an example of some of the new features that **PolySharp** can enable down - `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771)) - `[CompilerLoweringPreserve]` (see [proposal](https://github.com/dotnet/runtime/issues/103430)) - `[ExtensionMarker]` (needed for [extensions](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-14#extension-members)) +- `[IsClosedType]` (needed for [closed hierarchies](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-15.0/closed-hierarchies)) +- `[RequiresUnsafe]` (needed for [unsafe evolution](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/unsafe-evolution)) To leverage them, make sure to bump your C# language version. You can do this by setting the `` MSBuild property in your project. For instance, by adding `14.0` (or your desired C# version) to the first `` of your .csproj file. For more info on this, [see here](https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb), but remember that you don't need to manually copy polyfills anymore: simply adding a reference to **PolySharp** will do this for you automatically. diff --git a/src/PolySharp.Package/README.md b/src/PolySharp.Package/README.md index 576713a..9e7a46f 100644 --- a/src/PolySharp.Package/README.md +++ b/src/PolySharp.Package/README.md @@ -55,6 +55,8 @@ Here's an example of some of the new features that **PolySharp** can enable down - `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771)) - `[CompilerLoweringPreserve]` (see [proposal](https://github.com/dotnet/runtime/issues/103430)) - `[ExtensionMarker]` (needed for [extensions](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-14#extension-members)) +- `[IsClosedType]` (needed for [closed hierarchies](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-15.0/closed-hierarchies)) +- `[RequiresUnsafe]` (needed for [unsafe evolution](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/unsafe-evolution)) To leverage them, make sure to bump your C# language version. You can do this by setting the `` MSBuild property in your project. For instance, by adding `14.0` (or your desired C# version) to the first `` of your .csproj file. For more info on this, [see here](https://sergiopedri.medium.com/enabling-and-using-c-9-features-on-older-and-unsupported-runtimes-ce384d8debb), but remember that you don't need to manually copy polyfills anymore: simply adding a reference to **PolySharp** will do this for you automatically.