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

ice: the loans out of scope must be the same as the borrows out of scope #117146

Closed
matthiaskrgr opened this issue Oct 24, 2023 · 3 comments · Fixed by #117560
Closed

ice: the loans out of scope must be the same as the borrows out of scope #117146

matthiaskrgr opened this issue Oct 24, 2023 · 3 comments · Fixed by #117560
Assignees
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ NLL-polonius Issues related for using Polonius in the borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

File: /tmp/im/2/code.rs

snippet:

use std::collections::HashMap;
use std::fmt::Debug;

fn main() {
    let a: HashMap<String, Box<dyn Debug>> = HashMap::new();
    let b = "hello";
    println!(
        "{:?}",
        (match &b[0..=0] {
            _ => Box::new(|x| a.get_mut(x).unwrap() as &Box<dyn Debug>)
                as Box<dyn FnMut(&str) -> &Box<dyn Debug>>,
        })("world")
    );
}

Version information

rustc 1.75.0-nightly (151256bd4 2023-10-24)
binary: rustc
commit-hash: 151256bd4b577f92922c0fbdf94b12d69cfb08d3
commit-date: 2023-10-24
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.3

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc -Zpolonius=next

Program output

error[E0596]: cannot borrow `a` as mutable, as it is not declared as mutable
  --> /tmp/icemaker_global_tempdir.6eZNMIGu1a2g/rustc_testrunner_tmpdir_reporting.MPJwrcNMKUbA/mvce.rs:10:31
   |
5  |     let a: HashMap<String, Box<dyn Debug>> = HashMap::new();
   |         - help: consider changing this to be mutable: `mut a`
...
10 |             _ => Box::new(|x| a.get_mut(x).unwrap() as &Box<dyn Debug>)
   |                               ^ cannot borrow as mutable

error: captured variable cannot escape `FnMut` closure body
  --> /tmp/icemaker_global_tempdir.6eZNMIGu1a2g/rustc_testrunner_tmpdir_reporting.MPJwrcNMKUbA/mvce.rs:10:31
   |
5  |     let a: HashMap<String, Box<dyn Debug>> = HashMap::new();
   |         - variable defined here
...
10 |             _ => Box::new(|x| a.get_mut(x).unwrap() as &Box<dyn Debug>)
   |                             - -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |                             | |
   |                             | returns a reference to a captured variable which escapes the closure body
   |                             | variable captured here
   |                             inferred to be a `FnMut` closure
   |
   = note: `FnMut` closures only have access to their captured variables while they are executing...
   = note: ...therefore, they cannot allow references to captured variables to escape

thread 'rustc' panicked at compiler/rustc_borrowck/src/dataflow.rs:432:13:
assertion `left == right` failed: the loans out of scope must be the same as the borrows out of scope
  left: {bb13[0]: [bw1, bw2, bw3], bb7[0]: [bw1], bb10[0]: [bw2, bw3]}
 right: {bb14[0]: [bw0], bb11[0]: [bw0], bb13[0]: [bw1, bw2, bw3], bb7[0]: [bw1], bb10[0]: [bw2, bw3]}
stack backtrace:
   0:     0x7fbefbbcb17c - std::backtrace_rs::backtrace::libunwind::trace::hcc0166ed719b5803
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7fbefbbcb17c - std::backtrace_rs::backtrace::trace_unsynchronized::hbbfdb0cdfd5a3694
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fbefbbcb17c - std::sys_common::backtrace::_print_fmt::h2acdf0dd6d88fb6d
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7fbefbbcb17c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h8c769e1ecfb97334
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7fbefbc2d400 - core::fmt::rt::Argument::fmt::h0b648738cd9c3905
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/core/src/fmt/rt.rs:142:9
   5:     0x7fbefbc2d400 - core::fmt::write::h8a5a209f873b8707
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/core/src/fmt/mod.rs:1117:17
   6:     0x7fbefbbbf00f - std::io::Write::write_fmt::h78561a5ec2d97aeb
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/io/mod.rs:1762:15
   7:     0x7fbefbbcaf64 - std::sys_common::backtrace::_print::ha6123e36392a6105
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7fbefbbcaf64 - std::sys_common::backtrace::print::h1b26d911e86a441a
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7fbefbbcdbf7 - std::panicking::default_hook::{{closure}}::h0eac92245a13cc26
  10:     0x7fbefbbcd95f - std::panicking::default_hook::ha6311ff3ada643b9
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/panicking.rs:292:9
  11:     0x7fbef8cc47b0 - std[59eea237a8c90763]::panicking::update_hook::<alloc[63ea5590ce573fa8]::boxed::Box<rustc_driver_impl[4dab64b5eaf3dea0]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7fbefbbce338 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h6c50cfa831996edb
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/alloc/src/boxed.rs:2021:9
  13:     0x7fbefbbce338 - std::panicking::rust_panic_with_hook::h57526d95e3d6ee7d
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/panicking.rs:735:13
  14:     0x7fbefbbce08e - std::panicking::begin_panic_handler::{{closure}}::h0e4be4e1cec2f135
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/panicking.rs:609:13
  15:     0x7fbefbbcb646 - std::sys_common::backtrace::__rust_end_short_backtrace::h5b8d993df457b165
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/sys_common/backtrace.rs:170:18
  16:     0x7fbefbbcddf2 - rust_begin_unwind
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/panicking.rs:597:5
  17:     0x7fbefbc29b25 - core::panicking::panic_fmt::h6845268c474f8c17
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/core/src/panicking.rs:72:14
  18:     0x7fbefbc2a181 - core::panicking::assert_failed_inner::h7a8a298c50edfefb
  19:     0x7fbef8a36cf7 - core[7a9d9f52bc7b45fb]::panicking::assert_failed::<indexmap[f4a693edbf7a6e33]::map::IndexMap<rustc_middle[ecb43faa60cb20bb]::mir::Location, alloc[63ea5590ce573fa8]::vec::Vec<rustc_borrowck[d33a3d5eefa9b602]::dataflow::BorrowIndex>, core[7a9d9f52bc7b45fb]::hash::BuildHasherDefault<rustc_hash[a030f08e88c01fdb]::FxHasher>>, indexmap[f4a693edbf7a6e33]::map::IndexMap<rustc_middle[ecb43faa60cb20bb]::mir::Location, alloc[63ea5590ce573fa8]::vec::Vec<rustc_borrowck[d33a3d5eefa9b602]::dataflow::BorrowIndex>, core[7a9d9f52bc7b45fb]::hash::BuildHasherDefault<rustc_hash[a030f08e88c01fdb]::FxHasher>>>
  20:     0x7fbefab9c599 - rustc_borrowck[d33a3d5eefa9b602]::do_mir_borrowck
  21:     0x7fbefab7ce39 - rustc_borrowck[d33a3d5eefa9b602]::mir_borrowck
  22:     0x7fbefab7c9d9 - rustc_query_impl[cc19e181f2c3ddb7]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[cc19e181f2c3ddb7]::query_impl::mir_borrowck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ecb43faa60cb20bb]::query::erase::Erased<[u8; 8usize]>>
  23:     0x7fbef9f4702c - rustc_query_system[1e36878fdb69a02b]::query::plumbing::try_execute_query::<rustc_query_impl[cc19e181f2c3ddb7]::DynamicConfig<rustc_query_system[1e36878fdb69a02b]::query::caches::VecCache<rustc_span[a9e0b1e4bb2a439]::def_id::LocalDefId, rustc_middle[ecb43faa60cb20bb]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[cc19e181f2c3ddb7]::plumbing::QueryCtxt, false>
  24:     0x7fbef9f46c50 - rustc_query_impl[cc19e181f2c3ddb7]::query_impl::mir_borrowck::get_query_non_incr::__rust_end_short_backtrace
  25:     0x7fbefa3fecec - rustc_interface[a6146946eb64d03a]::passes::analysis
  26:     0x7fbefa3fe621 - rustc_query_impl[cc19e181f2c3ddb7]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[cc19e181f2c3ddb7]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ecb43faa60cb20bb]::query::erase::Erased<[u8; 1usize]>>
  27:     0x7fbefa922c26 - rustc_query_system[1e36878fdb69a02b]::query::plumbing::try_execute_query::<rustc_query_impl[cc19e181f2c3ddb7]::DynamicConfig<rustc_query_system[1e36878fdb69a02b]::query::caches::SingleCache<rustc_middle[ecb43faa60cb20bb]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[cc19e181f2c3ddb7]::plumbing::QueryCtxt, false>
  28:     0x7fbefa922a55 - rustc_query_impl[cc19e181f2c3ddb7]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7fbefab0300d - std[59eea237a8c90763]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[a6146946eb64d03a]::util::run_in_thread_with_globals<rustc_interface[a6146946eb64d03a]::interface::run_compiler<core[7a9d9f52bc7b45fb]::result::Result<(), rustc_span[a9e0b1e4bb2a439]::ErrorGuaranteed>, rustc_driver_impl[4dab64b5eaf3dea0]::run_compiler::{closure#1}>::{closure#0}, core[7a9d9f52bc7b45fb]::result::Result<(), rustc_span[a9e0b1e4bb2a439]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[7a9d9f52bc7b45fb]::result::Result<(), rustc_span[a9e0b1e4bb2a439]::ErrorGuaranteed>>
  30:     0x7fbefab022f3 - <<std[59eea237a8c90763]::thread::Builder>::spawn_unchecked_<rustc_interface[a6146946eb64d03a]::util::run_in_thread_with_globals<rustc_interface[a6146946eb64d03a]::interface::run_compiler<core[7a9d9f52bc7b45fb]::result::Result<(), rustc_span[a9e0b1e4bb2a439]::ErrorGuaranteed>, rustc_driver_impl[4dab64b5eaf3dea0]::run_compiler::{closure#1}>::{closure#0}, core[7a9d9f52bc7b45fb]::result::Result<(), rustc_span[a9e0b1e4bb2a439]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[7a9d9f52bc7b45fb]::result::Result<(), rustc_span[a9e0b1e4bb2a439]::ErrorGuaranteed>>::{closure#1} as core[7a9d9f52bc7b45fb]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  31:     0x7fbefbbd9105 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h1e5011952b28f75a
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/alloc/src/boxed.rs:2007:9
  32:     0x7fbefbbd9105 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::ha8fd064af386c0c4
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/alloc/src/boxed.rs:2007:9
  33:     0x7fbefbbd9105 - std::sys::unix::thread::Thread::new::thread_start::h8b6a97c3db8ef5a6
                               at /rustc/151256bd4b577f92922c0fbdf94b12d69cfb08d3/library/std/src/sys/unix/thread.rs:108:17
  34:     0x7fbef60759eb - <unknown>
  35:     0x7fbef60f97cc - <unknown>
  36:                0x0 - <unknown>

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.75.0-nightly (151256bd4 2023-10-24) running on x86_64-unknown-linux-gnu

note: compiler flags: -Z polonius=next -Z dump-mir-dir=dir

query stack during panic:
#0 [mir_borrowck] borrow-checking `main`
#1 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0596`.

@matthiaskrgr matthiaskrgr added I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. labels Oct 24, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 24, 2023
@matthiaskrgr
Copy link
Member Author

cc @lqd

removing the println macro from around the match avoids the ice 🤔

@lqd
Copy link
Member

lqd commented Oct 24, 2023

Fun :)

@lqd lqd self-assigned this Oct 24, 2023
@lqd lqd added NLL-polonius Issues related for using Polonius in the borrow checker and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 25, 2023
@lqd
Copy link
Member

lqd commented Oct 30, 2023

A more minimal version would be:

fn main() {
    let a = ();
    let b = |_| &a;
    bad(&b);
}

fn bad<F: Fn(&()) -> &()>(_: F) {}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ NLL-polonius Issues related for using Polonius in the borrow checker 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.

3 participants