Skip to content

where clause not honored in trait impl body #20414

Closed
@Twisol

Description

Even though I've stipulated in the where clause that Newtype<'a, T> must implement Foo for this impl to apply, I can't call answer on a Newtype<'a, T> within the impl body.

Workaround

Per @jroesch below, this can be worked around by using Foo::answer(&mut newtype) instead of newtype.answer().

Test case

rustc 0.13.0-nightly (7608dbad6 2014-12-31 10:06:21 -0800)
binary: rustc
commit-hash: 7608dbad651f02e837ed05eef3d74a6662a6e928
commit-date: 2014-12-31 10:06:21 -0800
host: x86_64-apple-darwin
release: 0.13.0-nightly
trait Foo {
  fn answer(&mut self) -> u8;
}

trait Bar<'a> {
  fn stuff(&'a mut self) -> u8;
}

struct Newtype<'a, T: 'a> {
  value: &'a mut T,
}


impl<'a, T> Bar<'a> for T
where Newtype<'a, T>: Foo {
  fn stuff(&'a mut self) -> u8 {
    let mut newtype: Newtype<'a, T> = Newtype{value: self};
    newtype.answer()
  }
}


fn main() {}
foo.rs:18:13: 18:21 error: type `Newtype<'a, T>` does not implement any method in scope named `answer`
foo.rs:18     newtype.answer()
                      ^~~~~~~~
error: aborting due to previous error

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-type-systemArea: Type systemE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions