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

ToOwned not implemented for Cow as it ought to be #100347

Closed
marmeladema opened this issue Aug 9, 2022 · 3 comments · Fixed by #133473
Closed

ToOwned not implemented for Cow as it ought to be #100347

marmeladema opened this issue Aug 9, 2022 · 3 comments · Fixed by #133473
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Comments

@marmeladema
Copy link
Contributor

I tried this code:

use std::borrow::Cow;

#[derive(Clone)]
enum Test <'a> {
    Int(u8),
    Array(Cow<'a, [Test<'a>]>),
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=442442eaa00a34e7c38f9d9db3654160

I expected to see this happen: it compiles successfully.

Instead, this happened: it fails to compile with the following error:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `[Test<'a>]: ToOwned` is not satisfied in `Cow<'a, [Test<'a>]>`
 --> src/lib.rs:5:11
  |
5 |     Array(Cow<'a, [Test<'a>]>),
  |           ^^^^^^^^^^^^^^^^^^^ within `Cow<'a, [Test<'a>]>`, the trait `ToOwned` is not implemented for `[Test<'a>]`
  |
  = help: the trait `ToOwned` is implemented for `[T]`
  = note: required because it appears within the type `Cow<'a, [Test<'a>]>`
  = note: no field of an enum variant may have a dynamically sized type
  = help: change the field's type to have a statically known size
help: borrowed types always have a statically known size
  |
5 |     Array(&Cow<'a, [Test<'a>]>),
  |           +
help: the `Box` type always has a statically known size and allocates its contents in the heap
  |
5 |     Array(Box<Cow<'a, [Test<'a>]>>),
  |           ++++                   +

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): the trait bound `[Test<'a>]: ToOwned` is not satisfied in `Test<'a>`
   --> src/lib.rs:3:10
    |
3   | #[derive(Clone)]
    |          ^^^^^ within `Test<'a>`, the trait `ToOwned` is not implemented for `[Test<'a>]`
    |
    = help: the trait `ToOwned` is implemented for `[T]`
note: required because it appears within the type `Test<'a>`
   --> src/lib.rs:4:6
    |
4   | enum Test <'a> {
    |      ^^^^
note: required by a 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 E0277`.

Meta

As of today's stable version 1.61.1, no versions have ever successfully accepted this code. However I believe there is nothing inherently wrong about it and I have faced this failure for such pattern multiple times already.

Workaround

You can workaround the issue by writing your Cow-like type.

@marmeladema marmeladema added the C-bug Category: This is a bug. label Aug 9, 2022
@lcnr lcnr added the T-types Relevant to the types team, which will review and decide on the PR/issue. label Aug 10, 2022
@Enselic Enselic changed the title ToOwned not implemented as it ought to be ToOwned not implemented for Cow as it ought to be Apr 15, 2024
@Enselic Enselic added T-libs Relevant to the library team, which will review and decide on the PR/issue. and removed T-types Relevant to the types team, which will review and decide on the PR/issue. labels Nov 12, 2024
@CatsAreFluffy
Copy link

This works as of Rust 1.79, so I think this should be closed.

@Enselic
Copy link
Member

Enselic commented Nov 18, 2024

Nice, thanks for the notice. Let's make sure there is a test for the variant in this issue. Either as part of #129541 or independently.

@Enselic Enselic added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. labels Nov 18, 2024
@Enselic
Copy link
Member

Enselic commented Nov 25, 2024

PR that adds test: #133473

@Enselic Enselic removed the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Nov 25, 2024
jhpratt added a commit to jhpratt/rust that referenced this issue Nov 26, 2024
tests: Add regression test for recursive enum with Cow and Clone

I could not find any existing test. `git grep "(Cow<'[^>]\+\["` gave no hits before this tests.

Closes rust-lang#100347
@bors bors closed this as completed in 52fcafe Nov 26, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Nov 26, 2024
Rollup merge of rust-lang#133473 - Enselic:cow, r=nnethercote

tests: Add regression test for recursive enum with Cow and Clone

I could not find any existing test. `git grep "(Cow<'[^>]\+\["` gave no hits before this tests.

Closes rust-lang#100347
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. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants