Rust: 继承的特征范围未完全检查

创建于 2017-08-10  ·  3评论  ·  资料来源: rust-lang/rust

原始讨论: https :

似乎与这个旧问题有关: https :

基本上,拥有trait Complete需要trait Partial ,而trait Partial需要trait PartialEq (或任何其他特征),一种类型( struct TypeB )和impl Complete不需要满足impl PartialEq ,这看起来只像个错误。

有趣的是,每当看到<TypeB as Partial> ,就会执行检查并抛出适当的错误。

这是一个复制品:

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`
    */
}
A-typesystem C-bug T-compiler

最有用的评论

这是ICE形式的最小化复制:

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

所有3条评论

这是ICE形式的最小化复制:

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

似乎@ arielb1

我相信这是#43784的重复项,即将结束。

此页面是否有帮助?
0 / 5 - 0 等级