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

thread 'main' panicked at helix-core/src/graphemes.rs:173:5: assertion failed: char_idx <= slice.len_chars() #12495

Closed
nik-rev opened this issue Jan 11, 2025 · 4 comments
Labels
C-bug Category: This is a bug

Comments

@nik-rev
Copy link
Contributor

nik-rev commented Jan 11, 2025

Summary

Helix panics when trying to insert newliens

Reproduction Steps

On the latest release

> RUST_BACKTRARCE=1 cargo run -- lol.sql
    Finished dev [unoptimized + debuginfo] target(s) in 0.11s
     Running `target/debug/hx lol.sql`
thread 'main' panicked at helix-core/src/graphemes.rs:173:5:
assertion failed: char_idx <= slice.len_chars()
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

With the following file, cursor at the beginning:

id 1,Item 1,cost 1,location 1
id 2,Item 2,cost 2,location 2
id 10,Item 10,cost 10,location 10
  • Press % to select full file
  • Place cursor on each digit with s\d
  • Go into insert mode i then add a newline <enter>
  • Panic happens here.
Backtrace
thread 'main' panicked at helix-core/src/graphemes.rs:173:5:
assertion failed: char_idx <= slice.len_chars()
stack backtrace:
   0: rust_begin_unwind
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_fmt
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
   2: core::panicking::panic
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:144:5
   3: helix_core::graphemes::nth_next_grapheme_boundary
             at ./helix-core/src/graphemes.rs:173:5
   4: helix_core::graphemes::next_grapheme_boundary
             at ./helix-core/src/graphemes.rs:252:5
   5: helix_core::graphemes::ensure_grapheme_boundary_next
             at ./helix-core/src/graphemes.rs:270:9
   6: helix_core::selection::Range::grapheme_aligned
             at ./helix-core/src/selection.rs:288:17
   7: helix_core::selection::Selection::ensure_invariants::{{closure}}
             at ./helix-core/src/selection.rs:664:28
   8: helix_core::selection::Selection::transform
             at ./helix-core/src/selection.rs:642:22
   9: helix_core::selection::Selection::ensure_invariants
             at ./helix-core/src/selection.rs:664:9
  10: helix_view::document::Document::apply_impl
             at ./helix-view/src/document.rs:1441:17
  11: helix_view::document::Document::apply_inner
             at ./helix-view/src/document.rs:1484:23
  12: helix_view::document::Document::apply
             at ./helix-view/src/document.rs:1496:9
  13: helix_term::commands::insert::insert_newline
             at ./helix-term/src/commands.rs:4110:9
  14: helix_term::commands::MappableCommand::execute
             at ./helix-term/src/commands.rs:261:41
  15: helix_term::ui::editor::EditorView::handle_keymap_event::{{closure}}
             at ./helix-term/src/ui/editor.rs:893:13
  16: helix_term::ui::editor::EditorView::handle_keymap_event
             at ./helix-term/src/ui/editor.rs:919:17
  17: helix_term::ui::editor::EditorView::insert_mode
             at ./helix-term/src/ui/editor.rs:933:34
  18: <helix_term::ui::editor::EditorView as helix_term::compositor::Component>::handle_event
             at ./helix-term/src/ui/editor.rs:1466:33
  19: helix_term::compositor::Compositor::handle_event
             at ./helix-term/src/compositor.rs:152:19
  20: helix_term::application::Application::handle_terminal_events::{{closure}}
             at ./helix-term/src/application.rs:668:22
  21: helix_term::application::Application::event_loop_until_idle::{{closure}}
             at ./helix-term/src/application.rs:341:56
  22: helix_term::application::Application::event_loop::{{closure}}
             at ./helix-term/src/application.rs:315:58
  23: helix_term::application::Application::run::{{closure}}
             at ./helix-term/src/application.rs:1230:39
  24: hx::main_impl::{{closure}}
             at ./helix-term/src/main.rs:157:54
  25: <core::pin::Pin<P> as core::future::future::Future>::poll
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/future/future.rs:124:9
  26: tokio::runtime::park::CachedParkThread::block_on::{{closure}}
             at /home/e/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/runtime/park.rs:281:63
  27: tokio::runtime::coop::with_budget
             at /home/e/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/runtime/coop.rs:107:5
  28: tokio::runtime::coop::budget
             at /home/e/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/runtime/coop.rs:73:5
  29: tokio::runtime::park::CachedParkThread::block_on
             at /home/e/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/runtime/park.rs:281:31
  30: tokio::runtime::context::blocking::BlockingRegionGuard::block_on
             at /home/e/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/runtime/context/blocking.rs:66:9
  31: tokio::runtime::scheduler::multi_thread::MultiThread::block_on::{{closure}}
             at /home/e/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/runtime/scheduler/multi_thread/mod.rs:87:13
  32: tokio::runtime::context::runtime::enter_runtime
             at /home/e/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/runtime/context/runtime.rs:65:16
  33: tokio::runtime::scheduler::multi_thread::MultiThread::block_on
             at /home/e/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/runtime/scheduler/multi_thread/mod.rs:86:9
  34: tokio::runtime::runtime::Runtime::block_on_inner
             at /home/e/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/runtime/runtime.rs:370:45
  35: tokio::runtime::runtime::Runtime::block_on
             at /home/e/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.42.0/src/runtime/runtime.rs:340:13
  36: hx::main_impl
             at ./helix-term/src/main.rs:159:5
  37: hx::main
             at ./helix-term/src/main.rs:37:21
  38: core::ops::function::FnOnce::call_once
             at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Helix log

empty

Platform

Linux

Terminal Emulator

wezterm 8e9cf91

Installation Method

Built from source

Helix Version

helix 25.1 (6c9619d)

@nik-rev nik-rev added the C-bug Category: This is a bug label Jan 11, 2025
@nik-rev nik-rev changed the title panic in ropey-1.6.1: Called Result::unwrap() on an Err value, char index out of bounds thread 'main' panicked at helix-core/src/graphemes.rs:173:5: assertion failed: char_idx <= slice.len_chars() Jan 11, 2025
@freelon
Copy link

freelon commented Jan 11, 2025

This seems to be a regression. I see the same panic with 25.1 but not with 24.7 (using konsole).

@RoloEdits
Copy link
Contributor

A bisect points to 1e6fe00

@nik-rev
Copy link
Contributor Author

nik-rev commented Jan 12, 2025

In this line:

id 10,Item 10,cost 10,location 10

Observations:

If you place a cursor at each digit except the last 0, then enter a newline, the panic won't happen

If you do have a cursor at the last 0, then the panic happens

If you have a cursor on the 4th, 5th, 6th, 7th and the last 8th digit then the panic won't happen

If you have a cursor on the 3rd digit as well as all the above, the panic happens.

@nik-rev
Copy link
Contributor Author

nik-rev commented Jan 12, 2025

I finally figured it out: #12510

rmburg pushed a commit to rmburg/helix that referenced this issue Jan 20, 2025
helix-editor#12177 changed `insert_newline`'s behavior to trim any trailing
whitespace on a line which came before a cursor. `insert_newline` would
previously never delete text. Even the whitespace stripping behavior in
helix-editor#4854 worked by inserting text - a line ending at the beginning of the
line. `global_offs`, a variable that tracks the number of characters
inserted between iterations over the existing selection ranges, was not
updated to also account for text deleted by the trimming behavior,
causing cursors to be offset by the amount of trailing space deleted
and causing panics in some cases.

To fix this we need to subtract the number of trimmed whitespace
characters from `global_offs`. `global_offs` must become an `isize`
(was a `usize`) because it may become negative in cases where a lot of
trailing whitespace is trimmed. Integration tests have been added for
each of these cases.

Fixes helix-editor#12461
Fixes helix-editor#12495
Fixes helix-editor#12539
@the-mikedavis the-mikedavis marked this as a duplicate of #12626 Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants