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

Panic when calling .format: "Format: was already formatted once" #694

Closed
flip1995 opened this issue Apr 23, 2023 · 3 comments · Fixed by rust-lang/rust#110721
Closed

Panic when calling .format: "Format: was already formatted once" #694

flip1995 opened this issue Apr 23, 2023 · 3 comments · Fixed by rust-lang/rust#110721

Comments

@flip1995
Copy link

I tried this code:

use itertools::Itertools;
fn main() {
    let mut v = Vec::new();
    v.push("foo");
    assert!(
        v.is_empty(),
        "{}",
        v.iter().format(", "),
    );
}

Playground

And when trying to run it, I got this panic:

Format: was already formatted once
thread 'main' panicked at 'thread 'main' panicked at 'Format: was already formatted once', /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/itertools-0.10.5/src/format.rs:81:21
stack backtrace:
   0:     0x55575722e373 - std::backtrace_rs::backtrace::libunwind::trace::hb9265895128f1f9c
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x55575722e373 - std::backtrace_rs::backtrace::trace_unsynchronized::h028901cb3a0224b1
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x55575722e373 - std::sys_common::backtrace::_print_fmt::hfa5c37f64d314fee
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/sys_common/backtrace.rs:65:5
   3:     0x55575722e373 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h5b30f1684e8bd3e6
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x55575724982f - core::fmt::write::h997ecd0e22e2af7a
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/core/src/fmt/mod.rs:1247:17
   5:     0x55575722c621 - std::io::Write::write_fmt::h291317e85b978e13
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/io/mod.rs:1698:15
   6:     0x55575722e185 - std::sys_common::backtrace::_print::h0133b24def7b99d9
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/sys_common/backtrace.rs:47:5
   7:     0x55575722e185 - std::sys_common::backtrace::print::h4a42174c7f3bd207
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/sys_common/backtrace.rs:34:9
   8:     0x55575722f4ff - std::panicking::default_hook::{{closure}}::h0847452fe926d6e5
   9:     0x55575722f1b7 - std::panicking::default_hook::hbcdd7833994c05d0
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:293:9
  10:     0x55575722fa21 - std::panicking::rust_panic_with_hook::hb55c52c8e21298f0
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:700:13
  11:     0x555757215394 - std::panicking::begin_panic::{{closure}}::hb08efbb470f664d6
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:624:9
  12:     0x5557572152bd - std::sys_common::backtrace::__rust_end_short_backtrace::hbf8634da0334b4e2
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/sys_common/backtrace.rs:150:18
  13:     0x555757215307 - std::panicking::begin_panic::h61b21b87c2e55e8f
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:623:12
  14:     0x555757214cdb - itertools::format::Format<I>::format::heb0f74abc06f4991
                               at /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/itertools-0.10.5/src/format.rs:81:21
  15:     0x5557572149b3 - <itertools::format::Format<I> as core::fmt::Display>::fmt::h064530a6ea8ffcbf
                               at /playground/.cargo/registry/src/index.crates.io-6f17d22bba15001f/itertools-0.10.5/src/format.rs:105:21
  16:     0x55575724982f - core::fmt::write::h997ecd0e22e2af7a
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/core/src/fmt/mod.rs:1247:17
  17:     0x55575724982f - core::fmt::write::h997ecd0e22e2af7a
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/core/src/fmt/mod.rs:1247:17
  18:     0x55575722c621 - std::io::Write::write_fmt::h291317e85b978e13
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/io/mod.rs:1698:15
  19:     0x55575722f3b3 - std::panicking::default_hook::{{closure}}::h0847452fe926d6e5
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:255:21
  20:     0x55575722f1b7 - std::panicking::default_hook::hbcdd7833994c05d0
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:293:9
  21:     0x55575722fa21 - std::panicking::rust_panic_with_hook::hb55c52c8e21298f0
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:700:13
  22:     0x55575722f913 - std::panicking::begin_panic_handler::{{closure}}::h51ad0ee3ee1b4d02
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:595:13
  23:     0x55575722e7b6 - std::sys_common::backtrace::__rust_end_short_backtrace::hd1006853c72eedaf
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/sys_common/backtrace.rs:150:18
  24:     0x55575722f612 - rust_begin_unwind
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:584:5
  25:     0x555757212ed3 - core::panicking::panic_fmt::h0405aceea833a54b
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/core/src/panicking.rs:67:14
  26:     0x555757213f7f - core::panicking::panic_display::he369e4705b14dda4
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/core/src/panicking.rs:150:5
  27:     0x555757215201 - playground::main::hed8c86f138364518
                               at /playground/src/main.rs:5:5
  28:     0x5557572137db - core::ops::function::FnOnce::call_once::hbebd13fb81ee85e2
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/core/src/ops/function.rs:250:5
  29:     0x5557572134ee - std::sys_common::backtrace::__rust_begin_short_backtrace::h8be5514a066de9a8
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/sys_common/backtrace.rs:134:18
  30:     0x555757213561 - std::rt::lang_start::{{closure}}::h283d7d606f728596
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/rt.rs:166:18
  31:     0x55575722a432 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::ha628a292c7e4d1fe
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/core/src/ops/function.rs:284:13
  32:     0x55575722a432 - std::panicking::try::do_call::haef2b0ee78305734
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:490:40
  33:     0x55575722a432 - std::panicking::try::h18b79ccaa1dcd46e
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:454:19
  34:     0x55575722a432 - std::panic::catch_unwind::h44fa38099b98dbce
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panic.rs:140:14
  35:     0x55575722a432 - std::rt::lang_start_internal::{{closure}}::h90d1d020380fa86c
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/rt.rs:148:48
  36:     0x55575722a432 - std::panicking::try::do_call::h96eb402652927015
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:490:40
  37:     0x55575722a432 - std::panicking::try::h9ac5ba472c3806a8
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panicking.rs:454:19
  38:     0x55575722a432 - std::panic::catch_unwind::ha6f72383b6160344
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/panic.rs:140:14
  39:     0x55575722a432 - std::rt::lang_start_internal::hc00ff9425137690f
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/rt.rs:148:20
  40:     0x55575721353a - std::rt::lang_start::hf0c3703aafa2e063
                               at /rustc/b628260df0587ae559253d8640ecb8738d3de613/library/std/src/rt.rs:165:17
  41:     0x55575721523e - main
  42:     0x7f45f2cf9083 - __libc_start_main
  43:     0x55575721340e - _start
  44:                0x0 - <unknown>
