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 #116657

Closed
matthiaskrgr opened this issue Oct 12, 2023 · 2 comments · Fixed by #116960
Closed

ICE: the loans out of scope must be the same as the borrows out of scope #116657

matthiaskrgr opened this issue Oct 12, 2023 · 2 comments · Fixed by #116960
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/F/491D45FF6373EF099BE38CA3B5E5458695A8A45DABFC75AABDAC9D34FE1EC1BC.rs

reduced:

trait Callable {
    type Output;
    fn call(x: Self) -> Self::Output;
}

trait PlusOne {}

impl<'a> PlusOne for &'a mut i32 {}

impl<T: PlusOne> Callable for T {
    type Output = impl PlusOne;
}

fn test<'a>(y: &'a mut i32) -> impl PlusOne {
    <&mut i32 as Callable>::call(y)
}

original:

#![feature(impl_trait_in_assoc_type)]

trait Callable {
    type Output;
    fn call(x: Self) -> Self::Output;
}

trait PlusOne {
    fn plus_one(&mut self);
}

impl<'a> PlusOne for &'a mut i32 {
    fn plus_one(&mut self) {
        **self += 1;
    }
}

impl<T: PlusOne> Callable for T {
    type Output = impl PlusOne;
    fn call(t: T) -> Self::Output {
        t
    }
}

fn test<'a>(y: &'a mut i32) -> impl PlusOne {
    <&'unused_assignments mut i32 as Callable>::call(y)
    //~^ ERROR hidden type for `impl PlusOne` captures lifetime that does not appear in bounds
}

fn main() {
    let mut z = 42;
    let mut thing = test(&mut z);
    let mut thing2 = test(&mut z);
    thing.plus_one();
    assert_eq!(z, 43);
    thing2.plus_one();
    assert_eq!(z, 44);
    thing.plus_one();
    assert_eq!(z, 45);
}

Version information

rustc 1.75.0-nightly (c1691db36 2023-10-11)
binary: rustc
commit-hash: c1691db366c0f2e2341c60377c248ca2d9335076
commit-date: 2023-10-11
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.2

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

Program output:

error[E0261]: use of undeclared lifetime name `'unused_assignments`
  --> /tmp/F/491D45FF6373EF099BE38CA3B5E5458695A8A45DABFC75AABDAC9D34FE1EC1BC.rs:26:7
   |
25 | fn test<'a>(y: &'a mut i32) -> impl PlusOne {
   |         - help: consider introducing lifetime `'unused_assignments` here: `'unused_assignments,`
26 |     <&'unused_assignments mut i32 as Callable>::call(y)
   |       ^^^^^^^^^^^^^^^^^^^ undeclared lifetime

thread 'rustc' panicked at compiler/rustc_borrowck/src/dataflow.rs:425:13:
assertion `left == right` failed: the loans out of scope must be the same as the borrows out of scope
  left: {bb2[0]: [bw0], bb1[0]: [bw0]}
 right: {}
stack backtrace:
   0:     0x7ff9acb6291c - std::backtrace_rs::backtrace::libunwind::trace::h1a396f4229b95b89
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7ff9acb6291c - std::backtrace_rs::backtrace::trace_unsynchronized::had22ccd71191bcf5
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7ff9acb6291c - std::sys_common::backtrace::_print_fmt::h6327fff1763299f2
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7ff9acb6291c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h5c308bc33123b3c6
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7ff9acbca370 - core::fmt::rt::Argument::fmt::hef0d41e90575ec4b
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/core/src/fmt/rt.rs:142:9
   5:     0x7ff9acbca370 - core::fmt::write::h9e48193d66acdcfa
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/core/src/fmt/mod.rs:1117:17
   6:     0x7ff9acb55bdf - std::io::Write::write_fmt::h28ef69c5512926cf
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/io/mod.rs:1763:15
   7:     0x7ff9acb62704 - std::sys_common::backtrace::_print::h55d64ccf5cbdab6d
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7ff9acb62704 - std::sys_common::backtrace::print::h2df81292c2f87861
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7ff9acb65517 - std::panicking::default_hook::{{closure}}::ha7e6872e55317a45
  10:     0x7ff9acb6525a - std::panicking::default_hook::hb6ef52fce67e4118
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/panicking.rs:292:9
  11:     0x7ff9af715295 - std[f742926918452c50]::panicking::update_hook::<alloc[f677c22cdd567b61]::boxed::Box<rustc_driver_impl[13831840ef09002e]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7ff9acb65cb8 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h9b42aa266f672cfd
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/alloc/src/boxed.rs:2021:9
  13:     0x7ff9acb65cb8 - std::panicking::rust_panic_with_hook::h69579898fe8322bc
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/panicking.rs:735:13
  14:     0x7ff9acb65a11 - std::panicking::begin_panic_handler::{{closure}}::h4923080684a4f0a3
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/panicking.rs:609:13
  15:     0x7ff9acb62e36 - std::sys_common::backtrace::__rust_end_short_backtrace::h4bb44302fa0e2d37
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/sys_common/backtrace.rs:170:18
  16:     0x7ff9acb65742 - rust_begin_unwind
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/panicking.rs:597:5
  17:     0x7ff9acbc6995 - core::panicking::panic_fmt::h27c06cc7c9e72203
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/core/src/panicking.rs:72:14
  18:     0x7ff9acbc6ff1 - core::panicking::assert_failed_inner::h733991948e48ad54
  19:     0x7ff9af499ab7 - core[95fcb21667417300]::panicking::assert_failed::<indexmap[fc43bc717b77442a]::map::IndexMap<rustc_middle[75501c6963a0c8e6]::mir::Location, alloc[f677c22cdd567b61]::vec::Vec<rustc_borrowck[806a826476d2135f]::dataflow::BorrowIndex>, core[95fcb21667417300]::hash::BuildHasherDefault<rustc_hash[9187fb2c08beaffe]::FxHasher>>, indexmap[fc43bc717b77442a]::map::IndexMap<rustc_middle[75501c6963a0c8e6]::mir::Location, alloc[f677c22cdd567b61]::vec::Vec<rustc_borrowck[806a826476d2135f]::dataflow::BorrowIndex>, core[95fcb21667417300]::hash::BuildHasherDefault<rustc_hash[9187fb2c08beaffe]::FxHasher>>>
  20:     0x7ff9ae01d832 - rustc_borrowck[806a826476d2135f]::do_mir_borrowck
  21:     0x7ff9ae006967 - rustc_borrowck[806a826476d2135f]::mir_borrowck
  22:     0x7ff9ada16e09 - rustc_query_impl[1ffb7affbb03209e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1ffb7affbb03209e]::query_impl::mir_borrowck::dynamic_query::{closure#2}::{closure#0}, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 8usize]>>
  23:     0x7ff9adddb722 - rustc_query_system[c3767f58cc42410a]::query::plumbing::try_execute_query::<rustc_query_impl[1ffb7affbb03209e]::DynamicConfig<rustc_query_system[c3767f58cc42410a]::query::caches::VecCache<rustc_span[afe2d1e5c4333398]::def_id::LocalDefId, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[1ffb7affbb03209e]::plumbing::QueryCtxt, false>
  24:     0x7ff9af1df4f4 - rustc_query_impl[1ffb7affbb03209e]::query_impl::mir_borrowck::get_query_non_incr::__rust_end_short_backtrace
  25:     0x7ff9af11b47b - rustc_hir_analysis[f8382607e94a3f7b]::collect::type_of::type_of_opaque
  26:     0x7ff9aee23807 - rustc_query_impl[1ffb7affbb03209e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1ffb7affbb03209e]::query_impl::type_of_opaque::dynamic_query::{closure#2}::{closure#0}, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 8usize]>>
  27:     0x7ff9addd9591 - rustc_query_system[c3767f58cc42410a]::query::plumbing::try_execute_query::<rustc_query_impl[1ffb7affbb03209e]::DynamicConfig<rustc_query_system[c3767f58cc42410a]::query::caches::DefaultCache<rustc_span[afe2d1e5c4333398]::def_id::DefId, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[1ffb7affbb03209e]::plumbing::QueryCtxt, false>
  28:     0x7ff9af1d3db2 - rustc_query_impl[1ffb7affbb03209e]::query_impl::type_of_opaque::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7ff9ada3cb4c - rustc_middle[75501c6963a0c8e6]::query::plumbing::query_get_at::<rustc_query_system[c3767f58cc42410a]::query::caches::DefaultCache<rustc_span[afe2d1e5c4333398]::def_id::DefId, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 8usize]>>>
  30:     0x7ff9ae0be95d - rustc_hir_analysis[f8382607e94a3f7b]::collect::type_of::type_of
  31:     0x7ff9adce0bb5 - rustc_query_impl[1ffb7affbb03209e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1ffb7affbb03209e]::query_impl::type_of::dynamic_query::{closure#2}::{closure#0}, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 8usize]>>
  32:     0x7ff9addd9591 - rustc_query_system[c3767f58cc42410a]::query::plumbing::try_execute_query::<rustc_query_impl[1ffb7affbb03209e]::DynamicConfig<rustc_query_system[c3767f58cc42410a]::query::caches::DefaultCache<rustc_span[afe2d1e5c4333398]::def_id::DefId, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[1ffb7affbb03209e]::plumbing::QueryCtxt, false>
  33:     0x7ff9af1d3a4f - rustc_query_impl[1ffb7affbb03209e]::query_impl::type_of::get_query_non_incr::__rust_end_short_backtrace
  34:     0x7ff9ada3cb4c - rustc_middle[75501c6963a0c8e6]::query::plumbing::query_get_at::<rustc_query_system[c3767f58cc42410a]::query::caches::DefaultCache<rustc_span[afe2d1e5c4333398]::def_id::DefId, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 8usize]>>>
  35:     0x7ff9aecbf6a9 - rustc_hir_analysis[f8382607e94a3f7b]::check::check::check_mod_item_types
  36:     0x7ff9adcad8d7 - rustc_query_impl[1ffb7affbb03209e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1ffb7affbb03209e]::query_impl::check_mod_item_types::dynamic_query::{closure#2}::{closure#0}, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 0usize]>>
  37:     0x7ff9ae9d3e04 - rustc_query_system[c3767f58cc42410a]::query::plumbing::try_execute_query::<rustc_query_impl[1ffb7affbb03209e]::DynamicConfig<rustc_query_system[c3767f58cc42410a]::query::caches::DefaultCache<rustc_span[afe2d1e5c4333398]::def_id::LocalModDefId, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[1ffb7affbb03209e]::plumbing::QueryCtxt, false>
  38:     0x7ff9af1dda77 - rustc_query_impl[1ffb7affbb03209e]::query_impl::check_mod_item_types::get_query_non_incr::__rust_end_short_backtrace
  39:     0x7ff9aed87167 - rustc_hir_analysis[f8382607e94a3f7b]::check_crate
  40:     0x7ff9aed7cc3a - rustc_interface[eddcebe151d82f7d]::passes::analysis
  41:     0x7ff9aefca1d7 - rustc_query_impl[1ffb7affbb03209e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[1ffb7affbb03209e]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 1usize]>>
  42:     0x7ff9aefc9dc6 - rustc_query_system[c3767f58cc42410a]::query::plumbing::try_execute_query::<rustc_query_impl[1ffb7affbb03209e]::DynamicConfig<rustc_query_system[c3767f58cc42410a]::query::caches::SingleCache<rustc_middle[75501c6963a0c8e6]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[1ffb7affbb03209e]::plumbing::QueryCtxt, false>
  43:     0x7ff9aefc9b7d - rustc_query_impl[1ffb7affbb03209e]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  44:     0x7ff9aeee4d5d - std[f742926918452c50]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[eddcebe151d82f7d]::util::run_in_thread_with_globals<rustc_interface[eddcebe151d82f7d]::interface::run_compiler<core[95fcb21667417300]::result::Result<(), rustc_span[afe2d1e5c4333398]::ErrorGuaranteed>, rustc_driver_impl[13831840ef09002e]::run_compiler::{closure#1}>::{closure#0}, core[95fcb21667417300]::result::Result<(), rustc_span[afe2d1e5c4333398]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[95fcb21667417300]::result::Result<(), rustc_span[afe2d1e5c4333398]::ErrorGuaranteed>>
  45:     0x7ff9aeee4013 - <<std[f742926918452c50]::thread::Builder>::spawn_unchecked_<rustc_interface[eddcebe151d82f7d]::util::run_in_thread_with_globals<rustc_interface[eddcebe151d82f7d]::interface::run_compiler<core[95fcb21667417300]::result::Result<(), rustc_span[afe2d1e5c4333398]::ErrorGuaranteed>, rustc_driver_impl[13831840ef09002e]::run_compiler::{closure#1}>::{closure#0}, core[95fcb21667417300]::result::Result<(), rustc_span[afe2d1e5c4333398]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[95fcb21667417300]::result::Result<(), rustc_span[afe2d1e5c4333398]::ErrorGuaranteed>>::{closure#1} as core[95fcb21667417300]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  46:     0x7ff9acb70c25 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h725394f575a3e436
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/alloc/src/boxed.rs:2007:9
  47:     0x7ff9acb70c25 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h657091e5671149c6
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/alloc/src/boxed.rs:2007:9
  48:     0x7ff9acb70c25 - std::sys::unix::thread::Thread::new::thread_start::h51f0a69447f1c1c5
                               at /rustc/c1691db366c0f2e2341c60377c248ca2d9335076/library/std/src/sys/unix/thread.rs:108:17
  49:     0x7ff9ac9139eb - <unknown>
  50:     0x7ff9ac9977cc - <unknown>
  51:                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 (c1691db36 2023-10-11) 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 `test`
#1 [type_of_opaque] computing type of opaque `test::{opaque#0}`
#2 [type_of] computing type of `test::{opaque#0}`
#3 [check_mod_item_types] checking item types in top-level module
#4 [analysis] running analysis passes on this crate
end of query stack
error: aborting due to previous error

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

@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. NLL-polonius Issues related for using Polonius in the borrow checker labels Oct 12, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 12, 2023
@matthiaskrgr
Copy link
Member Author

#113218 cc @lqd

@lqd
Copy link
Member

lqd commented Oct 19, 2023

Some notes dump:

  • the original test fuzzed into this MCVE is tests/ui/type-alias-impl-trait/imply_bounds_from_bounds_param.rs. NLLs and -Zpolonius=next do compute the same scopes on that original test.

  • the difference in scopes here is on a somewhat ambiguous case in the current implementation: the loan ultimately flows into 'static via member constraints, not a regular outlives constraint. NLL still computes a scope for it, -Zpolonius=next doesn't since it considers the loan to escape the function.

  • if we ignored the member constraint here and computed the scope anyways, it would result in the same results as NLLs and not trigger the assert. A handful of other tests would then fail of course.

  • the SCC member constraints difference between this test and these other 4 seems to me to be about applied member constraints.

That is, in this test the member constraint is likely not applied, so maybe the loan should only be considered to escape via these. We had discussed this very subject before, but thought the distinction didn't matter for the purpose of computing scopes -- maybe we were wrong in these edge cases.

That should be a correct fix and should work on these 5 tests. So I'll look into that next.

@bors bors closed this as completed in 726709b Oct 23, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 23, 2023
Rollup merge of rust-lang#116960 - lqd:applied-member-constraints-scope, r=matthewjasper

Location-insensitive polonius: consider a loan escaping if an SCC has member constraints applied only

The location-insensitive analysis considered loans to escape if there were member constraints, which makes *some* sense for scopes and matches the scopes that NLL computes on all the tests.

However, polonius and NLLs differ on the fuzzed case rust-lang#116657, where an SCC has member constraints but no applied ones (and is kinda surprising). The existing UI tests with member constraints impacting scopes all have some constraint applied.

This PR changes the location-insensitive analysis to consider a loan to escape if there are applied member constraints, and for extra paranoia/insurance via fuzzing and crater: actually checks the constraint's min choice is indeed a universal region as we expect. (This could be turned into a `debug_assert` and early return as a slight optimization after these periods of verification)

The 4 UI tests where member constraints are meaningful for computing scopes still pass obviously, and this also fixes rust-lang#116657.

r? `@matthewjasper`
@fmease fmease removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 28, 2023
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
4 participants