Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
pnkfelix opened this issue Jun 16, 2013 · 3 comments
Closed

Comments

@pnkfelix
Copy link
Member

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.

@pnkfelix
Copy link
Member Author

Part of #2794 as well.

@pnkfelix
Copy link
Member Author

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.

@msullivan
Copy link
Contributor

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

flip1995 pushed a commit to flip1995/rust that referenced this issue May 20, 2021
Handle write!(buf, "\n") case better

Make `write!(buf, "\n")` suggest `writeln!(buf)` by removing
the trailing comma from `writeln!(buf, )`.

changelog: [`write_with_newline`] suggestion on only "\n" improved
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants