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

Implement conditional expression changes in definite assignment #51498

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
50 changes: 42 additions & 8 deletions src/Compilers/CSharp/Portable/FlowAnalysis/AbstractFlowPass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,7 +1115,11 @@ public override BoundNode VisitGlobalStatementInitializer(BoundGlobalStatementIn

public override BoundNode VisitLambda(BoundLambda node) => null;

public override BoundNode VisitLocal(BoundLocal node) => null;
public override BoundNode VisitLocal(BoundLocal node)
{
SplitIfBooleanConstant(node);
return null;
}

public override BoundNode VisitLocalDeclaration(BoundLocalDeclaration node)
{
Expand Down Expand Up @@ -1459,9 +1463,27 @@ public override BoundNode VisitTypeOrValueExpression(BoundTypeOrValueExpression

public override BoundNode VisitLiteral(BoundLiteral node)
{
SplitIfBooleanConstant(node);
return null;
}

protected void SplitIfBooleanConstant(BoundExpression node)
{
if (node.ConstantValue is { IsBoolean: true, BooleanValue: bool booleanValue })
{
var unreachable = UnreachableState();
Split();
if (booleanValue)
{
StateWhenFalse = unreachable;
}
else
{
StateWhenTrue = unreachable;
}
}
}

public override BoundNode VisitMethodDefIndex(BoundMethodDefIndex node)
{
return null;
Expand Down Expand Up @@ -1929,6 +1951,7 @@ protected void AfterRightHasBeenVisited(BoundCompoundAssignmentOperator node)
public override BoundNode VisitFieldAccess(BoundFieldAccess node)
{
VisitFieldAccessInternal(node.ReceiverOpt, node.FieldSymbol);
SplitIfBooleanConstant(node);
return null;
}

Expand Down Expand Up @@ -2619,23 +2642,34 @@ protected virtual BoundNode VisitConditionalOperatorCore(
{
VisitConditionalOperand(alternativeState, alternative, isByRef);
VisitConditionalOperand(consequenceState, consequence, isByRef);
// it may be a boolean state at this point.
}
else if (IsConstantFalse(condition))
{
VisitConditionalOperand(consequenceState, consequence, isByRef);
VisitConditionalOperand(alternativeState, alternative, isByRef);
// it may be a boolean state at this point.
}
else
{
// at this point, the state is conditional after a conditional expression if:
// 1. the state is conditional after the consequence, or
// 2. the state is conditional after the alternative

VisitConditionalOperand(consequenceState, consequence, isByRef);
Unsplit();
consequenceState = this.State;
var conditionalAfterConsequence = IsConditionalState;
var (afterConsequenceWhenTrue, afterConsequenceWhenFalse) = conditionalAfterConsequence ? (StateWhenTrue, StateWhenFalse) : (State, State);

VisitConditionalOperand(alternativeState, alternative, isByRef);
Unsplit();
Join(ref this.State, ref consequenceState);
// it may not be a boolean state at this point (5.3.3.28)
if (!conditionalAfterConsequence && !IsConditionalState)
{
// simplify in the common case
Join(ref this.State, ref afterConsequenceWhenTrue);
}
else
{
Split();
Join(ref this.StateWhenTrue, ref afterConsequenceWhenTrue);
Join(ref this.StateWhenFalse, ref afterConsequenceWhenFalse);
}
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1891,6 +1891,8 @@ public override BoundNode VisitLocal(BoundLocal node)
{
Diagnostics.Add(ErrorCode.ERR_FixedLocalInLambda, new SourceLocation(node.Syntax), localSymbol);
}

SplitIfBooleanConstant(node);
return null;
}

Expand Down
21 changes: 1 addition & 20 deletions src/Compilers/CSharp/Portable/FlowAnalysis/NullableWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8182,22 +8182,6 @@ private TypeWithAnnotations GetDeclaredParameterResult(ParameterSymbol parameter
return null;
}

private void SplitIfBooleanConstant(BoundExpression node)
{
if (node.ConstantValue is { IsBoolean: true, BooleanValue: bool booleanValue })
{
Split();
if (booleanValue)
{
StateWhenFalse = UnreachableState();
}
else
{
StateWhenTrue = UnreachableState();
}
}
}

public override BoundNode? VisitFieldAccess(BoundFieldAccess node)
{
var updatedSymbol = VisitMemberAccess(node, node.ReceiverOpt, node.FieldSymbol);
Expand Down Expand Up @@ -9051,12 +9035,9 @@ private TypeWithState InferResultNullabilityOfBinaryLogicalOperator(BoundExpress

public override BoundNode? VisitLiteral(BoundLiteral node)
{
var result = base.VisitLiteral(node);

Debug.Assert(!IsConditionalState);
var result = base.VisitLiteral(node);
SetResultType(node, TypeWithState.Create(node.Type, node.Type?.CanContainNull() != false && node.ConstantValue?.IsNull == true ? NullableFlowState.MaybeDefault : NullableFlowState.NotNull));

SplitIfBooleanConstant(node);
return result;
}

Expand Down
Loading