Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
Dretch opened this issue Dec 23, 2012 · 9 comments
Closed

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

Dretch opened this issue Dec 23, 2012 · 9 comments
Labels
A-traits Area: Trait system P-medium Medium priority

Comments

@Dretch
Copy link
Contributor

Dretch commented Dec 23, 2012

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);
@ghost ghost assigned catamorphism Feb 20, 2013
@nikomatsakis
Copy link
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.

@nikomatsakis
Copy link
Contributor

Not critical for 0.6; removing milestone

@nikomatsakis
Copy link
Contributor

cc #5527

@catamorphism
Copy link
Contributor

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

@emberian
Copy link
Member

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?

@graydon
Copy link
Contributor

graydon commented Aug 1, 2013

accepted for well-defined milestone

@pcwalton
Copy link
Contributor

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

@pnkfelix
Copy link
Member

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

@pnkfelix pnkfelix removed this from the 1.0 milestone Jun 26, 2014
@steveklabnik
Copy link
Member

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!

calebcartwright pushed a commit to calebcartwright/rust that referenced this issue Oct 20, 2021
* Adds query param for version no

This adds support for using a query parameter for selecting the version no

* Adds error handling to configuration request

Catch request exception in case fetching the configuration from the url fails, this can happen either if non existent version number is passed in or because of server issues.

* Makes version selection better

Covers a few common cases in which the version number can be specified.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system P-medium Medium priority
Projects
None yet
Development

No branches or pull requests

8 participants