Closed
Description
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
Labels
No labels
Activity
pnkfelix commentedon Jun 16, 2013
Part of #2794 as well.
pnkfelix commentedon Jun 25, 2013
Note that this simpler test case should also work (when compiled via
rustc --lib), but does not, and may help one avoid false strategies for resolving this:Its the same as the original code from the description, with the
implandmainremoved; it yields the same error, but since there are no impls, it won't run any of the loop over all the impls in the "lookup_vtable sty not ty_param" case.checkpoint using aatch's hack around issue rust-lang#7183.
msullivan commentedon Jun 28, 2013
I have a fix for this that I will land soon. There will still be problems with self and supertraits, though.
Auto merge of rust-lang#7183 - th1000s:write_nl_hint, r=flip1995
Merge rust-lang#7183
Merge pull request rust-lang#7183 from ytmimi/issue_7045