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

Weird Debug trait constraint when using associated type #32216

Closed
Twinside opened this issue Mar 12, 2016 · 2 comments
Closed

Weird Debug trait constraint when using associated type #32216

Twinside opened this issue Mar 12, 2016 · 2 comments

Comments

@Twinside
Copy link

Using the (reduced) code below, I don't understand why B needs to implement the Debug trait, as we are only interested in the Debug trait for the associated type Bar::Bartype, and Bar doesn't even appear in the output if we add the Debug constraint and derivation to it.

Rustc emmits the following error messages:

minmal.rs:42:14: 42:23 error: the trait `core::fmt::Debug` is not implemented for the type `B` [E0277]
minmal.rs:42 impl<B: Bar> Framework for Dummy<B> {
                          ^~~~~~~~~
minmal.rs:42:14: 42:23 help: run `rustc --explain E0277` to see a detailed explanation
minmal.rs:42:14: 42:23 note: `B` cannot be formatted using `:?`; if it is defined in your crate, add `#[derive(Debug)]` or manually implement it
minmal.rs:42:14: 42:23 note: required by `Framework`

And here is the listing:

use std::fmt::Debug;

pub trait Bar {
    /// Only real interesting point here, the associated type
    type Bartype : Debug;
    fn neu() -> Self::Bartype;
}

pub trait Framework {
    /// The important part here is the Debug constraint
    /// on the associated type
    type FrameType : Debug;
    fn do_stuff(&self);
}

/// uncomment next line to make it compile
// #[derive(Debug)]
struct ABar { dummy: u32 }
impl Bar for ABar {
    type Bartype = u32;
    fn neu() -> Self::Bartype { 0 as u32 }
}


struct Dummy<B : Bar> { dummy: B::Bartype }

impl<B:Bar> Dummy<B> {
    fn new() -> Dummy<B> {
        Dummy { dummy: B::neu() }
    }
}

/// If a real Debug constraint would by required, it would
/// be nice to get the error here.
#[derive(Debug)]
struct Foo<B : Bar> { a_field: B::Bartype }

impl<B:Bar> Foo<B> {
    fn new() -> Foo<B> {
        Foo { a_field: B::neu() }
    }
}

/// Why does B needs the Debug trait?
/// uncomment + Debug to make it compile
impl<B: Bar /* + Debug */> Framework for Dummy<B> {
    type FrameType = Foo<B>;

    fn do_stuff(&self) {
        print!("{:?}", Foo::<B>::new())
    }
}

fn main() {
    let dumm = Dummy::<ABar>::new();
    dumm.do_stuff();
    print!("\n{:?}\n", dumm.dummy)
}

I would expect that the B parameter of Dummy didn't require the instantiation of the Debug trait for it, as the associated type clearly has the Debug contraint on it.

Meta

rustc 1.7.0 (a5d1e7a59 2016-02-29)                    
binary: rustc                                         
commit-hash: a5d1e7a59a2a3413c377b003075349f854304b5e 
commit-date: 2016-02-29                               
host: x86_64-pc-windows-gnu                           
release: 1.7.0                                        
@Stebalien
Copy link
Contributor

See #26925

@Twinside
Copy link
Author

Ok, I'm closing the ticket as it is a duplicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants