You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Pass constants to parameters marked as [ConstantExpected] #33771
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.
publicstaticclassC{publicstaticvoidMyBool1([ConstantExpected]boolb){}publicstaticvoidMyLong1([ConstantExpected]longb){}publicstaticvoidMyLong2([ConstantExpected(Min=-5,Max=10)]longb){}publicstaticvoidMyFloat1([ConstantExpected]floatb){}publicstaticvoidMyFloat2([ConstantExpected(Min=-5.3f,Max=10.1f)]floatb){}// Might want to warn for the negative values and out of range valuespublicstaticvoidMyInvalidUshort([ConstantExpected(Min=-5,Max=-1)]ushortb){}publicstaticvoidMyInvalidRange([ConstantExpected(Min=5,Max=-5)]intb){}// flag any ref type usage as not applicablepublicstaticvoidMyString([ConstantExpected]stringb){}// Diagnostics examplespublicstaticvoidTest(longb,ushortu){// OKconstlonga=10;MyLong1(a);MyLong2(a);MyLong1(1L);MyLong2(2L);MyInvalidUshort(1);constushortus=0;MyInvalidUshort(us);// FlagMyLong1(b);// FlagMyLong2(b);// FlagMyLong2(20);// Flag, out of rangeMyInvalidUshort(u);// FlagMyInvalidUshort(10);// Flag, out of the range}}
@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.
We shipped .NET Core 3.0 without such an attribute and I'm sure we'll be fine if we do the same for .NET 5.
Do we allow composition of different types? Like what should the analyzer do with something like below:
Not sure I follow the question, though I believe the analyzer should warn on TestMethod2(constant); invocation because it is not passing a constant
publicstaticvoidTestMethod([ConstantExpected(Min=0,Max=5)]uintconstant){TestMethod2(constant);// should warn here as constant expected for 'val' parameter}publicstaticvoidTestMethod2([ConstantExpected(Min=0,Max=10)]intval){}
Also, does casting influence the analyzer?
TestMethod((uint)100);
Not sure what issue you are having here, but TestMethod((uint)100); and TestMethod(100); in your example should behave the same
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?
It might be more relevant for the composition case.
I guess it is not an issue for the literal scenario.
I assume the analyzer should warn for not having [ConstantExpected] in the more concrete type parameter?
I'd expect so, yes. There is some "wonkiness" here though, in that [ConstantExpected] is "impossible" to fulfill for non-primitive types. So if T were a Guid, for example, we'd need to decide what happens.
Ok, i have written the tests for many of the cases.
For cases where the attribute is applied to T, setting Min Max is an error, and warning at callsite only appears if concrete type for T can be constant
@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"
Activity
jeffhandley commentedon Mar 21, 2020
Estimates:
jeffhandley commentedon Mar 23, 2020
This issue should capture:
The work to apply the attribute (especially through intrinsics) could be a separate issue/PR.
jeffhandley commentedon May 26, 2020
@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?
tannergooding commentedon May 26, 2020
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.
We shipped .NET Core 3.0 without such an attribute and I'm sure we'll be fine if we do the same for .NET 5.
44 remaining items
wzchua commentedon Dec 11, 2021
Do we allow composition of different types? Like what should the analyzer do with something like below:
wzchua commentedon Dec 13, 2021
Also, does casting influence the analyzer?
buyaa-n commentedon Dec 13, 2021
Not sure I follow the question, though I believe the analyzer should warn on
TestMethod2(constant);invocation because it is not passing a constantNot sure what issue you are having here, but
TestMethod((uint)100);andTestMethod(100);in your example should behave the samewzchua commentedon Dec 13, 2021
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?
It might be more relevant for the composition case.
I guess it is not an issue for the literal scenario.
buyaa-n commentedon Dec 13, 2021
Good question, tagging @tannergooding @GrabYourPitchforks @bartonjs for answer
wzchua commentedon Dec 17, 2021
Given,
I assume the analyzer should warn for not having
[ConstantExpected]in the more concrete type parameter?tannergooding commentedon Jan 7, 2022
I'd expect so, yes. There is some "wonkiness" here though, in that
[ConstantExpected]is "impossible" to fulfill for non-primitive types. So ifTwere aGuid, for example, we'd need to decide what happens.wzchua commentedon Jan 9, 2022
Ok, i have written the tests for many of the cases.
For cases where the attribute is applied to T, setting Min Max is an error, and warning at callsite only appears if concrete type for T can be constant
deeprobin commentedon Feb 24, 2022
dotnet/roslyn-analyzers#5766 (comment)
@bartonjs Do we need a VB analyzer?
/cc @wzchua
bartonjs commentedon Feb 24, 2022
@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"