Skip to content

Commit d64b410

Browse files
committed
auto merge of #17464 : pcwalton/rust/inherent-methods-on-equal-footing, r=nikomatsakis
over inherent methods accessible via more autoderefs. This simplifies the trait matching algorithm. It breaks code like: impl Foo { fn foo(self) { // before this change, this will be called } } impl<'a,'b,'c> Trait for &'a &'b &'c Foo { fn foo(self) { // after this change, this will be called } } fn main() { let x = &(&(&Foo)); x.foo(); } To explicitly indicate that you wish to call the inherent method, perform explicit dereferences. For example: fn main() { let x = &(&(&Foo)); (***x).foo(); } Part of #17282. [breaking-change] r? @nikomatsakis
2 parents 5d653c1 + 21df9c8 commit d64b410

File tree

7 files changed

+50
-19
lines changed

7 files changed

+50
-19
lines changed

src/librustc/middle/typeck/check/method.rs

-11
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,7 @@ pub fn lookup<'a, 'tcx>(
158158

159159
debug!("searching inherent candidates");
160160
lcx.push_inherent_candidates(self_ty);
161-
let mme = lcx.search(self_ty);
162-
if mme.is_some() {
163-
return mme;
164-
}
165-
166161
debug!("searching extension candidates");
167-
lcx.reset_candidates();
168162
lcx.push_bound_candidates(self_ty, None);
169163
lcx.push_extension_candidates(expr.id);
170164
lcx.search(self_ty)
@@ -425,11 +419,6 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
425419
// ______________________________________________________________________
426420
// Candidate collection (see comment at start of file)
427421

428-
fn reset_candidates(&mut self) {
429-
self.inherent_candidates = Vec::new();
430-
self.extension_candidates = Vec::new();
431-
}
432-
433422
fn push_inherent_candidates(&mut self, self_ty: ty::t) {
434423
/*!
435424
* Collect all inherent candidates into

src/librustc/util/ppaux.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,8 @@ impl Repr for ty::Variance {
881881
// The first `.to_string()` returns a &'static str (it is not an implementation
882882
// of the ToString trait). Because of that, we need to call `.to_string()` again
883883
// if we want to have a `String`.
884-
self.to_string().to_string()
884+
let result: &'static str = (*self).to_string();
885+
result.to_string()
885886
}
886887
}
887888

src/libstd/io/mod.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -946,11 +946,14 @@ pub trait Reader {
946946
}
947947

948948
impl<'a> Reader for Box<Reader+'a> {
949-
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.read(buf) }
949+
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
950+
let reader: &mut Reader = &mut **self;
951+
reader.read(buf)
952+
}
950953
}
951954

952955
impl<'a> Reader for &'a mut Reader+'a {
953-
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { self.read(buf) }
956+
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> { (*self).read(buf) }
954957
}
955958

956959
/// Returns a slice of `v` between `start` and `end`.
@@ -1281,10 +1284,14 @@ pub trait Writer {
12811284

12821285
impl<'a> Writer for Box<Writer+'a> {
12831286
#[inline]
1284-
fn write(&mut self, buf: &[u8]) -> IoResult<()> { self.write(buf) }
1287+
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
1288+
(&mut **self).write(buf)
1289+
}
12851290

12861291
#[inline]
1287-
fn flush(&mut self) -> IoResult<()> { self.flush() }
1292+
fn flush(&mut self) -> IoResult<()> {
1293+
(&mut **self).flush()
1294+
}
12881295
}
12891296

12901297
impl<'a> Writer for &'a mut Writer+'a {

src/test/run-pass/class-cast-to-trait-cross-crate-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::to_string::ToString;
1515
use cci_class_cast::kitty::cat;
1616

1717
fn print_out(thing: Box<ToString>, expected: String) {
18-
let actual = thing.to_string();
18+
let actual = (*thing).to_string();
1919
println!("{}", actual);
2020
assert_eq!(actual.to_string(), expected);
2121
}

src/test/run-pass/class-separate-impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl fmt::Show for cat {
5858
}
5959

6060
fn print_out(thing: Box<ToString>, expected: String) {
61-
let actual = thing.to_string();
61+
let actual = (*thing).to_string();
6262
println!("{}", actual);
6363
assert_eq!(actual.to_string(), expected);
6464
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct Foo;
12+
13+
impl Foo {
14+
#[allow(dead_code)]
15+
fn foo(self) {
16+
fail!("wrong method!")
17+
}
18+
}
19+
20+
trait Trait {
21+
fn foo(self);
22+
}
23+
24+
impl<'a,'b,'c> Trait for &'a &'b &'c Foo {
25+
fn foo(self) {
26+
// ok
27+
}
28+
}
29+
30+
fn main() {
31+
let x = &(&(&Foo));
32+
x.foo();
33+
}
34+

src/test/run-pass/issue-3702.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn main() {
1515
}
1616

1717
fn to_string(t: Box<Text>) {
18-
println!("{}", t.to_string());
18+
println!("{}", (*t).to_string());
1919
}
2020

2121
}

0 commit comments

Comments
 (0)