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: Infer constraints for HasValue property #6850

Merged
merged 2 commits into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -73,16 +73,28 @@ protected override IPropertyReferenceOperationWrapper Convert(IOperation operati

protected override ProgramState Process(SymbolicContext context, IPropertyReferenceOperationWrapper propertyReference)
{
var state = context.State;
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
? state.SetOperationValue(context.Operation, value)
: state;
if (propertyReference.Instance.Type.IsNullableValueType())
{
if (propertyReference.Property.Name == "Value" && state[symbol] is { } value)
{
state = state.SetOperationValue(context.Operation, value);
}
else if (propertyReference.Property.Name == "HasValue")
{
// Return directly, do not set NotNull on the symbol itself
return state[symbol]?.Constraint<ObjectConstraint>() is { } objectConstraint
? state.SetOperationConstraint(context.Operation, BoolConstraint.From(objectConstraint == ObjectConstraint.NotNull))
: state;
}
}
return state.SetSymbolConstraint(symbol, ObjectConstraint.NotNull);
}
else
{
return context.State;
return state;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,30 @@ public void Nullable_Value_ReadsConstraintsFromInstance()
validator.ValidateTag("FalseFirst", x => x.HasConstraint(BoolConstraint.False).Should().BeTrue());
validator.ValidateTag("FalseFirst", x => x.HasConstraint(TestConstraint.First).Should().BeTrue());
}

[TestMethod]
public void Nullable_HasValue_ReadsBoolConstraintFromObjectConstraint()
{
const string code = """
var hasValue = arg.HasValue;
Tag("HasValueUnknown", hasValue);
Tag("SymbolUnknown", arg);
arg = true;
hasValue = arg.HasValue;
Tag("HasValueAfterTrue", hasValue);
Tag("SymbolAfterTrue", arg);
arg = null;
hasValue = arg.HasValue;
Tag("HasValueAfterNull", hasValue);
Tag("SymbolAfterNull", arg);
""";
var validator = SETestContext.CreateCS(code, ", bool? arg").Validator;

Choose a reason for hiding this comment

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

The test would be easier to read if we use int? instead. It's confusing to keep track of HasValue, arg, and BoolConstraint at the same time. Maybe add a second test case with int??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

int doesn't have any constraint on it. I wouldn't mind if you rewrite it in your PRs. As there's ToDo for you here anyway :)

validator.ValidateTag("HasValueUnknown", x => x.Should().BeNull());
validator.ValidateTag("SymbolUnknown", x => x.Should().BeNull());
validator.ValidateTag("HasValueAfterTrue", x => x.Should().BeNull()); // ToDo: Should be x.HasConstraint(BoolConstraint.True).Should().BeTrue()); after we build NotNull for bool literal.
validator.ValidateTag("SymbolAfterTrue", x => x.HasConstraint(BoolConstraint.True).Should().BeTrue());
validator.ValidateTag("SymbolAfterTrue", x => x.HasConstraint(ObjectConstraint.NotNull).Should().BeFalse()); // ToDo: Should be BeTrue() after we build NotNull for bool literal
validator.ValidateTag("HasValueAfterNull", x => x.HasConstraint(BoolConstraint.False).Should().BeTrue());
validator.ValidateTag("SymbolAfterNull", x => x.HasConstraint(ObjectConstraint.Null).Should().BeTrue());
}
}