You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
An actual typeck error is the cause of many failed compilations but an
unrelated bug is being reported instead. It is triggered because a typeck
error is presumably not yet identified during compiler execution, which
would normally bypass an invariant in the presence of other errors. In
this particular situation, we delay the reporting of the bug until
abort_if_errors().
Closes#23827, closes#24356, closes#23041, closes#22897, closes#23966,
closes#24013, and closes#23729
**There is at least one situation where this bug may still be genuinely
triggered (#23437).**
Just playing with higher-order functions and structs with function fields.
I tried this code:
I expected to see this happen:
Instead, this happened:
Meta
Backtrace:
The text was updated successfully, but these errors were encountered: