Skip to content

Generic impls of traits don't autoderef #10456

Closed

Description

@alexcrichton
pub struct Foo;

pub trait Bar {
    fn bar(&self);
}

pub trait Baz {}

impl<T: Baz> Bar for T {
    fn bar(&self) {}
}

impl Baz for Foo {}

pub fn foo(t: ~Foo) {
    t.bar() // ~Foo doesn't implement Baz
    (*t).bar() // ok b/c Foo implements Baz
}

fn main() {}

From a code-writer point of view, it'd be nice for t to auto-deref to (*t).bar() to appease the compiler, but I can see that the compiler may not know a whole lot about this.

If we don't get auto-deref here, perhaps a better error message may work. It's a little confusing to see that ~Foo doesn't implement Baz when Foo clearly implements Baz

Activity

  1. emberian commented on Apr 7, 2014

    @emberian
    Contributor

    cc @eddyb

    this would be pretty convenient.

  2. eddyb commented on Apr 7, 2014

    @eddyb
    Contributor

    @cmr we have to see how @nikomatsakis intends to improve method lookup.

    Though I have to point out that the X doesn't implement Y error comes from vtable checking which comes after type-checking finishes, and I'm pretty sure it has access to concrete types (without type inference variables at least. region inference isn't resolved until after regionck).

    That said, if the type information is available at the time of the method lookup, the vtable error can be prevented by searching for another suitable candidate (but this would also complicate simpler cases).

  3. nikomatsakis commented on Apr 9, 2014

    @nikomatsakis
    Contributor

    This actually has nothing to do with auto-deref, but it is addressed by the method lookup algorithm that I'm working on. That is ALMOST ready for publication, just working on the last bits.

  4. japaric commented on Nov 7, 2014

    @japaric
    Contributor

    @alexcrichton updated example is working with a recent version:

    rustc 0.13.0-dev (97a57ec90 2014-11-07 11:31:25 +0000)

    I think Box<T> now derefs to &T although Box<T> doesn't implement Deref<T> (#18624)

  5. alexcrichton commented on Nov 7, 2014

    @alexcrichton
    MemberAuthor

    Looks to be so, thanks @japaric! Flagging as needstest.

  6. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Nov 7, 2014
  7. added 3 commits that reference this issue on Dec 12, 2014
    20cbbff
    c59b9b7
  8. added a commit that references this issue on Mar 24, 2023
  9. added a commit that references this issue on Aug 21, 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

    A-trait-systemArea: Trait 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