Skip to content

Commit

Permalink
Merge pull request #114 from Sergio0694/dev/constant-expected-attribute
Browse files Browse the repository at this point in the history
Add [ConstantExpected] polyfill
  • Loading branch information
Sergio0694 authored Nov 21, 2024
2 parents 06e5008 + cd55412 commit 02753db
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<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.

Expand Down
1 change: 1 addition & 0 deletions src/PolySharp.Package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<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.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// <auto-generated/>
#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
{
/// <summary>
/// Indicates that the specified method parameter expects a constant.
/// </summary>
/// <remarks>
/// This can be used to inform tooling that a constant should be used as an argument for the annotated parameter.
/// </remarks>
[global::System.AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
internal sealed class ConstantExpectedAttribute : global::System.Attribute
{
/// <summary>
/// Indicates the minimum bound of the expected constant, inclusive.
/// </summary>
public object? Min { get; set; }

/// <summary>
/// Indicates the maximum bound of the expected constant, inclusive.
/// </summary>
public object? Max { get; set; }
}
}
13 changes: 12 additions & 1 deletion tests/PolySharp.Tests/LanguageFeatures.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,15 @@ public static void TestOverload(int x)
public static void TestOverload(int x, int y = 0)
{
}
}
}

internal static class ConstantExpectedTests
{
public static void CpuIntrinsic([ConstantExpected] int value)
{
}

public static void AnotherCpuIntrinsic([ConstantExpected(Min = 0, Max = 8)] int value)
{
}
}

0 comments on commit 02753db

Please sign in to comment.