rust-lang/rust

ICE: collection encountered polymorphic constant: UnevaluatedConst

frankblubaugh opened this issue ยท 10 comments

Code

use rand::prelude::*;
use std::time::Instant;
use sci_rs::signal::filter::{design::Sos, sosfilt_dyn};
use sci_rs::signal::filter::design::{butter_st, FilterBandType, FilterOutputType};

fn main() {

    let mut rng = rand::thread_rng();

    let vals: Vec<f64> = (0..25000*120).map(|_| rng.gen()).collect();

    // let y = Array1::from_vec(vec![0.0, 0.09414586007215595, 0.18745540640340155, ...]);

    let filter = butter_st::<f64,4,2>(
        [10.,50.],
        Some(FilterBandType::Bandpass),
        Some(false),
        Some(FilterOutputType::Zpk),
        Some(1666.)
    );

    // check filter params
    println!("{:?}",filter)
 
}

Meta

rustc --version --verbose:

rustc 1.68.0-nightly (d6f99e535 2023-01-02)
binary: rustc
commit-hash: d6f99e535a301a421dfee52a7c25bb4bdf420344
commit-date: 2023-01-02
host: aarch64-apple-darwin
release: 1.68.0-nightly
LLVM version: 15.0.6

Error output

error: internal compiler error: compiler/rustc_monomorphize/src/collector.rs:786:54: collection encountered polymorphic constant: Unevaluated(UnevaluatedConst { def: WithOptConstParam { did: DefId(43:1008 ~ heapless[99d5]::vec::{impl#0}::INIT), const_param_did: None }, substs: [num_complex::Complex<f64>, Const { ty: usize, kind: Unevaluated(UnevaluatedConst { def: WithOptConstParam { did: DefId(27:400 ~ sci_rs[d81d]::signal::filter::design::zpk2tf::poly_st::{constant#2}), const_param_did: Some(DefId(43:1000 ~ heapless[99d5]::vec::Vec::N)) }, substs: [num_complex::Complex<f64>, Const { ty: usize, kind: Expr(Binop(Mul, Const { ty: usize, kind: Value(Leaf(0x0000000000000004)) }, Const { ty: usize, kind: Value(Leaf(0x0000000000000002)) })) }] }) }], promoted: None }, [std::mem::MaybeUninit<num_complex::Complex<f64>>; _])
  --> /Users/blubafc1/.cargo/registry/src/github.com-1ecc6299db9ec823/heapless-0.7.16/src/vec.rs:70:21
   |
70 |             buffer: Self::INIT,
   |                     ^^^^^^^^^^

thread 'rustc' panicked at 'Box<dyn Any>', /rustc/d6f99e535a301a421dfee52a7c25bb4bdf420344/compiler/rustc_errors/src/lib.rs:978:33

Backtrace

stack backtrace:
   0:        0x1054f9bac - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hea68b7b20126fcc2
   1:        0x10554b30c - core::fmt::write::h76ef1456439f6e44
   2:        0x1054ed404 - std::io::Write::write_fmt::ha1f61edbd5acf3b5
   3:        0x1054f99c0 - std::sys_common::backtrace::print::h8649bc4326f5f277
   4:        0x1054fc4f4 - std::panicking::default_hook::{{closure}}::h7887887a17185173
   5:        0x1054fc24c - std::panicking::default_hook::h197221548797e88a
   6:        0x10d73b324 - rustc_driver[69cf2497f4e7534a]::DEFAULT_HOOK::{closure#0}::{closure#0}
   7:        0x1054fcbec - std::panicking::rust_panic_with_hook::h0e871605208fade7
   8:        0x10fcff64c - std[8e1853b45c5b494b]::panicking::begin_panic::<rustc_errors[e6281b614dbb5e3f]::ExplicitBug>::{closure#0}
   9:        0x10fcfef20 - std[8e1853b45c5b494b]::sys_common::backtrace::__rust_end_short_backtrace::<std[8e1853b45c5b494b]::panicking::begin_panic<rustc_errors[e6281b614dbb5e3f]::ExplicitBug>::{closure#0}, !>
  10:        0x11197db58 - std[8e1853b45c5b494b]::panicking::begin_panic::<rustc_errors[e6281b614dbb5e3f]::ExplicitBug>
  11:        0x10fd151d8 - std[8e1853b45c5b494b]::panic::panic_any::<rustc_errors[e6281b614dbb5e3f]::ExplicitBug>
  12:        0x10fd145c8 - <rustc_errors[e6281b614dbb5e3f]::HandlerInner>::span_bug::<rustc_span[35545e7b8de4724]::span_encoding::Span, &alloc[5863fc2f9119b42e]::string::String>
  13:        0x10fd1440c - <rustc_errors[e6281b614dbb5e3f]::Handler>::span_bug::<rustc_span[35545e7b8de4724]::span_encoding::Span, &alloc[5863fc2f9119b42e]::string::String>
  14:        0x10fd1536c - rustc_middle[6ee9a7af4dced80c]::ty::context::tls::with_context_opt::<rustc_middle[6ee9a7af4dced80c]::ty::context::tls::with_opt<rustc_middle[6ee9a7af4dced80c]::util::bug::opt_span_bug_fmt<rustc_span[35545e7b8de4724]::span_encoding::Span>::{closure#0}, ()>::{closure#0}, ()>
  15:        0x10fd15238 - rustc_middle[6ee9a7af4dced80c]::util::bug::opt_span_bug_fmt::<rustc_span[35545e7b8de4724]::span_encoding::Span>
  16:        0x111984ce8 - rustc_middle[6ee9a7af4dced80c]::util::bug::span_bug_fmt::<rustc_span[35545e7b8de4724]::span_encoding::Span>
  17:        0x10fcf0178 - <rustc_monomorphize[674d628f34238242]::collector::MirNeighborCollector as rustc_middle[6ee9a7af4dced80c]::mir::visit::Visitor>::visit_constant
  18:        0x10fcebbd4 - <rustc_monomorphize[674d628f34238242]::collector::MirNeighborCollector as rustc_middle[6ee9a7af4dced80c]::mir::visit::Visitor>::visit_operand
  19:        0x10fceab50 - <rustc_monomorphize[674d628f34238242]::collector::MirNeighborCollector as rustc_middle[6ee9a7af4dced80c]::mir::visit::Visitor>::visit_rvalue
  20:        0x10fcf1128 - rustc_monomorphize[674d628f34238242]::collector::collect_neighbours
  21:        0x10fcef290 - rustc_monomorphize[674d628f34238242]::collector::collect_items_rec
  22:        0x10fcef708 - rustc_monomorphize[674d628f34238242]::collector::collect_items_rec
  23:        0x10fcef708 - rustc_monomorphize[674d628f34238242]::collector::collect_items_rec
  24:        0x10fcef708 - rustc_monomorphize[674d628f34238242]::collector::collect_items_rec
  25:        0x10fcef708 - rustc_monomorphize[674d628f34238242]::collector::collect_items_rec
  26:        0x10fcef708 - rustc_monomorphize[674d628f34238242]::collector::collect_items_rec
  27:        0x10fcf7f04 - std[8e1853b45c5b494b]::panicking::try::<(), core[584ed36222eb190f]::panic::unwind_safe::AssertUnwindSafe<rustc_data_structures[b95ec3f837caec24]::sync::par_for_each_in<alloc[5863fc2f9119b42e]::vec::Vec<rustc_middle[6ee9a7af4dced80c]::mir::mono::MonoItem>, rustc_monomorphize[674d628f34238242]::collector::collect_crate_mono_items::{closure#1}::{closure#0}>::{closure#0}::{closure#0}>>
  28:        0x10fd0df24 - <rustc_session[3969ec7f4fda3833]::session::Session>::time::<(), rustc_monomorphize[674d628f34238242]::collector::collect_crate_mono_items::{closure#1}>
  29:        0x10fced448 - rustc_monomorphize[674d628f34238242]::collector::collect_crate_mono_items
  30:        0x10fcf5600 - rustc_monomorphize[674d628f34238242]::partitioning::collect_and_partition_mono_items
  31:        0x11080110c - rustc_query_system[4bcc622c2d45b20f]::query::plumbing::try_execute_query::<rustc_query_impl[6aadb7c2b2165783]::queries::collect_and_partition_mono_items, rustc_query_impl[6aadb7c2b2165783]::plumbing::QueryCtxt>
  32:        0x110951548 - <rustc_query_impl[6aadb7c2b2165783]::Queries as rustc_middle[6ee9a7af4dced80c]::ty::query::QueryEngine>::collect_and_partition_mono_items
  33:        0x10d84c27c - rustc_codegen_ssa[3b93f269012db227]::base::codegen_crate::<rustc_codegen_llvm[fb132dddf7640da0]::LlvmCodegenBackend>
  34:        0x10d9374f4 - <rustc_codegen_llvm[fb132dddf7640da0]::LlvmCodegenBackend as rustc_codegen_ssa[3b93f269012db227]::traits::backend::CodegenBackend>::codegen_crate
  35:        0x10d7b2aa8 - <rustc_session[3969ec7f4fda3833]::session::Session>::time::<alloc[5863fc2f9119b42e]::boxed::Box<dyn core[584ed36222eb190f]::any::Any>, rustc_interface[99b2e32c496d056e]::passes::start_codegen::{closure#0}>
  36:        0x10d7cfc24 - rustc_interface[99b2e32c496d056e]::passes::start_codegen
  37:        0x10d7cec2c - <rustc_interface[99b2e32c496d056e]::passes::QueryContext>::enter::<<rustc_interface[99b2e32c496d056e]::queries::Queries>::ongoing_codegen::{closure#0}::{closure#0}, core[584ed36222eb190f]::result::Result<alloc[5863fc2f9119b42e]::boxed::Box<dyn core[584ed36222eb190f]::any::Any>, rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>>
  38:        0x10d822f10 - <rustc_interface[99b2e32c496d056e]::queries::Queries>::ongoing_codegen
  39:        0x10d71759c - rustc_span[35545e7b8de4724]::with_source_map::<core[584ed36222eb190f]::result::Result<(), rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>, rustc_interface[99b2e32c496d056e]::interface::run_compiler<core[584ed36222eb190f]::result::Result<(), rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>, rustc_driver[69cf2497f4e7534a]::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
  40:        0x10d70b0d8 - <scoped_tls[c35d5098e8adf783]::ScopedKey<rustc_span[35545e7b8de4724]::SessionGlobals>>::set::<rustc_interface[99b2e32c496d056e]::interface::run_compiler<core[584ed36222eb190f]::result::Result<(), rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>, rustc_driver[69cf2497f4e7534a]::run_compiler::{closure#1}>::{closure#0}, core[584ed36222eb190f]::result::Result<(), rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>>
  41:        0x10d6e0180 - std[8e1853b45c5b494b]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[99b2e32c496d056e]::util::run_in_thread_pool_with_globals<rustc_interface[99b2e32c496d056e]::interface::run_compiler<core[584ed36222eb190f]::result::Result<(), rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>, rustc_driver[69cf2497f4e7534a]::run_compiler::{closure#1}>::{closure#0}, core[584ed36222eb190f]::result::Result<(), rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[584ed36222eb190f]::result::Result<(), rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>>
  42:        0x10d6c6638 - <<std[8e1853b45c5b494b]::thread::Builder>::spawn_unchecked_<rustc_interface[99b2e32c496d056e]::util::run_in_thread_pool_with_globals<rustc_interface[99b2e32c496d056e]::interface::run_compiler<core[584ed36222eb190f]::result::Result<(), rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>, rustc_driver[69cf2497f4e7534a]::run_compiler::{closure#1}>::{closure#0}, core[584ed36222eb190f]::result::Result<(), rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[584ed36222eb190f]::result::Result<(), rustc_errors[e6281b614dbb5e3f]::ErrorGuaranteed>>::{closure#1} as core[584ed36222eb190f]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  43:        0x105505624 - std::sys::unix::thread::Thread::new::thread_start::h7328e7d271fec60e
  44:        0x1b4f1c26c - __pthread_deallocate

Hi @frankblubaugh, cool to see someone interested in sci-rs. sci-rs makes heavy use of const generic sized arrays (provided by heapless) to allow functionality for no_std. I hit several ICEs across several nightly versions, while putting together the current crate functionality. Do be warned to keep track of what nightly works for you if you plan to use the crate as it is now. The features required by sci-rs are #![feature(generic_const_exprs, generic_arg_infer)], which are known to be undergoing changes.

I find that the internal compiler error hits in 2022-11-27 but not 2022-11-26. I tried both the aarch64-apple-ios and aarch64-apple-darwin targets on a library that uses sci-rs like your example.

sci-rs has a lot of const generic usage. The call that crashed was way down inside of the butter_st call near poly_st and heapless. Here is an example of how the functions calls use computed const generic values to compute math on the stack for no_std compatibility in this call tree.

pub fn poly_st<F, const N: usize>(z: &Vec<F, N>) -> Vec<F, { N + 1 }>
where
    F: ComplexField,
    [(); { N + 1 }]: Sized,
{
    let mut a: Vec<F, { N + 1 }> = Vec::new();
    a.push(F::one());

    const KER: usize = 2;
    for zi in z {
        let mut b = Vec::new();
        b.resize(a.len() + 1, F::zero());
        let k = [F::one(), -zi.clone()];
        for i in 0..a.len() + KER - 1 {
            let u_i = if i > a.len() { i - KER } else { 0 };
            let u_f = i.min(a.len() - 1);
            if u_i == u_f {
                b[i] += a[u_i].clone() * k[i - u_i].clone();
            } else {
                for u in u_i..(u_f + 1) {
                    if i - u < KER {
                        b[i] += a[u].clone() * k[i - u].clone();
                    }
                }
            }
        }
        a = b;
    }

    let mut roots = z.clone();
    sort_cplx_st(&mut roots);
    let mut root_conjs = z
        .iter()
        .map(|zi| zi.clone().conjugate())
        .collect::<Vec<_, N>>();
    sort_cplx_st(&mut root_conjs);
    if roots.into_iter().zip(root_conjs).all(|(a, b)| a == b) {
        a = a
            .into_iter()
            .map(|ai| ComplexField::from_real(ai.real()))
            .collect();
    }

    a
}

Additionally, here is a call site for poly_st

pub fn zpk2tf_st<C, F, const N: usize>(
    z: &Vec<C, N>,
    p: &Vec<C, N>,
    k: F,
) -> BaFormatFilter<F, { N + 1 }>
where
    C: ComplexField<RealField = F>,
    F: Float + RealField,
    [(); { N + 1 }]: Sized,
{
    let b = poly_st(z)
        .into_iter()
        .map(|bi| <C as ComplexField>::from_real(k) * bi)
        .collect::<Vec<_, _>>();
    let a = poly_st(p);

    // lots of math not shown
    todo!()
}

searched nightlies: from nightly-2022-11-25 to nightly-2022-12-01
regressed nightly: nightly-2022-11-27
searched commit range: 8681d4c...80a9646
regressed commit: aff003b

bisected with cargo-bisect-rustc v0.6.5

Host triple: aarch64-apple-darwin
Reproduce with:

cargo bisect-rustc --end=2022-12-01 

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-medium

@trueb2 Is there a github repo for sci_rs (The one linked on docs.rs is not visible)? I suspect that the issue is coming from a combination of generic_arg_infer and generic_const_exprs. I've been looking through the source on crates.io, but it'd be a bit more convenient to see the whole source.

@JulianKnodt you can use https://docs.rs/crate/cargo-download/latest to see the whole source locally.

Somewhat minimized:

#![feature(generic_const_exprs, generic_arg_infer)]
#![allow(incomplete_features)]
#![allow(unused)]

use core::mem::MaybeUninit;

pub struct Arr<T, const N: usize> {
    v: [MaybeUninit<T>; N],
}

impl<T, const N: usize> Arr<T, N> {
    const ELEM: MaybeUninit<T> = MaybeUninit::uninit();
    const INIT: [MaybeUninit<T>; N] = [Self::ELEM; N]; // important for optimization of `new`

    fn new() -> Self {
        Arr { v: Self::INIT }
    }
}

pub struct BaFormatFilter<const N: usize> {}

pub enum DigitalFilter<const N: usize>
where
    [(); N * 2 + 1]: Sized,
    [(); N * 2]: Sized,
{
    Ba(BaFormatFilter<{ N * 2 + 1 }>),
}

pub fn iirfilter_st_copy<const N: usize, const M: usize>(_: [f32; M]) -> DigitalFilter<N>
where
    [(); N * 2 + 1]: Sized,
    [(); N * 2]: Sized,
{
    let zpk = zpk2tf_st(&Arr::<f32, { N * 2 }>::new(), &Arr::<f32, { N * 2 }>::new());
    DigitalFilter::Ba(zpk)
}

pub fn zpk2tf_st<const N: usize>(
    _z: &Arr<f32, N>,
    _p: &Arr<f32, N>,
) -> BaFormatFilter<{ N + 1 }>
where
    [(); N + 1]: Sized,
{
    BaFormatFilter {}
}


fn main() {
    iirfilter_st_copy::<4, 2>([10., 50.,]);
}

I checked the GitHub Actions CI logs that picked up the nightly changes. The error logs are nicer now:

error: internal compiler error: compiler/rustc_monomorphize/src/collector.rs:786:54: collection encountered polymorphic constant: Unevaluated(UnevaluatedConst { def: WithOptConstParam { did: DefId(35:1008 ~ heapless[11c0]::vec::{impl#0}::INIT), const_param_did: None }, substs: [nalgebra::Complex<f64>, Const { ty: usize, kind: Unevaluated(UnevaluatedConst { def: WithOptConstParam { did: DefId(0:426 ~ sci_rs[3467]::signal::filter::design::zpk2tf::poly_st::{constant#2}), const_param_did: Some(DefId(35:1000 ~ heapless[11c0]::vec::Vec::N)) }, substs: [nalgebra::Complex<f64>, Const { ty: usize, kind: Expr(Binop(Mul, Const { ty: usize, kind: Value(Leaf(0x0000000000000004)) }, Const { ty: usize, kind: Value(Leaf(0x0000000000000002)) })) }] }) }], promoted: None }, [std::mem::MaybeUninit<nalgebra::Complex<f64>>; { N + 1 }])
[14](https://github.com/qsib-cbie/sci-rs/actions/runs/4001647129/jobs/6868094727#step:6:15)
  --> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/heapless-0.7.16/src/vec.rs:70:21
[15](https://github.com/qsib-cbie/sci-rs/actions/runs/4001647129/jobs/6868094727#step:6:16)
   |
[16](https://github.com/qsib-cbie/sci-rs/actions/runs/4001647129/jobs/6868094727#step:6:17)
70 |             buffer: Self::INIT,
[17](https://github.com/qsib-cbie/sci-rs/actions/runs/4001647129/jobs/6868094727#step:6:18)
   |                     ^^^^^^^^^^
[18](https://github.com/qsib-cbie/sci-rs/actions/runs/4001647129/jobs/6868094727#step:6:19)

Given this is tagged with requires-nightly, I don't think it qualifies as a beta regression. Removing that label.

Triage: Fixed on the latest nightly, marking as E-needs-test
@rustbot labels: +E-needs-test