Skip to content
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 <the type> in pub functions #107837

Open
Mingun opened this issue Feb 9, 2023 · 1 comment
Open
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Mingun
Copy link

Mingun commented Feb 9, 2023

This issue was reported in quick-xml repo, but I think, this is a bug in rustc. After minification I ended with that code:

pub fn main() {// removing "pub" eliminates error
    use serde::ser::{SerializeStructVariant, Serializer};

    let serializer = quick_xml::se::Serializer::new(String::new());

    let mut state = serializer.serialize_map(None).unwrap();
    serde::__private::ser::FlatMapSerializer(&mut state)
        .serialize_struct_variant("Enum", 0u32, "Variant", 0)
        .unwrap().end() // Commenting this line eliminates error
        .unwrap();
}

Used libraries:

[dependencies]
quick-xml = { version = "0.27.1", features = ["serialize"] }
serde = "1.0.152"

I expected to see this happen: this code should compile

Instead, this happened:

$ cargo build
   Compiling hk_modlinks v0.1.0 (/home/mingun/Projects/hk_modlinks)
error: reached the recursion limit while instantiating `<serde::__private::ser::content:... &mut &mut std::string::String>>`
   --> /home/mingun/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.152/src/ser/impls.rs:433:1
    |
433 | deref_impl!(<'a, T: ?Sized> Serialize for &'a T where T: Serialize);
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: `<serde::__private::ser::content::Content as Serialize>::serialize` defined here
   --> /home/mingun/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.152/src/private/ser.rs:472:9
    |
472 | /         fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
473 | |         where
474 | |             S: Serializer,
    | |__________________________^
    = note: the full type name has been written to '/home/mingun/Projects/hk_modlinks/target/debug/deps/hk_modlinks-59717a86221e1ac9.long-type.txt'
    = note: this error originates in the macro `deref_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

error: could not compile `hk_modlinks` due to previous error
$

Meta

I've tries several compilers (the first I had, then I've updated and checked again):
rustc --version --verbose:

rustc 1.66.1 (90743e729 2023-01-10)
binary: rustc
commit-hash: 90743e7298aca107ddaa0c202a4d3604e29bfeb6
commit-date: 2023-01-10
host: x86_64-unknown-linux-gnu
release: 1.66.1
LLVM version: 15.0.2
rustc 1.67.0 (fc594f156 2023-01-24)
binary: rustc
commit-hash: fc594f15669680fa70d255faec3ca3fb507c3405
commit-date: 2023-01-24
host: x86_64-unknown-linux-gnu
release: 1.67.0
LLVM version: 15.0.6
rustc 1.69.0-nightly (ef934d9b6 2023-02-08)
binary: rustc
commit-hash: ef934d9b632b8ac00276558824664c104b92b5f0
commit-date: 2023-02-08
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7

Content of hk_modlinks-59717a86221e1ac9.long-type.txt (&mut ...128...String):

<serde::__private::ser::content::Content as Serialize>::serialize::<se::element::ElementSerializer<'_, &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut &mut std::string::String>>

The strange thing here is that the compilation succeed if I remove pub modifier from the function, this is why I think this is a bug in rustc and not in my code. Moreover, the compiled code never create 128 &mut references

Backtrace is not generated with RUST_BACKTRACE=1 cargo build.

@Mingun
Copy link
Author

Mingun commented Feb 12, 2023

It seems, that the simplified testcase for this is reported here, because quick-xml:

  • also uses std::fmt::Write for implementing Serializer
  • uses the trick to temporary borrow any Write implementation using &mut writer and use it as another std::fmt::Write implementation

Mingun added a commit to Mingun/quick-xml that referenced this issue Feb 18, 2023
Mingun added a commit to Mingun/quick-xml that referenced this issue Feb 18, 2023
dralley pushed a commit to tafia/quick-xml that referenced this issue Feb 20, 2023
@jyn514 jyn514 added A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants