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

serde causes 'rustc' stack overflow with in function struct definition #1953

Closed
xu-cheng opened this issue Jan 18, 2021 · 4 comments
Closed

Comments

@xu-cheng
Copy link

xu-cheng commented Jan 18, 2021

Example to reproduce the bug.

  • Cargo.toml
[dependencies]
serde = { version = "1.0", features = ["derive"] }
  • src/lib.rs
use serde::{
    ser::{SerializeSeq, Serializer},
    Serialize,
};

pub struct Container<T> {
    inner: Vec<T>,
}

impl<T: Serialize + Clone> Serialize for Container<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        #[derive(Serialize)]
        struct Foo<T> {
            obj: T,
        }

        let mut seq = serializer.serialize_seq(Some(self.inner.len()))?;
        for k in self.inner.iter() {
            seq.serialize_element(&Foo { obj: k.clone() })?;
        }
        seq.end()
    }
}
  • Compile result
❯ cargo +nightly check
    Checking test-rust v0.1.0 (/private/tmp/test-rust)

thread 'rustc' has overflowed its stack
fatal runtime error: stack overflow
error: could not compile `test-rust`

Caused by:
  process didn't exit successfully: `rustc --crate-name test_rust --edition=2018 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata -C embed-bitcode=no -C debuginfo=2 -C metadata=6743fa578220919d -C extra-filename=-6743fa578220919d --out-dir /private/tmp/test-rust/target/debug/deps -C incremental=/private/tmp/test-rust/target/debug/incremental -L dependency=/private/tmp/test-rust/target/debug/deps --extern serde=/private/tmp/test-rust/target/debug/deps/libserde-80a0b09a70478260.rmeta` (signal: 6, SIGABRT: process abort signal)

I am on macOS. This bug can be reproduced with both the stable (1.49.0 (e1884a8e3 2020-12-29)) and latest nightly (1.51.0-nightly (4253153db 2021-01-17)) rustc.

If we move Foo struct to the outside of the serialize function, it can compile successfully.

@xu-cheng
Copy link
Author

Also, this may be a regression. Because the same code was able to be compiled one year ago.

@xu-cheng xu-cheng changed the title serde causes 'rustc' stack overflow with nested struct definition serde causes 'rustc' stack overflow with in function struct definition Jan 18, 2021
@xu-cheng
Copy link
Author

xu-cheng commented Jan 18, 2021

Using cargo expand, I reduce the example to the following:

use serde::{ser::Serializer, Serialize};

pub struct Container;

impl Serialize for Container {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        const _: () = {
            extern crate serde as _serde;
        };

        todo!()
    }
}
❯ cargo +nightly check
    Checking test-rust v0.1.0 (/private/tmp/test-rust)
warning: unused variable: `serializer`
 --> src/lib.rs:6:28
  |
6 |     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  |                            ^^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_serializer`
  |
  = note: `#[warn(unused_variables)]` on by default


thread 'rustc' has overflowed its stack
fatal runtime error: stack overflow
error: could not compile `test-rust`

Caused by:
  process didn't exit successfully: `rustc --crate-name test_rust --edition=2018 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata -C embed-bitcode=no -C debuginfo=2 -C metadata=6743fa578220919d -C extra-filename=-6743fa578220919d --out-dir /private/tmp/test-rust/target/debug/deps -C incremental=/private/tmp/test-rust/target/debug/incremental -L dependency=/private/tmp/test-rust/target/debug/deps --extern serde=/private/tmp/test-rust/target/debug/deps/libserde-80a0b09a70478260.rmeta` (signal: 6, SIGABRT: process abort signal)

@dtolnay
Copy link
Member

dtolnay commented Jan 19, 2021

This is a rustc bug. rust-lang/rust#55779

@xu-cheng
Copy link
Author

Thanks for the info. Should I close this issue? Or leave it until it is fixed by rustc?

@dtolnay dtolnay closed this as completed Jan 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants