Skip to content

Commit 02753db

Browse files
authored
Merge pull request #114 from Sergio0694/dev/constant-expected-attribute
Add [ConstantExpected] polyfill
2 parents 06e5008 + cd55412 commit 02753db

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Here's an example of some of the new features that **PolySharp** can enable down
5454
- `[Experimental]` (needed for [experimental features](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/experimental-attribute))
5555
- `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority))
5656
- `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections))
57+
- `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771))
5758

5859
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.
5960

src/PolySharp.Package/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Here's an example of some of the new features that **PolySharp** can enable down
5252
- `[Experimental]` (needed for [experimental features](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-12.0/experimental-attribute))
5353
- `[OverloadResolutionPriority]` (needed for [overload resolution priority](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#overload-resolution-priority))
5454
- `[ParamsCollection]` (needed for [params collection](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-13#params-collections))
55+
- `[ConstantExpected]` (see [proposal](https://github.com/dotnet/runtime/issues/33771))
5556

5657
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.
5758

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 method parameter expects a constant.
12+
/// </summary>
13+
/// <remarks>
14+
/// This can be used to inform tooling that a constant should be used as an argument for the annotated parameter.
15+
/// </remarks>
16+
[global::System.AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
17+
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
18+
internal sealed class ConstantExpectedAttribute : global::System.Attribute
19+
{
20+
/// <summary>
21+
/// Indicates the minimum bound of the expected constant, inclusive.
22+
/// </summary>
23+
public object? Min { get; set; }
24+
25+
/// <summary>
26+
/// Indicates the maximum bound of the expected constant, inclusive.
27+
/// </summary>
28+
public object? Max { get; set; }
29+
}
30+
}

tests/PolySharp.Tests/LanguageFeatures.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,15 @@ public static void TestOverload(int x)
285285
public static void TestOverload(int x, int y = 0)
286286
{
287287
}
288-
}
288+
}
289+
290+
internal static class ConstantExpectedTests
291+
{
292+
public static void CpuIntrinsic([ConstantExpected] int value)
293+
{
294+
}
295+
296+
public static void AnotherCpuIntrinsic([ConstantExpected(Min = 0, Max = 8)] int value)
297+
{
298+
}
299+
}

0 commit comments

Comments
 (0)