Skip to content

Commit 06e5008

Browse files
authored
Merge pull request #108 from teo-tsirpanis/net9
Add polyfills for .NET 9 attributes.
2 parents d519f71 + 81cedeb commit 06e5008

13 files changed

+234
-13
lines changed

.github/CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Thank you for your interest in contributing to **PolySharp**! Below you'll find some info on how to get started with the project.
44

5-
> **NOTE:** it is highly recommended to carfully read the [README](/README.md) first to get a general understanding of the library.
5+
> **NOTE:** it is highly recommended to carefully read the [README](/README.md) first to get a general understanding of the library.
66
77
## 🙋 Questions, bug reports, feature proposals
88

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
33
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4-
<LangVersion>12.0</LangVersion>
4+
<LangVersion>13.0</LangVersion>
55
<Nullable>enable</Nullable>
66
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
77

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
# TLDR? What is this for? ✨
1010

11-
Put simply: are you working on .NET Framework, or UWP, or some other older .NET runtime and still would like to use all the cool new features that C# 12 has? Well this library lets you do just that! It will generate for you all the "magic types" that the C# compiler needs to "see" in order for it to allow using new language features even if you're not using the latest framework out there.
11+
Put simply: are you working on .NET Framework, or UWP, or some other older .NET runtime and still would like to use all the cool new features that C# 13 has? Well this library lets you do just that! It will generate for you all the "magic types" that the C# compiler needs to "see" in order for it to allow using new language features even if you're not using the latest framework out there.
1212

1313
Here's an example of some of the new features that **PolySharp** can enable downlevel:
1414

@@ -52,8 +52,10 @@ Here's an example of some of the new features that **PolySharp** can enable down
5252
- `[RequiresLocation]` (needed to enable [ref readonly parameters](https://github.com/dotnet/csharplang/issues/6010))
5353
- `[CollectionBuilder]` (needed for [collection expressions](https://github.com/dotnet/csharplang/issues/5354))
5454
- `[Experimental]` (needed for [experimental features](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/experimental-attribute))
55+
- `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority))
56+
- `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections))
5557

56-
To leverage them, make sure to bump your C# language version. You can do this by setting the `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>12.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` 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.
58+
To leverage them, make sure to bump your C# language version. You can do this by setting the `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>13.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` 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.
5759

5860
It also includes the following optional runtime-supported polyfills:
5961
- Reflection annotation attributes (see [docs](https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming)):
@@ -64,7 +66,7 @@ It also includes the following optional runtime-supported polyfills:
6466
- `[UnconditionalSuppressMessage]`
6567
- `[RequiresAssemblyFiles]`
6668
- `[StackTraceHidden]` (see [here](https://makolyte.com/csharp-exclude-exception-throw-helper-methods-from-the-stack-trace/))
67-
- `[UnmanagedCallersOnly]` (see [docs](https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute)))
69+
- `[UnmanagedCallersOnly]` (see [docs](https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute))
6870
- Platform support annotation attributes (see [docs](https://learn.microsoft.com/dotnet/standard/analyzers/platform-compat-analyzer)):
6971
- `[ObsoletedOSPlatform]`
7072
- `[SupportedOSPlatform]`
@@ -76,6 +78,11 @@ It also includes the following optional runtime-supported polyfills:
7678
- `[DisableRuntimeMarshalling]` (see [here](https://learn.microsoft.com/dotnet/standard/native-interop/disabled-marshalling))
7779
- `[UnsafeAccessor]` (see [here](https://github.com/dotnet/runtime/issues/81741))
7880
- `[InlineArray]` (see [here](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-12.0/inline-arrays))
81+
- `[DisableUserUnhandledExceptions]` (see [here](https://github.com/dotnet/runtime/issues/103105))
82+
- Attribute model for feature switches with trimming support (see [docs](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-9/runtime#attribute-model-for-feature-switches-with-trimming-support)):
83+
- `[FeatureGuard]`
84+
- `[FeatureSwitchDefinition]`
85+
- `[WasmImportLinkage]` (see [here](https://github.com/dotnet/runtime/pull/93823))
7986

8087
# Options ⚙️
8188

global.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"sdk": {
3-
"version": "8.0.100",
3+
"version": "9.0.100",
44
"rollForward": "latestFeature",
55
"allowPrerelease": false
66
}
7-
}
7+
}

src/PolySharp.Package/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# TLDR? What is this for? ✨
88

9-
Put simply: are you working on .NET Framework, or UWP, or some other older .NET runtime and still would like to use all the cool new features that C# 12 has? Well this library lets you do just that! It will generate for you all the "magic types" that the C# compiler needs to "see" in order for it to allow using new language features even if you're not using the latest framework out there.
9+
Put simply: are you working on .NET Framework, or UWP, or some other older .NET runtime and still would like to use all the cool new features that C# 13 has? Well this library lets you do just that! It will generate for you all the "magic types" that the C# compiler needs to "see" in order for it to allow using new language features even if you're not using the latest framework out there.
1010

1111
Here's an example of some of the new features that **PolySharp** can enable downlevel:
1212

@@ -50,8 +50,10 @@ Here's an example of some of the new features that **PolySharp** can enable down
5050
- `[RequiresLocation]` (needed to enable [ref readonly parameters](https://github.com/dotnet/csharplang/issues/6010))
5151
- `[CollectionBuilder]` (needed for [collection expressions](https://github.com/dotnet/csharplang/issues/5354))
5252
- `[Experimental]` (needed for [experimental features](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/experimental-attribute))
53+
- `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority))
54+
- `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections))
5355

54-
To leverage them, make sure to bump your C# language version. You can do this by setting the `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>12.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` 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.
56+
To leverage them, make sure to bump your C# language version. You can do this by setting the `<LangVersion>` MSBuild property in your project. For instance, by adding `<LangVersion>13.0</LangVersion>` (or your desired C# version) to the first `<PropertyGroup>` 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.
5557

5658
It also includes the following optional runtime-supported polyfills:
5759
- Reflection annotation attributes (see [docs](https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming)):
@@ -62,7 +64,7 @@ It also includes the following optional runtime-supported polyfills:
6264
- `[UnconditionalSuppressMessage]`
6365
- `[RequiresAssemblyFiles]`
6466
- `[StackTraceHidden]` (see [here](https://makolyte.com/csharp-exclude-exception-throw-helper-methods-from-the-stack-trace/))
65-
- `[UnmanagedCallersOnly]` (see [docs](https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute)))
67+
- `[UnmanagedCallersOnly]` (see [docs](https://learn.microsoft.com/dotnet/api/system.runtime.interopservices.unmanagedcallersonlyattribute))
6668
- Platform support annotation attributes (see [docs](https://learn.microsoft.com/dotnet/standard/analyzers/platform-compat-analyzer)):
6769
- `[ObsoletedOSPlatform]`
6870
- `[SupportedOSPlatform]`
@@ -74,6 +76,11 @@ It also includes the following optional runtime-supported polyfills:
7476
- `[DisableRuntimeMarshalling]` (see [here](https://learn.microsoft.com/dotnet/standard/native-interop/disabled-marshalling))
7577
- `[UnsafeAccessor]` (see [here](https://github.com/dotnet/runtime/issues/81741))
7678
- `[InlineArray]` (see [here](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-12.0/inline-arrays))
79+
- `[DisableUserUnhandledExceptions]` (see [here](https://github.com/dotnet/runtime/issues/103105))
80+
- Attribute model for feature switches with trimming support (see [docs](https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-9/runtime#attribute-model-for-feature-switches-with-trimming-support)):
81+
- `[FeatureGuard]`
82+
- `[FeatureSwitchDefinition]`
83+
- `[WasmImportLinkage]` (see [here](https://github.com/dotnet/runtime/pull/93823))
7784

7885
# Options ⚙️
7986

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// <auto-generated/>
2+
#pragma warning disable
3+
#nullable enable annotations
4+
5+
// Licensed to the .NET Foundation under one or more agreements.
6+
// The .NET Foundation licenses this file to you under the MIT license.
7+
8+
namespace System.Diagnostics.CodeAnalysis
9+
{
10+
/// <summary>
11+
/// Indicates that the specified public static boolean get-only property
12+
/// guards access to the specified feature.
13+
/// </summary>
14+
/// <remarks>
15+
/// Analyzers can use this to prevent warnings on calls to code that is
16+
/// annotated as requiring that feature, when the callsite is guarded by a
17+
/// call to the property.
18+
/// </remarks>
19+
[global::System.AttributeUsage(global::System.AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
20+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
21+
[global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")]
22+
internal sealed class FeatureGuardAttribute : global::System.Attribute
23+
{
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="global::System.Diagnostics.CodeAnalysis.FeatureGuardAttribute"/> class
26+
/// with the specified feature type.
27+
/// </summary>
28+
/// <param name="featureType">
29+
/// The type that represents the feature guarded by the property.
30+
/// </param>
31+
public FeatureGuardAttribute(global::System.Type featureType)
32+
{
33+
FeatureType = featureType;
34+
}
35+
36+
/// <summary>
37+
/// The type that represents the feature guarded by the property.
38+
/// </summary>
39+
public global::System.Type FeatureType { get; }
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// <auto-generated/>
2+
#pragma warning disable
3+
#nullable enable annotations
4+
5+
// Licensed to the .NET Foundation under one or more agreements.
6+
// The .NET Foundation licenses this file to you under the MIT license.
7+
8+
namespace System.Diagnostics.CodeAnalysis
9+
{
10+
/// <summary>
11+
/// Indicates that the specified public static boolean get-only property
12+
/// corresponds to the feature switch specified by name.
13+
/// </summary>
14+
/// <remarks>
15+
/// IL rewriters and compilers can use this to substitute the return value
16+
/// of the specified property with the value of the feature switch.
17+
/// </remarks>
18+
[global::System.AttributeUsage(global::System.AttributeTargets.Property, Inherited = false)]
19+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
20+
[global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")]
21+
internal sealed class FeatureSwitchDefinitionAttribute : global::System.Attribute
22+
{
23+
/// <summary>
24+
/// Initializes a new instance of the <see cref="global::System.Diagnostics.CodeAnalysis.FeatureSwitchDefinitionAttribute"/> class
25+
/// with the specified feature switch name.
26+
/// </summary>
27+
/// <param name="switchName">
28+
/// The name of the feature switch that provides the value for the specified property.
29+
/// </param>
30+
public FeatureSwitchDefinitionAttribute(string switchName)
31+
{
32+
SwitchName = switchName;
33+
}
34+
35+
/// <summary>
36+
/// The name of the feature switch that provides the value for the specified property.
37+
/// </summary>
38+
public string SwitchName { get; }
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// <auto-generated/>
2+
#pragma warning disable
3+
#nullable enable annotations
4+
5+
// Licensed to the .NET Foundation under one or more agreements.
6+
// The .NET Foundation licenses this file to you under the MIT license.
7+
8+
namespace System.Diagnostics
9+
{
10+
/// <summary>
11+
/// If a .NET Debugger is attached which supports the Debugger.BreakForUserUnhandledException(Exception) API,
12+
/// this attribute will prevent the debugger from breaking on user-unhandled exceptions when the
13+
/// exception is caught by a method with this attribute, unless BreakForUserUnhandledException is called.
14+
/// </summary>
15+
[global::System.AttributeUsage(global::System.AttributeTargets.Method)]
16+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
17+
[global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")]
18+
internal sealed class DebuggerDisableUserUnhandledExceptionsAttribute : global::System.Attribute
19+
{
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// <auto-generated/>
2+
#pragma warning disable
3+
#nullable enable annotations
4+
5+
// Licensed to the .NET Foundation under one or more agreements.
6+
// The .NET Foundation licenses this file to you under the MIT license.
7+
8+
namespace System.Runtime.InteropServices
9+
{
10+
/// <summary>
11+
/// Specifies that the P/Invoke marked with this attribute should be linked in as a WASM import.
12+
/// </summary>
13+
/// <remarks>
14+
/// See https://webassembly.github.io/spec/core/syntax/modules.html#imports.
15+
/// </remarks>
16+
[global::System.AttributeUsage(global::System.AttributeTargets.Method, Inherited = false)]
17+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
18+
[global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")]
19+
internal sealed class WasmImportLinkageAttribute : global::System.Attribute
20+
{
21+
/// <summary>
22+
/// Instance constructor.
23+
/// </summary>
24+
public WasmImportLinkageAttribute() { }
25+
}
26+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// <auto-generated/>
2+
#pragma warning disable
3+
#nullable enable annotations
4+
5+
// Licensed to the .NET Foundation under one or more agreements.
6+
// The .NET Foundation licenses this file to you under the MIT license.
7+
8+
namespace System.Runtime.CompilerServices
9+
{
10+
/// <summary>
11+
/// Specifies the priority of a member in overload resolution. When unspecified, the default priority is 0.
12+
/// </summary>
13+
[global::System.AttributeUsage(
14+
global::System.AttributeTargets.Method |
15+
global::System.AttributeTargets.Constructor |
16+
global::System.AttributeTargets.Property,
17+
AllowMultiple = false,
18+
Inherited = false)]
19+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
20+
internal sealed class OverloadResolutionPriorityAttribute : global::System.Attribute
21+
{
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="global::System.Runtime.CompilerServices.OverloadResolutionPriorityAttribute"/> class.
24+
/// </summary>
25+
/// <param name="priority">The priority of the attributed member. Higher numbers are prioritized, lower numbers are deprioritized. 0 is the default if no attribute is present.</param>
26+
public OverloadResolutionPriorityAttribute(int priority)
27+
{
28+
Priority = priority;
29+
}
30+
31+
/// <summary>
32+
/// The priority of the member.
33+
/// </summary>
34+
public int Priority { get; }
35+
}
36+
}

0 commit comments

Comments
 (0)