Skip to content

default method of T that calls generic fn<S:T>(&S) on self does not work #7183

Closed
@pnkfelix

Description

@pnkfelix

If you try to implement a default method of trait T by calling out to a generic helper function with a type-parameter bounded by T, rustc claims that it cannot find an implementation of trait T for Self. (Of course the default method is itself within trait T, so any possible choice for Self should indeed implement T.)

Test case:

#[allow(default_methods)]
trait Speak {
    fn say(&self, s:&str);

    #[cfg(not(work_around_the_bug))]
    fn hi(&self) { hello(self); }

    // To see things "working", we can remove
    // the offending default method and
    // cut-and-paste its implementation
    // instead (see below).
    #[cfg(work_around_the_bug)] fn hi(&self);
}

fn hello<S:Speak>(s:&S) {
    s.say("hello");
}

impl Speak for int {
    fn say(&self, s:&str) { println(s); }

    #[cfg(work_around_the_bug)] fn hi(&self) { hello(self); }
}

fn main() {
    3.hi();
}

Transcript of rustc invocation:

% RUST_LOG=rustc=1,::rt::backtrace  x86_64-apple-darwin/stage2/bin/rustc ~/Dev/Rust/default-calls-generic.rs
/Users/pnkfelix/Dev/Rust/default-calls-generic.rs:6:19: 6:24 error: failed to find an implementation of trait Speak for Self
/Users/pnkfelix/Dev/Rust/default-calls-generic.rs:6     fn hi(&self) { hello(self); }
                                                                       ^~~~~
rust: task failed at 'explicit failure', /Users/pnkfelix/Dev/Mozilla/rust.git/src/libsyntax/diagnostic.rs:72
rust: task failed at 'explicit failure', /Users/pnkfelix/Dev/Mozilla/rust.git/src/librustc/rustc.rc:398
rust: domain main @0x7fd9bb015810 root task failed

Discovered while prototyping the visit.rs refactoring (#7081); probably blocks it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions