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

Nullable analysis of conditional operator should use best common type unless target typed #73047

Merged
merged 6 commits into from
Apr 18, 2024

Conversation

cston
Copy link
Member

@cston cston commented Apr 16, 2024

Fixes #72898

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 16, 2024
hasErrors = true;
}
else
{
trueExpr = GenerateConversionForAssignment(bestType, trueExpr, diagnostics);
falseExpr = GenerateConversionForAssignment(bestType, falseExpr, diagnostics);
hasErrors = trueExpr.HasAnyErrors || falseExpr.HasAnyErrors;
// If one of the conversions went wrong (e.g. return type of method group being converted
// didn't match), then we don't want to use bestType because it's not accurate.
type = hasErrors ? CreateErrorType() : bestType;
Copy link
Member Author

Choose a reason for hiding this comment

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

To fix the bug, it seems sufficient to either take this change (to use the best common type in binding always), or the change in NullableWalker (to use best common in nullable analysis always). But both changes seemed appropriate to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let me check my understanding.

  1. In this path, bestType is not null and not ErrorType. If one of the conditional expr operands had an ErrorType, we would have inferred ErrorType as the bestType.
  2. The removed lines here are checking if there is some error within the operands, which didn't cause them to have an ErrorType, per (1). We previously thought in this case that we need to change the conditional-expr type to ErrorType in this case. Now we are simply keeping the bestType which we already determined in the face of such errors.

Basically I'm not seeing what purpose the original check was serving, it feels like this change is just propagating along reasonable type information, which allows our assumptions about the bound tree shape to be met when we reach nullable analysis.

Copy link
Contributor

@RikkiGibson RikkiGibson Apr 16, 2024

Choose a reason for hiding this comment

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

The intent, from the comment, seems to be that we are doing a conversion check from the operands to the best type, which can fail, and I also find that a bit confusing. One issue here is that we are not checking specifically if the conversion had errors, we are checking for any kind of errors within the whole operand.

Essentially if one of the operands doesn't convert to the "best type", why was that chosen as the "best type"? Are there some invariants in best type analysis we can rely on in this regard?

Copy link
Member Author

Choose a reason for hiding this comment

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

BestTypeInferrer.InferBestTypeForConditionalOperator() and GenerateConversionForAssignment() are both using ClassifyImplicitConversionFromExpression(), so it's not clear when the GenerateConversionForAssignment() calls above would fail if the best common type is not an error type.

@cston cston marked this pull request as ready for review April 16, 2024 20:23
@cston cston requested a review from a team as a code owner April 16, 2024 20:23
@@ -5867,7 +5867,7 @@ void makeAndAdjustReceiverSlot(BoundExpression receiver)

TypeSymbol? resultType;
bool wasTargetTyped = node is BoundConditionalOperator { WasTargetTyped: true };
if (node.HasErrors || wasTargetTyped)
if (wasTargetTyped)
Copy link
Contributor

@RikkiGibson RikkiGibson Apr 16, 2024

Choose a reason for hiding this comment

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

Would it make sense to change the condition on 5898 if (!wasTargetTyped) to Debug.Assert(!wasTargetTyped)? #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added an assert rather than replacing the if.

IFlowCaptureReferenceOperation: 0 (OperationKind.FlowCaptureReference, Type: ?, IsInvalid, IsImplicit) (Syntax: 'b ? &i1 : &i2')

Right:
IFlowCaptureReferenceOperation: 0 (OperationKind.FlowCaptureReference, Type: System.Int32*, IsInvalid, IsImplicit) (Syntax: 'b ? &i1 : &i2')
Copy link
Contributor

@RikkiGibson RikkiGibson Apr 16, 2024

Choose a reason for hiding this comment

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

It looks like this is the meaningful change in here. I didn't notice it until after looking at it for a bit.

Perhaps we should also exercise the difference in semanticModel.GetTypeInfo(conditionalExprSyntax) as a result of this change. #Resolved

hasErrors = true;
}
else
{
trueExpr = GenerateConversionForAssignment(bestType, trueExpr, diagnostics);
falseExpr = GenerateConversionForAssignment(bestType, falseExpr, diagnostics);
hasErrors = trueExpr.HasAnyErrors || falseExpr.HasAnyErrors;
// If one of the conversions went wrong (e.g. return type of method group being converted
// didn't match), then we don't want to use bestType because it's not accurate.
type = hasErrors ? CreateErrorType() : bestType;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let me check my understanding.

  1. In this path, bestType is not null and not ErrorType. If one of the conditional expr operands had an ErrorType, we would have inferred ErrorType as the bestType.
  2. The removed lines here are checking if there is some error within the operands, which didn't cause them to have an ErrorType, per (1). We previously thought in this case that we need to change the conditional-expr type to ErrorType in this case. Now we are simply keeping the bestType which we already determined in the face of such errors.

Basically I'm not seeing what purpose the original check was serving, it feels like this change is just propagating along reasonable type information, which allows our assumptions about the bound tree shape to be met when we reach nullable analysis.

@cston cston requested a review from a team April 17, 2024 00:09
@cston
Copy link
Member Author

cston commented Apr 17, 2024

@dotnet/roslyn-compiler for a second review, thanks.

var model = comp.GetSemanticModel(tree);
var expr = tree.GetRoot().DescendantNodes().OfType<IdentifierNameSyntax>().Last();
Assert.Equal("y", expr.ToString());
_ = model.GetSymbolInfo(expr);
Copy link
Member

@jcouv jcouv Apr 18, 2024

Choose a reason for hiding this comment

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

Consider verifying the result of GetSymbolInfo on the conditional expression since the change to BoundConditionalOperator.NaturalTypeOpt is surfaced through .Type from this API #Resolved

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 5) with a small test suggestion to consider

@jcouv jcouv self-assigned this Apr 18, 2024
@cston cston merged commit 3dd59d6 into dotnet:main Apr 18, 2024
24 checks passed
@cston cston deleted the 72898 branch April 18, 2024 01:51
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Apr 18, 2024
@dibarbet dibarbet modified the milestones: Next, 17.11 P1 Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compiler conversion crash with collectoin exprs.
4 participants