Skip to content

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

Closed

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.

Activity

  1. pnkfelix commented on Jun 16, 2013

    @pnkfelix
    ContributorAuthor

    Part of #2794 as well.

  2. pnkfelix commented on Jun 25, 2013

    @pnkfelix
    ContributorAuthor

    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:

    #[allow(default_methods)]
    trait Speak { fn say(&self, s:&str); fn hi(&self) { hello(self); } }
    fn hello<S:Speak>(s:&S) { s.say("hello"); }

    Its the same as the original code from the description, with the impl and main removed; 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.

  3. msullivan commented on Jun 28, 2013

    @msullivan
    Contributor

    I have a fix for this that I will land soon. There will still be problems with self and supertraits, though.

  4. added a commit that references this issue on May 20, 2021
  5. added a commit that references this issue on Aug 21, 2026
  6. added a commit that references this issue on Oct 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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