Skip to content

Self-referential struct with Cow<Container<Self>> as the last field errors with overflow #107481

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

Closed
PizzasBear opened this issue Jan 30, 2023 · 1 comment · Fixed by #133488
Closed
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@PizzasBear
Copy link
Contributor

I tried this code:

use std::{borrow::Cow, collections::HashMap};

// #[derive(Clone)]
// struct Foo<'a>(Cow<'a, [Self]>);

#[derive(Clone)]
struct Bar<'a>(Cow<'a, HashMap<String, Self>>);

#[derive(Clone)]
struct Baz<'a>(Cow<'a, Vec<Self>>);

#[derive(Clone)]
struct Qux<'a>(Cow<'a, Box<Self>>);

fn main() {}

Compiling it, I got these errors:

error[E0275]: overflow evaluating the requirement `HashMap<String, Bar<'a>>: Clone`
   --> src/main.rs:7:16
    |
7   | struct Bar<'a>(Cow<'a, HashMap<String, Self>>);
    |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: required for `HashMap<String, Bar<'a>>` to implement `ToOwned`
note: required by a bound in `Cow`
   --> /home/max/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/borrow.rs:181:8
    |
181 |     B: ToOwned,
    |        ^^^^^^^ required by this bound in `Cow`

error[E0275]: overflow evaluating the requirement `HashMap<String, Bar<'a>>: Clone`
   --> src/main.rs:6:10
    |
6   | #[derive(Clone)]
    |          ^^^^^
    |
    = note: required for `HashMap<String, Bar<'a>>` to implement `ToOwned`
note: required because it appears within the type `Bar<'a>`
   --> src/main.rs:7:8
    |
7   | struct Bar<'a>(Cow<'a, HashMap<String, Self>>);
    |        ^^^
note: required by a bound in `Clone`
   --> /home/max/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/clone.rs:110:18
    |
110 | pub trait Clone: Sized {
    |                  ^^^^^ required by this bound in `Clone`
    = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0275]: overflow evaluating the requirement `Vec<Baz<'a>>: Clone`
   --> src/main.rs:10:16
    |
10  | struct Baz<'a>(Cow<'a, Vec<Self>>);
    |                ^^^^^^^^^^^^^^^^^^
    |
    = note: required for `Vec<Baz<'a>>` to implement `ToOwned`
note: required by a bound in `Cow`
   --> /home/max/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/borrow.rs:181:8
    |
181 |     B: ToOwned,
    |        ^^^^^^^ required by this bound in `Cow`

error[E0275]: overflow evaluating the requirement `Vec<Baz<'a>>: Clone`
   --> src/main.rs:9:10
    |
9   | #[derive(Clone)]
    |          ^^^^^
    |
    = note: required for `Vec<Baz<'a>>` to implement `ToOwned`
note: required because it appears within the type `Baz<'a>`
   --> src/main.rs:10:8
    |
10  | struct Baz<'a>(Cow<'a, Vec<Self>>);
    |        ^^^
note: required by a bound in `Clone`
   --> /home/max/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/clone.rs:110:18
    |
110 | pub trait Clone: Sized {
    |                  ^^^^^ required by this bound in `Clone`
    = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0275]: overflow evaluating the requirement `Qux<'a>: Sized`
   --> src/main.rs:13:16
    |
13  | struct Qux<'a>(Cow<'a, Box<Self>>);
    |                ^^^^^^^^^^^^^^^^^^
    |
    = note: required for `Box<Qux<'a>>` to implement `Clone`
    = note: required for `Box<Qux<'a>>` to implement `ToOwned`
note: required by a bound in `Cow`
   --> /home/max/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/borrow.rs:181:8
    |
181 |     B: ToOwned,
    |        ^^^^^^^ required by this bound in `Cow`

error[E0275]: overflow evaluating the requirement `Box<Qux<'a>>: Clone`
   --> src/main.rs:12:10
    |
12  | #[derive(Clone)]
    |          ^^^^^
    |
    = note: required for `Box<Qux<'a>>` to implement `ToOwned`
note: required because it appears within the type `Qux<'a>`
   --> src/main.rs:13:8
    |
13  | struct Qux<'a>(Cow<'a, Box<Self>>);
    |        ^^^
note: required by a bound in `Clone`
   --> /home/max/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/clone.rs:110:18
    |
110 | pub trait Clone: Sized {
    |                  ^^^^^ required by this bound in `Clone`
    = note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0275`.
error: could not compile `test-issue` due to 6 previous errors

This issue seems similar to #89940, but the error you get is different (an overflow instead of a trait bound not satisfied).

Meta

rustc --version --verbose:

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
@Enselic
Copy link
Member

Enselic commented Nov 26, 2024

This began to compile in 1.79. Regression test added via #133488 which closes this issue.

jieyouxu added a commit to jieyouxu/rust that referenced this issue Nov 30, 2024
tests: Add regression test for self referential structs with cow as last field

Making compilation pass for this code was retroactively stabilized via FCP in 1.79. The code does not compile in 1.78.

See rust-lang#129541 for details.

Closes rust-lang#107481
@bors bors closed this as completed in f1d9ba8 Nov 30, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 30, 2024
Rollup merge of rust-lang#133488 - Enselic:recurse-2, r=BoxyUwU

tests: Add regression test for self referential structs with cow as last field

Making compilation pass for this code was retroactively stabilized via FCP in 1.79. The code does not compile in 1.78.

See rust-lang#129541 for details.

Closes rust-lang#107481
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

Successfully merging a pull request may close this issue.

3 participants