Skip to content

Commit 2a6e35e

Browse files
authored
Merge pull request #81 from Sergio0694/dev/net8-polyfills
Add C# 12 and .NET 8 polyfills
2 parents 2689b7e + d171c2e commit 2a6e35e

24 files changed

+447
-35
lines changed

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>11.0</LangVersion>
4+
<LangVersion>12.0</LangVersion>
55
<Nullable>enable</Nullable>
66
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
77

PolySharp.sln

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolySharp.SourceGenerators"
66
EndProject
77
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Projects", "Projects", "{8DAA0C71-4529-44EB-8994-DE331A3ADC52}"
88
ProjectSection(SolutionItems) = preProject
9+
src\PolySharp.Package\icon.png = src\PolySharp.Package\icon.png
910
src\PolySharp.Package\PolySharp.Package.msbuildproj = src\PolySharp.Package\PolySharp.Package.msbuildproj
11+
src\PolySharp.Package\README.md = src\PolySharp.Package\README.md
1012
EndProjectSection
1113
EndProject
1214
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{D65D0307-1D0F-499D-945B-E33E71F251A4}"
@@ -37,7 +39,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolySharp.MinimumCSharpVers
3739
EndProject
3840
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolySharp.PolySharpUseTypeAliasForUnmanagedCallersOnlyAttribute.Tests", "tests\PolySharp.PolySharpUseTypeAliasForUnmanagedCallersOnlyAttribute.Tests\PolySharp.PolySharpUseTypeAliasForUnmanagedCallersOnlyAttribute.Tests.csproj", "{C865CEDE-2DC9-4E1E-BB58-529E6E2A9DBB}"
3941
EndProject
40-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PolySharp.TypeForwards.Tests", "tests\PolySharp.TypeForwards.Tests\PolySharp.TypeForwards.Tests.csproj", "{AAF3B574-66F1-4EF0-936A-82390E30D539}"
42+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PolySharp.TypeForwards.Tests", "tests\PolySharp.TypeForwards.Tests\PolySharp.TypeForwards.Tests.csproj", "{AAF3B574-66F1-4EF0-936A-82390E30D539}"
4143
EndProject
4244
Global
4345
GlobalSection(SolutionConfigurationPlatforms) = preSolution

README.md

Lines changed: 7 additions & 2 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# 11 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# 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.
1212

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

@@ -49,8 +49,11 @@ Here's an example of some of the new features that **PolySharp** can enable down
4949
- `[AsyncMethodBuilder]` (needed for [custom method builder types](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-10.0/async-method-builders))
5050
- `[StringSyntax]` (needed to enable [syntax highlight in the IDE](https://github.com/dotnet/runtime/issues/62505))
5151
- `[ModuleInitializer]` (needed to enable [custom module initializers](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-9.0/module-initializers))
52+
- `[RequiresLocation]` (needed to enable [ref readonly parameters](https://github.com/dotnet/csharplang/issues/6010))
53+
- `[CollectionBuilder]` (needed for [collection expressions](https://github.com/dotnet/csharplang/issues/5354))
54+
- `[Experimental]` (needed for [experimental features](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/experimental-attribute))
5255

53-
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>11.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>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.
5457

5558
It also includes the following optional runtime-supported polyfills:
5659
- Reflection annotation attributes (see [docs](https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming)):
@@ -71,6 +74,8 @@ It also includes the following optional runtime-supported polyfills:
7174
- `[UnsupportedOSPlatformGuard]`
7275
- `[SuppressGCTransition]` (see [here](https://devblogs.microsoft.com/dotnet/improvements-in-native-code-interop-in-net-5-0/))
7376
- `[DisableRuntimeMarshalling]` (see [here](https://learn.microsoft.com/dotnet/standard/native-interop/disabled-marshalling))
77+
- `[UnsafeAccessor]` (see [here](https://github.com/dotnet/runtime/issues/81741))
78+
- `[InlineArray]` (see [here](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-12.0/inline-arrays))
7479

7580
# Options ⚙️
7681

global.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"sdk": {
3+
"version": "8.0.100",
4+
"rollForward": "latestFeature",
5+
"allowPrerelease": false
6+
}
7+
}

src/PolySharp.Package/README.md

Lines changed: 7 additions & 2 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# 11 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# 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.
1010

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

@@ -47,8 +47,11 @@ Here's an example of some of the new features that **PolySharp** can enable down
4747
- `[AsyncMethodBuilder]` (needed for [custom method builder types](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-10.0/async-method-builders))
4848
- `[StringSyntax]` (needed to enable [syntax highlight in the IDE](https://github.com/dotnet/runtime/issues/62505))
4949
- `[ModuleInitializer]` (needed to enable [custom module initializers](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-9.0/module-initializers))
50+
- `[RequiresLocation]` (needed to enable [ref readonly parameters](https://github.com/dotnet/csharplang/issues/6010))
51+
- `[CollectionBuilder]` (needed for [collection expressions](https://github.com/dotnet/csharplang/issues/5354))
52+
- `[Experimental]` (needed for [experimental features](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/experimental-attribute))
5053

51-
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>11.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.
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.
5255

5356
It also includes the following optional runtime-supported polyfills:
5457
- Reflection annotation attributes (see [docs](https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming)):
@@ -69,6 +72,8 @@ It also includes the following optional runtime-supported polyfills:
6972
- `[UnsupportedOSPlatformGuard]`
7073
- `[SuppressGCTransition]` (see [here](https://devblogs.microsoft.com/dotnet/improvements-in-native-code-interop-in-net-5-0/))
7174
- `[DisableRuntimeMarshalling]` (see [here](https://learn.microsoft.com/dotnet/standard/native-interop/disabled-marshalling))
75+
- `[UnsafeAccessor]` (see [here](https://github.com/dotnet/runtime/issues/81741))
76+
- `[InlineArray]` (see [here](https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-12.0/inline-arrays))
7277

7378
# Options ⚙️
7479

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.CompilerServices2
9+
{
10+
/// <summary>
11+
/// Indicates that the instance's storage is sequentially replicated "length" times.
12+
/// </summary>
13+
/// <remarks>
14+
/// <para>
15+
/// This attribute can be used to annotate a <see langword="struct"/> type with a single field.
16+
/// The runtime will replicate that field in the actual type layout as many times as is specified.
17+
/// </para>
18+
/// <para>
19+
/// Here's an example of how an inline array type with 8 <see cref="float"/> values can be declared:
20+
/// <code lang="csharp">
21+
/// [InlineArray(8)]
22+
/// struct Float8InlineArray
23+
/// {
24+
/// private float _value;
25+
/// }
26+
/// </code>
27+
/// </para>
28+
/// </remarks>
29+
[global::System.AttributeUsage(global::System.AttributeTargets.Struct, AllowMultiple = false)]
30+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
31+
[global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")]
32+
public sealed class InlineArrayAttribute : global::System.Attribute
33+
{
34+
/// <summary>Creates a new <see cref="global::System.Runtime.CompilerServices2.InlineArrayAttribute"/> instance with the specified length.</summary>
35+
/// <param name="length">The number of sequential fields to replicate in the inline array type.</param>
36+
public InlineArrayAttribute(int length)
37+
{
38+
Length = length;
39+
}
40+
41+
/// <summary>Gets the number of sequential fields to replicate in the inline array type.</summary>
42+
public int Length { get; }
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
/// Provides access to an inaccessible member of a specific type.
12+
/// </summary>
13+
/// <remarks>
14+
/// This attribute may be applied to an <code>extern static</code> method.
15+
/// The implementation of the <code>extern static</code> method annotated with
16+
/// this attribute will be provided by the runtime based on the information in
17+
/// the attribute and the signature of the method that the attribute is applied to.
18+
/// The runtime will try to find the matching method or field and forward the call
19+
/// to it. If the matching method or field is not found, the body of the <code>extern</code>
20+
/// method will throw <see cref="global::System.MissingFieldException" /> or <see cref="global::System.MissingMethodException" />.
21+
/// Only the specific type defined will be examined for inaccessible members. The type hierarchy
22+
/// is not walked looking for a match.
23+
///
24+
/// For <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind.Method"/>,
25+
/// <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind.StaticMethod"/>,
26+
/// <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind.Field"/>,
27+
/// and <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind.StaticField"/>, the type of
28+
/// the first argument of the annotated <code>extern</code> method identifies the owning type.
29+
/// The value of the first argument is treated as <code>this</code> pointer for instance fields and methods.
30+
/// The first argument must be passed as <code>ref</code> for instance fields and methods on structs.
31+
/// The value of the first argument is not used by the implementation for <code>static</code> fields and methods.
32+
///
33+
/// Return type is considered for the signature match. modreqs and modopts are initially not considered for
34+
/// the signature match. However, if an ambiguity exists ignoring modreqs and modopts, a precise match
35+
/// is attempted. If an ambiguity still exists <see cref="global::System.Reflection.AmbiguousMatchException" /> is thrown.
36+
///
37+
/// By default, the attributed method's name dictates the name of the method/field. This can cause confusion
38+
/// in some cases since language abstractions, like C# local functions, generate mangled IL names. The
39+
/// solution to this is to use the <code>nameof</code> mechanism and define the <see cref="Name"/> property.
40+
///
41+
/// <code>
42+
/// public void Method(Class c)
43+
/// {
44+
/// PrivateMethod(c);
45+
///
46+
/// [UnsafeAccessor(UnsafeAccessorKind.Method, Name = nameof(PrivateMethod))]
47+
/// extern static void PrivateMethod(Class c);
48+
/// }
49+
/// </code>
50+
/// </remarks>
51+
[global::System.AttributeUsage(global::System.AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
52+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
53+
[global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")]
54+
internal sealed class UnsafeAccessorAttribute : global::System.Attribute
55+
{
56+
/// <summary>
57+
/// Instantiates an <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorAttribute"/>
58+
/// providing access to a member of kind <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind"/>.
59+
/// </summary>
60+
/// <param name="kind">The kind of the target to which access is provided.</param>
61+
public UnsafeAccessorAttribute(global::System.Runtime.CompilerServices.UnsafeAccessorKind kind)
62+
{
63+
Kind = kind;
64+
}
65+
66+
/// <summary>
67+
/// Gets the kind of member to which access is provided.
68+
/// </summary>
69+
public global::System.Runtime.CompilerServices.UnsafeAccessorKind Kind { get; }
70+
71+
/// <summary>
72+
/// Gets or sets the name of the member to which access is provided.
73+
/// </summary>
74+
/// <remarks>
75+
/// The name defaults to the annotated method name if not specified.
76+
/// The name must be unset/<code>null</code> for <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorKind.Constructor"/>.
77+
/// </remarks>
78+
public string? Name { get; set; }
79+
}
80+
}
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.Runtime.CompilerServices
9+
{
10+
/// <summary>
11+
/// Specifies the kind of target to which an <see cref="global::System.Runtime.CompilerServices.UnsafeAccessorAttribute" /> is providing access.
12+
/// </summary>
13+
internal enum UnsafeAccessorKind
14+
{
15+
/// <summary>
16+
/// Provide access to a constructor.
17+
/// </summary>
18+
Constructor,
19+
20+
/// <summary>
21+
/// Provide access to a method.
22+
/// </summary>
23+
Method,
24+
25+
/// <summary>
26+
/// Provide access to a static method.
27+
/// </summary>
28+
StaticMethod,
29+
30+
/// <summary>
31+
/// Provide access to a field.
32+
/// </summary>
33+
Field,
34+
35+
/// <summary>
36+
/// Provide access to a static field.
37+
/// </summary>
38+
StaticField
39+
}
40+
}

src/PolySharp.SourceGenerators/EmbeddedResources/RuntimeSupported/System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ namespace System.Runtime.InteropServices
2323
[global::System.Diagnostics.Conditional("MULTI_TARGETING_SUPPORT_ATTRIBUTES")]
2424
internal sealed class UnmanagedCallersOnlyAttribute : global::System.Attribute
2525
{
26-
public UnmanagedCallersOnlyAttribute()
27-
{
28-
}
29-
3026
/// <summary>
3127
/// Optional. If omitted, the runtime will use the default platform calling convention.
3228
/// </summary>

0 commit comments

Comments
 (0)