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

Improve and clarify the logic when speculatively comparing old/new code to ensure semantics are the same. #69261

Merged
merged 11 commits into from
Jul 28, 2023

Conversation

CyrusNajmabadi
Copy link
Member

I'm not a fan of the existing style, which i feel was a little too brittle/unclear. The new style attempts to be much crisper about what it is doing and why.

@CyrusNajmabadi CyrusNajmabadi requested a review from a team as a code owner July 27, 2023 19:32
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 27, 2023
semanticModel,
cancellationToken,
skipVerificationForReplacedNode: true,
skipVerificationForReplacedNode: false,
Copy link
Member Author

Choose a reason for hiding this comment

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

changed this (was a hack originally). we were previously making a change, but then skipping checking the actual node we were replacing. now we check it like all the rest above it to make sure it's ok.

@@ -722,28 +722,65 @@ private bool ReplacementBreaksQueryClause(QueryClauseSyntax originalClause, Quer
!SymbolInfosAreCompatible(originalClauseInfo.OperationInfo, newClauseInfo.OperationInfo);
}

protected override bool ReplacementIntroducesErrorType(ExpressionSyntax originalExpression, ExpressionSyntax newExpression)
protected override bool ReplacementIntroducesDisallowedNullType(
Copy link
Member Author

Choose a reason for hiding this comment

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

broke this into two checks. a check for error-types (always bad), and a check for a null type (sometimes bad, sometimes ok).


// If we didn't have an error before, but now we got one, that's bad and should block conversion in all cases.
if (newTypeInfo.Type.IsErrorType() && !originalTypeInfo.Type.IsErrorType())
return true;
Copy link
Member Author

Choose a reason for hiding this comment

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

this is inlining the error check from before. it is not something that should be overridable.

if (newTypeInfo.Type.IsErrorType() && !originalTypeInfo.Type.IsErrorType())
return true;

if (ReplacementIntroducesDisallowedNullType(originalExpression, newExpression, originalTypeInfo, newTypeInfo))
Copy link
Member Author

Choose a reason for hiding this comment

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

it is then followed by the null-type check. where the language can override if null is a problem or not.

semanticModel,
cancellationToken,
skipVerificationForReplacedNode: true,
Copy link
Member Author

Choose a reason for hiding this comment

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

changed this (was a hack originally). we were previously making a change, but then skipping checking the actual node we were replacing. now we check it like all the rest above it to make sure it's ok.

SyntaxKind.ThisConstructorInitializer or
SyntaxKind.BaseConstructorInitializer or
SyntaxKind.EqualsValueClause or
SyntaxKind.ArrowExpressionClause;
Copy link
Member Author

Choose a reason for hiding this comment

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

just a simplification.

@@ -506,7 +498,7 @@ protected override bool ReplacementChangesSemanticsForNodeLanguageSpecific(Synta
}
else if (currentOriginalNode.Kind() == SyntaxKind.ImplicitArrayCreationExpression)
{
return !TypesAreCompatible((ImplicitArrayCreationExpressionSyntax)currentOriginalNode, (ImplicitArrayCreationExpressionSyntax)currentReplacedNode);
return !TypesAreCompatible((ExpressionSyntax)currentOriginalNode, (ExpressionSyntax)currentReplacedNode);
Copy link
Member Author

Choose a reason for hiding this comment

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

incorrect logic before. the point of this analyzer is that the old construct and new construct might not match, but semantics shoudl still be preserved. so, in this case, the new construct just needs to be an expression, not an implicit array like before.

@@ -42,10 +42,11 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Utilities
''' True if semantic analysis should fail when any of the invocation expression ancestors of <paramref name="expression"/> in original code has overload resolution failures.
''' </param>
Public Sub New(expression As ExpressionSyntax, newExpression As ExpressionSyntax, semanticModel As SemanticModel, cancellationToken As CancellationToken, Optional skipVerificationForReplacedNode As Boolean = False, Optional failOnOverloadResolutionFailuresInOriginalCode As Boolean = False)
MyBase.New(expression, newExpression, semanticModel, cancellationToken, skipVerificationForReplacedNode, failOnOverloadResolutionFailuresInOriginalCode)
MyBase.New(expression, newExpression, semanticModel, skipVerificationForReplacedNode, failOnOverloadResolutionFailuresInOriginalCode, cancellationToken)
Copy link
Member Author

Choose a reason for hiding this comment

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

VB still has skipVerificationForReplacedNode in a few places. I don't love it and i'd like to remove in teh future as well. but out of scope for this PR.

Copy link
Member

@genlu genlu left a comment

Choose a reason for hiding this comment

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

:shipit:

@CyrusNajmabadi CyrusNajmabadi merged commit 411b306 into dotnet:main Jul 28, 2023
23 of 24 checks passed
@ghost ghost added this to the Next milestone Jul 28, 2023
@CyrusNajmabadi CyrusNajmabadi deleted the speculativeConversions branch July 28, 2023 16:42
@dibarbet dibarbet modified the milestones: Next, 17.8 P2 Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE 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.

4 participants