rust-lang / rust

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

ICE (OutputTypeParameterMismatch) with type_alias_impl_trait #72793

Closed SNCPlay42 closed 2 years ago

SNCPlay42 commented 4 years ago

Code

Here's MCVE: https://github.com/rust-lang/rust/issues/72793#issuecomment-637043261 (Edited by JohnTitor)

#![feature(type_alias_impl_trait)]

type FilteredIter<'a> = impl Iterator<Item = &'a i32> + 'a;

fn filter_positive<'a>(vals: &'a [i32]) -> FilteredIter<'a> {
    vals.iter().filter(|x| **x >= 0)
}

fn with_positive<F>(vals: &[i32], fun: F)
where
    F: Fn(FilteredIter<'_>),
{
    fun(filter_positive(vals));
}

fn main() {
    let x = vec![-1, 1, -2, 2, -3, 3];

    with_positive(&x, |iter| {
        iter.for_each(|x| println!("{}", x));
    });
}

Playground

Error output

error: internal compiler error: src/librustc_trait_selection/traits/codegen/mod.rs:62: Encountered error `OutputTypeParameterMismatch(Binder(<[closure@src/main.rs:19:23: 21:6] as std::ops::Fn<(FilteredIter::<'_>,)>>), Binder(<[closure@src/main.rs:19:23: 21:6] as std::ops::Fn<(std::iter::Filter<std::slice::Iter<i32>, [closure@src/main.rs:6:24: 6:36]>,)>>), Sorts(ExpectedFound { expected: std::iter::Filter<std::slice::Iter<i32>, [closure@src/main.rs:6:24: 6:36]>, found: FilteredIter::<'_> }))` selecting `Binder(<[closure@src/main.rs:19:23: 21:6] as std::ops::Fn<(std::iter::Filter<std::slice::Iter<i32>, [closure@src/main.rs:6:24: 6:36]>,)>>)` during codegen

Prettied a bit:

error: internal compiler error: 
src/librustc_trait_selection/traits/codegen/mod.rs:62: 
Encountered error
`OutputTypeParameterMismatch(
    Binder(<[closure@src/main.rs:19:23: 21:6] as std::ops::Fn<(FilteredIter::<'_>,)>>),
    Binder(<[closure@src/main.rs:19:23: 21:6] as std::ops::Fn<(std::iter::Filter<std::slice::Iter<i32>, [closure@src/main.rs:6:24: 6:36]>,)>>),
    Sorts(ExpectedFound {
        expected: std::iter::Filter<std::slice::Iter<i32>, [closure@src/main.rs:6:24: 6:36]>,
        found: FilteredIter::<'_>
    })
)`
selecting `Binder(<[closure@src/main.rs:19:23: 21:6] as std::ops::Fn<(std::iter::Filter<std::slice::Iter<i32>, [closure@src/main.rs:6:24: 6:36]>,)>>)`
during codegen
Backtrace

``` thread 'rustc' panicked at 'Box', src/librustc_errors/lib.rs:907:9 stack backtrace: 0: backtrace::backtrace::libunwind::trace at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86 1: backtrace::backtrace::trace_unsynchronized at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66 2: std::sys_common::backtrace::_print_fmt at src/libstd/sys_common/backtrace.rs:78 3: ::fmt at src/libstd/sys_common/backtrace.rs:59 4: core::fmt::write at src/libcore/fmt/mod.rs:1076 5: std::io::Write::write_fmt at src/libstd/io/mod.rs:1537 6: std::sys_common::backtrace::_print at src/libstd/sys_common/backtrace.rs:62 7: std::sys_common::backtrace::print at src/libstd/sys_common/backtrace.rs:49 8: std::panicking::default_hook::{{closure}} at src/libstd/panicking.rs:198 9: std::panicking::default_hook at src/libstd/panicking.rs:218 10: rustc_driver::report_ice 11: std::panicking::rust_panic_with_hook at src/libstd/panicking.rs:490 12: std::panicking::begin_panic 13: rustc_errors::HandlerInner::bug 14: rustc_errors::Handler::bug 15: rustc_middle::util::bug::opt_span_bug_fmt::{{closure}} 16: rustc_middle::ty::context::tls::with_opt::{{closure}} 17: rustc_middle::ty::context::tls::with_opt 18: rustc_middle::util::bug::opt_span_bug_fmt 19: rustc_middle::util::bug::bug_fmt 20: rustc_middle::ty::context::tls::with_context::{{closure}} 21: rustc_trait_selection::traits::codegen::codegen_fulfill_obligation 22: rustc_middle::ty::query:: for rustc_middle::ty::query::queries::codegen_fulfill_obligation>::compute 23: rustc_query_system::dep_graph::graph::DepGraph::with_task_impl 24: rustc_data_structures::stack::ensure_sufficient_stack 25: rustc_query_system::query::plumbing::get_query_impl 26: rustc_ty::instance::resolve_instance 27: rustc_middle::ty::query:: for rustc_middle::ty::query::queries::resolve_instance>::compute 28: rustc_query_system::dep_graph::graph::DepGraph::with_task_impl 29: rustc_data_structures::stack::ensure_sufficient_stack 30: rustc_query_system::query::plumbing::get_query_impl 31: rustc_middle::ty::instance::Instance::resolve 32: ::visit_terminator_kind 33: rustc_mir::monomorphize::collector::collect_neighbours 34: rustc_data_structures::stack::ensure_sufficient_stack 35: rustc_mir::monomorphize::collector::collect_items_rec 36: rustc_mir::monomorphize::collector::collect_items_rec 37: rustc_mir::monomorphize::collector::collect_crate_mono_items 38: rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items 39: rustc_middle::ty::query:: for rustc_middle::ty::query::queries::collect_and_partition_mono_items>::compute 40: rustc_query_system::dep_graph::graph::DepGraph::with_task_impl 41: rustc_data_structures::stack::ensure_sufficient_stack 42: rustc_query_system::query::plumbing::get_query_impl 43: rustc_codegen_ssa::base::codegen_crate 44: ::codegen_crate 45: rustc_interface::passes::start_codegen 46: rustc_middle::ty::context::tls::enter_global 47: rustc_interface::queries::Queries::ongoing_codegen 48: rustc_interface::interface::run_compiler_in_existing_thread_pool 49: rustc_ast::attr::with_globals note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. note: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports note: rustc 1.45.0-nightly (4bd32c980 2020-05-29) running on x86_64-unknown-linux-gnu note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin note: some of the compiler flags provided by cargo are hidden query stack during panic: #0 [codegen_fulfill_obligation] checking if `std::ops::Fn` fulfills its obligations #1 [resolve_instance] resolving instance `<[closure@src/main.rs:19:23: 21:6] as std::ops::Fn<(std::iter::Filter, [closure@src/main.rs:6:24: 6:36]>,)>>::call` #2 [collect_and_partition_mono_items] collect_and_partition_mono_items end of query stack ```

Patryk27 commented 4 years ago

Just for reference - works fine with explicit lifetimes:

fn with_positive<'a, F>(vals: &'a [i32], fun: F)
where
    F: Fn(FilteredIter<'a>),
{
    fun(filter_positive(vals));
}
SNCPlay42 commented 4 years ago

@rustbot modify labels: +requires-nightly +F-type_alias_impl_trait

hellow554 commented 4 years ago

Smaller:

#![feature(type_alias_impl_trait)]

trait T { type Item; }

type Alias<'a> = impl T<Item = &'a ()>;

struct S;
impl<'a> T for &'a S {
    type Item = &'a ();
}

fn filter_positive<'a>() -> Alias<'a> {
    &S
}

fn with_positive(fun: impl Fn(Alias<'_>)) {
    fun(filter_positive());
}

fn main() {
    with_positive(|_| ());
}
JohnTitor commented 4 years ago

Seems this is fixed in the latest nightly (not bisected yet). Marking as E-needs-test and will try to bisect later.

matthiaskrgr commented 4 years ago

Still crashes with -Zmir-opt-level=2

error: internal compiler error: Encountered error `Unimplemented` selecting `Binder(<impl Fn(Alias<'_>) as std::ops::Fn<(impl T,)>>)` during codegen
  |
  = note: delayed at /home/matthias/vcs/github/rust/src/librustc_session/session.rs:441:27

thread 'rustc' panicked at 'no errors encountered even though `delay_span_bug` issued', src/librustc_errors/lib.rs:366:17
stack backtrace:
   0:     0x7f6662e2ae31 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h9a65342185cb7022
   1:     0x7f6662e999dd - core::fmt::write::hc3de054a6627b208
   2:     0x7f6662e0ab53 - std::io::Write::write_fmt::h64bd8d8dca95fd5c
   3:     0x7f6662e03bb0 - std::panicking::default_hook::{{closure}}::ha9c6947a872dc513
   4:     0x7f6662e037ed - std::panicking::default_hook::h3e5e46d076456208
   5:     0x7f6663ef0269 - rustc_driver::report_ice::he0572c9ff71e0a8c
   6:     0x7f6662e041f6 - std::panicking::rust_panic_with_hook::h30c558028146672c
   7:     0x7f6666991a56 - std::panicking::begin_panic::{{closure}}::h62cde3015c32ea83
   8:     0x7f666699191e - std::sys_common::backtrace::__rust_end_short_backtrace::h8d34e3f6f4e20c59
   9:     0x7f666696859c - std::panicking::begin_panic::h6e7f1f8f159dddb1
  10:     0x7f6666971dae - <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop::h5013360fb564f32c
  11:     0x7f6663d4a3d6 - core::ptr::drop_in_place::h4e9bf5d5a9a1b0b3
  12:     0x7f6663d6dad0 - <alloc::rc::Rc<T> as core::ops::drop::Drop>::drop::h0ba476d8189f3dd5
  13:     0x7f6663ec6a4d - core::ptr::drop_in_place::hb83cdb4088f009cf
  14:     0x7f6663eacc92 - rustc_span::with_source_map::hdfa547cd4d1341a2
  15:     0x7f6663d24038 - rustc_interface::interface::create_compiler_and_run::h10b475d531695c0e
  16:     0x7f6663eb7bfd - std::sys_common::backtrace::__rust_begin_short_backtrace::h4c9162f49c0dc935
  17:     0x7f6663eba9be - core::ops::function::FnOnce::call_once{{vtable.shim}}::hbba71a1842b71242
  18:     0x7f6662e05cda - std::sys::unix::thread::Thread::new::thread_start::hdc96d124dc384f80
  19:     0x7f665fc04422 - start_thread
  20:     0x7f6662c07bf3 - __clone
  21:                0x0 - <unknown>

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.47.0-dev running on x86_64-unknown-linux-gnu

note: compiler flags: -Z mir-opt-level=2

query stack during panic:
end of query stack
JohnTitor commented 4 years ago

Ohh, good catch! Still, the regression test here is useful but let's keep this open then :)

Oops, closed by mistake XD

JohnTitor commented 4 years ago

Bisected, it's fixed by #75443.

matthiaskrgr commented 3 years ago

This ICEs again now:

error: internal compiler error: compiler/rustc_trait_selection/src/traits/codegen.rs:78:17: Encountered error `OutputTypeParameterMismatch(Binder(<[closure@./72793.rs:21:19: 21:25] as std::ops::Fn<(impl T,)>>), Binder(<[closure@./72793.rs:21:19: 21:25] as std::ops::Fn<(&S,)>>), Sorts(ExpectedFound { expected: &S, found: impl T }))` selecting `Binder(<[closure@./72793.rs:21:19: 21:25] as std::ops::Fn<(&S,)>>)` during codegen

thread 'rustc' panicked at 'Box<Any>', compiler/rustc_errors/src/lib.rs:958:9
stack backtrace:
   0:     0x7f714147aa50 - std::backtrace_rs::backtrace::libunwind::trace::h448c56741b6011d3
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/../../backtrace/src/backtrace/libunwind.rs:100:5
   1:     0x7f714147aa50 - std::backtrace_rs::backtrace::trace_unsynchronized::h2d26397c5720fdbb
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f714147aa50 - std::sys_common::backtrace::_print_fmt::h720a2f61f75b9f58
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x7f714147aa50 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h8d62bbfda6d5c836
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x7f71414ea94c - core::fmt::write::h1857a60b204f1b6a
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/core/src/fmt/mod.rs:1078:17
   5:     0x7f714146c6b2 - std::io::Write::write_fmt::h16a9fe7680ac245c
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/io/mod.rs:1518:15
   6:     0x7f714147fa05 - std::sys_common::backtrace::_print::h9ba51a6db618de7d
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x7f714147fa05 - std::sys_common::backtrace::print::h5f20e41c85e91716
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x7f714147fa05 - std::panicking::default_hook::{{closure}}::h5b7294ca19e8edab
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/panicking.rs:208:50
   9:     0x7f714147f6a8 - std::panicking::default_hook::hb3948d1f74b6ff4c
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/panicking.rs:227:9
  10:     0x7f7141d5a2d4 - rustc_driver::report_ice::h021ea70c3b174afa
  11:     0x7f7141480306 - std::panicking::rust_panic_with_hook::h55d23fef0ad751bc
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/panicking.rs:597:17
  12:     0x7f7144da3f7d - std::panicking::begin_panic::{{closure}}::h2d2fd75c8f2a2341
  13:     0x7f7144da3de6 - std::sys_common::backtrace::__rust_end_short_backtrace::h463d6160b8d38e51
  14:     0x7f7144da3f1f - std::panicking::begin_panic::h7716ff6593f9c4fe
  15:     0x7f7144ddcc9c - rustc_errors::HandlerInner::bug::hcaaf59cd7aedcc3b
  16:     0x7f7144ddb290 - rustc_errors::Handler::bug::h709d540b8038ac8d
  17:     0x7f71448b1324 - rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}::hd783d6169452aa9a
  18:     0x7f71448b05db - rustc_middle::ty::context::tls::with_opt::{{closure}}::hb843c547a56f3e04
  19:     0x7f71448b0582 - rustc_middle::ty::context::tls::with_opt::h3f9cf1292158aa7f
  20:     0x7f71448b1249 - rustc_middle::util::bug::opt_span_bug_fmt::hbec46092491724ec
  21:     0x7f71448b11be - rustc_middle::util::bug::bug_fmt::hd9854762eb11f3b1
  22:     0x7f71440d0667 - rustc_infer::infer::InferCtxtBuilder::enter::hd27c91343128654f
  23:     0x7f71442fb38f - rustc_trait_selection::traits::codegen::codegen_fulfill_obligation::hbb8f1b498b15c1ea
  24:     0x7f7142351a2b - rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::codegen_fulfill_obligation>::compute::h1cd7b59c9a9732c0
  25:     0x7f7142340028 - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl::h6569c8e990e04dd1
  26:     0x7f7142369ed6 - rustc_data_structures::stack::ensure_sufficient_stack::h4d0f969f57d162bc
  27:     0x7f71422f677c - rustc_query_system::query::plumbing::get_query_impl::h225377355e8b4967
  28:     0x7f714235a448 - rustc_ty::instance::inner_resolve_instance::hcd318f1ced437469
  29:     0x7f714235a0aa - rustc_ty::instance::resolve_instance::h3bc1bc12db816545
  30:     0x7f7144c4102a - rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::resolve_instance>::compute::he88c4b2daaf352f7
  31:     0x7f7144c703f0 - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl::h66ffbab719a699ee
  32:     0x7f71448518c6 - rustc_data_structures::stack::ensure_sufficient_stack::h5d70dde26801a158
  33:     0x7f71445b46bb - rustc_query_system::query::plumbing::get_query_impl::h0f9977ba7047dd61
  34:     0x7f7144c322c4 - rustc_middle::ty::instance::Instance::resolve_opt_const_arg::he0415d3539e95d51
  35:     0x7f7144c31f80 - rustc_middle::ty::instance::Instance::resolve::h7b74795a5a17f452
  36:     0x7f7143340c3f - <rustc_mir::monomorphize::collector::MirNeighborCollector as rustc_middle::mir::visit::Visitor>::visit_terminator::hecf45b8e4c8ef86f
  37:     0x7f71433438cf - rustc_mir::monomorphize::collector::collect_neighbours::h007bc9dac1822f58
  38:     0x7f714333e192 - rustc_mir::monomorphize::collector::collect_items_rec::h8b80aa1d8c5baccb
  39:     0x7f714333e404 - rustc_mir::monomorphize::collector::collect_items_rec::h8b80aa1d8c5baccb
  40:     0x7f7143607841 - rustc_session::utils::<impl rustc_session::session::Session>::time::h0769395397f4e773
  41:     0x7f714333d10d - rustc_mir::monomorphize::collector::collect_crate_mono_items::h16fd359bc82e9cba
  42:     0x7f71433f727c - rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items::hd62bc8ab380467e4
  43:     0x7f714217ebc2 - rustc_middle::ty::query::<impl rustc_query_system::query::config::QueryAccessors<rustc_middle::ty::context::TyCtxt> for rustc_middle::ty::query::queries::collect_and_partition_mono_items>::compute::ha94f6ecf95919685
  44:     0x7f71420f00c9 - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl::h11581c1c2273af7b
  45:     0x7f714215a25a - rustc_data_structures::stack::ensure_sufficient_stack::h5cc442ccbebc74cd
  46:     0x7f714204da4b - rustc_query_system::query::plumbing::get_query_impl::h4e7fa7005ff50500
  47:     0x7f714217f9cc - rustc_codegen_ssa::base::codegen_crate::h90cf2d1d07dcdf6e
  48:     0x7f71421d6a15 - <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate::h2a59653aff37ec7b
  49:     0x7f7141f94e3f - rustc_interface::passes::QueryContext::enter::hae4ed674370a9e23
  50:     0x7f7141fc52d3 - rustc_interface::queries::Queries::ongoing_codegen::h54d5e3fae50fcc04
  51:     0x7f7141d6ee51 - rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter::hefaa7b63637d8d8e
  52:     0x7f7141d5ce7e - rustc_span::with_source_map::h829c6eccedcb37ca
  53:     0x7f7141d70252 - rustc_interface::interface::create_compiler_and_run::h5c2da4096753e17e
  54:     0x7f7141d5da5a - rustc_span::with_session_globals::h3b2e3a8e7e59ecdc
  55:     0x7f7141d75be1 - std::sys_common::backtrace::__rust_begin_short_backtrace::hf69e25d23954f1a0
  56:     0x7f7141cdcc18 - core::ops::function::FnOnce::call_once{{vtable.shim}}::hed3ab0291d940426
  57:     0x7f714148f30a - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h9e7afb7a0a438236
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/alloc/src/boxed.rs:1307:9
  58:     0x7f714148f30a - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h70c646c4271337a1
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/alloc/src/boxed.rs:1307:9
  59:     0x7f714148f30a - std::sys::unix::thread::Thread::new::thread_start::h35d2b8d36f210d02
                               at /rustc/b1277d04db0dc8009037e872a1be7cdc2bd74a43/library/std/src/sys/unix/thread.rs:89:17
  60:     0x7f714138a3e9 - start_thread
  61:     0x7f71412a7293 - __GI___clone
  62:                0x0 - <unknown>

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.49.0-nightly (b1277d04d 2020-11-08) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [codegen_fulfill_obligation] checking if `std::ops::Fn` fulfills its obligations
#1 [resolve_instance] resolving instance `<[closure@./72793.rs:21:19: 21:25] as std::ops::Fn<(&S,)>>::call`
#2 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
error: aborting due to previous error
JohnTitor commented 3 years ago

Because of #78410, dropped E-needs-test.

matthiaskrgr commented 3 years ago

This no longer crashes with rustc-git !

@rustbot modify labels: E-needs-test

matthiaskrgr commented 3 years ago

Hm, I was too quick. It no longer crashes because it misses a feature gate.

error[E0658]: `impl Trait` in type aliases is unstable
 --> bad.rs:3:25
  |
3 | type FilteredIter<'a> = impl Iterator<Item = &'a i32> + 'a;
  |                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
  = help: add `#![feature(min_type_alias_impl_trait)]` to the crate attributes to enable

If I add the min_type_alias_impl_trait gate, it crashes again :/

@rustbot modify labels: E-needs-test

jackh726 commented 3 years ago

cc #85499 which probably fixes this

oli-obk commented 2 years ago

Seems to be fixed again