thread panicked while panicking. aborting.
timeout: the monitored command dumped core

My workaround is to use join instead.

@flip1995
Copy link
Author

flip1995 commented Apr 23, 2023

Trying this with different compiler versions, it seems like this is a regression in 1.71.0 nightly. I will open a rust-lang/rust issue about this as well.

EDIT: rust-lang/rust#110717

@phimuemue
Copy link
Member

Seems to be the same as #667.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 24, 2023
format panic message only once

For `panic!` and friends, the `std` panic runtime will always set the `.payload()` of `PanicInfo` to the formatted string. The linked issues show that formatting the message twice can cause problems, so we simply print the already formatted message instead of formatting it again. We can't remove the preformatted payload, because it can be observed by custom panic hooks.

fixes rust-lang#110717
fixes rust-itertools/itertools#694

cc `@Amanieu` who broke this in rust-lang#109507
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 24, 2023
format panic message only once

For `panic!` and friends, the `std` panic runtime will always set the `.payload()` of `PanicInfo` to the formatted string. The linked issues show that formatting the message twice can cause problems, so we simply print the already formatted message instead of formatting it again. We can't remove the preformatted payload, because it can be observed by custom panic hooks.

fixes rust-lang#110717
fixes rust-itertools/itertools#694

cc ``@Amanieu`` who broke this in rust-lang#109507
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Apr 24, 2023
format panic message only once

For `panic!` and friends, the `std` panic runtime will always set the `.payload()` of `PanicInfo` to the formatted string. The linked issues show that formatting the message twice can cause problems, so we simply print the already formatted message instead of formatting it again. We can't remove the preformatted payload, because it can be observed by custom panic hooks.

fixes rust-lang#110717
fixes rust-itertools/itertools#694

cc ```@Amanieu``` who broke this in rust-lang#109507
JohnTitor added a commit to JohnTitor/rust that referenced this issue Apr 28, 2023
format panic message only once

Formatting the panic message multiple times can cause problems for some real-world crates, so here's a test to ensure that we don't do that.

This was regressed in rust-lang#109507 and reverted in rust-lang#110782.

fixes rust-lang#110717
fixes rust-itertools/itertools#694
@flip1995
Copy link
Author

With rust-lang/rust#110721 merged, this will work in the next nightly Rust version again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants