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

Inherited trait bounds are not completely checked #43777

Closed
behnam opened this issue Aug 9, 2017 · 3 comments
Closed

Inherited trait bounds are not completely checked #43777

behnam opened this issue Aug 9, 2017 · 3 comments
Labels
A-typesystem Area: The type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@behnam
Copy link
Contributor

behnam commented Aug 9, 2017

Original discussion: https://users.rust-lang.org/t/unexpected-behaviors-of-trait-bounds/12286/10

Looks like it's related to this old issue: #29859

Basically, having trait Complete require trait Partial, and trait Partial require trait PartialEq (or any other trait), a type (struct TypeB) with impl Complete doesn't need to satisfy impl PartialEq, which only looks like a bug.

Interestingly, whenever <TypeB as Partial> is seen, the check is performed and appropriate error is thrown.

Here's a repro:

pub trait Partial: PartialEq {
    fn foo() -> Option<bool>;
}

pub trait Complete: Partial {
    fn foo() -> bool;
}

impl<T> Partial for T
where
    T: Complete,
{
    fn foo() -> Option<bool> {
        Some(<Self as Complete>::foo())
    }
}


// ----

#[derive(PartialEq)]
pub struct TypeA {}

impl Partial for TypeA {
    fn foo() -> Option<bool> {
        None
    }
}


// ----

// BUG: No compile warning about `PartialEq` not being implemented
// #[derive(PartialEq)]
pub struct TypeB {}

impl Complete for TypeB {
    fn foo() -> bool {
        true
    }
}


// ----

pub fn main() {
    println!("{:?}", TypeA::foo());

    // This works, but shouldn't!
    println!("{:?}", <TypeB as Complete>::foo());

    // This would trigger the issue, though.
    //println!("{:?}", <TypeB as Partial>::foo());
    /* Result in:
    error[E0277]: the trait bound `TypeB: std::cmp::PartialEq` is not satisfied
      --> src/bin/cyclic_traits.rs:48:22
       |
    48 |     println!("{:?}", <TypeB as Partial>::foo());
       |                      ^^^^^^^^^^^^^^^^^^^^^^^ can't compare `TypeB` with `TypeB`
       |
       = help: the trait `std::cmp::PartialEq` is not implemented for `TypeB`
       = note: required by `Partial::foo`
    */
}
@dtolnay
Copy link
Member

dtolnay commented Aug 9, 2017

Here is a minimized repro in ICE form:

trait Base {
    fn base() {}
}

trait Partial: Base {}
trait Complete: Partial {}

impl<T: Complete> Partial for T {}

struct TypeB;
impl Complete for TypeB {}

fn main() {
    ice::<TypeB>();
}

fn ice<P: Partial>() {
    P::base();
}
error: internal compiler error: /checkout/src/librustc/traits/trans/mod.rs:75: Encountered error `Unimplemented` selecting `Binder(<TypeB as Base>)` during trans

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports

note: rustc 1.21.0-nightly (cbbe17aa7 2017-08-07) running on x86_64-unknown-linux-gnu

bors bot added a commit to open-i18n/rust-unic that referenced this issue Aug 10, 2017
96: [char_property] Manually enforce CompleteCharProperty inherited traits r=behnam

See this bug report for why we need to do this at the moment: rust-lang/rust#43777
@scalexm
Copy link
Member

scalexm commented Aug 10, 2017

Seems like @arielb1 also opened his own issue (#43784) for this. Anyway, this is fixed (well should be) by #43786.

@Mark-Simulacrum Mark-Simulacrum added A-typesystem Area: The type system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 10, 2017
@nikomatsakis
Copy link
Contributor

I believe this is a dup of #43784, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-typesystem Area: The type system C-bug Category: This is a bug. 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

5 participants