Skip to content

assign a Closure in Enum #48838

Description

@ulinlong

Compiler say it's a bug :note: the compiler unexpectedly panicked. this is a bug.

I tried this code:

fn main() {
    
    let x:i32 = 0;

    let square = Functions::Square;

    println!("x square is {}", square(x));

}

enum Functions{
    Square = |x:i32|{x*x},
}

Meta

 Compiling advanced_function v0.1.0 (file:///C:/Users/tt/IdeaProjects/advanced_function)
error: internal compiler error: src\librustc_typeck\check\mod.rs:820: can't type-check body of DefId(0/0:4 ~ advanced_function[1c36]::Functions[0])
  --> src\main.rs:13:1
   |
13 | / enum Functions{
14 | |     Square = |x:i32|{x*x},
15 | | }
   | |_^

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.24.1 (d3ae9a9e0 2018-02-27) running on x86_64-pc-windows-msvc

note: run with `RUST_BACKTRACE=1` for a backtrace

thread 'rustc' panicked at 'Box<Any>', src\librustc_errors\lib.rs:451:9
stack backtrace:
   0: <std::sync::condvar::WaitTimeoutResult as core::fmt::Debug>::fmt
   1: <std::time::SystemTimeError as core::fmt::Display>::fmt
   2: std::panicking::Location::column
   3: std::panicking::Location::column
   4: std::panicking::rust_panic_with_hook
   5: <rustc_typeck::check::method::probe::ProbeScope as core::fmt::Debug>::fmt
   6: <rustc_typeck::constrained_type_params::Parameter as core::fmt::Debug>::fmt
   7: <rustc::hir::Arm as rustc_typeck::check::coercion::AsCoercionSite>::as_coercion_site
   8: <rustc_typeck::check::upvar::InferBorrowKindVisitor<'a, 'gcx, 'tcx> as rustc::hir::intravisit::Visitor<'gcx>>::nested_visit_map
   9: <rustc_typeck::check::op::Op as core::fmt::Debug>::fmt
  10: <rustc_typeck::check::CheckItemTypesVisitor<'a, 'tcx> as rustc::hir::itemlikevisit::ItemLikeVisitor<'tcx>>::visit_item
  11: rustc::ty::maps::<impl rustc::ty::maps::queries::typeck_tables_of<'tcx>>::ensure
  12: rustc::dep_graph::graph::DepGraph::in_ignore
  13: rustc::util::ppaux::<impl core::fmt::Debug for rustc::ty::Predicate<'tcx>>::fmt
  14: <rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx> as rustc::ty::layout::HasTyCtxt<'gcx>>::tcx
  15: rustc::ty::maps::<impl rustc::ty::maps::queries::typeck_tables_of<'tcx>>::ensure
  16: rustc::ty::maps::<impl rustc::ty::maps::queries::typeck_tables_of<'tcx>>::try_get
  17: rustc::ty::maps::TyCtxtAt::typeck_tables_of
  18: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::typeck_tables_of
  19: <rustc_typeck::check::CheckItemTypesVisitor<'a, 'tcx> as rustc::hir::itemlikevisit::ItemLikeVisitor<'tcx>>::visit_item
  20: rustc::ty::maps::<impl rustc::ty::maps::queries::typeck_tables_of<'tcx>>::ensure
  21: rustc::dep_graph::graph::DepGraph::in_ignore
  22: rustc::util::ppaux::<impl core::fmt::Debug for rustc::ty::Predicate<'tcx>>::fmt
  23: <rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx> as rustc::ty::layout::HasTyCtxt<'gcx>>::tcx
  24: rustc::ty::maps::<impl rustc::ty::maps::queries::typeck_tables_of<'tcx>>::ensure
  25: rustc::ty::maps::<impl rustc::ty::maps::queries::typeck_tables_of<'tcx>>::try_get
  26: rustc::ty::maps::TyCtxtAt::typeck_tables_of
  27: rustc::ty::maps::<impl rustc::ty::context::TyCtxt<'a, 'tcx, 'lcx>>::typeck_tables_of
  28: rustc_mir::interpret::const_eval::const_eval_provider
  29: rustc::ty::maps::<impl rustc::ty::maps::queries::const_eval<'tcx>>::ensure
  30: rustc::dep_graph::graph::DepGraph::in_ignore
  31: rustc::util::ppaux::<impl core::fmt::Debug for rustc::ty::Predicate<'tcx>>::fmt
  32: <rustc::ty::context::TyCtxt<'a, 'gcx, 'tcx> as rustc::ty::layout::HasTyCtxt<'gcx>>::tcx
  33: rustc::ty::maps::<impl rustc::ty::maps::queries::const_eval<'tcx>>::ensure
  34: rustc::ty::maps::<impl rustc::ty::maps::queries::const_eval<'tcx>>::try_get
  35: rustc::ty::maps::TyCtxtAt::const_eval
  36: <rustc_typeck::collect::CollectItemTypesVisitor<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_item
  37: <unknown>
  38: rustc_typeck::check_crate
  39: rustc_driver::profile::dump
  40: rustc_driver::profile::dump
  41: <rustc_driver::Compilation as core::fmt::Debug>::fmt
  42: rustc_driver::driver::compile_input
  43: rustc_driver::run_compiler
  44: rustc_driver::profile::dump
  45: _rust_maybe_catch_panic
  46: <rustc_driver::pretty::UserIdentifiedItem as core::fmt::Debug>::fmt
  47: <std::sync::condvar::Condvar as core::fmt::Debug>::fmt
  48: std::sys::windows::thread::Thread::new
  49: BaseThreadInitThunk

error: Could not compile `advanced_function`.

To learn more, run the command again with --verbose.

Process finished with exit code 101

Activity

  1. added
    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️
    on Mar 8, 2018
  2. Centril commented on Mar 8, 2018

    @Centril
    Contributor

    Minimal reproducing example

    #![crate_type = "rlib"]
    
    enum Functions {
        Square = |x| x,
    }
  3. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on May 14, 2018
  4. added a commit that references this issue on May 20, 2018
    538fea5
  5. added 3 commits that reference this issue on Feb 5, 2025
  6. added a commit that references this issue on Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.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