-
Notifications
You must be signed in to change notification settings - Fork 231
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
SE - Nullable: Copy constraints to Value
property
#6841
Conversation
Value
property
Kudos, SonarCloud Quality Gate passed! |
Kudos, SonarCloud Quality Gate passed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I left some questions to make sure I got the overall plan right.
var setter = new PreProcessTestCheck(OperationKind.Literal, x => x.Operation.Instance.ConstantValue.Value is false ? x.SetOperationConstraint(TestConstraint.First) : x.State); | ||
var validator = SETestContext.CreateCS(code, ", bool? arg", setter).Validator; | ||
validator.ValidateTag("Unknown", x => x.Should().BeNull()); | ||
validator.ValidateTag("True", x => x.HasConstraint(BoolConstraint.True).Should().BeTrue()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
validator.ValidateTag("True", x => x.HasConstraint(BoolConstraint.True).Should().BeTrue()); | |
validator.ValidateTag("True", x => x.HasConstraint(BoolConstraint.True).Should().BeTrue()); | |
validator.ValidateTag("True", x => x.HasConstraint(TestConstraint.First).Should().BeFalse()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would assert that scaffolding works. We don't need that. The point of this is that all constraints are propagated.
return propertyReference.Property.Name == "Value" && propertyReference.Instance.Type.IsNullableValueType() && context.State[symbol] is { } value | ||
? state.SetOperationValue(context.Operation, value) | ||
: state; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. This should work.
Just to make sure, I understand the plan correctly:
I suppose the idea is to later also learn
ObjectConstraint.NotNull
forsymbol
wheneverHasValue
gets theBoolConstraint.True
ObjectConstraint.IsNull
forsymbol
wheneverHasValue
gets theBoolConstraint.False
if (nullable.HasValue)
{
// nullable has ObjectConstraint.NotNull
}
else
{
// nullable has ObjectConstraint.Null
}
If so we need to learn
ObjectConstraint.NotNull
forsymbol
here as well as it is known to beNotNull
from here on.
I suppose these are the next steps, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's exactly the plan. See 2nd step on #6812
{ | ||
const string code = """ | ||
var value = arg.Value; | ||
Tag("Unknown", value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this will change to Tag("NoNull", value);
in the future.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that will most likely happen on your PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💥
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Educational questions, for my and other people's benefit.
if (propertyReference.Instance.TrackedSymbol() is { } symbol) | ||
{ | ||
var state = context.State.SetSymbolConstraint(symbol, ObjectConstraint.NotNull); | ||
return propertyReference.Property.Name == "Value" && propertyReference.Instance.Type.IsNullableValueType() && context.State[symbol] is { } value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Educational, to clarify levels of abstractions: Is the following statement correct?
"Value"
here is the name of the property at SE level, directly coming from the symbol at Semantic level and not the syntactical token. So, if the code being symbolic-analyzed is VB.NET and the user types .value
, the Name
will still be "Value"
. That's why you don't use nameof(Nullable.Value)
, which would return the name at syntactical level.
Extension properties (as defined here) would not break this, for the same reason.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "Value"
statement is correct. It could have used nameof
if that would compile (but it doesn't).
propertyReference.Instance.TrackedSymbol() is { } symbol | ||
? context.SetSymbolConstraint(symbol, ObjectConstraint.NotNull) | ||
: context.State; | ||
protected override ProgramState Process(SymbolicContext context, IPropertyReferenceOperationWrapper propertyReference) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Educational: explicit cast, while calling .Value
, is not supported by this because the cast is a method call at CFG level (is it?) and SE is not cross-level. So you will need to learn the same you learn here (i.e. Value
as a non-null value) in a "IInvocationOperationWrapper". Correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All casts are represented as ConversionOperation
(implicit or explicit). That will be handled later.
InvocationOperation
is not relevant here.
{ | ||
const string code = """ | ||
bool? value = null; | ||
Tag("Null", value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Educational. The method built by CreateCS
is empty. Why "Null"
learns to be null and "True"
learns to be true? The two parameters of Tag
seem not correlated to each other.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method built by CreateCS
is not empty. It has this body.
The Tag
method is a special empty method in the SETestContext
that we use to tag a capture a specific state. It works like a probe.
The first argument is a string and servers as a key. Because we can tag more things in a single run.
As the constraints evolves over time, we're probing it twice, asserting different state on each place of code.
"""; | ||
var setter = new PreProcessTestCheck(OperationKind.Literal, x => x.Operation.Instance.ConstantValue.Value is false ? x.SetOperationConstraint(TestConstraint.First) : x.State); | ||
var validator = SETestContext.CreateCS(code, ", bool? arg", setter).Validator; | ||
validator.ValidateTag("Unknown", x => x.Should().BeNull()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Educational. Shouldn't this be unknown, because arg
is unknown in code
? Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arg
is a parameter of the method. So the method goes like
public void Main(bool boolParameter, bool? arg)
{
var value = arg.Value;
Tag("Unknown", value);
// ...
}
and as arg
is a parameter, we cannot infer anything about its initial value. So the value is unknown to us, it has no constraints.
Then we change it, and after that, we can assume some constraints.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For C#, you can see everything that is available (and that we can use in the snippets) here:
sonar-dotnet/analyzers/tests/SonarAnalyzer.UnitTest/TestFramework/SymbolicExecution/SETestContext.cs
Lines 106 to 165 in dffe8ff
private static string ClassCodeCS(string methodBody, string additionalParameters) => | |
$@" | |
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Linq; | |
using static Tagger; | |
public unsafe class Sample | |
{{ | |
public object ObjectField; | |
public static int StaticField; | |
public static object StaticObjectField; | |
public static int StaticProperty {{ get; set; }} | |
public static event EventHandler StaticEvent; | |
public event EventHandler Event; | |
public int Property {{ get; set; }} | |
public Sample SampleProperty {{ get; set; }} | |
public NotImplementedException PropertyException {{ get; set; }} | |
public int this[int index] {{get => 42; set {{ }} }} | |
private int field; | |
private NotImplementedException fieldException; | |
private bool Condition => Environment.ProcessorCount == 42; // Something that cannot have constraint | |
public Sample(){{ }} | |
public Sample(int i){{ }} | |
public void Main(bool boolParameter{additionalParameters}) | |
{{ | |
{methodBody} | |
}} | |
public NotImplementedException CreateException() => new NotImplementedException(); | |
public void InstanceMethod(object parameter = null) {{ }} | |
public static void StaticMethod() {{ }} | |
}} | |
public class Person : PersonBase | |
{{ | |
public static string StaticProperty {{ get; set; }} | |
public string Field; | |
public event EventHandler Event; | |
public string Method() => null; | |
public static void StaticMethod() {{ }} | |
}} | |
public class PersonBase | |
{{ | |
}} | |
public static class Tagger | |
{{ | |
public static void Tag(string name, object arg = null) {{ }} | |
public static void Tag(this object o, string name) {{ }} | |
public static T Unknown<T>() => default; | |
}} | |
"; | |
} |
Value
propertyValue
property
Value
propertyValue
property
Part of #6812
Copy known constraints from nullable symbol to
.Value
property.