Skip to content

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

Merged
CyrusNajmabadi merged 11 commits intodotnet:mainfrom
CyrusNajmabadi:speculativeConversions
Jul 28, 2023
Merged

Improve and clarify the logic when speculatively comparing old/new code to ensure semantics are the same.#69261
CyrusNajmabadi merged 11 commits intodotnet:mainfrom
CyrusNajmabadi:speculativeConversions

Conversation

@CyrusNajmabadi
Copy link
Copy Markdown
Contributor

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
@ghost ghost 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
Copy Markdown
Contributor 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.

}

protected override bool ReplacementIntroducesErrorType(ExpressionSyntax originalExpression, ExpressionSyntax newExpression)
protected override bool ReplacementIntroducesDisallowedNullType(
Copy link
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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
Copy Markdown
Contributor 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.

@CyrusNajmabadi CyrusNajmabadi requested a review from akhera99 July 27, 2023 19:46
s_emptyCollectionExpression,
semanticModel,
cancellationToken,
skipVerificationForReplacedNode: true,
Copy link
Copy Markdown
Contributor 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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

just a simplification.

else if (currentOriginalNode.Kind() == SyntaxKind.ImplicitArrayCreationExpression)
{
return !TypesAreCompatible((ImplicitArrayCreationExpressionSyntax)currentOriginalNode, (ImplicitArrayCreationExpressionSyntax)currentReplacedNode);
return !TypesAreCompatible((ExpressionSyntax)currentOriginalNode, (ExpressionSyntax)currentReplacedNode);
Copy link
Copy Markdown
Contributor 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.

''' </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
Copy Markdown
Contributor 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
Copy Markdown
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 enabled auto-merge July 27, 2023 20:31
@CyrusNajmabadi CyrusNajmabadi merged commit 411b306 into dotnet:main Jul 28, 2023
@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