Skip to content

compiler error when using static dispatch with traits #122994

Closed as not planned
Closed as not planned
@alirezasariri78

Description

@alirezasariri78
trait StaticFactory {
    type B: Button;
    type C: CheckBox;

    fn create_button() -> Self::B;
    fn create_checkbox() -> Self::C;
}

trait DynamicFactory {
    fn create_button() -> Box<dyn Button>;
    fn create_checkbox() -> Box<dyn CheckBox>;
}

trait Button {
    fn press(&self);
}

trait CheckBox {
    fn switch(&self);
}

struct WindowsGui;
struct MacGui;

impl StaticFactory for WindowsGui {
    type B = SimpleButton;
    type C = SimpleCheckBox;

    fn create_button() -> Self::B {
        SimpleButton
    }

    fn create_checkbox() -> Self::C {
        SimpleCheckBox
    }
}

impl StaticFactory for MacGui {
    type B = BetterButton;
    type C = BetterCheckBox;

    fn create_button() -> Self::B {
        BetterButton
    }

    fn create_checkbox() -> Self::C {
        BetterCheckBox
    }
}

struct SimpleButton;
struct BetterButton;

struct SimpleCheckBox;
struct BetterCheckBox;

impl Button for SimpleButton {
    fn press(&self) {
        println!("simple button");
    }
}

impl Button for BetterButton {
    fn press(&self) {
        println!("better button");
    }
}

impl CheckBox for SimpleCheckBox {
    fn switch(&self) {
        println!("simple CheckBox");
    }
}

impl CheckBox for BetterCheckBox {
    fn switch(&self) {
        println!("better CheckBox");
    }
}

fn main() {
    let is_win = true;

    if is_win {}
}

fn render(factory: impl StaticFactory) {
    let button = factory.create_button();
    let chbox = factory.create_checkbox();
}

hi . this is my code . i got following error in compiler :

thread 'rustc' panicked at compiler\rustc_hir_typeck\src\method\suggest.rs:3299:28:
index out of bounds: the len is 0 but the index is 0
stack backtrace:
   0:     0x7fff0a0d3053 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h99d8f55dc77ea58e
   1:     0x7fff0a105a3d - core::fmt::write::hde05c98aa8466c89
   2:     0x7fff0a0c9951 - <std::io::IoSlice as core::fmt::Debug>::fmt::hfbd96d963048182d
   3:     0x7fff0a0d2e5a - std::sys_common::backtrace::lock::h19b13d03fb733c4f
   4:     0x7fff0a0d6329 - std::panicking::default_hook::h809e3ce877795dde
   5:     0x7fff0a0d5feb - std::panicking::default_hook::h809e3ce877795dde
   6:     0x7ffea701f522 - <tracing_subscriber[6409740a17f48d06]::fmt::format::Writer>::write_fmt
   7:     0x7fff0a0d6903 - std::panicking::rust_panic_with_hook::hac29101b109c3744
   8:     0x7fff0a0d67a5 - <std::panicking::begin_panic_handler::StaticStrPayload as core::panic::PanicPayload>::take_box::h1529e0c3d6e161ce
   9:     0x7fff0a0d3949 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h99d8f55dc77ea58e
  10:     0x7fff0a0d64b4 - rust_begin_unwind
  11:     0x7fff0a12ae57 - core::panicking::panic_fmt::ha2a0d31008962eb7
  12:     0x7fff0a12b094 - core::panicking::panic_bounds_check::h8fe0e36ff80060e3
  13:     0x7ffea72bf8c6 - <rustc_hir_typeck[da4bc085033ff0d6]::method::suggest::TraitInfo as core[94f19dc4588870f6]::cmp::PartialOrd>::partial_cmp
  14:     0x7ffea728d5ea - <rustc_hir_typeck[da4bc085033ff0d6]::fn_ctxt::FnCtxt>::report_no_match_method_error
  15:     0x7ffea7281042 - <rustc_hir_typeck[da4bc085033ff0d6]::fn_ctxt::FnCtxt>::report_no_match_method_error
  16:     0x7ffea729ef5a - <rustc_hir_typeck[da4bc085033ff0d6]::fn_ctxt::FnCtxt>::report_method_error
  17:     0x7ffea619a286 - <rustc_hir_typeck[da4bc085033ff0d6]::fn_ctxt::FnCtxt>::demand_coerce
  18:     0x7ffea61c8816 - <rustc_hir_typeck[da4bc085033ff0d6]::fn_ctxt::FnCtxt>::check_struct_path
  19:     0x7ffea61c9ef4 - <rustc_hir_typeck[da4bc085033ff0d6]::fn_ctxt::FnCtxt>::check_struct_path
  20:     0x7ffea6193956 - <rustc_hir_typeck[da4bc085033ff0d6]::fn_ctxt::FnCtxt>::demand_coerce
  21:     0x7ffea61ae9c8 - <rustc_hir_typeck[da4bc085033ff0d6]::fn_ctxt::FnCtxt>::demand_coerce
  22:     0x7ffea625be61 - <rustc_hir_typeck[da4bc085033ff0d6]::upvar::InferBorrowKind as rustc_hir_typeck[da4bc085033ff0d6]::expr_use_visitor::Delegate>::borrow
  23:     0x7ffea6172947 - rustc_hir_typeck[da4bc085033ff0d6]::typeck
  24:     0x7ffea696190f - <rustc_query_impl[70f5d3af722ab40a]::plumbing::QueryCtxt as rustc_query_system[924d71a2b3348c3f]::query::QueryContext>::load_side_effects
  25:     0x7ffea694765f - rustc_query_impl[70f5d3af722ab40a]::query_callbacks
  26:     0x7ffea6a23499 - <rustc_query_impl[70f5d3af722ab40a]::plumbing::QueryCtxt as rustc_query_system[924d71a2b3348c3f]::query::QueryContext>::load_side_effects
  27:     0x7ffea69bec54 - <rustc_query_impl[70f5d3af722ab40a]::plumbing::QueryCtxt as rustc_query_system[924d71a2b3348c3f]::query::QueryContext>::load_side_effects
  28:     0x7ffea630f021 - <rustc_hir_analysis[9da19c1a75884dcd]::collect::generics_of::has_late_bound_regions::LateBoundRegionsDetector as rustc_hir[6c7798e1a793feda]::intravisit::Visitor>::visit_ty
  29:     0x7ffea62ac7e2 - rustc_hir_analysis[9da19c1a75884dcd]::check_crate
  30:     0x7ffea3216a07 - rustc_interface[845e99c2fcb4bb36]::passes::analysis
  31:     0x7ffea5c456fd - rustc_query_impl[70f5d3af722ab40a]::profiling_support::alloc_self_profile_query_strings
  32:     0x7ffea5c292d9 - rustc_query_impl[70f5d3af722ab40a]::dynamic_queries
  33:     0x7ffea5c7ccec - rustc_query_impl[70f5d3af722ab40a]::profiling_support::alloc_self_profile_query_strings
  34:     0x7ffea5caecbc - rustc_query_impl[70f5d3af722ab40a]::profiling_support::alloc_self_profile_query_strings
  35:     0x7ffea5c83eda - rustc_query_impl[70f5d3af722ab40a]::profiling_support::alloc_self_profile_query_strings
  36:     0x7ffea31cf53b - rustc_driver_impl[b4ac4ed7e8bb44fd]::args::arg_expand_all
  37:     0x7ffea31cd631 - rustc_driver_impl[b4ac4ed7e8bb44fd]::args::arg_expand_all
  38:     0x7ffea31bf4bb - rustc_driver_impl[b4ac4ed7e8bb44fd]::args::arg_expand_all
  39:     0x7ffea31cf101 - rustc_driver_impl[b4ac4ed7e8bb44fd]::args::arg_expand_all
  40:     0x7ffea31ca6df - rustc_driver_impl[b4ac4ed7e8bb44fd]::args::arg_expand_all
  41:     0x7ffea31c2861 - rustc_driver_impl[b4ac4ed7e8bb44fd]::args::arg_expand_all
  42:     0x7fff0a0e8b6c - std::sys::windows::thread::Thread::new::h7b3ce3a09b163019
  43:     0x7fff5ed553e0 - BaseThreadInitThunk
  44:     0x7fff6052485b - RtlUserThreadStart

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.75.0 (82e1608df 2023-12-21) running on x86_64-pc-windows-msvc

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 `render`
#1 [analysis] running analysis passes on this crate
end of query stack
error: could not compile `design_patterns` (bin "design_patterns")

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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions