Skip to content

Compiler panic because of adding 2 scope.spawn to each other when running cargo check #143188

Open
@Sylvanbain

Description

@Sylvanbain

Code

// I'm a newbie and I was following Mainmatter's 100-exercises-to-learn-rust. Specifically I was at chapter 07_threads/04_scoped_threads.
// I know I'm not supposed to `+ scope.spawn` but I just wanted to see what `cargo check` will output.
// Apparently compiler panicked because of a bug and a report would be appreciated so here I am.

use std::thread;

pub fn sum(v: Vec<i32>) -> i32 {
    let mid = v.len() / 2;

    let handle = thread::scope(|scope| {
        scope.spawn(|| {
            let slc1 = &v[..mid];
            slc1.iter().sum::<i32>()
        }) + scope.spawn(|| {
            let slc2 = &v[mid..];
            slc2.iter().sum::<i32>()
        })
    });
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn empty() {
        assert_eq!(sum(vec![]), 0);
    }

    #[test]
    fn one() {
        assert_eq!(sum(vec![1]), 1);
    }

    #[test]
    fn five() {
        assert_eq!(sum(vec![1, 2, 3, 4, 5]), 15);
    }

    #[test]
    fn nine() {
        assert_eq!(sum(vec![1, 2, 3, 4, 5, 6, 7, 8, 9]), 45);
    }

    #[test]
    fn ten() {
        assert_eq!(sum(vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 55);
    }
}

Meta

rustc --version --verbose:

rustc 1.88.0 (6b00bc388 2025-06-23)
binary: rustc
commit-hash: 6b00bc3880198600130e1cf62b8f8a93494488cc
commit-date: 2025-06-23
host: x86_64-unknown-linux-gnu
release: 1.88.0
LLVM version: 20.1.5

In rustc 1.89.0-beta.2 the `cargo check` errors were normal without any panics
In rustc 1.90.0-nightly the `cargo check` errors were also normal without any panics

Error output

error: internal compiler error: this path really should be doomed...
  --> exercises/07_threads/04_scoped_threads/src/lib.rs:13:12
   |
13 |         }) + scope.spawn(|| {
   |            ^
   |
note: delayed at compiler/rustc_hir_typeck/src/op.rs:1003:28
         0: <rustc_errors::DiagCtxtInner>::emit_diagnostic
         1: <rustc_errors::DiagCtxtHandle>::emit_diagnostic
         2: <rustc_span::ErrorGuaranteed as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
         3: <rustc_errors::DiagCtxtHandle>::span_delayed_bug::<rustc_span::span_encoding::Span, &str>
         4: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_overloaded_binop
         5: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
         6: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
         7: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
         8: rustc_hir_typeck::check::check_fn
         9: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_closure
        10: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
        11: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_call
        12: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
        13: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_decl
        14: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
        15: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
        16: rustc_hir_typeck::check::check_fn
        17: rustc_hir_typeck::typeck_with_inspect::{closure#0}
        18: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
        19: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_data_structures::vec_cache::VecCache<rustc_span::def_id::LocalDefId, rustc_middle::query::erase::Erased<[u8; 8]>, rustc_query_system::dep_
        20: rustc_query_impl::query_impl::typeck::get_query_incr::__rust_end_short_backtrace
        21: rustc_hir_analysis::check_crate
        22: rustc_interface::passes::run_required_analyses
        23: rustc_interface::passes::analysis
        24: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>
        25: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::Q
        26: rustc_query_impl::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
        27: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
        28: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
        29: std::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler
        30: <<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure
        31: std::sys::pal::unix::thread::Thread::new::thread_start
        32: start_thread
                   at ./nptl/pthread_create.c:447:8
        33: clone3
                   at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:78:0
  --> exercises/07_threads/04_scoped_threads/src/lib.rs:13:12
   |
13 |         }) + scope.spawn(|| {
   |            ^

error: internal compiler error: `&Scope<'_, '_>` overridden by `&Scope<'_, '_>` for HirId(DefId(0:4 ~ scoped_threads[9a6e]::sum).52) in DefId(0:5 ~ scoped_threads[9a6e]::sum::{closure#0})
  --> exercises/07_threads/04_scoped_threads/src/lib.rs:13:14
   |
13 |         }) + scope.spawn(|| {
   |              ^^^^^
   |
note: delayed at compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs:159:28
         0: <rustc_errors::DiagCtxtInner>::emit_diagnostic
         1: <rustc_errors::DiagCtxtHandle>::emit_diagnostic
         2: <rustc_span::ErrorGuaranteed as rustc_errors::diagnostic::EmissionGuarantee>::emit_producing_guarantee
         3: <rustc_errors::DiagCtxtHandle>::span_delayed_bug::<rustc_span::span_encoding::Span, alloc::string::String>
         4: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
         5: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
         6: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_overloaded_binop
         7: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
         8: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
         9: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
        10: rustc_hir_typeck::check::check_fn
        11: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_closure
        12: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
        13: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_call
        14: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
        15: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_decl
        16: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
        17: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
        18: rustc_hir_typeck::check::check_fn
        19: rustc_hir_typeck::typeck_with_inspect::{closure#0}
        20: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::typeck::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 8]>>
        21: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_data_structures::vec_cache::VecCache<rustc_span::def_id::LocalDefId, rustc_middle::query::erase::Erased<[u8; 8]>, rustc_query_system::dep_
        22: rustc_query_impl::query_impl::typeck::get_query_incr::__rust_end_short_backtrace
        23: rustc_hir_analysis::check_crate
        24: rustc_interface::passes::run_required_analyses
        25: rustc_interface::passes::analysis
        26: rustc_query_impl::plumbing::__rust_begin_short_backtrace::<rustc_query_impl::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle::query::erase::Erased<[u8; 0]>>
        27: rustc_query_system::query::plumbing::try_execute_query::<rustc_query_impl::DynamicConfig<rustc_query_system::query::caches::SingleCache<rustc_middle::query::erase::Erased<[u8; 0]>>, false, false, false>, rustc_query_impl::plumbing::QueryCtxt, true>
        28: rustc_query_impl::query_impl::analysis::get_query_incr::__rust_end_short_backtrace
        29: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
        30: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
        31: std::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>
        32: <<std::thread::Builder>::spawn_unchecked_<rustc_interface::util::run_in_thread_with_globals<rustc_interface::util::run_in_thread_pool_with_globals<rustc_interface::interface::run_compiler<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}, ()>::{closure#0}, ()>::{closure#0}::{closure#0}, ()>::{closure#1} as core::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
        33: std::sys::pal::unix::thread::Thread::new::thread_start
        34: start_thread
                   at ./nptl/pthread_create.c:447:8
        35: clone3
                   at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:78:0
  --> exercises/07_threads/04_scoped_threads/src/lib.rs:13:14
   |
13 |         }) + scope.spawn(|| {
   |              ^^^^^

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.88.0 (6b00bc388 2025-06-23) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C overflow-checks=off -C incremental=[REDACTED]

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

query stack during panic:
end of query stack
error: could not compile `scoped_threads` (lib)

Caused by:
  process didn't exit successfully: `/home/sy/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name scoped_threads --edition=2021 exercises/07_threads/04_scoped_threads/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=245 --crate-type lib --emit=dep-info,metadata -C embed-bitcode=no -C debuginfo=2 -C overflow-checks=off --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=8b927482d1ccde1c -C extra-filename=-0b653169ae24ebf1 --out-dir /home/sy/projects/100ex/100-exercises-to-learn-rust/target/debug/deps -C incremental=/home/sy/projects/100ex/100-exercises-to-learn-rust/target/debug/incremental -L dependency=/home/sy/projects/100ex/100-exercises-to-learn-rust/target/debug/deps` (exit status: 101)
Backtrace

❯ RUST_BACKTRACE=1 cargo check
    Checking scoped_threads v0.1.0 (/home/sy/projects/100ex/100-exercises-to-learn-rust/exercises/07_threads/04_scoped_threads)

thread 'rustc' panicked at compiler/rustc_hir_typeck/src/gather_locals.rs:112:17:
assertion `left == right` failed
  left: Some(?42t)
 right: None
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::assert_failed_inner
   3: core::panicking::assert_failed::<core::option::Option<rustc_middle::ty::Ty>, core::option::Option<rustc_middle::ty::Ty>>
   4: <rustc_hir_typeck::gather_locals::GatherLocalsVisitor>::declare
   5: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
   6: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
   7: rustc_hir_typeck::check::check_fn
   8: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_closure
   9: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  10: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_argument_types
  11: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  12: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_overloaded_binop
  13: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  14: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
  15: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  16: rustc_hir_typeck::check::check_fn
  17: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_closure
  18: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  19: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_call
  20: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  21: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_decl
  22: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_block
  23: <rustc_hir_typeck::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  24: rustc_hir_typeck::check::check_fn
  25: rustc_hir_typeck::typeck_with_inspect::{closure#0}
      [... omitted 1 frame ...]
  26: rustc_hir_analysis::check_crate
  27: rustc_interface::passes::run_required_analyses
  28: rustc_interface::passes::analysis
      [... omitted 1 frame ...]
  29: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>::{closure#2}::{closure#0}
  30: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: 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.88.0 (6b00bc388 2025-06-23) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=2 -C overflow-checks=off -C incremental=[REDACTED]

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

query stack during panic:
#0 [typeck] type-checking `sum`
#1 [analysis] running analysis passes on this crate
end of query stack
note: no errors encountered even though delayed bugs were created

note: those delayed bugs will now be shown as internal compiler errors

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