Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
KiitoX opened this issue May 18, 2023 · 4 comments · Fixed by #111747
Closed

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

KiitoX opened this issue May 18, 2023 · 4 comments · Fixed by #111747
Assignees
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-low Low priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@KiitoX
Copy link

KiitoX commented May 18, 2023

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

@KiitoX KiitoX added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 18, 2023
@matthiaskrgr
Copy link
Member

reduced a bit

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

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> {}

impl<F, R, A1, A2> Wrap<F> for Value<Rc<dyn Fn(A1, A2) -> R>> {}

impl<F> Deref for Value<Rc<F>> {
    type Target = F;
}

fn fn_select(var_x: bool) -> i64 {
    let var_fn = Value::wrap();
    match var_fn.clone() {}
}

@matthiaskrgr
Copy link
Member

Regression in nightly-2020-06-24

@matthiaskrgr matthiaskrgr added the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label May 18, 2023
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label May 18, 2023
@compiler-errors compiler-errors self-assigned this May 18, 2023
@compiler-errors
Copy link
Member

compiler-errors commented May 18, 2023

This is minimized in the sense that spurious errors are avoided:

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

struct Value<T>(T);

pub trait Wrap<T> {
    fn wrap() -> Self;
}

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

impl<F, R, A1, A2> Wrap<F> for Value<Rc<dyn Fn(A1, A2) -> R>> {
    fn wrap() -> Self {
        todo!()
    }
}

impl<F> Deref for Value<Rc<F>> {
    type Target = F;

    fn deref(&self) -> &Self::Target {
        &*self.0
    }
}

fn main() {
    let var_fn = Value::wrap();
    let _ = var_fn.clone();
}

The problem here is that the autoderef of a method is interacting strangely with the Value<_>: Wrap obligation registered by the let var_fn = ... line. This actually guides inference in such a way that an assertion in the compiler is not upheld -- that assertion is kinda nonsense though, so I'm just gonna rework it.

@apiraino
Copy link
Contributor

apiraino commented May 19, 2023

I've tried bisecting to a narrower point but didn't get far:

commit[0] 2020-06-22: Auto merge of 73415 - ehuss:update-cargo, r=ehuss
commit[1] 2020-06-22: Auto merge of 73594 - Aaron1011:revert/move-fn-self-msg, r=Manishearth
commit[2] 2020-06-23: Auto merge of 73007 - yoshuawuyts:socketaddr-from-string-u16, r=sfackler
commit[3] 2020-06-23: Auto merge of 73635 - Dylan-DPC:rollup-b4wbp42, r=Dylan-DPC
commit[4] 2020-06-23: Auto merge of 73643 - Manishearth:rollup-68dr8fz, r=Manishearth
commit[5] 2020-06-23: Auto merge of 73644 - ollie27:rustdoc_alias_filter, r=GuillaumeGomez

So I guess one #72389, #73635 or #73643

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-low

@rustbot rustbot added P-low Low priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels May 19, 2023
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 23, 2023
…ide-effects, r=fee1-dead

Don't structurally resolve during method ambiguity in probe

See comment in UI test for reason for the failure. This is all on the error path anyways, not really sure what the assertion is there to achieve anyways...

Fixes rust-lang#111739
@bors bors closed this as completed in 01cc9ca Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-low Low priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants