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

ICE: string indexed not at a char boundary when constructing error message #91268

Closed
Badel2 opened this issue Nov 26, 2021 · 1 comment
Closed
Assignees
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Badel2
Copy link
Contributor

Badel2 commented Nov 26, 2021

I'm seeing an internal compiler error on the following input, found by fuzz-rustc:

Code

// ţ must be the last character in this file, it cannot be followed by a newline
fn main() {
    0: u8(ţ

Error output

error: this file contains an unclosed delimiter
 --> src/main.rs:3:12
  |
2 | fn main() {
  |           - unclosed delimiter
3 |     0: u8(ţ
  |          - ^
  |          |
  |          unclosed delimiter

error[E0412]: cannot find type `ţ` in this scope
 --> src/main.rs:3:11
  |
3 |     0: u8(ţ
  |           ^ expecting a type here because of type ascription

thread 'rustc' panicked at 'byte index 4 is not a char boundary; it is inside 'ţ' (bytes 3..5) of `u8(ţ`', compiler/rustc_ast_lowering/src/path.rs:236:49
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Affected versions: I tried nightly 2021-11-25 and stable 1.56.1 and they both show the same ICE.

Backtrace

   Compiling playground v0.0.1 (/playground)
error: this file contains an unclosed delimiter
 --> src/main.rs:3:12
  |
2 | fn main() {
  |           - unclosed delimiter
3 |     0: u8(ţ
  |          - ^
  |          |
  |          unclosed delimiter

error[E0412]: cannot find type `ţ` in this scope
 --> src/main.rs:3:11
  |
3 |     0: u8(ţ
  |           ^ expecting a type here because of type ascription

thread 'rustc' panicked at 'byte index 4 is not a char boundary; it is inside 'ţ' (bytes 3..5) of `u8(ţ`', compiler/rustc_ast_lowering/src/path.rs:236:49
stack backtrace:
   0: rust_begin_unwind
             at /rustc/dd549dcab404ec4c7d07b5a83aca5bdd7171138f/library/std/src/panicking.rs:498:5
   1: core::panicking::panic_fmt
             at /rustc/dd549dcab404ec4c7d07b5a83aca5bdd7171138f/library/core/src/panicking.rs:107:14
   2: core::str::slice_error_fail
   3: <rustc_ast_lowering::LoweringContext>::lower_qpath
   4: <rustc_ast_lowering::LoweringContext>::lower_ty_direct
   5: <rustc_ast_lowering::LoweringContext>::lower_ty
   6: rustc_data_structures::stack::ensure_sufficient_stack::<rustc_hir::hir::Expr, <rustc_ast_lowering::LoweringContext>::lower_expr_mut::{closure#0}>
   7: <rustc_ast_lowering::LoweringContext>::lower_stmts
   8: <rustc_ast_lowering::LoweringContext>::lower_block
   9: <rustc_ast_lowering::LoweringContext>::lower_maybe_async_body
  10: <rustc_ast_lowering::LoweringContext>::with_hir_id_owner::<<rustc_ast_lowering::item::ItemLowerer as rustc_ast::visit::Visitor>::visit_item::{closure#0}>
  11: rustc_ast_lowering::lower_crate
  12: <rustc_interface::passes::boxed_resolver::BoxedResolver>::access::<rustc_interface::passes::create_global_ctxt::{closure#0}, &rustc_hir::hir::Crate>
  13: rustc_interface::passes::create_global_ctxt
  14: <rustc_interface::queries::Queries>::global_ctxt
  15: <rustc_interface::interface::Compiler>::enter::<rustc_driver::run_compiler::{closure#1}::{closure#2}, core::result::Result<core::option::Option<rustc_interface::queries::Linker>, rustc_errors::ErrorReported>>
  16: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#1}>
  17: rustc_interface::interface::create_compiler_and_run::<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>
  18: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::util::setup_callbacks_and_run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorReported>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>::{closure#0}::{closure#0}, core::result::Result<(), rustc_errors::ErrorReported>>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: internal compiler error: unexpected panic

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.58.0-nightly (dd549dcab 2021-11-25) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
For more information about this error, try `rustc --explain E0412`.
error: could not compile `playground` due to 2 previous errors

Thanks to the detailed error message I was able to quickly locate the issue, on this line:

let args = &snippet[split + 1..snippet.len() - 1];

It is assumed that the last character of snippet will be a ), so using snippet.len() - 1 as an index should be safe. However as we see in the input code, it is possible to enter that branch without a closing ), and it will trigger the panic if the last character happens to be multibyte.

@Badel2 Badel2 added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 26, 2021
@Badel2
Copy link
Contributor Author

Badel2 commented Nov 26, 2021

@rustbot claim

Badel2 added a commit to Badel2/rust that referenced this issue Nov 26, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 3, 2021
Fix ICE rust-lang#91268 by checking that the snippet ends with a `)`

Fix rust-lang#91268

Previously it was assumed that the last character of `snippet` will be a `)`, so using `snippet.len() - 1` as an index should be safe. However as we see in the test, it is possible to enter that branch without a closing `)`, and it will trigger the panic if the last character happens to be multibyte.

The fix is to ensure that the snippet ends with `)`, and skip the suggestion otherwise.
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 3, 2021
…askrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#88906 (Implement write() method for Box<MaybeUninit<T>>)
 - rust-lang#90269 (Make `Option::expect` unstably const)
 - rust-lang#90854 (Type can be unsized and uninhabited)
 - rust-lang#91170 (rustdoc: preload fonts)
 - rust-lang#91273 (Fix ICE rust-lang#91268 by checking that the snippet ends with a `)`)
 - rust-lang#91381 (Android: -ldl must appear after -lgcc when linking)
 - rust-lang#91453 (Document Windows TLS drop behaviour)
 - rust-lang#91462 (Use try_normalize_erasing_regions in needs_drop)
 - rust-lang#91474 (suppress warning about set_errno being unused on DragonFly)
 - rust-lang#91483 (Sync rustfmt subtree)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in a5ee722 Dec 3, 2021
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. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant