Skip to content

Commit fbb35c2

Browse files
[automated] Merge branch 'main' => 'main-vs-deps' (#79094)
I detected changes in the main branch which have not been merged yet to main-vs-deps. I'm a robot and am configured to help you automatically keep main-vs-deps up to date, so I've opened this PR. This PR merges commits made on main by the following committers: * CyrusNajmabadi * DoctorKrolic ## Instructions for merging from UI This PR will not be auto-merged. When pull request checks pass, complete this PR by creating a merge commit, *not* a squash or rebase commit. <img alt="merge button instructions" src="https://i.imgur.com/GepcNJV.png" width="300" /> If this repo does not allow creating merge commits from the GitHub UI, use command line instructions. ## Instructions for merging via command line Run these commands to merge this pull request from the command line. ``` sh git fetch git checkout main git pull --ff-only git checkout main-vs-deps git pull --ff-only git merge --no-ff main # If there are merge conflicts, resolve them and then run git merge --continue to complete the merge # Pushing the changes to the PR branch will re-trigger PR validation. git push https://github.com/dotnet/roslyn HEAD:merge/main-to-main-vs-deps ``` <details> <summary>or if you are using SSH</summary> ``` git push git@github.com:dotnet/roslyn HEAD:merge/main-to-main-vs-deps ``` </details> After PR checks are complete push the branch ``` git push ``` ## Instructions for resolving conflicts :warning: If there are merge conflicts, you will need to resolve them manually before merging. You can do this [using GitHub][resolve-github] or using the [command line][resolve-cli]. [resolve-github]: https://help.github.com/articles/resolving-a-merge-conflict-on-github/ [resolve-cli]: https://help.github.com/articles/resolving-a-merge-conflict-using-the-command-line/ ## Instructions for updating this pull request Contributors to this repo have permission update this pull request by pushing to the branch 'merge/main-to-main-vs-deps'. This can be done to resolve conflicts or make other changes to this pull request before it is merged. The provided examples assume that the remote is named 'origin'. If you have a different remote name, please replace 'origin' with the name of your remote. ``` git fetch git checkout -b merge/main-to-main-vs-deps origin/main-vs-deps git pull https://github.com/dotnet/roslyn merge/main-to-main-vs-deps (make changes) git commit -m "Updated PR with my changes" git push https://github.com/dotnet/roslyn HEAD:merge/main-to-main-vs-deps ``` <details> <summary>or if you are using SSH</summary> ``` git fetch git checkout -b merge/main-to-main-vs-deps origin/main-vs-deps git pull git@github.com:dotnet/roslyn merge/main-to-main-vs-deps (make changes) git commit -m "Updated PR with my changes" git push git@github.com:dotnet/roslyn HEAD:merge/main-to-main-vs-deps ``` </details> Contact .NET Core Engineering (dotnet/dnceng) if you have questions or issues. Also, if this PR was generated incorrectly, help us fix it. See https://github.com/dotnet/arcade/blob/main/.github/workflows/scripts/inter-branch-merge.ps1.
2 parents 89c6895 + ad14335 commit fbb35c2

File tree

2 files changed

+539
-11
lines changed

2 files changed

+539
-11
lines changed

src/Features/CSharp/Portable/ConvertPrimaryToRegularConstructor/ConvertPrimaryToRegularConstructorCodeRefactoringProvider.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private static async Task<Solution> ConvertAsync(
119119
RemovePrimaryConstructorParameterList();
120120
RemovePrimaryConstructorBaseTypeArgumentList();
121121
RemovePrimaryConstructorTargetingAttributes();
122-
RemoveDirectFieldAndPropertyAssignments();
122+
await RemoveDirectFieldAndPropertyAssignmentsAsync().ConfigureAwait(false);
123123
AddNewFields();
124124
AddConstructorDeclaration();
125125
await RewritePrimaryConstructorParameterReferencesAsync().ConfigureAwait(false);
@@ -265,14 +265,16 @@ void RemovePrimaryConstructorTargetingAttributes()
265265
mainDocumentEditor.RemoveNode(attributeList);
266266
}
267267

268-
void RemoveDirectFieldAndPropertyAssignments()
268+
async Task RemoveDirectFieldAndPropertyAssignmentsAsync()
269269
{
270270
// Remove all the initializers from existing fields/props the params are assigned to.
271271
foreach (var (_, initializer) in initializedFieldsAndProperties)
272272
{
273+
var editor = await solutionEditor.GetDocumentEditorAsync(solution.GetDocumentId(initializer.SyntaxTree), cancellationToken).ConfigureAwait(false);
274+
273275
if (initializer.Parent is PropertyDeclarationSyntax propertyDeclaration)
274276
{
275-
mainDocumentEditor.ReplaceNode(
277+
editor.ReplaceNode(
276278
propertyDeclaration,
277279
propertyDeclaration
278280
.WithInitializer(null)
@@ -281,7 +283,7 @@ void RemoveDirectFieldAndPropertyAssignments()
281283
}
282284
else if (initializer.Parent is VariableDeclaratorSyntax)
283285
{
284-
mainDocumentEditor.RemoveNode(initializer);
286+
editor.RemoveNode(initializer);
285287
}
286288
else
287289
{
@@ -468,14 +470,17 @@ ConstructorDeclarationSyntax CreateConstructorDeclaration()
468470
assignmentStatements.Add(ExpressionStatement(assignment));
469471
}
470472

471-
// Next, actually assign to all the fields/properties that were previously referencing any primary
472-
// constructor parameters.
473-
foreach (var (fieldOrProperty, initializer) in initializedFieldsAndProperties.OrderBy(i => i.initializer.SpanStart))
473+
// Next, actually assign to all the fields/properties that were previously referencing any primary constructor parameters.
474+
// Chunk assignments by declarations they are in starting from the declaration with the primary constructor
475+
foreach (var location in namedType.Locations.OrderBy(l => !ReferenceEquals(l.SourceTree, typeDeclaration.SyntaxTree)))
474476
{
475-
var left = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), fieldOrProperty.Name.ToIdentifierName())
476-
.WithAdditionalAnnotations(Simplifier.Annotation);
477-
var assignment = AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, left, initializer.EqualsToken, initializer.Value);
478-
assignmentStatements.Add(ExpressionStatement(assignment));
477+
foreach (var (fieldOrProperty, initializer) in initializedFieldsAndProperties.Where(i => ReferenceEquals(i.initializer.SyntaxTree, location.SourceTree)).OrderBy(i => i.initializer.SpanStart))
478+
{
479+
var left = MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, ThisExpression(), fieldOrProperty.Name.ToIdentifierName())
480+
.WithAdditionalAnnotations(Simplifier.Annotation);
481+
var assignment = AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, left, initializer.EqualsToken, initializer.Value);
482+
assignmentStatements.Add(ExpressionStatement(assignment));
483+
}
479484
}
480485

481486
var rewrittenParameters = parameterList.ReplaceNodes(

0 commit comments

Comments
 (0)