-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Fix crash with introduce local within a compilation unit #78701
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
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,7 +15,6 @@ | |
| using Microsoft.CodeAnalysis.Editing; | ||
| using Microsoft.CodeAnalysis.Internal.Log; | ||
| using Microsoft.CodeAnalysis.LanguageService; | ||
| using Microsoft.CodeAnalysis.Operations; | ||
| using Microsoft.CodeAnalysis.PooledObjects; | ||
| using Microsoft.CodeAnalysis.Shared.Extensions; | ||
| using Microsoft.CodeAnalysis.Simplification; | ||
|
|
@@ -32,6 +31,8 @@ internal abstract partial class AbstractIntroduceVariableService<TService, TExpr | |
| where TQueryExpressionSyntax : TExpressionSyntax | ||
| where TNameSyntax : TTypeSyntax | ||
| { | ||
| protected abstract ISyntaxFacts SyntaxFacts { get; } | ||
|
|
||
| protected abstract bool IsInNonFirstQueryClause(TExpressionSyntax expression); | ||
| protected abstract bool IsInFieldInitializer(TExpressionSyntax expression); | ||
| protected abstract bool IsInParameterInitializer(TExpressionSyntax expression); | ||
|
|
@@ -319,54 +320,28 @@ private bool NodeMatchesExpression( | |
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| if (nodeInCurrent == expressionInOriginal) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (allOccurrences && CanReplace(nodeInCurrent)) | ||
| var syntaxFacts = this.SyntaxFacts; | ||
| if (syntaxFacts.IsNameOfAnyMemberAccessExpression(nodeInCurrent) || | ||
| syntaxFacts.IsMemberInitializerNamedAssignmentIdentifier(nodeInCurrent)) | ||
| { | ||
| // Original expression and current node being semantically equivalent isn't enough when the original expression | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment was redundant. SemanticEquivalence.AreEquivalent already has the same comment within it. |
||
| // is a member access via instance reference (either implicit or explicit), the check only ensures that the expression | ||
| // and current node are both backed by the same member symbol. So in this case, in addition to SemanticEquivalence check, | ||
| // we also check if expression and current node are both instance member access. | ||
| // | ||
| // For example, even though the first `c` binds to a field and we are introducing a local for it, | ||
| // we don't want other references to that field to be replaced as well (i.e. the second `c` in the expression). | ||
| // | ||
| // class C | ||
| // { | ||
| // C c; | ||
| // void Test() | ||
| // { | ||
| // var x = [|c|].c; | ||
| // } | ||
| // } | ||
|
|
||
| if (SemanticEquivalence.AreEquivalent(originalSemanticModel, currentSemanticModel, expressionInOriginal, nodeInCurrent)) | ||
| { | ||
| var originalOperation = originalSemanticModel.GetOperation(expressionInOriginal, cancellationToken); | ||
| if (IsInstanceMemberReference(originalOperation)) | ||
| { | ||
| var currentOperation = currentSemanticModel.GetOperation(nodeInCurrent, cancellationToken); | ||
| return IsInstanceMemberReference(currentOperation); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| // If the original expression is within a static local function, further checks are unnecessary since our scope has already been narrowed down to within the local function. | ||
| // If the original expression is not within a static local function, we need to further check whether the expression we're comparing against is within a static local | ||
| // function. If so, the expression is not a valid match since we cannot refer to instance variables from within static local functions. | ||
| if (!IsExpressionInStaticLocalFunction(expressionInOriginal)) | ||
| { | ||
| return !IsExpressionInStaticLocalFunction(nodeInCurrent); | ||
| } | ||
| if (allOccurrences && CanReplace(nodeInCurrent) && | ||
| SemanticEquivalence.AreEquivalent(originalSemanticModel, currentSemanticModel, expressionInOriginal, nodeInCurrent)) | ||
| { | ||
| // If the original expression is within a static local function, further checks are unnecessary since our scope has already been narrowed down to within the local function. | ||
| // If the original expression is not within a static local function, we need to further check whether the expression we're comparing against is within a static local | ||
| // function. If so, the expression is not a valid match since we cannot refer to instance variables from within static local functions. | ||
| if (!IsExpressionInStaticLocalFunction(expressionInOriginal)) | ||
| return !IsExpressionInStaticLocalFunction(nodeInCurrent); | ||
|
|
||
| return true; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
|
|
||
| static bool IsInstanceMemberReference(IOperation operation) | ||
| => operation is IMemberReferenceOperation { Instance.Kind: OperationKind.InstanceReference }; | ||
| } | ||
|
|
||
| protected TNode Rewrite<TNode>( | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
top check was redundant with check in the last clause.