Skip to content

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

Closed
@PizzasBear

Description

@PizzasBear

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.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