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

SE - Nullable: Learn object constraint from value comparison #6925

Merged
merged 5 commits into from
Mar 16, 2023
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 @@ -43,11 +43,13 @@ protected override ProgramState LearnBranchingConstraint(ProgramState state, IBi
private static ProgramState LearnBranchingConstraint<T>(ProgramState state, IBinaryOperationWrapper binary, bool falseBranch)
where T : SymbolicConstraint
{
var useOpposite = falseBranch ^ binary.OperatorKind.IsNotEquals();
// We can fall through ?? because "constraint" and "testedSymbol" are exclusive. Symbols with the constraint will be recognized as "constraint" side.
if ((OperandConstraint(binary.LeftOperand) ?? OperandConstraint(binary.RightOperand)) is { } constraint
&& (OperandSymbolWithoutConstraint(binary.LeftOperand) ?? OperandSymbolWithoutConstraint(binary.RightOperand)) is { } testedSymbol)
&& (OperandSymbolWithoutConstraint(binary.LeftOperand) ?? OperandSymbolWithoutConstraint(binary.RightOperand)) is { } testedSymbol
&& !(useOpposite && constraint is BoolConstraint && testedSymbol.GetSymbolType().IsNullableBoolean()))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really ugly special casing. Shouldn't this do the same thing 🤔 ?

Suggested change
&& !(useOpposite && constraint is BoolConstraint && testedSymbol.GetSymbolType().IsNullableBoolean()))
&& !(useOpposite && testedSymbol.GetSymbolType().IsNullable()))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory should, until someone adds a BoolConstraint to something else than bool somewhere. I'll keep it as we don't have IsNullable and I don't want to add it for this

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D'accord. Can you add a code comment with examples about why we need this special case and also add a hint that ObjectConstraint.ApplyOpposite(true) does the same logic but at a different place? Our future self will have a hard time understanding what is going on here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

{
constraint = constraint.ApplyOpposite(falseBranch ^ binary.OperatorKind.IsNotEquals());
constraint = constraint.ApplyOpposite(useOpposite);
return constraint is null ? null : state.SetSymbolConstraint(testedSymbol, constraint);
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void Branching_LearnsObjectConstraint_CS(string expression)
[DataRow("!(bool)(object)!!(null != arg)")]
[DataRow("!!!((object)arg != (object)null)")]
[DataRow("!!!((object)null != (object)arg)")]
public void Branching_LearnsObjectConstraint_Nullable_CS(string expression)
public void Branching_LearnsObjectConstraint_NullableInt_CS(string expression)
{
var validator = CreateIfElseEndValidatorCS(expression, OperationKind.Binary, "int?");
validator.ValidateTag("If", x => x.HasConstraint(ObjectConstraint.Null).Should().BeTrue());
Expand All @@ -187,6 +187,27 @@ public void Branching_LearnsObjectConstraint_Nullable_CS(string expression)
.And.ContainSingle(x => x.HasConstraint(ObjectConstraint.NotNull));
}

[DataTestMethod]
[DataRow("arg == null")]
[DataRow("null == arg")]
[DataRow("(object)(object)arg == (object)(object)null")]
[DataRow("(object)(object)arg == (object)(object)isNull")]
[DataRow("!!!(arg != null)")]
[DataRow("!!!(null != arg)")]
[DataRow("!(bool)(object)!!(arg != null)")]
[DataRow("!(bool)(object)!!(null != arg)")]
[DataRow("!!!((object)arg != (object)null)")]
[DataRow("!!!((object)null != (object)arg)")]
public void Branching_LearnsObjectConstraint_NullableBool_CS(string expression)
{
var validator = CreateIfElseEndValidatorCS(expression, OperationKind.Binary, "bool?");
validator.ValidateTag("If", x => x.HasConstraint(ObjectConstraint.Null).Should().BeTrue());
validator.ValidateTag("Else", x => x.HasConstraint(ObjectConstraint.NotNull).Should().BeTrue());
validator.TagValues("End").Should().HaveCount(2)
.And.ContainSingle(x => x.HasConstraint(ObjectConstraint.Null))
.And.ContainSingle(x => x.HasConstraint(ObjectConstraint.NotNull));
}

[DataTestMethod]
[DataRow("arg != null")]
[DataRow("arg != isNull")]
Expand Down Expand Up @@ -217,7 +238,7 @@ public void Branching_LearnsObjectConstraint_Binary_Negated_CS(string expression
[DataRow("!!!(null == arg)")]
[DataRow("!(bool)(object)!!(arg == null)")]
[DataRow("!(bool)(object)!!(null == arg)")]
public void Branching_LearnsObjectConstraint_Binary_Negated_Nullable_CS(string expression)
public void Branching_LearnsObjectConstraint_Binary_Negated_NullableInt_CS(string expression)
{
var validator = CreateIfElseEndValidatorCS(expression, OperationKind.Binary, "int?");
validator.ValidateTag("If", x => x.HasConstraint(ObjectConstraint.NotNull).Should().BeTrue());
Expand All @@ -228,11 +249,36 @@ public void Branching_LearnsObjectConstraint_Binary_Negated_Nullable_CS(string e
}

[DataTestMethod]
[DataRow("arg == isObject")]
[DataRow("isObject == arg")]
public void Branching_LearnsObjectConstraint_Binary_UndefinedInOtherBranch_CS(string expression)
[DataRow("arg != null")]
[DataRow("null != arg")]
[DataRow("(object)(object)arg != (object)(object)null")]
[DataRow("(object)(object)arg != (object)(object)isNull")]
[DataRow("!!!(arg == null)")]
[DataRow("!!!(null == arg)")]
[DataRow("!(bool)(object)!!(arg == null)")]
[DataRow("!(bool)(object)!!(null == arg)")]
public void Branching_LearnsObjectConstraint_Binary_Negated_NullableBool_CS(string expression)
{
var validator = CreateIfElseEndValidatorCS(expression, OperationKind.Binary);
var validator = CreateIfElseEndValidatorCS(expression, OperationKind.Binary, "bool?");
validator.ValidateTag("If", x => x.HasConstraint(ObjectConstraint.NotNull).Should().BeTrue());
validator.ValidateTag("Else", x => x.HasConstraint(ObjectConstraint.Null).Should().BeTrue());
validator.TagValues("End").Should().HaveCount(2)
.And.ContainSingle(x => x.HasConstraint(ObjectConstraint.Null))
.And.ContainSingle(x => x.HasConstraint(ObjectConstraint.NotNull));
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
}

[DataTestMethod]
[DataRow("arg == isObject", "object")]
[DataRow("isObject == arg", "object")]
[DataRow("arg == true", "bool?")]
[DataRow("arg == false", "bool?")]
[DataRow("true == arg", "bool?")]
[DataRow("false == arg", "bool?")]
[DataRow("arg == 42", "int?")]
[DataRow("42 == arg", "int?")]
public void Branching_LearnsObjectConstraint_Binary_UndefinedInOtherBranch_CS(string expression, string argType)
{
var validator = CreateIfElseEndValidatorCS(expression, OperationKind.Binary, argType);
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
validator.ValidateTag("If", x => x.HasConstraint(ObjectConstraint.NotNull).Should().BeTrue());
validator.ValidateTag("Else", x => x.Should().BeNull("We can't tell if it is Null or NotNull in this branch"));
validator.TagValues("End").Should().HaveCount(2)
Expand All @@ -241,11 +287,17 @@ public void Branching_LearnsObjectConstraint_Binary_UndefinedInOtherBranch_CS(st
}

[DataTestMethod]
[DataRow("arg != isObject")]
[DataRow("isObject != arg")]
public void Branching_LearnsObjectConstraint_Binary_UndefinedInOtherBranch_Negated_CS(string expression)
[DataRow("arg != isObject", "object")]
[DataRow("isObject != arg", "object")]
[DataRow("arg != true", "bool?")]
[DataRow("arg != false", "bool?")]
[DataRow("true != arg", "bool?")]
[DataRow("false != arg", "bool?")]
[DataRow("arg != 42", "int?")]
[DataRow("42 != arg", "int?")]
public void Branching_LearnsObjectConstraint_Binary_UndefinedInOtherBranch_Negated_CS(string expression, string argType)
{
var validator = CreateIfElseEndValidatorCS(expression, OperationKind.Binary);
var validator = CreateIfElseEndValidatorCS(expression, OperationKind.Binary, argType);
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
validator.ValidateTag("If", x => x.Should().BeNull("We can't tell if it is Null or NotNull in this branch"));
validator.ValidateTag("Else", x => x.HasConstraint(ObjectConstraint.NotNull).Should().BeTrue());
validator.TagValues("End").Should().HaveCount(2)
Expand Down Expand Up @@ -393,8 +445,11 @@ public void Branching_LearnsObjectConstraint_ConstantPattern_Null_Negated(string
[DataTestMethod]
[DataRow("arg is true")]
[DataRow("arg is true", "bool")]
[DataRow("arg is true", "bool?")]
[DataRow("!!(arg is true)")]
[DataRow("!!(arg is true)", "bool?")]
[DataRow("arg is not not true")]
[DataRow("arg is not not true", "bool?")]
public void Branching_LearnsObjectConstraint_ConstantPattern_True(string expression, string argType = "object")
{
var validator = CreateIfElseEndValidatorCS(expression, OperationKind.ConstantPattern, argType);
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -408,6 +463,7 @@ public void Branching_LearnsObjectConstraint_ConstantPattern_True(string express
[DataTestMethod]
[DataRow("arg is not true", "object")]
[DataRow("arg is not true", "bool")]
[DataRow("arg is not true", "bool?")]
public void Branching_LearnsObjectConstraint_ConstantPattern_True_Negated(string expression, string argType)
{
var validator = CreateIfElseEndValidatorCS(expression, OperationKind.ConstantPattern, argType);
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -433,6 +489,7 @@ public void Branching_LearnsObjectConstraint_ConstantPattern_False(string expres

[DataTestMethod]
[DataRow("arg is 42", "int")]
[DataRow("arg is 42", "int?")]
[DataRow("arg is 42", "T")]
[DataRow("arg is 42", "TStruct")]
public void Branching_LearnsObjectConstraint_ConstantPattern_ValueTypes_InputIsNotReferenceType(string expression, string argType)
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -447,6 +504,7 @@ public void Branching_LearnsObjectConstraint_ConstantPattern_ValueTypes_InputIsN
[DataRow(@"arg is ""some text""")]
[DataRow(@"arg is """"")]
[DataRow("arg is 42")]
[DataRow("arg is 42", "int?")]
[DataRow("arg is System.ConsoleKey.Enter")] // Enum
[DataRow("arg is 42", "TClass")]
[DataRow("arg is 42", "IComparable")] // arg is either a class implementing the interface or a boxed value type
martin-strecker-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
Expand Down