Skip to content

Borrow checking fails in case a closure appears in a const expression #81899

@YohDeadfall

Description

@YohDeadfall

The following code causes compiler panics due to the first line without which the issue isn't reproducible. Calling the function outside of a constant context emits the right compiler errors.

Code

const CACHE: &[u32] = &const_sort(&[2, 3, 1], |a, b| a < b); // that line is a failure reason const fn const_sort<F, const N: usize>(v: &[u32; N], mut is_less: F) -> [u32; N] where F: FnMut(&u32, &u32) -> bool, { let mut r = [0; N]; let mut i = 0; while i < N { r[i] = v[i]; i += 1; } let mut i = 1; while i < N { if is_less(&r[i], &r[i - 1]) { let tmp = r[i]; r[i] = r[i - 1]; let mut j = i - 1; while j > 0 && is_less(&tmp, &r[j - 1]) { r[j] = r[j - 1]; j -= 1; }	r[j] = tmp; } i += 1; } r }

Meta

rustc --version --verbose:

<version> 

Error output

 Compiling playground v0.0.1 (/playground) thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', compiler/rustc_mir/src/borrow_check/region_infer/mod.rs:2136:35 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/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md note: rustc 1.52.0-nightly (9778068cb 2021-02-07) running on x86_64-unknown-linux-gnu note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type lib note: some of the compiler flags provided by cargo are hidden query stack during panic: #0 [mir_borrowck] borrow-checking `CACHE` #1 [analysis] running analysis passes on this crate end of query stack error: could not compile `playground` 
Backtrace

 0: rust_begin_unwind at /rustc/9778068cbc1e06cc3685422323ff38a2f397de26/library/std/src/panicking.rs:493:5 1: core::panicking::panic_fmt at /rustc/9778068cbc1e06cc3685422323ff38a2f397de26/library/core/src/panicking.rs:92:14 2: core::panicking::panic at /rustc/9778068cbc1e06cc3685422323ff38a2f397de26/library/core/src/panicking.rs:50:5 3: rustc_mir::borrow_check::region_infer::RegionInferenceContext::find_outlives_blame_span 4: rustc_mir::borrow_check::do_mir_borrowck 5: rustc_infer::infer::InferCtxtBuilder::enter 6: rustc_mir::borrow_check::mir_borrowck 7: core::ops::function::FnOnce::call_once 8: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::mir_borrowck>::compute 9: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl 10: rustc_data_structures::stack::ensure_sufficient_stack 11: rustc_query_system::query::plumbing::force_query_with_job 12: rustc_query_system::query::plumbing::get_query_impl 13: rustc_query_system::query::plumbing::ensure_query_impl 14: rustc_session::utils::<impl rustc_session::session::Session>::time 15: rustc_interface::passes::analysis 16: rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::analysis>::compute 17: rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl 18: rustc_data_structures::stack::ensure_sufficient_stack 19: rustc_query_system::query::plumbing::force_query_with_job 20: rustc_query_system::query::plumbing::get_query_impl 21: rustc_interface::passes::QueryContext::enter 22: rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter 23: rustc_span::with_source_map 24: rustc_interface::interface::create_compiler_and_run 25: rustc_span::with_session_globals 

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.const-generics-bad-diagnosticsAn error is correctly emitted, but is confusing, for `min_const_generics`.glacierICE tracked in rust-lang/glacier.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions