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

Input param postconditions #49576

Merged
merged 2 commits into from
Dec 7, 2020
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
41 changes: 30 additions & 11 deletions src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,22 @@ private void ReportNullableAssignmentIfNecessary(
ReportDiagnostic(ErrorCode.WRN_NullReferenceArgument, location,
GetParameterAsDiagnosticArgument(parameterOpt),
GetContainingSymbolAsDiagnosticArgument(parameterOpt));

if (targetType.Type.IsPossiblyNullableReferenceTypeTypeParameter())
{
var slotBuilder = ArrayBuilder<int>.GetInstance();
GetSlotsToMarkAsNotNullable(value, slotBuilder);
foreach (var slot in slotBuilder)
{
Debug.Assert(State[slot] == NullableFlowState.MaybeDefault);
State[slot] = NullableFlowState.MaybeNull;
}
slotBuilder.Free();
}
else
{
LearnFromNonNullTest(value, ref State);
}
}
else if (useLegacyWarnings)
{
Expand Down Expand Up @@ -4230,7 +4246,7 @@ private static TypeWithState GetNullCoalescingResultType(TypeWithState rightResu

// Per LDM 2019-02-13 decision, the result of a conditional access "may be null" even if
// both the receiver and right-hand-side are believed not to be null.
SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeNull));
SetResultType(node, TypeWithState.Create(resultType, NullableFlowState.MaybeDefault));
_currentConditionalReceiverVisitResult = default;
_lastConditionalAccessSlot = previousConditionalAccessSlot;
return null;
Expand Down Expand Up @@ -5308,7 +5324,10 @@ private void VisitArgumentConversionAndInboundAssignmentsAndPreConditions(
stateForLambda: result.StateForLambda);

// If the parameter has annotations, we perform an additional check for nullable value types
Copy link
Member

Choose a reason for hiding this comment

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

nullable value types [](start = 96, length = 20)

Is this just for nullable value types?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, for example in the case of a parameter [DisallowNull] string? s we can adjust the type to just string and then visit the conversion. But in the case of [DisallowNull] int? i we can't adjust the type in that way. SharpLab. Note particularly the difference in diagnostic IDs.

CheckDisallowedNullAssignment(stateAfterConversion, parameterAnnotations, argumentNoConversion.Syntax.Location);
if (CheckDisallowedNullAssignment(stateAfterConversion, parameterAnnotations, argumentNoConversion.Syntax.Location))
{
LearnFromNonNullTest(argumentNoConversion, ref State);
}
SetResultType(argumentNoConversion, stateAfterConversion, updateAnalyzedNullability: false);
}
break;
Expand Down Expand Up @@ -5340,19 +5359,23 @@ private void VisitArgumentConversionAndInboundAssignmentsAndPreConditions(
Debug.Assert(!this.IsConditionalState);
}

private void CheckDisallowedNullAssignment(TypeWithState state, FlowAnalysisAnnotations annotations, Location location, BoundExpression? boundValueOpt = null)
/// <summary>Returns <see langword="true"/> if this is an assignment forbidden by DisallowNullAttribute, otherwise <see langword="false"/>.</summary>
private bool CheckDisallowedNullAssignment(TypeWithState state, FlowAnalysisAnnotations annotations, Location location, BoundExpression? boundValueOpt = null)
{
if (boundValueOpt is { WasCompilerGenerated: true })
{
// We need to skip `return backingField;` in auto-prop getters
return;
return false;
}

// We do this extra check for types whose non-nullable version cannot be represented
if (IsDisallowedNullAssignment(state, annotations))
{
ReportDiagnostic(ErrorCode.WRN_DisallowNullAttributeForbidsMaybeNullAssignment, location);
return true;
}

return false;
}

private static bool IsDisallowedNullAssignment(TypeWithState valueState, FlowAnalysisAnnotations targetAnnotations)
Expand Down Expand Up @@ -5595,10 +5618,6 @@ void learnFromPostConditions(BoundExpression argument, TypeWithAnnotations param
// Note: NotNull = NotNullWhen(true) + NotNullWhen(false)
bool notNullWhenTrue = (parameterAnnotations & FlowAnalysisAnnotations.NotNullWhenTrue) != 0;
bool notNullWhenFalse = (parameterAnnotations & FlowAnalysisAnnotations.NotNullWhenFalse) != 0;
bool disallowNull = (parameterAnnotations & FlowAnalysisAnnotations.DisallowNull) != 0;
bool setNotNullFromParameterType = !argument.IsSuppressed
&& !parameterType.Type.IsPossiblyNullableReferenceTypeTypeParameter()
&& parameterType.NullableAnnotation.IsNotAnnotated();

// Note: MaybeNull = MaybeNullWhen(true) + MaybeNullWhen(false)
bool maybeNullWhenTrue = (parameterAnnotations & FlowAnalysisAnnotations.MaybeNullWhenTrue) != 0;
Expand All @@ -5608,7 +5627,7 @@ void learnFromPostConditions(BoundExpression argument, TypeWithAnnotations param
{
LearnFromNullTest(argument, ref State);
}
else if (((notNullWhenTrue && notNullWhenFalse) || disallowNull || setNotNullFromParameterType)
else if (notNullWhenTrue && notNullWhenFalse
&& !IsConditionalState
&& !(maybeNullWhenTrue || maybeNullWhenFalse))
{
Expand Down Expand Up @@ -7045,9 +7064,9 @@ private TypeWithState VisitUserDefinedConversion(
diagnosticLocation: operandLocation);

// in the case of a lifted conversion, we assume that the call to the operator occurs only if the argument is not-null
if (!isLiftedConversion)
if (!isLiftedConversion && CheckDisallowedNullAssignment(operandType, parameterAnnotations, conversionOperand.Syntax.Location))
{
CheckDisallowedNullAssignment(operandType, parameterAnnotations, conversionOperand.Syntax.Location);
LearnFromNonNullTest(conversionOperand, ref State);
}

// method parameter type -> method return type
Expand Down
Loading