diff options
| author | Jeffrey Seyfried <jeffrey.seyfried@gmail.com> | 2016-03-30 00:35:53 +0000 |
|---|---|---|
| committer | Jeffrey Seyfried <jeffrey.seyfried@gmail.com> | 2016-03-30 21:26:35 +0000 |
| commit | 48c20b0e73b083090c6dcf65ecd460eb073cc0b4 (patch) | |
| tree | 3fb6e691e3837ce8f3d3e0af58a9a677d4447459 | |
| parent | 38bef4365219dba7b82a3de19a2b84b1d322abf1 (diff) | |
| download | rust-48c20b0e73b083090c6dcf65ecd460eb073cc0b4.tar.gz rust-48c20b0e73b083090c6dcf65ecd460eb073cc0b4.zip | |
Improve tests
| -rw-r--r-- | src/test/compile-fail/struct-field-privacy.rs | 6 | ||||
| -rw-r--r-- | src/test/run-pass/autoderef-privacy.rs (renamed from src/test/compile-fail/autoderef-privacy.rs) | 39 |
2 files changed, 30 insertions, 15 deletions
diff --git a/src/test/compile-fail/struct-field-privacy.rs b/src/test/compile-fail/struct-field-privacy.rs index 1dd8ec0136e..f487ef62aa4 100644 --- a/src/test/compile-fail/struct-field-privacy.rs +++ b/src/test/compile-fail/struct-field-privacy.rs @@ -25,9 +25,10 @@ mod inner { pub a: isize, b: isize, } + pub struct Z(pub isize, isize); } -fn test(a: A, b: inner::A, c: inner::B, d: xc::A, e: xc::B) { +fn test(a: A, b: inner::A, c: inner::B, d: xc::A, e: xc::B, z: inner::Z) { a.a; b.a; //~ ERROR: field `a` of struct `inner::A` is private b.b; @@ -39,6 +40,9 @@ fn test(a: A, b: inner::A, c: inner::B, d: xc::A, e: xc::B) { e.a; e.b; //~ ERROR: field `b` of struct `xc::B` is private + + z.0; + z.1; //~ ERROR: field `1` of struct `inner::Z` is private } fn main() {} diff --git a/src/test/compile-fail/autoderef-privacy.rs b/src/test/run-pass/autoderef-privacy.rs index 359cee760db..e50f1bea0d3 100644 --- a/src/test/compile-fail/autoderef-privacy.rs +++ b/src/test/run-pass/autoderef-privacy.rs @@ -10,40 +10,51 @@ // Check we do not select a private method or field when computing autoderefs -#![feature(rustc_attrs)] #![allow(unused)] +#[derive(Default)] pub struct Bar2 { i: i32 } +#[derive(Default)] pub struct Baz2(i32); impl Bar2 { - fn f(&self) {} + fn f(&self) -> bool { true } } mod foo { - pub struct Bar { i: i32 } - pub struct Baz(i32); + #[derive(Default)] + pub struct Bar { i: ::Bar2 } + #[derive(Default)] + pub struct Baz(::Baz2); impl Bar { - fn f(&self) {} + fn f(&self) -> bool { false } } impl ::std::ops::Deref for Bar { type Target = ::Bar2; - fn deref(&self) -> &::Bar2 { unimplemented!() } + fn deref(&self) -> &::Bar2 { &self.i } } impl ::std::ops::Deref for Baz { type Target = ::Baz2; - fn deref(&self) -> &::Baz2 { unimplemented!() } + fn deref(&self) -> &::Baz2 { &self.0 } } -} -fn f(bar: foo::Bar, baz: foo::Baz) { - let _ = bar.i; - let _ = baz.0; - let _ = bar.f(); + pub fn f(bar: &Bar, baz: &Baz) { + // Since the private fields and methods are visible here, there should be no autoderefs. + let _: &::Bar2 = &bar.i; + let _: &::Baz2 = &baz.0; + assert!(!bar.f()); + } } -#[rustc_error] -fn main() {} //~ ERROR compilation successful +fn main() { + let bar = foo::Bar::default(); + let baz = foo::Baz::default(); + foo::f(&bar, &baz); + + let _: i32 = bar.i; + let _: i32 = baz.0; + assert!(bar.f()); +} |
