Skip to content

ICE: panic on type inference with possible Deref coercion #111739

Closed
@KiitoX

Description

@KiitoX

Code

It seems this is a particularly tricky interaction of Clone, Deref coercion and these two impl; take away any one, and the compiler spits out a type annotation error as seen below.

use std::rc::Rc;
use std::ops::Deref;

#[derive(Clone)]
struct Value<T>(T);

pub trait Wrap<T: ?Sized> {
    fn wrap(f: T) -> Self;
}

impl<R, A1, A2> Wrap<fn(A1, A2) -> R> for Value<fn(A1, A2) -> R> {
    fn wrap(f: fn(A1, A2) -> R) -> Self { Value(f) }
}

impl<F, R, A1, A2> Wrap<F> for Value<Rc<dyn Fn(A1, A2) -> R>>
    where F: 'static + Fn(A1, A2) -> R {
    fn wrap(f: F) -> Self { Value(Rc::new(f)) }
}

// https://users.rust-lang.org/t/callable-struct-on-stable/54689/7
impl<F> Deref for Value<Rc<F>> {
    type Target = F;

    fn deref(&self) -> &Self::Target {
        match self { Value(value) => value }
    }
}

fn fn_plus(var_a: i64, var_b: i64) -> i64 {
    return var_a + var_b;
}
fn fn_select(var_x: bool) -> i64 {
    let var_fn = if var_x {
        Value::wrap(fn_plus as fn(_, _) -> _)
    } else {
        Value::wrap((|a, b| a - b) as fn(_, _) -> _)
    };
    match var_fn.clone() {
        Value(fun) => fun(2_i64, 3_i64)
    }
}

fn main() {
    println!("select(false)(2, 3) = {}", fn_select(false));
    println!("select(true)(2, 3) = {}", fn_select(true));
}

The compiler is also happy if var_fn is annotated as Value<fn(_, _) -> _> but I don't quite understand why this needs to be specified, given the inner casts on both branches.

Meta

rustc --version --verbose:

rustc 1.71.0-nightly (18bfe5d8a 2023-05-14)
binary: rustc
commit-hash: 18bfe5d8a9ca0e226171e98f8f4ef071790f3352
commit-date: 2023-05-14
host: x86_64-unknown-linux-gnu
release: 1.71.0-nightly
LLVM version: 16.0.2

Error output

error[E0282]: type annotations needed for `Value<_>`
  --> src/main.rs:33:9
   |
33 |     let var_fn = if var_x {
   |         ^^^^^^
...
39 |         Value(fun) => fun(2_i64, 3_i64)
   |                       ----------------- type must be known at this point
   |
help: consider giving `var_fn` an explicit type, where the placeholders `_` are specified
   |
33 |     let var_fn: Value<_> = if var_x {
   |               ++++++++++

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

thread 'rustc' panicked at 'assertion failed: matches!(ty.kind(), ty :: Error(_))', compiler/rustc_hir_typeck/src/method/probe.rs:459:17
stack backtrace:
   0:     0x7f0de7b696e1 - std::backtrace_rs::backtrace::libunwind::trace::h4354896f1663baaf
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7f0de7b696e1 - std::backtrace_rs::backtrace::trace_unsynchronized::h8ac49f89c23585dd
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f0de7b696e1 - std::sys_common::backtrace::_print_fmt::h9f5f16b3ef080000
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/sys_common/backtrace.rs:65:5
   3:     0x7f0de7b696e1 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hcfed927151d1ad83
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f0de7bc9bdf - core::fmt::rt::Argument::fmt::h12fb43eea2fe23a8
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/core/src/fmt/rt.rs:138:9
   5:     0x7f0de7bc9bdf - core::fmt::write::hcf94a34baaaea06f
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/core/src/fmt/mod.rs:1094:21
   6:     0x7f0de7b5c941 - std::io::Write::write_fmt::h60003491edc2e074
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/io/mod.rs:1712:15
   7:     0x7f0de7b694f5 - std::sys_common::backtrace::_print::h9a8311322b9d8667
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f0de7b694f5 - std::sys_common::backtrace::print::h8503eaeeae92ea08
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f0de7b6c177 - std::panicking::default_hook::{{closure}}::h36a123b73b99c0f3
  10:     0x7f0de7b6bf64 - std::panicking::default_hook::ha2efb9fc5f628e61
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/panicking.rs:288:9
  11:     0x7f0deacc87db - rustc_driver_impl[64b7dd1911194932]::install_ice_hook::{closure#0}
  12:     0x7f0de7b6c897 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h250e911c4e8d011a
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/alloc/src/boxed.rs:1999:9
  13:     0x7f0de7b6c897 - std::panicking::rust_panic_with_hook::h5d9e02f555bc48d2
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/panicking.rs:695:13
  14:     0x7f0de7b6c5d1 - std::panicking::begin_panic_handler::{{closure}}::hbb00bd8fdd3417d9
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/panicking.rs:580:13
  15:     0x7f0de7b69b26 - std::sys_common::backtrace::__rust_end_short_backtrace::ha9ceea58b8cf1deb
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/sys_common/backtrace.rs:150:18
  16:     0x7f0de7b6c382 - rust_begin_unwind
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/panicking.rs:578:5
  17:     0x7f0de7bc5e63 - core::panicking::panic_fmt::h1df8faa11491e0c5
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/core/src/panicking.rs:67:14
  18:     0x7f0de7bc5ef3 - core::panicking::panic::he8f7538d58c14f28
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/core/src/panicking.rs:117:5
  19:     0x7f0de931d32a - <rustc_hir_typeck[1d4551b31af27f04]::fn_ctxt::FnCtxt>::lookup_method
  20:     0x7f0de930868a - <rustc_hir_typeck[1d4551b31af27f04]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  21:     0x7f0de936faf6 - <rustc_hir_typeck[1d4551b31af27f04]::fn_ctxt::FnCtxt>::check_match::{closure#0}
  22:     0x7f0de9309aa6 - <rustc_hir_typeck[1d4551b31af27f04]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  23:     0x7f0de9776b16 - <rustc_hir_typeck[1d4551b31af27f04]::fn_ctxt::FnCtxt>::check_return_expr
  24:     0x7f0de9309b7c - <rustc_hir_typeck[1d4551b31af27f04]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  25:     0x7f0de934dee0 - <rustc_hir_typeck[1d4551b31af27f04]::fn_ctxt::FnCtxt>::check_block_with_expected
  26:     0x7f0de9308594 - <rustc_hir_typeck[1d4551b31af27f04]::fn_ctxt::FnCtxt>::check_expr_with_expectation_and_args
  27:     0x7f0de9776b16 - <rustc_hir_typeck[1d4551b31af27f04]::fn_ctxt::FnCtxt>::check_return_expr
  28:     0x7f0de976e7c0 - rustc_hir_typeck[1d4551b31af27f04]::check::check_fn
  29:     0x7f0de975638a - rustc_hir_typeck[1d4551b31af27f04]::typeck
  30:     0x7f0de9755cd9 - <rustc_query_impl[29ff09dda6aeedc5]::dynamic_query::typeck::{closure#2} as core[b4a8d54be4c63c12]::ops::function::FnOnce<(rustc_middle[f607de435e9a4418]::ty::context::TyCtxt, rustc_span[f3562a9d228bc34b]::def_id::LocalDefId)>>::call_once
  31:     0x7f0de94addff - rustc_query_system[7835b3e98a87b812]::query::plumbing::try_execute_query::<rustc_query_impl[29ff09dda6aeedc5]::DynamicConfig<rustc_query_system[7835b3e98a87b812]::query::caches::VecCache<rustc_span[f3562a9d228bc34b]::def_id::LocalDefId, rustc_middle[f607de435e9a4418]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[29ff09dda6aeedc5]::plumbing::QueryCtxt>
  32:     0x7f0de99b2c48 - rustc_query_system[7835b3e98a87b812]::query::plumbing::force_query::<rustc_query_impl[29ff09dda6aeedc5]::DynamicConfig<rustc_query_system[7835b3e98a87b812]::query::caches::VecCache<rustc_span[f3562a9d228bc34b]::def_id::LocalDefId, rustc_middle[f607de435e9a4418]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[29ff09dda6aeedc5]::plumbing::QueryCtxt>
  33:     0x7f0dea6a0c91 - <rustc_query_impl[29ff09dda6aeedc5]::plumbing::query_callback<rustc_query_impl[29ff09dda6aeedc5]::queries::typeck>::{closure#0} as core[b4a8d54be4c63c12]::ops::function::FnOnce<(rustc_middle[f607de435e9a4418]::ty::context::TyCtxt, rustc_query_system[7835b3e98a87b812]::dep_graph::dep_node::DepNode<rustc_middle[f607de435e9a4418]::dep_graph::dep_node::DepKind>)>>::call_once
  34:     0x7f0de8ef0b0f - <rustc_query_system[7835b3e98a87b812]::dep_graph::graph::DepGraphData<rustc_middle[f607de435e9a4418]::dep_graph::dep_node::DepKind>>::try_mark_previous_green::<rustc_query_impl[29ff09dda6aeedc5]::plumbing::QueryCtxt>
  35:     0x7f0de94adc5d - rustc_query_system[7835b3e98a87b812]::query::plumbing::try_execute_query::<rustc_query_impl[29ff09dda6aeedc5]::DynamicConfig<rustc_query_system[7835b3e98a87b812]::query::caches::VecCache<rustc_span[f3562a9d228bc34b]::def_id::LocalDefId, rustc_middle[f607de435e9a4418]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[29ff09dda6aeedc5]::plumbing::QueryCtxt>
  36:     0x7f0dea107562 - rustc_hir_analysis[7867a2c041e18122]::check_crate
  37:     0x7f0dea0fa3ed - rustc_interface[d75252421fea963]::passes::analysis
  38:     0x7f0deb569b17 - <rustc_query_impl[29ff09dda6aeedc5]::dynamic_query::analysis::{closure#2} as core[b4a8d54be4c63c12]::ops::function::FnOnce<(rustc_middle[f607de435e9a4418]::ty::context::TyCtxt, ())>>::call_once
  39:     0x7f0dea4a5b7f - rustc_query_system[7835b3e98a87b812]::query::plumbing::try_execute_query::<rustc_query_impl[29ff09dda6aeedc5]::DynamicConfig<rustc_query_system[7835b3e98a87b812]::query::caches::SingleCache<rustc_middle[f607de435e9a4418]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[29ff09dda6aeedc5]::plumbing::QueryCtxt>
  40:     0x7f0dea4a5624 - rustc_query_impl[29ff09dda6aeedc5]::get_query::analysis
  41:     0x7f0dea260e3c - <rustc_interface[d75252421fea963]::queries::QueryResult<&rustc_middle[f607de435e9a4418]::ty::context::GlobalCtxt>>::enter::<core[b4a8d54be4c63c12]::result::Result<(), rustc_span[f3562a9d228bc34b]::ErrorGuaranteed>, rustc_driver_impl[64b7dd1911194932]::run_compiler::{closure#1}::{closure#2}::{closure#4}>
  42:     0x7f0dea25ffd5 - <rustc_interface[d75252421fea963]::interface::Compiler>::enter::<rustc_driver_impl[64b7dd1911194932]::run_compiler::{closure#1}::{closure#2}, core[b4a8d54be4c63c12]::result::Result<core[b4a8d54be4c63c12]::option::Option<rustc_interface[d75252421fea963]::queries::Linker>, rustc_span[f3562a9d228bc34b]::ErrorGuaranteed>>
  43:     0x7f0dea25dcfd - <scoped_tls[36ad800287692b7a]::ScopedKey<rustc_span[f3562a9d228bc34b]::SessionGlobals>>::set::<rustc_interface[d75252421fea963]::interface::run_compiler<core[b4a8d54be4c63c12]::result::Result<(), rustc_span[f3562a9d228bc34b]::ErrorGuaranteed>, rustc_driver_impl[64b7dd1911194932]::run_compiler::{closure#1}>::{closure#0}, core[b4a8d54be4c63c12]::result::Result<(), rustc_span[f3562a9d228bc34b]::ErrorGuaranteed>>
  44:     0x7f0dea25d156 - std[d9d2839bce1dd559]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[d75252421fea963]::util::run_in_thread_pool_with_globals<rustc_interface[d75252421fea963]::interface::run_compiler<core[b4a8d54be4c63c12]::result::Result<(), rustc_span[f3562a9d228bc34b]::ErrorGuaranteed>, rustc_driver_impl[64b7dd1911194932]::run_compiler::{closure#1}>::{closure#0}, core[b4a8d54be4c63c12]::result::Result<(), rustc_span[f3562a9d228bc34b]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[b4a8d54be4c63c12]::result::Result<(), rustc_span[f3562a9d228bc34b]::ErrorGuaranteed>>
  45:     0x7f0dea25cf05 - <<std[d9d2839bce1dd559]::thread::Builder>::spawn_unchecked_<rustc_interface[d75252421fea963]::util::run_in_thread_pool_with_globals<rustc_interface[d75252421fea963]::interface::run_compiler<core[b4a8d54be4c63c12]::result::Result<(), rustc_span[f3562a9d228bc34b]::ErrorGuaranteed>, rustc_driver_impl[64b7dd1911194932]::run_compiler::{closure#1}>::{closure#0}, core[b4a8d54be4c63c12]::result::Result<(), rustc_span[f3562a9d228bc34b]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[b4a8d54be4c63c12]::result::Result<(), rustc_span[f3562a9d228bc34b]::ErrorGuaranteed>>::{closure#1} as core[b4a8d54be4c63c12]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  46:     0x7f0de7b76d45 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h0ee7a0a44efbcb8b
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/alloc/src/boxed.rs:1985:9
  47:     0x7f0de7b76d45 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h8f984f8b1c075279
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/alloc/src/boxed.rs:1985:9
  48:     0x7f0de7b76d45 - std::sys::unix::thread::Thread::new::thread_start::h054937194df87b6d
                               at /rustc/18bfe5d8a9ca0e226171e98f8f4ef071790f3352/library/std/src/sys/unix/thread.rs:108:17
  49:     0x7f0de791244b - <unknown>
  50:     0x7f0de7995e40 - <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.71.0-nightly (18bfe5d8a 2023-05-14) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type bin -C embed-bitcode=no -C debuginfo=2 -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [typeck] type-checking `fn_select`
#1 [used_trait_imports] finding used_trait_imports `fn_select`
#2 [analysis] running analysis passes on this crate
end of query stack
there was a panic while trying to force a dep node
try_mark_green dep node stack:
#0 used_trait_imports(mon[dbae]::fn_select)
end of try_mark_green dep node stack

Metadata

Metadata

Labels

C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-lowLow priorityT-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