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

Migrate reorder_fields Assist to Use SyntaxFactory #18495

Merged

Conversation

tareknaser
Copy link
Contributor

This PR is part of #15710 and #18285

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 8, 2024
fields: impl Iterator<Item = T>,
sorted_fields: impl IntoIterator<Item = T>,
) {
fields.zip(sorted_fields).for_each(|(field, sorted_field)| {
ted::replace(field.syntax(), sorted_field.syntax().clone_for_update())
editor.replace(field.syntax(), sorted_field.syntax().clone_for_update())
Copy link
Contributor

Choose a reason for hiding this comment

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

We need to eliminate this clone_for_update using SyntaxFactory. It looks like we will need to add constructors for record_expr_field and record_pat_field. For now, I would inline this replace function so you don't need to worry about how to abstract it over the two cases. We can try to clean it up afterwards.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Inlined the editor.replace() calls, but the clone_for_update() calls are still needed. Removing them causes the assist to fail tests with an ‘immutable tree’ error

Copy link
Contributor

Choose a reason for hiding this comment

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

Removing them causes the assist to fail tests with an ‘immutable tree’ error

Right, we do still need a mutable node, but my understanding is that we want to hide this under SyntaxFactory so it is abstracted away and so that we track the mappings, hence my suggestion of adding those constructors. That said, in these cases where we do just want to clone, those constructors might not be very convenient to use. @DropDemBits can you check my understanding here?

Copy link
Contributor

Choose a reason for hiding this comment

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

@darichey Generally we'd like to hide the details of creating new nodes under SyntaxFactory. In this case though, we're picking nodes from the original syntax tree so we wouldn't need to go through a SyntaxFactory.

SyntaxEditor should handle transform any node in an edit that reference the original syntax tree into their mutable equivalents, but seems like I forgot this case for the new nodes in replace edits 😅. For now it should be okay to use clone_for_update and add a fixme, and then we can remove those fixmes once SyntaxEditor is fixed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it, thank you! Sorry about that, @tareknaser, you can revert inlining replace and just add a FIXME for the clone_for_update :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All set 5c41c20

@tareknaser tareknaser requested a review from darichey November 12, 2024 17:21
Copy link
Contributor

@darichey darichey left a comment

Choose a reason for hiding this comment

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

Sorry for the churn, this should be good after this!

Comment on lines 79 to 87
fn replace<T: AstNode + PartialEq>(
fields: impl Iterator<Item = T>,
sorted_fields: impl IntoIterator<Item = T>,
) {
fields.zip(sorted_fields).for_each(|(field, sorted_field)| {
ted::replace(field.syntax(), sorted_field.syntax().clone_for_update())
});
}

Copy link
Contributor

Choose a reason for hiding this comment

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

So just bring this back and add like

// FIXME: remove clone_for_update when SyntaxEditor handles it for us
editor.replace(field.syntax(), sorted_field.syntax().clone_for_update());

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
Signed-off-by: Tarek <tareknaser360@gmail.com>
@tareknaser tareknaser force-pushed the syntax_factory_reorder_fields branch from bef15fc to 5c41c20 Compare November 13, 2024 13:02
Copy link
Contributor

@darichey darichey left a comment

Choose a reason for hiding this comment

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

Awesome!

@davidbarsky davidbarsky added this pull request to the merge queue Nov 15, 2024
Merged via the queue into rust-lang:master with commit 1b90e97 Nov 15, 2024
9 checks passed
@tareknaser tareknaser deleted the syntax_factory_reorder_fields branch November 20, 2024 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants