Skip to content

rustc panics when working with regexes and closures #110592

@muja

Description

@muja

Code

use regex; // 1.7.3

fn main() {
    let x = regex::Regex::new("hello|world").unwrap().replace_all(
        "good morning, hello world to you.",
        |m: regex::Captures<'_>| {
            if &m[0] == "hello" {
                "bye"
            } else {
                "earth"
            }
        },
    );

    println!("{x}");
}

Playground link

The same code but with regex::Captures instead of regex::Captures<'_> also panicked:

use regex; // 1.7.3

fn main() {
    let x = regex::Regex::new("hello|world").unwrap().replace_all(
        "good morning, hello world to you.",
        |m: regex::Captures| {
            if &m[0] == "hello" {
                "bye"
            } else {
                "earth"
            }
        },
    );

    println!("{x}");
}

Meta

No panic if I switch to nightly in the playground

Error output

   Compiling playground v0.0.1 (/playground)
error: internal compiler error: compiler/rustc_infer/src/infer/region_constraints/mod.rs:568:17: cannot relate bound region: ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:6 ~ playground[273b]::main::'_), '_) }) <= ReLateBound(DebruijnIndex(0), BoundRegion { var: 1, kind: BrNamed(DefId(20:3266 ~ regex[122a]::re_unicode::{impl#36}::'_#1), '_) })

thread 'rustc' panicked at 'Box<dyn Any>', /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/compiler/rustc_errors/src/lib.rs:987:33
stack backtrace:
   0:     0x7f2fcc45d59a - std::backtrace_rs::backtrace::libunwind::trace::ha271a8a7e1f3d4ef
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7f2fcc45d59a - std::backtrace_rs::backtrace::trace_unsynchronized::h85739da0352c791a
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f2fcc45d59a - std::sys_common::backtrace::_print_fmt::hbc6ebcfb2910b329
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/backtrace.rs:65:5
   3:     0x7f2fcc45d59a - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::he1c117e52d53614f
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f2fcc4bf39e - core::fmt::write::h25eb51b9526b8e0c
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/core/src/fmt/mod.rs:1213:17
   5:     0x7f2fcc44dbe5 - std::io::Write::write_fmt::ha9edec5fb1621933
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/io/mod.rs:1682:15
   6:     0x7f2fcc45d365 - std::sys_common::backtrace::_print::hf8657cd429fc3452
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/backtrace.rs:47:5
   7:     0x7f2fcc45d365 - std::sys_common::backtrace::print::h41b9b18ed86f86bd
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys_common/backtrace.rs:34:9
   8:     0x7f2fcc46012f - std::panicking::default_hook::{{closure}}::h22a91871f4454152
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/panicking.rs:267:22
   9:     0x7f2fcc45fe6b - std::panicking::default_hook::h21ddc36de0cd4ae7
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/panicking.rs:286:9
  10:     0x7f2fcf7b0324 - rustc_driver[70f63b52fde826b7]::DEFAULT_HOOK::{closure#0}::{closure#0}
  11:     0x7f2fcc46096a - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h6f7e3c94ecc52e2f
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/alloc/src/boxed.rs:2002:9
  12:     0x7f2fcc46096a - std::panicking::rust_panic_with_hook::h5059419d6d59b3d0
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/panicking.rs:692:13
  13:     0x7f2fcfa6bf81 - std[f8b79e573431a86c]::panicking::begin_panic::<rustc_errors[53e74ce803854a4d]::ExplicitBug>::{closure#0}
  14:     0x7f2fcfa648d6 - std[f8b79e573431a86c]::sys_common::backtrace::__rust_end_short_backtrace::<std[f8b79e573431a86c]::panicking::begin_panic<rustc_errors[53e74ce803854a4d]::ExplicitBug>::{closure#0}, !>
  15:     0x7f2fcfaf4da6 - std[f8b79e573431a86c]::panicking::begin_panic::<rustc_errors[53e74ce803854a4d]::ExplicitBug>
  16:     0x7f2fcfaf4d96 - std[f8b79e573431a86c]::panic::panic_any::<rustc_errors[53e74ce803854a4d]::ExplicitBug>
  17:     0x7f2fcfaf36c2 - <rustc_errors[53e74ce803854a4d]::HandlerInner>::span_bug::<rustc_span[7c23fb27ec020b97]::span_encoding::Span, &alloc[b346c7f99e9347e5]::string::String>
  18:     0x7f2fcfaf3567 - <rustc_errors[53e74ce803854a4d]::Handler>::span_bug::<rustc_span[7c23fb27ec020b97]::span_encoding::Span, &alloc[b346c7f99e9347e5]::string::String>
  19:     0x7f2fcfaa8acb - rustc_middle[6dfce017f6b7786d]::util::bug::opt_span_bug_fmt::<rustc_span[7c23fb27ec020b97]::span_encoding::Span>::{closure#0}
  20:     0x7f2fcfaa8b1a - rustc_middle[6dfce017f6b7786d]::ty::context::tls::with_opt::<rustc_middle[6dfce017f6b7786d]::util::bug::opt_span_bug_fmt<rustc_span[7c23fb27ec020b97]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  21:     0x7f2fcfaa7bda - rustc_middle[6dfce017f6b7786d]::ty::context::tls::with_context_opt::<rustc_middle[6dfce017f6b7786d]::ty::context::tls::with_opt<rustc_middle[6dfce017f6b7786d]::util::bug::opt_span_bug_fmt<rustc_span[7c23fb27ec020b97]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  22:     0x7f2fcfaa7b06 - rustc_middle[6dfce017f6b7786d]::util::bug::opt_span_bug_fmt::<rustc_span[7c23fb27ec020b97]::span_encoding::Span>
  23:     0x7f2fcfaa7ac4 - rustc_middle[6dfce017f6b7786d]::util::bug::span_bug_fmt::<rustc_span[7c23fb27ec020b97]::span_encoding::Span>
  24:     0x7f2fcd73b604 - <rustc_middle[6dfce017f6b7786d]::ty::sty::Region as rustc_middle[6dfce017f6b7786d]::ty::relate::Relate>::relate::<rustc_infer[83c7bda8c7ffae41]::infer::equate::Equate>
  25:     0x7f2fcd7368bf - rustc_middle[6dfce017f6b7786d]::ty::relate::super_relate_tys::<rustc_infer[83c7bda8c7ffae41]::infer::equate::Equate>
  26:     0x7f2fcd7338be - <rustc_infer[83c7bda8c7ffae41]::infer::equate::Equate as rustc_middle[6dfce017f6b7786d]::ty::relate::TypeRelation>::tys
  27:     0x7f2fcdfc5db3 - <rustc_infer[83c7bda8c7ffae41]::infer::InferCtxt>::commit_if_ok::<rustc_infer[83c7bda8c7ffae41]::infer::InferOk<()>, rustc_middle[6dfce017f6b7786d]::ty::error::TypeError, <rustc_infer[83c7bda8c7ffae41]::infer::at::Trace>::eq<rustc_middle[6dfce017f6b7786d]::ty::Ty>::{closure#0}>
  28:     0x7f2fcdfc5ca6 - <rustc_infer[83c7bda8c7ffae41]::infer::at::At>::eq::<rustc_middle[6dfce017f6b7786d]::ty::Ty>
  29:     0x7f2fd038e39e - <rustc_infer[83c7bda8c7ffae41]::infer::InferCtxt>::can_eq::<rustc_middle[6dfce017f6b7786d]::ty::Ty>
  30:     0x7f2fd03711d2 - rustc_trait_selection[d385e741675330b5]::traits::error_reporting::suggestions::hint_missing_borrow
  31:     0x7f2fd0414934 - <rustc_infer[83c7bda8c7ffae41]::infer::error_reporting::TypeErrCtxt as rustc_trait_selection[d385e741675330b5]::traits::error_reporting::suggestions::TypeErrCtxtExt>::report_closure_arg_mismatch
  32:     0x7f2fd0424788 - <rustc_infer[83c7bda8c7ffae41]::infer::error_reporting::TypeErrCtxt as rustc_trait_selection[d385e741675330b5]::traits::error_reporting::TypeErrCtxtExt>::report_selection_error
  33:     0x7f2fd042e167 - <rustc_infer[83c7bda8c7ffae41]::infer::error_reporting::TypeErrCtxt as rustc_trait_selection[d385e741675330b5]::traits::error_reporting::InferCtxtPrivExt>::report_fulfillment_error
  34:     0x7f2fd04217cf - <rustc_infer[83c7bda8c7ffae41]::infer::error_reporting::TypeErrCtxt as rustc_trait_selection[d385e741675330b5]::traits::error_reporting::TypeErrCtxtExt>::report_fulfillment_errors
  35:     0x7f2fcd9acb93 - <rustc_hir_typeck[698e9809084ff9ec]::fn_ctxt::FnCtxt>::demand_coerce
  36:     0x7f2fcd975689 - <rustc_hir_typeck[698e9809084ff9ec]::fn_ctxt::FnCtxt>::check_decl
  37:     0x7f2fcd972773 - <rustc_hir_typeck[698e9809084ff9ec]::fn_ctxt::FnCtxt>::check_block_with_expected
  38:     0x7f2fcd93cd3c - <rustc_hir_typeck[698e9809084ff9ec]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  39:     0x7f2fcd8d78d1 - <rustc_hir_typeck[698e9809084ff9ec]::fn_ctxt::FnCtxt>::check_return_expr
  40:     0x7f2fcd8ce802 - rustc_hir_typeck[698e9809084ff9ec]::check::check_fn
  41:     0x7f2fcd8b70be - rustc_hir_typeck[698e9809084ff9ec]::typeck
  42:     0x7f2fcd89fda3 - rustc_query_system[59807289ca97d521]::query::plumbing::try_execute_query::<rustc_query_impl[81153385010d7886]::queries::typeck, rustc_query_impl[81153385010d7886]::plumbing::QueryCtxt>
  43:     0x7f2fcef0d3cd - rustc_data_structures[560608da72c35763]::sync::par_for_each_in::<&[rustc_span[7c23fb27ec020b97]::def_id::LocalDefId], <rustc_middle[6dfce017f6b7786d]::hir::map::Map>::par_body_owners<rustc_hir_typeck[698e9809084ff9ec]::typeck_item_bodies::{closure#0}>::{closure#0}>
  44:     0x7f2fcef0d173 - rustc_hir_typeck[698e9809084ff9ec]::typeck_item_bodies
  45:     0x7f2fced309a2 - rustc_query_system[59807289ca97d521]::query::plumbing::try_execute_query::<rustc_query_impl[81153385010d7886]::queries::typeck_item_bodies, rustc_query_impl[81153385010d7886]::plumbing::QueryCtxt>
  46:     0x7f2fcf168d5b - <rustc_query_impl[81153385010d7886]::Queries as rustc_middle[6dfce017f6b7786d]::ty::query::QueryEngine>::typeck_item_bodies
  47:     0x7f2fcdc45ee2 - <rustc_session[cd7c78827427c6d6]::session::Session>::time::<(), rustc_hir_analysis[651175ae4d1746c1]::check_crate::{closure#7}>
  48:     0x7f2fcdc45352 - rustc_hir_analysis[651175ae4d1746c1]::check_crate
  49:     0x7f2fcdc44fab - rustc_interface[698bad460dbd68d3]::passes::analysis
  50:     0x7f2fceedaeee - rustc_query_system[59807289ca97d521]::query::plumbing::try_execute_query::<rustc_query_impl[81153385010d7886]::queries::analysis, rustc_query_impl[81153385010d7886]::plumbing::QueryCtxt>
  51:     0x7f2fcf16560a - <rustc_query_impl[81153385010d7886]::Queries as rustc_middle[6dfce017f6b7786d]::ty::query::QueryEngine>::analysis
  52:     0x7f2fce991010 - <rustc_interface[698bad460dbd68d3]::passes::QueryContext>::enter::<rustc_driver[70f63b52fde826b7]::run_compiler::{closure#1}::{closure#2}::{closure#2}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>
  53:     0x7f2fce98ea84 - rustc_span[7c23fb27ec020b97]::with_source_map::<core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>, rustc_interface[698bad460dbd68d3]::interface::run_compiler<core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>, rustc_driver[70f63b52fde826b7]::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
  54:     0x7f2fce9872d4 - <scoped_tls[1044df3f3db9be03]::ScopedKey<rustc_span[7c23fb27ec020b97]::SessionGlobals>>::set::<rustc_interface[698bad460dbd68d3]::interface::run_compiler<core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>, rustc_driver[70f63b52fde826b7]::run_compiler::{closure#1}>::{closure#0}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>
  55:     0x7f2fce9869d2 - std[f8b79e573431a86c]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[698bad460dbd68d3]::util::run_in_thread_pool_with_globals<rustc_interface[698bad460dbd68d3]::interface::run_compiler<core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>, rustc_driver[70f63b52fde826b7]::run_compiler::{closure#1}>::{closure#0}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>
  56:     0x7f2fce98677a - <<std[f8b79e573431a86c]::thread::Builder>::spawn_unchecked_<rustc_interface[698bad460dbd68d3]::util::run_in_thread_pool_with_globals<rustc_interface[698bad460dbd68d3]::interface::run_compiler<core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>, rustc_driver[70f63b52fde826b7]::run_compiler::{closure#1}>::{closure#0}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[1d432356d8e1e9f1]::result::Result<(), rustc_errors[53e74ce803854a4d]::ErrorGuaranteed>>::{closure#1} as core[1d432356d8e1e9f1]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  57:     0x7f2fcc46a823 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3205ec2d7fc232c5
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/alloc/src/boxed.rs:1988:9
  58:     0x7f2fcc46a823 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3bb5daec8177f56b
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/alloc/src/boxed.rs:1988:9
  59:     0x7f2fcc46a823 - std::sys::unix::thread::Thread::new::thread_start::had7b8061e306bb5c
                               at /rustc/9eb3afe9ebe9c7d2b84b71002d44f4a0edac95e0/library/std/src/sys/unix/thread.rs:108:17
  60:     0x7f2fcc32d609 - start_thread
  61:     0x7f2fcc250133 - clone
  62:                0x0 - <unknown>

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.68.2 (9eb3afe9e 2023-03-27) running on x86_64-unknown-linux-gnu

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

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

query stack during panic:
#0 [typeck] type-checking `main`
#1 [typeck_item_bodies] type-checking all item bodies
#2 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `playground`

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions