-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Pass constants to parameters marked as [ConstantExpected] #33771
Comments
Estimates:
|
This issue should capture:
The work to apply the attribute (especially through intrinsics) could be a separate issue/PR. |
@tannergooding Do you think this is a must-have attribute & analyzer for .NET 5 to round out the intrinsics work we've done, or do you think this could be added in .NET 6? |
It is not. The hardware intrinsics don't even require constant inputs, they just benefit from them and you will get very poor codegen if you don't. |
Thank you, @tannergooding. I'm going to move this to Future so that we can consider it for .NET 6 then. |
How would we catch expressions which are evaluated at compile time? I.e. |
@GrabYourPitchforks @tannergooding should we have a separate API proposal for the attribute itself? Or in other words, should we reopen #33814 for that purpose specifically? I don't mind if we can get both the attribute and the analyzer approved in one single issue. Attribute questions
namespace System.Runtime.Intrinsics.X86
{
public abstract class Sse2 : Sse
{
public static Vector128<short> ShiftLeftLogical(Vector128<short> value, [ConstantExpected(/* Min = 0, Max = 15 */)] byte count);
}
} A proposal of the attribute could be: [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
public sealed class ConstantExpectedAttribute : Attribute
{
public int? Min { get; set; }
public int? Max { get; set; }
}
Analyzer questions
|
If we wanted min / max, they could be typed as object?. This would allow |
I think that this issue is fine for tracking the API, and I believe that was @terrajobst intent when he tagged it |
I'm late to the discussion, but in relation to use with intrinsics, I'd prefer something like It could possibly be extended to have a generic constraint |
That would be a breaking change and go against the goals of hardware intrinsics in .NET. It also has several issues, including that you couldn't write code that works with values that become constant after inlining. |
I would like to be assigned this. Does the work start with getting the attribute merged into runtime repo, then writing the analyzer? Are there other steps for the attribute? |
Yes start with the attribute getting merged to the runtime repo, for the analyzer you can use a mock attribute for testing until you were able to consume it in the analyzers repo
I would say no, but not sure about what exactly you are asking, here is a sample PR for adding an attribute |
Also, there could be a step "applying the attribute" to the APIs in the runtime repo but that is not needed to be covered by this issue |
Do we allow composition of different types? Like what should the analyzer do with something like below: public static void TestMethod([ConstantExpected(Min=0, Max=5)] uint constant)
{
TestMethod2(constant);
}
public static void TestMethod2([ConstantExpected(Min=0, Max=10)] int val)
{
} |
Also, does casting influence the analyzer? TestMethod((uint)100); |
Not sure I follow the question, though I believe the analyzer should warn on public static void TestMethod([ConstantExpected(Min=0, Max=5)] uint constant)
{
TestMethod2(constant); // should warn here as constant expected for 'val' parameter
}
public static void TestMethod2([ConstantExpected(Min=0, Max=10)] int val)
{
}
Not sure what issue you are having here, but |
It seems we want to be able to compose the ConstantExpectedAttribute using another ConstantExpectedAttribute. I'm clarifying what should be done if the types don't match. I guess I'm asking does casting make it not a constant? |
Good question, tagging @tannergooding @GrabYourPitchforks @bartonjs for answer |
Given, using System;
using System.Diagnostics.CodeAnalysis;
#nullable enable
public class Test
{
public interface ITest<T>
{
T Add(T operand1, [ConstantExpected] T operand2);
}
public abstract class AbstractTest<T>
{
public abstract T Add2(T operand1, [ConstantExpected] T operand2);
}
public class GenericInt : AbstractTest<int>, ITest<int>
{
public int Add(int operand1, int operand2)
{
return 0;
}
public override int Add2(int operand1, int operand2)
{
return 0;
}
}
} I assume the analyzer should warn for not having |
I'd expect so, yes. There is some "wonkiness" here though, in that |
Ok, i have written the tests for many of the cases. |
dotnet/roslyn-analyzers#5766 (comment) /cc @wzchua |
@deeprobin The general stance is "most analyzers should Just Work for both C# and VB, so you should add VB tests and enjoy that it Just Worked... but if your analyzer requires special VB code it's probably not worth it" |
Consider adding an
[ConstantExpected]
attribute that could be put on parameters to indicate that arguments should be constants rather than variables. See #30740 (comment) for details.The attribute proposal
Usage and Diagnostic examples
Category: Reliability
Severity: Info
The text was updated successfully, but these errors were encountered: