From 7238d5a141745b24d2b78c1bd212974c4335f5ce Mon Sep 17 00:00:00 2001 From: Michael Sullivan Date: Tue, 2 Jul 2013 14:39:25 -0700 Subject: [PATCH] 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... --- src/librustc/middle/privacy.rs | 8 ++++++ .../auxiliary/trait_default_method_xc_aux.rs | 6 ++++ src/test/run-pass/trait-default-method-xc.rs | 28 +++++++++++-------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/librustc/middle/privacy.rs b/src/librustc/middle/privacy.rs index 5e544dc06e3..64ed3b0211d 100644 --- a/src/librustc/middle/privacy.rs +++ b/src/librustc/middle/privacy.rs @@ -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, diff --git a/src/test/auxiliary/trait_default_method_xc_aux.rs b/src/test/auxiliary/trait_default_method_xc_aux.rs index 5ee243179df..7ae648f113a 100644 --- a/src/test/auxiliary/trait_default_method_xc_aux.rs +++ b/src/test/auxiliary/trait_default_method_xc_aux.rs @@ -1,5 +1,7 @@ #[allow(default_methods)]; +pub struct Something { x: int } + pub trait A { fn f(&self) -> int; fn g(&self) -> int { 10 } @@ -11,6 +13,10 @@ impl A for int { fn f(&self) -> int { 10 } } +impl A for Something { + fn f(&self) -> int { 10 } +} + trait B { fn thing(&self, x: T, y: U) -> (T, U) { (x, y) } } diff --git a/src/test/run-pass/trait-default-method-xc.rs b/src/test/run-pass/trait-default-method-xc.rs index f6c119c4fae..4eac1a1e730 100644 --- a/src/test/run-pass/trait-default-method-xc.rs +++ b/src/test/run-pass/trait-default-method-xc.rs @@ -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(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 } } @@ -29,8 +31,8 @@ fn neq(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) } @@ -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)); @@ -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));