rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
96.07k stars 12.41k forks source link

[const_closures, const_mut_refs, effects] thread 'rustc' panicked at rustc_middle/src/ty/util.rs:945:22: ConstContext::Maybe must have host effect param #125866

Open gendx opened 2 months ago

gendx commented 2 months ago

I know that const_closures feature is incomplete (https://github.com/rust-lang/rust/issues/106003). This is to report that I managed to use it with simple const closures such as const |i| 2 * i as u32, but slightly more complex expressions like const |i| 2 * i as u32 + 1 cause an ICE.

Code

#![feature(const_closures, const_mut_refs, effects)]

const fn create_array<const N: usize>(mut f: impl FnMut(usize) -> u32 + Copy) -> [u32; N] {
    let mut array = [0; N];
    let mut i = 0;
    loop {
        array[i] = f(i);
        i += 1;
        if i == N {
            break;
        }
    }
    array
}

fn main() {
    // This works fine.
    let x = create_array(const |i| 2 * i as u32);
    assert_eq!(x, [0, 2, 4, 6, 8]);

    // This closure causes an ICE.
    let y = create_array(const |i| 2 * i as u32 + 1);
    assert_eq!(y, [1, 3, 5, 7, 9]);
}

Meta

rustc --version --verbose:

I'm using the Rust Playground today:

1.80.0-nightly (2024-05-31 ada5e2c7b5427a591e30)

Error output

warning: the feature `const_closures` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/main.rs:1:12
  |
1 | #![feature(const_closures, const_mut_refs, effects)]
  |            ^^^^^^^^^^^^^^
  |
  = note: see issue #106003 <https://github.com/rust-lang/rust/issues/106003> for more information
  = note: `#[warn(incomplete_features)]` on by default

thread 'rustc' panicked at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/compiler/rustc_middle/src/ty/util.rs:945:22:
ConstContext::Maybe must have host effect param
query stack during panic:
#0 [typeck] type-checking `main`
#1 [analysis] running analysis passes on this crate
end of query stack
warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground"); 1 warning emitted
Backtrace

``` stack backtrace: 0: 0x7f26fb2fecc5 - std::backtrace_rs::backtrace::libunwind::trace::h0bda62405550d460 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5 1: 0x7f26fb2fecc5 - std::backtrace_rs::backtrace::trace_unsynchronized::hc2c3ea3cf2f5c7f0 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: 0x7f26fb2fecc5 - std::sys_common::backtrace::_print_fmt::h8de6a59eaee45226 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/sys_common/backtrace.rs:68:5 3: 0x7f26fb2fecc5 - ::fmt::hd6ffc154782334f4 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/sys_common/backtrace.rs:44:22 4: 0x7f26fb34f6bb - core::fmt::rt::Argument::fmt::h1ed1f880d19ca3e5 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/core/src/fmt/rt.rs:165:63 5: 0x7f26fb34f6bb - core::fmt::write::h7e2d247389cb3e97 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/core/src/fmt/mod.rs:1168:21 6: 0x7f26fb2f399f - std::io::Write::write_fmt::ha7add8792955e794 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/io/mod.rs:1835:15 7: 0x7f26fb2fea9e - std::sys_common::backtrace::_print::ha9e91abfc6e8d12d at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/sys_common/backtrace.rs:47:5 8: 0x7f26fb2fea9e - std::sys_common::backtrace::print::ha4520664df90140c at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/sys_common/backtrace.rs:34:9 9: 0x7f26fb3014d9 - std::panicking::default_hook::{{closure}}::h04aa22b6205d98a5 10: 0x7f26fb30127a - std::panicking::default_hook::hf52ae4d8be29501c at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/panicking.rs:298:9 11: 0x7f26fe4a79af - std[a75323165fe08653]::panicking::update_hook::>::{closure#0} 12: 0x7f26fb301c0b - as core::ops::function::Fn>::call::h3a2a9442c441a378 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/alloc/src/boxed.rs:2077:9 13: 0x7f26fb301c0b - std::panicking::rust_panic_with_hook::he3c87e486ff05d6b at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/panicking.rs:799:13 14: 0x7f26fb301984 - std::panicking::begin_panic_handler::{{closure}}::h01ce2bfd91e16fe0 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/panicking.rs:664:13 15: 0x7f26fb2ff189 - std::sys_common::backtrace::__rust_end_short_backtrace::ha36f641af1493052 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/sys_common/backtrace.rs:171:18 16: 0x7f26fb3016b7 - rust_begin_unwind at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/panicking.rs:652:5 17: 0x7f26fb34bc53 - core::panicking::panic_fmt::h8cbe41bd36f1ccd3 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/core/src/panicking.rs:72:14 18: 0x7f26fb34b9fb - core::panicking::panic_display::ha747c5534e5ad54b at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/core/src/panicking.rs:262:5 19: 0x7f26fb34b9fb - core::option::expect_failed::hbb084febe087c246 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/core/src/option.rs:1997:5 20: 0x7f270033c110 - ::write_method_call_and_enforce_effects 21: 0x7f26ff9bc9b4 - ::check_overloaded_binop 22: 0x7f270035111b - ::check_expr_with_expectation_and_args 23: 0x7f26fff47f4c - rustc_hir_typeck[6b466a1699c07763]::check::check_fn 24: 0x7f270013dabd - ::check_expr_closure 25: 0x7f27003538ee - ::check_expr_with_expectation_and_args 26: 0x7f26fcc57fdd - ::confirm_builtin_call 27: 0x7f270034f762 - ::check_expr_with_expectation_and_args 28: 0x7f2700349251 - ::check_decl 29: 0x7f270034bf47 - ::check_block_with_expected 30: 0x7f2700350122 - ::check_expr_with_expectation_and_args 31: 0x7f26fff47f4c - rustc_hir_typeck[6b466a1699c07763]::check::check_fn 32: 0x7f26fff3d66c - rustc_hir_typeck[6b466a1699c07763]::typeck 33: 0x7f26fff3d0d1 - rustc_query_impl[9b16a2dee6878131]::plumbing::__rust_begin_short_backtrace::> 34: 0x7f26ffffa8ae - rustc_query_system[965e295a0f1b428c]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[9b16a2dee6878131]::plumbing::QueryCtxt, false> 35: 0x7f26ffff93d5 - rustc_query_impl[9b16a2dee6878131]::query_impl::typeck::get_query_non_incr::__rust_end_short_backtrace 36: 0x7f26ffff900b - ::par_body_owners::::{closure#0} 37: 0x7f26ffff7ac7 - rustc_hir_analysis[2361776b1da3ecb6]::check_crate 38: 0x7f270001087e - rustc_interface[cb0c5c0af19cf814]::passes::analysis 39: 0x7f27000103db - rustc_query_impl[9b16a2dee6878131]::plumbing::__rust_begin_short_backtrace::> 40: 0x7f27007604a5 - rustc_query_system[965e295a0f1b428c]::query::plumbing::try_execute_query::>, false, false, false>, rustc_query_impl[9b16a2dee6878131]::plumbing::QueryCtxt, false> 41: 0x7f270076020f - rustc_query_impl[9b16a2dee6878131]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace 42: 0x7f27005eb19c - rustc_interface[cb0c5c0af19cf814]::interface::run_compiler::, rustc_driver_impl[9be41471faca984]::run_compiler::{closure#0}>::{closure#1} 43: 0x7f27005b50c9 - std[a75323165fe08653]::sys_common::backtrace::__rust_begin_short_backtrace::, rustc_driver_impl[9be41471faca984]::run_compiler::{closure#0}>::{closure#1}, core[bd782e33ccb6b408]::result::Result<(), rustc_span[5e338462bab9c833]::ErrorGuaranteed>>::{closure#0}, core[bd782e33ccb6b408]::result::Result<(), rustc_span[5e338462bab9c833]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[bd782e33ccb6b408]::result::Result<(), rustc_span[5e338462bab9c833]::ErrorGuaranteed>> 44: 0x7f27005b4e80 - <::spawn_unchecked_, rustc_driver_impl[9be41471faca984]::run_compiler::{closure#0}>::{closure#1}, core[bd782e33ccb6b408]::result::Result<(), rustc_span[5e338462bab9c833]::ErrorGuaranteed>>::{closure#0}, core[bd782e33ccb6b408]::result::Result<(), rustc_span[5e338462bab9c833]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[bd782e33ccb6b408]::result::Result<(), rustc_span[5e338462bab9c833]::ErrorGuaranteed>>::{closure#2} as core[bd782e33ccb6b408]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0} 45: 0x7f26fb30bbfb - as core::ops::function::FnOnce>::call_once::h149a8f3bd2b6d616 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/alloc/src/boxed.rs:2063:9 46: 0x7f26fb30bbfb - as core::ops::function::FnOnce>::call_once::hc8ac52b5c9166594 at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/alloc/src/boxed.rs:2063:9 47: 0x7f26fb30bbfb - std::sys::pal::unix::thread::Thread::new::thread_start::h4464ec80ffa1747a at /rustc/ada5e2c7b5427a591e30baeeee2698a5eb6db0bd/library/std/src/sys/pal/unix/thread.rs:108:17 48: 0x7f26fb219609 - start_thread 49: 0x7f26fb13c353 - clone 50: 0x0 - ```

GrigorenkoPV commented 1 month ago

Looked for the PR introducing the ICE

searched nightlies: from nightly-2022-02-19 to nightly-2024-05-31 regressed nightly: nightly-2024-02-09 searched commit range: https://github.com/rust-lang/rust/compare/8ace7ea1f7cbba7b4f031e66c54ca237a0d65de6...98aa3624be70462d6a25ed5544333e3df62f4c66 regressed commit: https://github.com/rust-lang/rust/commit/6894f435d35d3d540dcefbc51390158ca5954861 (#120381)

Funnily enough, the ICE disappeared a week ago

searched nightlies: from nightly-2024-05-31 to nightly-2024-06-30 regressed nightly: nightly-2024-06-23 searched commit range: https://github.com/rust-lang/rust/compare/c1b336cb6b491b3be02cd821774f03af4992f413...3cb521a4344f0b556b81c55eec8facddeb1ead83 Regression in https://github.com/rust-lang-ci/rust/commit/7ba756ad7c2b63bc2aa77e2f8c8e03e9466b4c36 The PR introducing the regression in this rollup is #126552