Rust: 상속 된 νŠΉμ„± 경계가 μ™„μ „νžˆ ν™•μΈλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

에 λ§Œλ“  2017λ…„ 08μ›” 10일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: rust-lang/rust

μ›λž˜ ν† λ‘  : https://users.rust-lang.org/t/unexpected-behaviors-of-trait-bounds/12286/10

이 였래된 λ¬Έμ œμ™€ κ΄€λ ¨λœ 것 κ°™μŠ΅λ‹ˆλ‹€ : https://github.com/rust-lang/rust/issues/29859

기본적 κ°–λŠ” 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)λ₯Ό μ—° 것 κ°™μŠ΅λ‹ˆλ‹€. μ–΄μ¨Œλ“  이것은 # 43786에 μ˜ν•΄ μˆ˜μ •λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” 이것이 # 43784의 볡제라고 λ―ΏμŠ΅λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