rust-lang / rust-lang/rust-analyzer

replace doesn't work on Theia but others like add labels or remove does

Open
#18,868 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

Discussed in https://github.com/rust-lang/rust-analyzer/discussions/18839

Originally posted by hsnoil January 6, 2025
When using theia, and you have something like this it doesn't work.

enum Pet {
    Cat,
    Dog
}


fn main() {
    let pet = Pet::Cat;
    match pet {
        
    }
}

But if you were to just add like add_label_to_loop, it works:

 fn main() {
     loop {
         break;
         continue;
     }
 }

Another interesting case is this one:

 struct X();
 mod foo {
     use super::X;
 }

If you do "split import" or "remove whole use item" they work but remove all unused imports doesn't.

From the looks of it, replacing with syntax::ted::replace is the issue. Replacing, adding and deleting works unless it touches that function.

Is this an issue with Theia or an underlying issue with how rowan's splice_children works? I know it works with vscode, but sometime things are done in workaround manners (like match arms in analyzer plugin has a bunch of FIXME there)

For now I opened a ticket with Theia:

https://github.com/eclipse-theia/theia/issues/13922

But it has been a while and likely it being rust and not javascript, if someone isn't explicitly familiar with rust I can understand the hesitation to go through it all. Anyone have any idea what the issue can be?

Edit:

I compiled it with with debug=2 and I got this error:

thread 'Worker' panicked at /home/user/Develop/rowan/src/ast.rs:99:9:
tree is mutable
stack backtrace:
   0: rust_begin_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:652:5
   1: core::panicking::panic_fmt
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/panicking.rs:72:14
   2: rowan::ast::SyntaxNodePtr<L>::try_to_node
             at /home/user/Develop/rowan/src/ast.rs:99:9
   3: rowan::ast::SyntaxNodePtr<L>::to_node
             at /home/user/Develop/rowan/src/ast.rs:81:9
   4: ide_db::source_change::TreeMutator::make_syntax_mut
             at /home/user/Develop/rust-analyzer-master/crates/ide-db/src/source_change.rs:230:9
   5: ide_db::source_change::SourceChangeBuilder::make_syntax_mut
             at /home/user/Develop/rust-analyzer-master/crates/ide-db/src/source_change.rs:282:9
   6: ide_assists::handlers::add_missing_match_arms::add_missing_match_arms::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/ide-assists/src/handlers/add_missing_match_arms.rs:274:53
   7: ide_assists::assist_context::Assists::add::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/ide-assists/src/assist_context.rs:161:65
   8: ide_assists::assist_context::Assists::add_impl
             at /home/user/Develop/rust-analyzer-master/crates/ide-assists/src/assist_context.rs:191:13
   9: ide_assists::assist_context::Assists::add
             at /home/user/Develop/rust-analyzer-master/crates/ide-assists/src/assist_context.rs:161:9
  10: ide_assists::handlers::add_missing_match_arms::add_missing_match_arms
             at /home/user/Develop/rust-analyzer-master/crates/ide-assists/src/handlers/add_missing_match_arms.rs:201:5
  11: ide_assists::assists::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/ide-assists/src/lib.rs:95:9
  12: <core::slice::iter::Iter<T> as core::iter::traits::iterator::Iterator>::for_each
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/slice/iter/macros.rs:252:21
  13: ide_assists::assists
             at /home/user/Develop/rust-analyzer-master/crates/ide-assists/src/lib.rs:94:5
  14: ide::Analysis::assists_with_fixes::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/ide/src/lib.rs:730:27
  15: ide::Analysis::with_db::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/ide/src/lib.rs:826:29
  16: std::panicking::try::do_call
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:559:40
  17: __rust_try
  18: std::panicking::try
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:523:19
  19: std::panic::catch_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panic.rs:149:14
  20: salsa::Cancelled::catch
             at /home/user/Develop/rust-analyzer-master/crates/salsa/src/lib.rs:633:15
  21: ide::Analysis::with_db
             at /home/user/Develop/rust-analyzer-master/crates/ide/src/lib.rs:826:9
  22: ide::Analysis::assists_with_fixes
             at /home/user/Develop/rust-analyzer-master/crates/ide/src/lib.rs:719:9
  23: rust_analyzer::handlers::request::handle_code_action_resolve
             at /home/user/Develop/rust-analyzer-master/crates/rust-analyzer/src/handlers/request.rs:1384:19
  24: rust_analyzer::handlers::dispatch::RequestDispatcher::on_with_thread_intent::{{closure}}::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/rust-analyzer/src/handlers/dispatch.rs:219:17
  25: std::panicking::try::do_call
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:559:40
  26: __rust_try
  27: std::panicking::try
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panicking.rs:523:19
  28: std::panic::catch_unwind
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/std/src/panic.rs:149:14
  29: rust_analyzer::handlers::dispatch::RequestDispatcher::on_with_thread_intent::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/rust-analyzer/src/handlers/dispatch.rs:217:26
  30: rust_analyzer::task_pool::TaskPool<T>::spawn::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/rust-analyzer/src/task_pool.rs:26:33
  31: stdx::thread::pool::Pool::spawn::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/stdx/src/thread/pool.rs:82:13
  32: core::ops::function::FnOnce::call_once{{vtable.shim}}
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/core/src/ops/function.rs:250:5
  33: <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once
             at /rustc/3f5fd8dd41153bc5fdca9427e9e05be2c767ba23/library/alloc/src/boxed.rs:2063:9
  34: stdx::thread::pool::Pool::new::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/stdx/src/thread/pool.rs:61:29
  35: stdx::thread::Builder::spawn::{{closure}}
             at /home/user/Develop/rust-analyzer-master/crates/stdx/src/thread.rs:66:13
```</div>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Theia failure with the match-arm and unused-import examples, then read crates/ide-db/src/source_change.rs around TreeMutator::make_syntax_mut and crates/ide-assists/src/handlers/add_missing_match_arms.rs around line 274. Compare the behavior with the linked Theia and Rowan discussions. Done means the affected assist no longer panics or fails in Theia, with a regression test covering the reproducer.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.