Skip to content

Wrong specialised implementation chosen on T or U where T: U #68159

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
CJKay opened this issue Jan 12, 2020 · 2 comments
Closed

Wrong specialised implementation chosen on T or U where T: U #68159

CJKay opened this issue Jan 12, 2020 · 2 comments
Labels
A-specialization Area: Trait impl specialization C-bug Category: This is a bug. F-specialization `#![feature(specialization)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@CJKay
Copy link

CJKay commented Jan 12, 2020

If a default function taking a generic type T is specialised by a concrete type U which fulfills the constraints on T, calls to the function will never choose the default implementation even if it is the only valid choice.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=a6b6b6f5a051db104630ec706708da08

#![feature(specialization)]

use core::{cmp::{Eq, PartialEq}, convert::From};

#[derive(Eq)]
pub struct Bit(bool);

impl From<bool> for Bit {
    fn from (from: bool) -> Bit {
        Bit(from)
    }
}

impl<T: Eq + From<bool>> PartialEq<T> for Bit {
    default fn eq(&self, other: &T) -> bool {
        other.eq(&self.0.into())
    }
}

impl PartialEq<Bit> for Bit {
    fn eq(&self, other: &Bit) -> bool {
        other.0.eq(&self.0)
    }
}

fn mismatched_types<T: Eq + From<bool>>(left: Bit, right: T) -> bool {
    left.eq(&right)
}
error[E0308]: mismatched types
  --> src/lib.rs:27:13
   |
26 | fn mismatched_types<T: Eq + From<bool>>(left: Bit, right: T) -> bool {
   |                     - this type parameter
27 |     left.eq(&right)
   |             ^^^^^^ expected struct `Bit`, found type parameter `T`
   |
   = note: expected reference `&Bit`
              found reference `&T`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

error: aborting due to previous error

The proper implementation is, however, correctly resolved using universal calling syntax:

fn mismatched_types<T: Eq + From<bool>>(left: Bit, right: T) -> bool {
    <Bit as PartialEq<T>>::eq(&left, &right) /* No error */
}

It also seems to resolve correctly when a second specialised implementation with another concrete type that also meets the constraints on the default implementation:

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=2687e226c51933ce99ed514c840bd317

@jonas-schievink jonas-schievink added A-specialization Area: Trait impl specialization C-bug Category: This is a bug. F-specialization `#![feature(specialization)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. requires-nightly This issue requires a nightly compiler in some way. labels Jan 12, 2020
@ExpHP
Copy link
Contributor

ExpHP commented Jan 13, 2020

Are we sure this is specialization? To me it kinda looks like #46697. (The compiler may be resolving .eq as belonging to PartialEq<Bit> because it appears in the trait bounds, indirectly through Eq)

@Dylan-DPC
Copy link
Member

This code doesn't emit the error any more.

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-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants