Skip to content

Type-directed method resolution does not work through pointers #4270

Closed

Description

@Dretch

I think that the following code should work, but it does not compile:

use io::WriterUtil;

struct NotAWriter {x: int}

pub impl NotAWriter {
    fn write_be_u32(y: u32) {}
}

fn main() {
    let x = &(NotAWriter {x: 1});
    x.write_be_u32(0);
}

The error from rustc is:

test-method-scope.rs:15:4: 15:22 error: failed to find an implementation of trait @core::io::Writer for <V2>
test-method-scope.rs:15     x.write_be_u32(0);
                            ^~~~~~~~~~~~~~~~~~

I don't think there should be an error here. NotAWriter is not a Writer, it just shares a method name with WriterUtil (which is defined for all Writers).

If x is not a pointer then rustc works fine, i.e.:

let x = NotAWriter {x: 1};
x.write_be_u32(0);

Activity

  1. nikomatsakis commented on Mar 24, 2013

    @nikomatsakis
    Contributor

    The problem here is that, currently, method resolution does not attempt to do full trait resolution. That is, write_be_u32 is defined on the WriterUtil trait which has an impl for all W where W:Writer. So method resolution unifies W with NotAWriter but leaves the actual resolution of whether NotAWriter implements Writer for a later step. This is sometimes good (because all types may not yet be known) but sometimes bad.

  2. nikomatsakis commented on Mar 24, 2013

    @nikomatsakis
    Contributor

    Not critical for 0.6; removing milestone

  3. nikomatsakis commented on Mar 24, 2013

    @nikomatsakis
    Contributor
  4. catamorphism commented on May 23, 2013

    @catamorphism
    Contributor

    Reproduced with 0252c30 . Nominating for milestone 5, production-ready.

  5. emberian commented on Jul 21, 2013

    @emberian
    Contributor

    Reproduces with

    use std::io::WriterUtil;
    
    struct NotAWriter {x: int}
    
    impl NotAWriter {
        fn write_be_u32(y: u32) {}
    }
    
    fn main() {
        let x = &(NotAWriter {x: 1});
        x.write_be_u32(0);
    }

    I think this is #5898. @nikomatsakis?

  6. graydon commented on Aug 1, 2013

    @graydon
    Contributor

    accepted for well-defined milestone

  7. pcwalton commented on Jun 26, 2014

    @pcwalton
    Contributor

    I don't believe this is backwards incompatible. Nominating.

  8. pnkfelix commented on Jun 26, 2014

    @pnkfelix
    Contributor

    Not a 1.0 issue; now P-high. Possibly linked / solvable via trait reform (#5527)

  9. removed this from the 1.0 milestone on Jun 26, 2014
  10. steveklabnik commented on Feb 11, 2015

    @steveklabnik
    Contributor

    Importing old_io::Writer rather than WriterUtil, since that's where this method lives today, we get:

    hello.rs:11:15: 11:30 error: type `&NotAWriter` does not implement any method in scope named `write_be_u32`
    hello.rs:11             x.write_be_u32(0);
                              ^~~~~~~~~~~~~~~
    
    

    Seems good!

  11. added a commit that references this issue on Apr 14, 2025
  12. 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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions