Skip to content

rustc panic involving generic contravariant constraint using associated type constrained by where for clause #87386

Closed
@tjjfvi

Description

@tjjfvi

Code

/// Doesn't trigger a statically analyzable panic
macro_rules! todo {
  () => {
    unsafe { std::mem::transmute_copy::<_, _>(&()) }
  };
}

trait Foo<'a> {
  type Value;
}

impl<'a> Foo<'a> for u32 {
  type Value = u32;
}

trait Bar<F> {
  fn bar(&self, cb: F);
}

impl<O, F> Bar<F> for O
where
  O: for<'a> Foo<'a>,
  F: FnMut(<O as Foo<'_>>::Value),
{
  fn bar(&self, mut cb: F) {
    let _x: Box<dyn FnMut()> = Box::new(|| cb(todo!())); // <- this coercion seems to be the issue
  }
}

fn main() {
  0.bar(|_| {});
}

Meta

rustc --version --verbose:

rustc 1.52.1 (9bc8c42bb 2021-05-09)
binary: rustc
commit-hash: 9bc8c42bb2f19e745a63f3445f1ac248fb015e53
commit-date: 2021-05-09
host: x86_64-apple-darwin
release: 1.52.1
LLVM version: 12.0.0

I've also checked https://play.rust-lang.org on stable, beta, and nightly, and the panic occurs in all three.

Error output

error: internal compiler error: compiler/rustc_trait_selection/src/traits/codegen.rs:78:17: Encountered error `OutputTypeParameterMismatch(Binder(<[closure@src/main.rs:31:9: 31:15] as std::ops::FnMut<(<u32 as Foo<'_>>::Value,)>>), Binder(<[closure@src/main.rs:31:9: 31:15] as std::ops::FnMut<(u32,)>>), Sorts(ExpectedFound { expected: u32, found: <u32 as Foo<'_>>::Value }))` selecting `Binder(<[closure@src/main.rs:31:9: 31:15] as std::ops::FnMut<(u32,)>>)` during codegen

thread 'rustc' panicked at 'Box<Any>', /rustc/9bc8c42bb2f19e745a63f3445f1ac248fb015e53/library/std/src/panic.rs:59:5
Backtrace

stack backtrace:
   0:        0x108d8c434 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h80b40bec9b25cdb7
   1:        0x108df077e - core::fmt::write::h367e10cc391177e1
   2:        0x108d7fce6 - std::io::Write::write_fmt::h69f760cf0ceceb64
   3:        0x108d901e9 - std::panicking::default_hook::{{closure}}::h6b461dbc832ae479
   4:        0x108d8fcd0 - std::panicking::default_hook::hf6b37b6d4ff1b717
   5:        0x1015c9058 - rustc_driver::report_ice::hb51af1729852eb71
   6:        0x108d909b8 - std::panicking::rust_panic_with_hook::hcc0074a2cdf70335
   7:        0x1057cf711 - std::panicking::begin_panic::{{closure}}::h9de552659edff14c
   8:        0x1057cf689 - std::sys_common::backtrace::__rust_end_short_backtrace::h8ce3fcb4b4655207
   9:        0x105a86801 - std::panicking::begin_panic::h1d6a787d0b51d1c4
  10:        0x1057bd9a0 - std::panic::panic_any::hdc1d724e6c0b6502
  11:        0x1057c431c - rustc_errors::HandlerInner::bug::hfde960d9d360e049
  12:        0x1057c2147 - rustc_errors::Handler::bug::h19b5c7521664e3b8
  13:        0x10569ffe3 - rustc_middle::util::bug::opt_span_bug_fmt::{{closure}}::h3cecb89853250fee
  14:        0x105698106 - rustc_middle::ty::context::tls::with_opt::{{closure}}::hca6d6b1f7cfbd401
  15:        0x1056980ca - rustc_middle::ty::context::tls::with_opt::h9aebcb541b9fbcf7
  16:        0x10569ff2b - rustc_middle::util::bug::opt_span_bug_fmt::h6e26b5d9065fffa9
  17:        0x105a8004f - rustc_middle::util::bug::bug_fmt::h5ac5e85b53b74869
  18:        0x10525106f - rustc_infer::infer::InferCtxtBuilder::enter::hebdbe0aeb8ffaf40
  19:        0x1052c4ebb - rustc_trait_selection::traits::codegen::codegen_fulfill_obligation::h8f976f38344e8464
  20:        0x10427ca30 - rustc_query_impl::<impl rustc_query_system::query::config::QueryAccessors<rustc_query_impl::plumbing::QueryCtxt> for rustc_query_impl::queries::codegen_fulfill_obligation>::compute::h1f3db9ab2e558f80
  21:        0x104439373 - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl::h0add85b56b74391b
  22:        0x1045292ea - rustc_data_structures::stack::ensure_sufficient_stack::ha08c62efa12fbd1a
  23:        0x1041e472e - rustc_query_system::query::plumbing::force_query_with_job::h97a479547d4410ff
  24:        0x10416aa11 - rustc_query_system::query::plumbing::get_query_impl::hc4b2dcfc978d85ef
  25:        0x1043fd6ea - <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::codegen_fulfill_obligation::h2033ab3fd2d8a9c3
  26:        0x103a08512 - rustc_ty_utils::instance::inner_resolve_instance::hd839a9681f6ba4a7
  27:        0x103a07226 - rustc_ty_utils::instance::resolve_instance::h92522d07b5c97fcd
  28:        0x1042801a8 - rustc_query_impl::<impl rustc_query_system::query::config::QueryAccessors<rustc_query_impl::plumbing::QueryCtxt> for rustc_query_impl::queries::resolve_instance>::compute::hcd7e4115c4979433
  29:        0x104464b93 - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl::hbd7d959ceb4a909b
  30:        0x104516745 - rustc_data_structures::stack::ensure_sufficient_stack::h22125a5a5db4f164
  31:        0x1041da545 - rustc_query_system::query::plumbing::force_query_with_job::h69ec8529e626b820
  32:        0x10414089e - rustc_query_system::query::plumbing::get_query_impl::h6b1a74ed805be5db
  33:        0x1044033ca - <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::resolve_instance::h0af16f4e8f6ba3d9
  34:        0x105602cb8 - rustc_middle::ty::instance::Instance::resolve_opt_const_arg::hc023f1ebe54a04ae
  35:        0x1055f3641 - rustc_middle::ty::instance::Instance::resolve::hd49ebdf607ead8e8
  36:        0x10483bc80 - <rustc_mir::monomorphize::collector::MirNeighborCollector as rustc_middle::mir::visit::Visitor>::visit_terminator::ha0aecf96e86441f8
  37:        0x10483f7c6 - rustc_mir::monomorphize::collector::collect_neighbours::h727ee14214262696
  38:        0x104838588 - rustc_mir::monomorphize::collector::collect_items_rec::hb6ffde2900b60ded
  39:        0x1048387bc - rustc_mir::monomorphize::collector::collect_items_rec::hb6ffde2900b60ded
  40:        0x1048387bc - rustc_mir::monomorphize::collector::collect_items_rec::hb6ffde2900b60ded
  41:        0x10483719b - rustc_mir::monomorphize::collector::collect_crate_mono_items::h3b15e71f9d13d8d0
  42:        0x104b3875c - rustc_mir::monomorphize::partitioning::collect_and_partition_mono_items::h7db8e53eddd83671
  43:        0x10427f5f9 - rustc_query_impl::<impl rustc_query_system::query::config::QueryAccessors<rustc_query_impl::plumbing::QueryCtxt> for rustc_query_impl::queries::collect_and_partition_mono_items>::compute::h32c3bf44e75fe421
  44:        0x104453f92 - rustc_query_system::dep_graph::graph::DepGraph<K>::with_task_impl::h7bfd926750ece29a
  45:        0x104517f8e - rustc_data_structures::stack::ensure_sufficient_stack::h3052e125a301fdff
  46:        0x1041f94b5 - rustc_query_system::query::plumbing::force_query_with_job::hf3a3a3c31e726a68
  47:        0x10413165a - rustc_query_system::query::plumbing::get_query_impl::h5072e7d241cab575
  48:        0x1044018f4 - <rustc_query_impl::Queries as rustc_middle::ty::query::QueryEngine>::collect_and_partition_mono_items::he585be757c9851df
  49:        0x101831941 - <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate::hc852cedf8aca38c9
  50:        0x1016fc05f - rustc_interface::passes::QueryContext::enter::hb333afeb5acf9798
  51:        0x10170f040 - rustc_interface::queries::Queries::ongoing_codegen::h8c97cc4e774fffa9
  52:        0x1015dec50 - rustc_interface::queries::<impl rustc_interface::interface::Compiler>::enter::h45421e55f990f5e8
  53:        0x1015cb1e1 - rustc_span::with_source_map::h7bf5df439a8d2287
  54:        0x1015dff23 - rustc_interface::interface::create_compiler_and_run::h6aaead704b55a38b
  55:        0x1015d2926 - scoped_tls::ScopedKey<T>::set::h79ff838cdb92d8d9
  56:        0x1015e0b79 - std::sys_common::backtrace::__rust_begin_short_backtrace::h8a9d21235d7a778a
  57:        0x1015fddcd - core::ops::function::FnOnce::call_once{{vtable.shim}}::hd426e7b5e9740ba1
  58:        0x108d9dfad - std::sys::unix::thread::Thread::new::thread_start::h4185c11ff3c2c216
  59:     0x7fff2058c8fc - __pthread_start

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions