Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ConstantExpectedAttribute #62436

Merged
merged 5 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@
<Compile Include="$(MSBuildThisFileDirectory)System\Decimal.DecCalc.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\DefaultBinder.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Delegate.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\ConstantExpectedAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresAssemblyFilesAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresDynamicCodeAttribute.cs" />
<Compile Include="$(MSBuildThisFileDirectory)System\Diagnostics\CodeAnalysis\RequiresUnreferencedCodeAttribute.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
public sealed class ConstantExpectedAttribute : 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; }
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
}
}
6 changes: 6 additions & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9041,6 +9041,12 @@ public sealed partial class AllowNullAttribute : System.Attribute
{
public AllowNullAttribute() { }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Parameter, Inherited=false)]
public sealed class ConstantExpectedAttribute : Attribute
{
public object? Min { get { throw null; } set { } }
public object? Max { get { throw null; } set { } }
}
[System.AttributeUsageAttribute(System.AttributeTargets.Field | System.AttributeTargets.Parameter | System.AttributeTargets.Property, Inherited=false)]
public sealed partial class DisallowNullAttribute : System.Attribute
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@
<Compile Include="System\ComponentModel\DefaultValueAttributeTests.cs" />
<Compile Include="System\ComponentModel\EditorBrowsableAttributeTests.cs" />
<Compile Include="System\Diagnostics\ConditionalAttributeTests.cs" />
<Compile Include="System\Diagnostics\CodeAnalysis\ConstantExpectedAttributeTests.cs" />
<Compile Include="System\Diagnostics\CodeAnalysis\DynamicallyAccessedMembersAttributeTests.cs" />
<Compile Include="System\Diagnostics\CodeAnalysis\DynamicDependencyAttributeTests.cs" />
<Compile Include="System\Diagnostics\CodeAnalysis\RequiresAssemblyFilesAttributeTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace System.Diagnostics.CodeAnalysis.Tests
{
public class ConstantExpectedAttributeTests
{
[Fact]
public void TestConstructor()
{
var attr = new ConstantExpectedAttribute();

Assert.Null(attr.Min);
Assert.Null(attr.Max);
}

[Theory]
[InlineData("https://dot.net")]
[InlineData("")]
[InlineData(10000)]
[InlineData(0.5f)]
[InlineData(null)]
public void TestSetMin(object min)
{
var attr = new ConstantExpectedAttribute
{
Min = min
};

Assert.Same(min, attr.Min);
}

[Theory]
[InlineData("https://dot.net")]
[InlineData("")]
[InlineData(10000)]
[InlineData(0.5f)]
[InlineData(null)]
public void TestSetMax(object max)
{
var attr = new ConstantExpectedAttribute
{
Max = max
};

Assert.Same(max, attr.Max);
}

[Theory]
[InlineData("", "https://dot.net")]
[InlineData(10000, 20000)]
[InlineData(0.5f, 2.0f)]
[InlineData(null, null)]
[InlineData(10, 0)]
[InlineData(10, "https://dot.net")]
public void TestSetMinAndMax(object min, object max)
{
var attr = new ConstantExpectedAttribute
{
Min = min,
Max = max
};

Assert.Same(min, attr.Min);
Assert.Same(max, attr.Max);
}
}
}