rustfix icon indicating copy to clipboard operation
rustfix copied to clipboard

Panic with "Cannot replace slice of data that was already replaced"

Open camelid opened this issue 2 years ago • 6 comments

I have a PR to add a new suggestion to rustc: https://github.com/rust-lang/rust/pull/88672. It uses Diagnostic::multipart_suggestions since there are multiple ways for the user to change their code. These multiple suggestions are exclusive since they modify the same code.

However, rustfix is panicking when I use run-rustfix in a test: "Cannot replace slice of data that was already replaced".

The relevant code in my PR is here: https://github.com/rust-lang/rust/blob/6a89e97ab14cd4c6be70b47fd139c87fd90150b7/compiler/rustc_parse/src/parser/diagnostics.rs#L226-L234 and here: https://github.com/rust-lang/rust/blob/6a89e97ab14cd4c6be70b47fd139c87fd90150b7/compiler/rustc_parse/src/parser/diagnostics.rs#L1272-L1277

I'm not sure how to fix this problem in rustfix, but I'm willing to try with some mentoring :)

camelid avatar Mar 12 '22 23:03 camelid

FWIW, there's some existing code in rustc that references this panic: https://github.com/rust-lang/rust/blob/f103b2969b0088953873dc1ac92eb3387c753596/compiler/rustc_builtin_macros/src/format.rs#L323-L330

camelid avatar Mar 12 '22 23:03 camelid

Diagnostics with multiple exclusive options shouldn't be marked as MachineApplicable (as rustfix obviously can't handle them).

To handle this, rustfix would need to gain the ability to provide a user-interactive multiple-choice UI for choosing which fix to apply. There is some discussion of something like this in #200. It is feasible, though I personally don't think it is very valuable. I prefer to interact within my editor and apply suggestions by clicking quick-fix buttons. That means you don't have to go out and use a separate tool, and makes it easier to see the changes in context, and is generally faster (for me).

ehuss avatar Mar 14 '22 17:03 ehuss

Hi @ehuss, thanks for your quick reply! I would have responded sooner but I missed your message :)

The multiple exclusive options actually are not marked as MachineApplicable. They were marked as MaybeIncorrect and then later Unspecified, but I'm still getting the panic. Any idea why this is happening?

camelid avatar Mar 23 '22 21:03 camelid

If you mean by a UI test, then you'll want to remove the run-rustfix header. That header will apply all suggestions ignoring the applicability. There's also rustfix-only-machine-applicable, but I don't think that applies to your case (assuming no suggestions are machine-applicable).

ehuss avatar Mar 23 '22 22:03 ehuss

Ah, I didn't realize that it applied suggestions indiscriminately! I moved the exclusive suggestions to another test since otherwise the generated .fixed file was invalid. Thanks so much for your help!

Is this panic still considered a bug (and thus I should leave the issue open) or is it considered a sign of something wrong with rustc's output that is thus not a rustfix bug?

camelid avatar Mar 24 '22 00:03 camelid

I'd leave it open. Even though these types of suggestions aren't currently supported, it probably shouldn't panic.

ehuss avatar Mar 24 '22 00:03 ehuss