-
Notifications
You must be signed in to change notification settings - Fork 1.9k
feat: convert unmerge_use to SyntaxFactory SyntaxEditor abstraction #18565
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
feat: convert unmerge_use to SyntaxFactory SyntaxEditor abstraction #18565
Conversation
darichey
left a comment
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.
let tree: ast::UseTree = ctx.find_node_at_offset::<ast::UseTree>()?.clone_for_update();This clone_for_update is the culprit of the assertion failed: !self.data().mutable errors. clone_for_update makes the whole tree mutable, and then we later make a SyntaxEditor from a node from the tree (new_parent). When applying the edits, we assume the tree we were given was immutable and do clone_for_update again, hitting this assertion.
Not for this PR, but it would be nice to check this assumption earlier in SyntaxEditor::new to make the mistake more clear.
| ) | ||
| .clone_for_update(); | ||
|
|
||
| tree.remove(); |
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.
This should use editor.delete(...)
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.
Okay so I will remove the .clone_for_update() method. I do still have one more question. The method editor.delete(...) requires an element argument. What should I put in for this argument?
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.
We want to pass a SyntaxNode which implements Element. In this case, the one from tree.
| ted::insert(Position::after(use_.syntax()), new_use.syntax()); | ||
| editor.insert(Position::after(use_.syntax()), new_use.syntax()); | ||
|
|
||
| builder.replace(old_parent_range, new_parent.to_string()); |
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.
This should use editor.replace(...)
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.
Similar to the question above. It appears the old_parent_range and new_parent.to_string() arguments are invalid for the editor.replace method as is.
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.
Right, we'll want to pass the corresponding SyntaxNodes instead. We don't need the range of the old parent, but the old parent itself. Similarly, we don't need the stringified new parent, but the new parent itself.
edit: Actually, we probably don't even need this replace call anymore! The logic with the mutable nodes was a bit roundabout. You can experiment and see if that's true.
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.
The original bug seems to be fixed but I am getting a new error. This one appears similar to the other 'immutable tree' error I was having in add_braces.
failures:
---- handlers::unmerge_use::tests::unmerge_glob_import stdout ----
thread 'handlers::unmerge_use::tests::unmerge_glob_import' panicked at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rowan-0.15.15/src/cursor.rs:824:9:
immutable tree: use std::fmt::*;
note: run with RUST_BACKTRACE=1 environment variable to display a backtrace
---- handlers::unmerge_use::tests::unmerge_indented_use_item stdout ----
thread 'handlers::unmerge_use::tests::unmerge_indented_use_item' panicked at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rowan-0.15.15/src/cursor.rs:824:9:
immutable tree: use std::fmt::Display as Disp;
---- handlers::unmerge_use::tests::unmerge_nested_use_item stdout ----
thread 'handlers::unmerge_use::tests::unmerge_nested_use_item' panicked at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rowan-0.15.15/src/cursor.rs:824:9:
immutable tree: use foo::bar::baz::qux;
---- handlers::unmerge_use::tests::unmerge_renamed_use_item stdout ----
thread 'handlers::unmerge_use::tests::unmerge_renamed_use_item' panicked at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rowan-0.15.15/src/cursor.rs:824:9:
immutable tree: use std::fmt::Display as Disp;
---- handlers::unmerge_use::tests::unmerge_use_item stdout ----
thread 'handlers::unmerge_use::tests::unmerge_use_item' panicked at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rowan-0.15.15/src/cursor.rs:824:9:
immutable tree: use std::fmt::Display;
---- handlers::unmerge_use::tests::unmerge_use_item_on_self stdout ----
thread 'handlers::unmerge_use::tests::unmerge_use_item_on_self' panicked at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rowan-0.15.15/src/cursor.rs:824:9:
immutable tree: use std::process;
---- handlers::unmerge_use::tests::unmerge_use_item_with_visibility stdout ----
thread 'handlers::unmerge_use::tests::unmerge_use_item_with_visibility' panicked at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rowan-0.15.15/src/cursor.rs:824:9:
immutable tree: pub use std::fmt::Display;
---- tests::generated::doctest_unmerge_use stdout ----
thread 'tests::generated::doctest_unmerge_use' panicked at /home/codespace/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rowan-0.15.15/src/cursor.rs:824:9:
immutable tree: use std::fmt::Display;
failures:
handlers::unmerge_use::tests::unmerge_glob_import
handlers::unmerge_use::tests::unmerge_indented_use_item
handlers::unmerge_use::tests::unmerge_nested_use_item
handlers::unmerge_use::tests::unmerge_renamed_use_item
handlers::unmerge_use::tests::unmerge_use_item
handlers::unmerge_use::tests::unmerge_use_item_on_self
handlers::unmerge_use::tests::unmerge_use_item_with_visibility
tests::generated::doctest_unmerge_use
test result: FAILED. 2323 passed; 8 failed; 1 ignored; 0 measured; 0 filtered out; finished in 1.84s
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.
I think you're probably missing a clone_for_update. If you push your changes I can try to help debug.
|
☔ The latest upstream changes (presumably #18483) made this pull request unmergeable. Please resolve the merge conflicts. |
darichey
left a comment
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.
Adding those clone_for_updates will resolve the errors. Run the tests to see the two remaining problems:
- We need to also remove the comma to turn
use std::fmt::{Debug, };intouse std::fmt::{Debug};. This was handled for us here when callingtree.remove(). @tareknaser ran into the same problem in #18484 where you can see he introduced a newSyntaxEditor-basedRemovabletrait and migrated that code. You could do the same thing here or just inline that code for now (either way, one of you will have to rebase over the other and fix up the code later). - We need to fix the whitespace:
use std::fmt::{Debug};use std::fmt::Display;. This was handled for us here when callingted::insert. We probably want a similar helper forSyntaxEditorto insert a fixup whitespace. Don't worry about this for now, I will look into it.
| } | ||
|
|
||
| pub fn use_(&self, visibility: Option<ast::Visibility>, use_tree: ast::UseTree) -> ast::Use { | ||
| make::use_(visibility, use_tree) |
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.
| make::use_(visibility, use_tree) | |
| make::use_(visibility, use_tree).clone_for_update() |
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.
I have implemented this change, but I am unable to run cargo test due to some build error on my side. I think it has to do with the new version of Rust, but I am unable to compile a number of crates like serde and linking with rust-lld with crates like libc and crossbeam-utils
| } | ||
|
|
||
| pub fn use_tree(&self, path: ast::Path, use_tree_list: Option<ast::UseTreeList>, alias: Option<ast::Rename>, add_star: bool) -> ast::UseTree { | ||
| make::use_tree(path, use_tree_list, alias, add_star) |
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.
| make::use_tree(path, use_tree_list, alias, add_star) | |
| make::use_tree(path, use_tree_list, alias, add_star).clone_for_update() |
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.
I have implemented this change, but I am unable to run cargo test due to some build error on my side. I think it has to do with the new version of Rust, but I am unable to compile a number of crates like serde and linking with rust-lld with crates like libc and crossbeam-utils
|
I believe this has been done in #19469 |
Contributes to issue #18285
I think I fully converted this but tests are failing with the following output: