diff --git a/README.md b/README.md index 85a8b87..3e65b86 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ Here's an example of some of the new features that **PolySharp** can enable down - `[Experimental]` (needed for [experimental features](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/experimental-attribute)) - `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority)) - `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections)) +- `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771)) 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 `13.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 c5ea60e..0cacf88 100644 --- a/src/PolySharp.Package/README.md +++ b/src/PolySharp.Package/README.md @@ -52,6 +52,7 @@ Here's an example of some of the new features that **PolySharp** can enable down - `[Experimental]` (needed for [experimental features](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/experimental-attribute)) - `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority)) - `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections)) +- `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771)) 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 `13.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.SourceGenerators/EmbeddedResources/System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute.cs b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute.cs new file mode 100644 index 0000000..0c85229 --- /dev/null +++ b/src/PolySharp.SourceGenerators/EmbeddedResources/System.Diagnostics.CodeAnalysis.ConstantExpectedAttribute.cs @@ -0,0 +1,30 @@ +// +#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. + +namespace System.Diagnostics.CodeAnalysis +{ + /// + /// Indicates that the specified method parameter expects a constant. + /// + /// + /// This can be used to inform tooling that a constant should be used as an argument for the annotated parameter. + /// + [global::System.AttributeUsage(AttributeTargets.Parameter, Inherited = false)] + [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] + internal sealed class ConstantExpectedAttribute : global::System.Attribute + { + /// + /// Indicates the minimum bound of the expected constant, inclusive. + /// + public object? Min { get; set; } + + /// + /// Indicates the maximum bound of the expected constant, inclusive. + /// + public object? Max { get; set; } + } +} \ No newline at end of file diff --git a/tests/PolySharp.Tests/LanguageFeatures.cs b/tests/PolySharp.Tests/LanguageFeatures.cs index d2558f6..d5df709 100644 --- a/tests/PolySharp.Tests/LanguageFeatures.cs +++ b/tests/PolySharp.Tests/LanguageFeatures.cs @@ -285,4 +285,15 @@ public static void TestOverload(int x) public static void TestOverload(int x, int y = 0) { } -} \ No newline at end of file +} + +internal static class ConstantExpectedTests +{ + public static void CpuIntrinsic([ConstantExpected] int value) + { + } + + public static void AnotherCpuIntrinsic([ConstantExpected(Min = 0, Max = 8)] int value) + { + } +}