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

Cow<'a, T> isn't specializable while FooCow<'a, T> is #106710

Open
ryoqun opened this issue Jan 11, 2023 · 1 comment
Open

Cow<'a, T> isn't specializable while FooCow<'a, T> is #106710

ryoqun opened this issue Jan 11, 2023 · 1 comment
Labels
A-specialization Area: Trait impl specialization C-bug Category: This is a bug. F-specialization `#![feature(specialization)]` requires-incomplete-features This issue requires the use of incomplete features.

Comments

@ryoqun
Copy link
Contributor

ryoqun commented Jan 11, 2023

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=60bb0c925354009304afd4b84aa2e8e6

I tried this code:

#![feature(specialization)]

use std::borrow::Cow;
use std::clone::Clone;
use std::ops::Deref;
use std::fmt::Debug;

trait Example {
    fn example(); 
}

struct UserDefinedCow<'a, T> {
    aa: &'a T,
}

impl<T: Sized> Example for T {
    default fn example() {
        println!("default impl: {}", std::any::type_name::<Self>());
    }
}

trait JJ {
}

impl<'a, T: Example /*+ Clone */ +'a> Example for Cow<'a, T> {
    fn example() {
        println!("specialized impl (Cow): {}", std::any::type_name::<Self>());
    }
}

impl<'a, T: Example + /*JJ +*/ 'a> Example for UserDefinedCow<'a, T> {
    fn example() {
        println!("specialized impol (UserDefinedCow): {}", std::any::type_name::<Self>());
    }
}
/*
impl<T: Example> Example for Box<T> {
    fn example() {
        println!("Example from Box<T> impl: {}", std::any::type_name::<Self>());
    }
}

impl<'a, T: Example + 'a> Example for Vec<&'a T> {
    fn example() {
        println!("Example from Vec<T> impl: {}", std::any::type_name::<Self>());
    }
}
*/

fn main() {
    usize::example();
    //<Vec<&u8>>::example();
    //<Box<&u8>>::example();
    <Cow<'_, &u8>>::example();
    <UserDefinedCow<'_, &u8>>::example();
}

I expected to see this happen:

no compilation error and the output should be like this:

default impl: usize
specialized impl (Cow): alloc::borrow::Cow<&u8>
specialized impol (UserDefinedCow): playground::UserDefinedCow<&u8>

Instead, this happened:

error[[E0119]](https://doc.rust-lang.org/nightly/error-index.html#E0119): conflicting implementations of trait `Example` for type `Cow<'_, _>`
  --> src/main.rs:25:1
   |
16 | impl<T: Sized> Example for T {
   | ---------------------------- first implementation here
...
25 | impl<'a, T: Example /*+ Clone*/ +'a> Example for Cow<'a, T> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Cow<'_, _>`

Oddly enough, I found that you can specialize Cow if it's further bound with + Clone. (try to comment in), after fiddling with the code a bit.

Also, another specialization for identical type shape is allowed if it's user defined. so, i guess Cow is specially treated internally?

Meta

Nightly channel

Build using the Nightly version: 1.68.0-nightly

(2023-01-10 0442fbabe24ec43636a8)

originally found and reduced to a minimal test case above: solana-labs/solana#29596

@ryoqun ryoqun added the C-bug Category: This is a bug. label Jan 11, 2023
@ryoqun ryoqun changed the title Cow<'a, T> isn't specializable while FooCow<'a, T> Cow<'a, T> isn't specializable while FooCow<'a, T> is Jan 11, 2023
@ryoqun
Copy link
Contributor Author

ryoqun commented Jan 26, 2023

@joshtriplett 👋

@workingjubilee workingjubilee added A-specialization Area: Trait impl specialization F-specialization `#![feature(specialization)]` requires-incomplete-features This issue requires the use of incomplete features. C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Jun 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-specialization Area: Trait impl specialization C-bug Category: This is a bug. F-specialization `#![feature(specialization)]` requires-incomplete-features This issue requires the use of incomplete features.
Projects
None yet
Development

No branches or pull requests

2 participants