Skip to content

Commit 7238d5a

Browse files
committed
Make privacy checking on default methods for cross crate structs not fail. Closes #7481.
It is unclear to me that the way method call privacy checking is done makes any sense, though. It is only performed if the type is a struct...
1 parent 419a147 commit 7238d5a

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

src/librustc/middle/privacy.rs

+8
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ pub fn check_crate<'mm>(tcx: ty::ctxt,
245245
method_id: def_id,
246246
name: &ident) =
247247
|span, method_id, name| {
248+
// If the method is a default method, we need to use the def_id of
249+
// the default implementation.
250+
// Having to do this this is really unfortunate.
251+
let method_id = match tcx.provided_method_sources.find(&method_id) {
252+
None => method_id,
253+
Some(source) => source.method_id
254+
};
255+
248256
if method_id.crate == local_crate {
249257
let is_private = method_is_private(span, method_id.node);
250258
let container_id = local_method_container_id(span,

src/test/auxiliary/trait_default_method_xc_aux.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[allow(default_methods)];
22

3+
pub struct Something { x: int }
4+
35
pub trait A {
46
fn f(&self) -> int;
57
fn g(&self) -> int { 10 }
@@ -11,6 +13,10 @@ impl A for int {
1113
fn f(&self) -> int { 10 }
1214
}
1315

16+
impl A for Something {
17+
fn f(&self) -> int { 10 }
18+
}
19+
1420
trait B<T> {
1521
fn thing<U>(&self, x: T, y: U) -> (T, U) { (x, y) }
1622
}

src/test/run-pass/trait-default-method-xc.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
#[allow(default_methods)];
55

66
extern mod aux(name = "trait_default_method_xc_aux");
7-
use aux::{A, B, TestEquality};
7+
use aux::{A, B, TestEquality, Something};
88

99

1010
fn f<T: aux::A>(i: T) {
1111
assert_eq!(i.g(), 10);
1212
}
1313

14+
mod stuff {
15+
pub struct thing { x: int }
16+
}
1417

15-
pub struct thing { x: int }
16-
impl A for thing {
18+
impl A for stuff::thing {
1719
fn f(&self) -> int { 10 }
1820
}
1921

@@ -29,8 +31,8 @@ fn neq<T: TestEquality>(lhs: &T, rhs: &T) -> bool {
2931
}
3032

3133

32-
impl TestEquality for thing {
33-
fn test_eq(&self, rhs: &thing) -> bool {
34+
impl TestEquality for stuff::thing {
35+
fn test_eq(&self, rhs: &stuff::thing) -> bool {
3436
//self.x.test_eq(&rhs.x)
3537
eq(&self.x, &rhs.x)
3638
}
@@ -41,15 +43,17 @@ fn main () {
4143
// Some tests of random things
4244
f(0);
4345

44-
let a = thing { x: 0 };
45-
let b = thing { x: 1 };
46+
let a = stuff::thing { x: 0 };
47+
let b = stuff::thing { x: 1 };
48+
let c = Something { x: 1 };
4649

47-
//assert_eq!(0i.g(), 10);
50+
assert_eq!(0i.g(), 10);
4851
assert_eq!(a.g(), 10);
4952
assert_eq!(a.h(), 10);
53+
assert_eq!(c.h(), 10);
5054

51-
52-
//assert_eq!(0i.thing(3.14, 1), (3.14, 1));
55+
0i.thing(3.14, 1);
56+
assert_eq!(0i.thing(3.14, 1), (3.14, 1));
5357

5458
assert_eq!(g(0i, 3.14, 1), (3.14, 1));
5559
assert_eq!(g(false, 3.14, 1), (3.14, 1));
@@ -59,8 +63,8 @@ fn main () {
5963

6064

6165
// Trying out a real one
62-
//assert!(12.test_neq(&10));
63-
//assert!(!10.test_neq(&10));
66+
assert!(12.test_neq(&10));
67+
assert!(!10.test_neq(&10));
6468
assert!(a.test_neq(&b));
6569
assert!(!a.test_neq(&a));
6670

0 commit comments

Comments
 (0)