Skip to content

Commit

Permalink
Make privacy checking on default methods for cross crate structs not …
Browse files Browse the repository at this point in the history
…fail. Closes rust-lang#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...
  • Loading branch information
msullivan committed Jul 3, 2013
1 parent 419a147 commit 7238d5a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ pub fn check_crate<'mm>(tcx: ty::ctxt,
method_id: def_id,
name: &ident) =
|span, method_id, name| {
// If the method is a default method, we need to use the def_id of
// the default implementation.
// Having to do this this is really unfortunate.
let method_id = match tcx.provided_method_sources.find(&method_id) {
None => method_id,
Some(source) => source.method_id
};

if method_id.crate == local_crate {
let is_private = method_is_private(span, method_id.node);
let container_id = local_method_container_id(span,
Expand Down
6 changes: 6 additions & 0 deletions src/test/auxiliary/trait_default_method_xc_aux.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[allow(default_methods)];

pub struct Something { x: int }

pub trait A {
fn f(&self) -> int;
fn g(&self) -> int { 10 }
Expand All @@ -11,6 +13,10 @@ impl A for int {
fn f(&self) -> int { 10 }
}

impl A for Something {
fn f(&self) -> int { 10 }
}

trait B<T> {
fn thing<U>(&self, x: T, y: U) -> (T, U) { (x, y) }
}
Expand Down
28 changes: 16 additions & 12 deletions src/test/run-pass/trait-default-method-xc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
#[allow(default_methods)];

extern mod aux(name = "trait_default_method_xc_aux");
use aux::{A, B, TestEquality};
use aux::{A, B, TestEquality, Something};


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

mod stuff {
pub struct thing { x: int }
}

pub struct thing { x: int }
impl A for thing {
impl A for stuff::thing {
fn f(&self) -> int { 10 }
}

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


impl TestEquality for thing {
fn test_eq(&self, rhs: &thing) -> bool {
impl TestEquality for stuff::thing {
fn test_eq(&self, rhs: &stuff::thing) -> bool {
//self.x.test_eq(&rhs.x)
eq(&self.x, &rhs.x)
}
Expand All @@ -41,15 +43,17 @@ fn main () {
// Some tests of random things
f(0);

let a = thing { x: 0 };
let b = thing { x: 1 };
let a = stuff::thing { x: 0 };
let b = stuff::thing { x: 1 };
let c = Something { x: 1 };

//assert_eq!(0i.g(), 10);
assert_eq!(0i.g(), 10);
assert_eq!(a.g(), 10);
assert_eq!(a.h(), 10);
assert_eq!(c.h(), 10);


//assert_eq!(0i.thing(3.14, 1), (3.14, 1));
0i.thing(3.14, 1);
assert_eq!(0i.thing(3.14, 1), (3.14, 1));

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


// Trying out a real one
//assert!(12.test_neq(&10));
//assert!(!10.test_neq(&10));
assert!(12.test_neq(&10));
assert!(!10.test_neq(&10));
assert!(a.test_neq(&b));
assert!(!a.test_neq(&a));

Expand Down

0 comments on commit 7238d5a

Please sign in to comment.