-
Notifications
You must be signed in to change notification settings - Fork 13.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
Infinite recursion while resolving traits #88925
Comments
Inside your implementation of You could either introduce a trait object (a |
I understand that my example features polymorphic recursion. But why is that forbidden? Perhaps this limitation is a consequence of monomorphization?
After a bit of exploration, I discovered that taking the writer by reference ( So I can do that to work around this issue (thanks for suggesting it!), but I still find it counterintuitive that the code cannot be completely type checked before instantiating generics (which seems like a codegen concern, i.e., part of the compiler "back end"). |
Indeed, Rust doesn't support polymorphic recursion: #4287, exactly because of monomorphization. |
Closing as wontfix then. |
Hi all! I'm running into an issue that is difficult for me to describe other than by giving a simple example. I believe the following code should compile, but instead it seems like the compiler gets stuck in a loop:
As is, the compiler gives this error when running
cargo build
:Since
writer: T
implementsWrite
(asT
has the trait bound), I would expectwriter.by_ref()
to also implementWrite
, per the documentation ofby_ref()
. But the compiler does not seem satisfied by that, and instead seems to get stuck in some kind of loop (until the recursion limit is reached).The build succeeds if the commented line is uncommented. It seems boxing the
writer
into a trait object terminates the recursion. But that requires an extra allocation, so it's not a satisfying workaround.(Strangely,
cargo check
succeeds on both versions and doesn't seem to run into this issue.)Thanks for looking into this, and I apologize if this is a duplicate of another issue. I tried searching and found a few possible instances of people running into the compiler's recursion limit, but it was hard for me to tell if it was the same issue as this one.
Meta
rustc --version --verbose
:Backtrace
The text was updated successfully, but these errors were encountered: