-
Notifications
You must be signed in to change notification settings - Fork 12.1k
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
reached the recursion limit while instantiating function_name::<[closure]>
#43520
Comments
I've got the same issue here: https://play.rust-lang.org/?gist=5253e1adb845862f26029cbf0423085c (I was trying to test if a given binary tree is a validate binary search tree) |
fn foo<F: Fn()>(x: bool, _: F) {
if x {
foo(false, || {})
}
}
fn main() {
foo(true, || {});
} Error:
The error is gone if we use a function instead of a closure: fn dummy() {}
fn foo<F: Fn()>(x: bool, _: F) {
if x {
foo(false, dummy)
}
} |
Triage: no change |
This seems to be only a naming problem here, considering the following Go code can compile: package main
type EmptyFunc func()
type FuncInterface interface {
EmptyFunc
}
func bar(f EmptyFunc) EmptyFunc { return f }
func foo[T FuncInterface](flag bool, fp T) {
if flag {
foo(false, bar(func() {}))
}
}
func main() {
foo(false, bar(func() {}))
} https://gotipplay.golang.org/p/k5ngVKP0XKE but this code cannot: fn bar<F: Fn()>(f: F) -> F {
f
}
fn foo<F: Fn()>(x: bool, _: F) {
if x {
foo(false, bar(|| {}))
}
}
fn main() {
foo(true, bar(|| {}));
} Also it is related to #77664 |
Go's generics are not monomorphized. Or more specifically, they're monomorphized per "GC shape," so in Rust terms you only have one instantiation |
I checked the Go example's objdump result:
We didn't observe reuse of monomorphized methods from real-world benchmarks in our recent paper. My understanding is, GC shape is an optimization similar to the contributions from Polymorphization Working Group. But, from this example, looks like the GC shape recognition happens eariler than Rust, which could be helpful in solving cases like this. |
rust-lang/rust#43520 there is a bug, that took my morning away from me.
Here is a shortened version of the code I hit this with:
https://play.rust-lang.org/?gist=01e76f65024cbf57bb018547932aaef2&version=stable
The error message is everything but enlightening as to what is going wrong, and from a programmer perspective, there is nothing in there that should hit a recursion limit.
@bluss had this to say on irc:
Note that I worked around it by just manually inlining the recursion, which is also shorter.
The text was updated successfully, but these errors were encountered: