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

fmt::Display compiler panic: len is 0 but the index is 0 #66065

Closed
andrewmorton opened this issue Nov 3, 2019 · 3 comments · Fixed by #66093
Closed

fmt::Display compiler panic: len is 0 but the index is 0 #66065

andrewmorton opened this issue Nov 3, 2019 · 3 comments · Fixed by #66093
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@andrewmorton
Copy link

Hi there! This is the second time I've compiled code with Rust ever, and my first time filing a bug report, so I may need some guidance if the info here isn't quite enough to work with :).

Learning about println! in Rust by Example, I incorrectly entered the below code. I looked at the documentation and realized I had made a mistake, but the compiler still panicked and recommended I open a bug report. I really believe in Rust and want to see this project succeed, hopefully this information is helpful!

  • I tried this code:

println!("Pi is roughly: {:.*}, 3, 3.141592")

  • Expected to see this happen:

"Pi is roughly 3.142"

  • Instead, this happened:

thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', /rustc/625451e376bb2e5283fc4741caa0a3e8a2ca4d54/src/libcore/slice/mod.rs:2715:10
note: run with RUST_BACKTRACE=1 environment variable to display a 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/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.38.0 (625451e 2019-09-23) running on x86_64-unknown-linux-gnu

Meta

rustc --version --verbose:rustc 1.38.0 (625451e 2019-09-23)
binary: rustc
commit-hash: 625451e
commit-date: 2019-09-23
host: x86_64-unknown-linux-gnu
release: 1.38.0
LLVM version: 9.0

Backtrace:

thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', /rustc/625451e376bb2e5283fc4741caa0a3e8a2ca4d54/src/libcore/slice/mod.rs:2715:10
stack backtrace:
0: backtrace::backtrace::libunwind::trace
at /cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/backtrace-0.3.34/src/backtrace/libunwind.rs:88
1: backtrace::backtrace::trace_unsynchronized
at /cargo/registry/src/github.jparrowsec.cn-1ecc6299db9ec823/backtrace-0.3.34/src/backtrace/mod.rs:66
2: std::sys_common::backtrace::_print
at src/libstd/sys_common/backtrace.rs:47
3: std::sys_common::backtrace::print
at src/libstd/sys_common/backtrace.rs:36
4: std::panicking::default_hook::{{closure}}
at src/libstd/panicking.rs:200
5: std::panicking::default_hook
at src/libstd/panicking.rs:214
6: rustc::util::common::panic_hook
7: std::panicking::rust_panic_with_hook
at src/libstd/panicking.rs:481
8: std::panicking::continue_panic_fmt
at src/libstd/panicking.rs:384
9: rust_begin_unwind
at src/libstd/panicking.rs:311
10: core::panicking::panic_fmt
at src/libcore/panicking.rs:85
11: core::panicking::panic_bounds_check
at src/libcore/panicking.rs:61
12: syntax_ext::format::expand_preparsed_format_args
13: syntax_ext::format::expand_format_args_impl
14: ::expand
15: syntax::ext::expand::MacroExpander::expand_fragment
16: syntax::ext::expand::MacroExpander::expand_crate
17: rustc_interface::passes::configure_and_expand_inner::{{closure}}
18: rustc::util::common::time
19: rustc_interface::passes::configure_and_expand_inner
20: rustc_interface::passes::configure_and_expand::{{closure}}
21: rustc_data_structures::box_region::PinnedGenerator<I,A,R>::new
22: rustc_interface::passes::configure_and_expand
23: rustc_interface::queries::Query::compute
24: rustc_interface::queries::::expansion
25: rustc_interface::interface::run_compiler_in_existing_thread_pool
26: std::thread::local::LocalKey::with
27: syntax::with_globals
note: Some details are omitted, run with RUST_BACKTRACE=full for a verbose backtrace.
query stack during panic:
end of query stack

@hman523
Copy link
Contributor

hman523 commented Nov 4, 2019

This code fails on nightly, beta, and stable. Here's a link to the minimal failing code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=e36af7e5086c952448b4138411c4b2a1

@rodrimati1992
Copy link
Contributor

rodrimati1992 commented Nov 4, 2019

The entirety of the println is a string:
println!("Pi is roughly: {:.*}, 3, 3.141592")

So it shouldn't compile,instead it should print this error:

error: 2 positional arguments in format string, but no arguments were given
 --> src/main.rs:3:29
  |
3 |     println!("Pi is roughly: {:.*}, 3, 3.141592")
  |                              ^^^^^

@estebank
Copy link
Contributor

estebank commented Nov 4, 2019

I believe the error is caused in the following line:

(cx.args[i].span, msg)

@andrewmorton the correct code would be println!("Pi is roughly: {:.*}", 3, 3.141592);, but you have encountered a bug in the compiler's diagnostic machinery. The output you should have seen is

error: 2 positional arguments in format string, but no arguments were given
 --> src/test/ui/issues/issue-66065.rs:2:15
  |
2 |     println!("{:.*}");
  |               ^^--^
  |                 |
  |                 this precision flag adds an extra required argument at position 0, which is why there are 2 arguments expected
  |
  = note: positional arguments are zero-based
  = note: for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html

@estebank estebank added A-diagnostics Area: Messages for errors, warnings, and lints I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Nov 4, 2019
@estebank estebank self-assigned this Nov 4, 2019
Centril added a commit to Centril/rust that referenced this issue Nov 5, 2019
Do not ICE with a precision flag in formatting str and no format arguments

Fix rust-lang#66065.
@bors bors closed this as completed in 7d66a09 Nov 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ regression-from-stable-to-stable Performance or correctness regression from one stable version to another. 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.

4 participants