Skip to content

compiler panic - struct function fields passed to higher order function. #23437

Closed
@mikehadlow

Description

@mikehadlow

Just playing with higher-order functions and structs with function fields.

I tried this code:

// the main function
fn main() {

    let funcs = Funcs {
        a: Box::new( |x| { 1 + x } ),
        b: Box::new( |x| { x * 2 } )
    };

    let composed = compose(&funcs.a, &funcs.b);

    let r1: i32 = composed(3);
    let r2: i32 = composed(10);

    println!("r1 is {}", r1);
    println!("r2 is {}", r2);

}

// a higher order function
fn compose<'a, T, F: 'a, G: 'a>(f: &'a F, g: &'a G) -> Box<Fn(T) -> T + 'a>
    where
        F: Fn(T) -> T,
        G: Fn(T) -> T {

    Box::new(move |x| { f(g(x)) })
}

struct Funcs {
    a: Box<Fn(i32) -> i32>,
    b: Box<Fn(i32) -> i32>,
}

I expected to see this happen:

Compiling rust_hello v0.0.1 (file:///home/mike/source/rust_hello)
Running `target/debug/rust_hello`
r1 is 7
r2 is 21

Instead, this happened:

Compiling rust_hello v0.0.1 (file:///home/mike/source/rust_hello)
src/main.rs:10:20: 10:47 error: internal compiler error: cat_expr Errd
src/main.rs:10     let composed = compose(&funcs.a, &funcs.b);
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
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: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:130

Meta

rustc --version --verbose
rustc 1.0.0-nightly (30e1f9a1c 2015-03-14) (built 2015-03-15)
binary: rustc
commit-hash: 30e1f9a1c2bf7134135800bc9afd082773defadc
commit-date: 2015-03-14
build-date: 2015-03-15
host: x86_64-unknown-linux-gnu
release: 1.0.0-nightly

Backtrace:

RUST_BACKTRACE=1 rustc src/main.rs
src/main.rs:10:20: 10:47 error: internal compiler error: cat_expr Errd
src/main.rs:10     let composed = compose(&funcs.a, &funcs.b);
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
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: run with `RUST_BACKTRACE=1` for a backtrace
thread 'rustc' panicked at 'Box<Any>', /home/rustbuild/src/rust-buildbot/slave/nightly-dist-rustc-linux/build/src/libsyntax/diagnostic.rs:130

stack backtrace:
   1:     0x7fb896007fd4 - sys::backtrace::write::hc598c94e8fe3368fYyC
   2:     0x7fb89602dd98 - panicking::on_panic::he6f0d5fd56790ce4OGI
   3:     0x7fb895f64faa - rt::unwind::begin_unwind_inner::h0de67d11bf40bff3mnI
   4:     0x7fb8933e140d - rt::unwind::begin_unwind::h1003680148821460282
   5:     0x7fb8933e13b3 - diagnostic::SpanHandler::span_bug::h1c5c4bceba1974e2FaB
   6:     0x7fb893c98523 - session::Session::span_bug::h04e509cfca0af677Qvn
   7:     0x7fb894cc60e1 - check::regionck::visit_expr::h6d73f9ea96a73481dCd
   8:     0x7fb894cc69fa - check::regionck::visit_local::h94f330bbd6249aa8izd
   9:     0x7fb894cc18a5 - check::regionck::Rcx<'a, 'tcx>::visit_fn_body::h91c2ca9bef55fb39qed
  10:     0x7fb894d4c566 - check::check_bare_fn::ha0fa7770160c8d54b8m
  11:     0x7fb894d44681 - check::check_item::h289f0c28a59d81b7Pqn
  12:     0x7fb894e18a03 - check_crate::closure.35785
  13:     0x7fb894e1373a - check_crate::h77f9ea673c087f80O3B
  14:     0x7fb89662fbf8 - driver::phase_3_run_analysis_passes::h716709d467e74aabqGa
  15:     0x7fb896616f59 - driver::compile_input::h56dbe0e66fe8a956Rba
  16:     0x7fb8966cea42 - run_compiler::hbbbe30f1ad654d0ex2b
  17:     0x7fb8966cc88c - thunk::F.Invoke<A, R>::invoke::h9510793753674638958
  18:     0x7fb8966cb910 - rt::unwind::try::try_fn::h15118916039308917284
  19:     0x7fb89609ef58 - rust_try_inner
  20:     0x7fb89609ef45 - rust_try
  21:     0x7fb8966cbdb6 - thunk::F.Invoke<A, R>::invoke::h8595907971712275588
  22:     0x7fb89601c995 - sys::thread::thread_start::h9f0c06661d546448B1G
  23:     0x7fb88fec2181 - start_thread
  24:     0x7fb895bd647c - __clone
  25:                0x0 - <unknown>

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions