Skip to content

Recursive function call generates confusing overflow message #72577

Closed

Description

@o0Ignition0o

error[E0275]: overflow evaluating the requirement &mut bytes::bytes_mut::BytesMut: bytes::buf::buf_impl::Buf

Here's a link to the playground : https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=e0d226183c371b79d9a234ccebdeafd0

I tried this code:

use bytes::{Buf, BytesMut};

fn get_value<B>(buf: &mut B) -> Result<i32, String>
where
    B: Buf,
{
    Ok(42)
}

fn do_more<B>(mut buf: B) -> Result<i32, String>
where
    B: Buf,
{
    if buf.has_remaining() {
        get_value(&mut buf)
    } else {
        do_more(&mut buf)
    }
}

fn main() {
    let payload = vec![1, 2, 3, 4, 5];
    let parsed = do_more(&mut BytesMut::from(&*payload)).unwrap();
}

I expected to see this happen: Nothing really, but I would love to see an error message that would help me out here ^^.

Instead, this happened:

image

Meta

rustc --version --verbose:

playground (stable) and tested it on my machine: rustc 1.43.1 (8d69840ab 2020-05-04)

Link to the twitter discussion https://twitter.com/ekuber/status/1264962969206525952?s=20

Paging @estebank who might be interested <3

Activity

  1. changed the title [-]error[E0275]: overflow evaluating the requirement `&mut bytes::bytes_mut::BytesMut: bytes::buf::buf_impl::Buf`[/-] [+]Error message that's a bit hard to figure out[/+] on May 25, 2020
  2. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    C-bugCategory: This is a bug.
    on May 25, 2020
  3. Aaron1011 commented on May 25, 2020

    @Aaron1011
    Contributor

    @o0Ignition0o: You can make this compile by changing get_value(&mut buf) to get_value(buf) - note that this will cause infinite recursion at runtime if buf.has_remaining() == false

    Minimized:

    struct BytesMut;
    
    trait Buf {}
    
    impl Buf for BytesMut {}
    impl<'a, T: Buf> Buf for &'a mut T {}
    
    fn do_more<B>(mut buf: B)
    where
        B: Buf,
    {
        do_more(&mut buf);
    }
    
    fn main() {
        do_more(&mut BytesMut)
    }

    Trying to monomorphize do_more::<B> causes us to recursively monomorphize do_more::<&mut B>, which will eventually hit the recursion limit.

    We should be able to detect this kind of recursive monomorphization in simple cases (when the function directly calls itself rather than going through a helper function).

  4. o0Ignition0o commented on May 25, 2020

    @o0Ignition0o
    ContributorAuthor

    Oh thanks a lot! So long story short I should try to avoid recursion in this kind of scenario ? (the real life use case is a bit more complicated, but i was willing about to use recursion to solve it ^^')

    thanks again :D

  5. added
    D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.
    on May 27, 2020
  6. changed the title [-]Error message that's a bit hard to figure out[/-] [+]Recursive function call generates confusing overflow message[/+] on May 28, 2020
  7. added 2 commits that reference this issue on Jun 23, 2020
  8. estebank commented on May 27, 2024

    @estebank
    Contributor

    Current output:

    error: reached the recursion limit while instantiating `do_more::<&mut &mut &mut &mut &mut ...>`
      --> src/main.rs:17:9
       |
    17 |         do_more(&mut buf)
       |         ^^^^^^^^^^^^^^^^^
       |
    note: `do_more` defined here
      --> src/main.rs:10:1
       |
    10 | / fn do_more<B>(mut buf: B) -> Result<i32, String>
    11 | | where
    12 | |     B: Buf,
       | |___________^
       = note: the full type name has been written to '/playground/target/debug/deps/playground-b0d2208020a7eb98.long-type.txt'
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions