-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Stack overflow in compiler involving closures #22638
Comments
I'm getting a similar stack overflow with closures. (I think this shouldn't be able to monomorphize?) extern crate threadpool;
use std::sync::{Arc,Mutex};
use threadpool::ScopedPool;
fn test<'pool, F: FnMut(usize) + Send + Sync + 'pool>(mut callback: F, depth: usize, pool: Arc<Mutex<ScopedPool<'pool>>>) {
if depth == 0 {
callback(100);
} else {
struct Foo<F: FnMut(usize)> {
callback: F
}
let foo = Arc::new(Mutex::new(Foo{callback: move |u| {
callback(u);
}}));
for _ in 0..50 {
struct Bar<F: FnMut(usize)> {
callback: F
}
let foo = foo.clone();
let bar = Arc::new(Mutex::new(Bar {
callback: move |u| {
let foo = &mut *foo.lock().unwrap();
(foo.callback)(u);
}
}));
for _ in 0..5 {
let descending_pool = pool.clone();
let pool = pool.lock().unwrap();
let bar = bar.clone();
pool.execute(move || {
test(move |n| {
let bar = &mut *bar.lock().unwrap();
(bar.callback)(n);
}, depth - 1, descending_pool);
});
}
}
}
}
fn main() {
let pool = Arc::new(Mutex::new(ScopedPool::new(4)));
test(|num| {
println!("woohoo {}", num);
}, 1, pool);
}
|
This code doesn't compile too.
|
Status update: my code no longer has an ICE, but instead has:
|
@frewsxcv That's good. Most of these code snippets should result in that sort of error. The type inference is hiding a recursive type definition which will necessarily arise during monomorphization. These are impossible to express statically. By the way, here's a reduced example of the stack overflow: fn main() {
let mut test = None;
test = Some(|| test.unwrap());
} I think because it's otherwise impossible to express recursive type definitions in code, a lot of different areas of the compiler assume this will not happen. But with closures it can right now; we should probably check for that somewhere. |
This seems to be fixed in nightly. |
@jimtla pasted his code he was working with on IRC and I simplified it a bit:
This might be a duplicate of #21410
The text was updated successfully, but these errors were encountered: