You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Type-directed method resolution does not work through pointers #4270
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.:
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.
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);
^~~~~~~~~~~~~~~
Activity
nikomatsakis commentedon Mar 24, 2013
The problem here is that, currently, method resolution does not attempt to do full trait resolution. That is,
write_be_u32is defined on the WriterUtil trait which has an impl for allWwhereW:Writer. So method resolution unifiesWwithNotAWriterbut leaves the actual resolution of whetherNotAWriterimplementsWriterfor a later step. This is sometimes good (because all types may not yet be known) but sometimes bad.nikomatsakis commentedon Mar 24, 2013
Not critical for 0.6; removing milestone
nikomatsakis commentedon Mar 24, 2013
cc #5527
catamorphism commentedon May 23, 2013
Reproduced with 0252c30 . Nominating for milestone 5, production-ready.
emberian commentedon Jul 21, 2013
Reproduces with
I think this is #5898. @nikomatsakis?
graydon commentedon Aug 1, 2013
accepted for well-defined milestone
pcwalton commentedon Jun 26, 2014
I don't believe this is backwards incompatible. Nominating.
pnkfelix commentedon Jun 26, 2014
Not a 1.0 issue; now P-high. Possibly linked / solvable via trait reform (#5527)
steveklabnik commentedon Feb 11, 2015
Importing
old_io::Writerrather thanWriterUtil, since that's where this method lives today, we get:Seems good!
Adds query param for version no (rust-lang#4270)
Merge pull request rust-lang#4270 from rust-lang/rustup-2025-04-12
Merge rust-lang#4270