Skip to content

Using Cow with the Vec/slice ToOwned impl for creating a recursive type fails to typecheck due to overflow #38962

Closed as not planned
@sdroege

Description

@sdroege

See the following code

use std::borrow::Cow;

// Does not work either: #[derive(Clone)]
enum Foo<'a> {
    // Does not work:
    Bar(Cow<'a, [Foo<'static>]>),
    // Works: Bar(Vec<Foo<'a>>),
    Baz(&'a str),
}

impl<'a> Clone for Foo<'a> {
    fn clone(&self) -> Self {
        unimplemented!();
    }
}

fn main() {}

This currently fails to compile with

error[E0275]: overflow evaluating the requirement `<[Foo<'a>] as std::borrow::ToOwned>::Owned`
 --> test.rs:5:9
  |
5 |     Bar(Cow<'a, [Foo<'a>]>),
  |         ^^^^^^^^^^^^^^^^^^^
  |
  = note: required because it appears within the type `Foo<'a>`
  = note: required because of the requirements on the impl of `std::borrow::ToOwned` for `[Foo<'a>]`
  = note: required because it appears within the type `std::borrow::Cow<'a, [Foo<'a>]>`
  = note: only the last field of a struct may have a dynamically sized type

However in the end the Cow here is just an enum that is either a &[Foo] or a Vec (the ToOwned instance of [T] uses Vec for the owned variant), both having a known size and thus this all should really work, or am I missing something?

Is this a bug, a missing feature or can't this possibly work?

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