diff --git a/src/test/auxiliary/pub_restricted.rs b/src/test/auxiliary/pub_restricted.rs new file mode 100644 index 0000000000000..b1c88ce6ce55c --- /dev/null +++ b/src/test/auxiliary/pub_restricted.rs @@ -0,0 +1,23 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(pub_restricted)] + +pub(crate) struct Crate; +#[derive(Default)] +pub struct Universe { + pub x: i32, + pub(crate) y: i32 +} + +impl Universe { + pub fn f(&self) {} + pub(crate) fn g(&self) {} +} diff --git a/src/test/compile-fail/privacy/restricted/feature-gate.rs b/src/test/compile-fail/privacy/restricted/feature-gate.rs new file mode 100644 index 0000000000000..53ae439867f98 --- /dev/null +++ b/src/test/compile-fail/privacy/restricted/feature-gate.rs @@ -0,0 +1,25 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +pub(crate) //~ ERROR experimental +mod foo {} + +pub(self) //~ ERROR experimental +mod bar {} + +struct S { + pub(self) x: i32, //~ ERROR experimental +} +impl S { + pub(self) fn f() {} //~ ERROR experimental +} +extern { + pub(self) fn f(); //~ ERROR experimental +} diff --git a/src/test/compile-fail/privacy/restricted/lookup-ignores-private.rs b/src/test/compile-fail/privacy/restricted/lookup-ignores-private.rs new file mode 100644 index 0000000000000..4e2a69cb79e19 --- /dev/null +++ b/src/test/compile-fail/privacy/restricted/lookup-ignores-private.rs @@ -0,0 +1,44 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(rustc_attrs, pub_restricted)] +#![allow(warnings)] + +mod foo { + pub use foo::bar::S; + mod bar { + #[derive(Default)] + pub struct S { + pub(foo) x: i32, + } + impl S { + pub(foo) fn f(&self) -> i32 { 0 } + } + + pub struct S2 { + pub(crate) x: bool, + } + impl S2 { + pub(crate) fn f(&self) -> bool { false } + } + + impl ::std::ops::Deref for S { + type Target = S2; + fn deref(&self) -> &S2 { unimplemented!() } + } + } +} + +#[rustc_error] +fn main() { //~ ERROR compilation successful + let s = foo::S::default(); + let _: bool = s.x; + let _: bool = s.f(); +} diff --git a/src/test/compile-fail/privacy/restricted/private-in-public.rs b/src/test/compile-fail/privacy/restricted/private-in-public.rs new file mode 100644 index 0000000000000..f8e7e6283a0af --- /dev/null +++ b/src/test/compile-fail/privacy/restricted/private-in-public.rs @@ -0,0 +1,20 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(pub_restricted)] + +mod foo { + struct Priv; + mod bar { + use foo::Priv; + pub(super) fn f(_: Priv) {} + pub(crate) fn f(_: Priv) {} //~ ERROR private + } +} diff --git a/src/test/compile-fail/privacy/restricted/struct-literal-field.rs b/src/test/compile-fail/privacy/restricted/struct-literal-field.rs new file mode 100644 index 0000000000000..e254e005656c1 --- /dev/null +++ b/src/test/compile-fail/privacy/restricted/struct-literal-field.rs @@ -0,0 +1,31 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(pub_restricted)] +#![deny(private_in_public)] +#![allow(warnings)] + +mod foo { + pub mod bar { + pub struct S { + pub(foo) x: i32, + } + } + + fn f() { + use foo::bar::S; + S { x: 0 }; // ok + } +} + +fn main() { + use foo::bar::S; + S { x: 0 }; //~ ERROR private +} diff --git a/src/test/compile-fail/privacy/restricted/test.rs b/src/test/compile-fail/privacy/restricted/test.rs new file mode 100644 index 0000000000000..3e1bb7666229c --- /dev/null +++ b/src/test/compile-fail/privacy/restricted/test.rs @@ -0,0 +1,62 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// aux-build:pub_restricted.rs + +#![feature(pub_restricted)] +#![deny(private_in_public)] +#![allow(warnings)] +extern crate pub_restricted; + +mod foo { + pub mod bar { + pub(super) fn f() {} + #[derive(Default)] + pub struct S { + pub(super) x: i32, + } + impl S { + pub(super) fn f(&self) {} + pub(super) fn g() {} + } + } + fn f() { + use foo::bar::S; + pub(self) use foo::bar::f; // ok + pub(super) use foo::bar::f as g; //~ ERROR cannot be reexported + S::default().x; // ok + S::default().f(); // ok + S::g(); // ok + } +} + +fn f() { + use foo::bar::S; + use foo::bar::f; //~ ERROR private + S::default().x; //~ ERROR private + S::default().f(); //~ ERROR private + S::g(); //~ ERROR private +} + +fn main() { + use pub_restricted::Universe; + use pub_restricted::Crate; //~ ERROR private + + let u = Universe::default(); + let _ = u.x; + let _ = u.y; //~ ERROR private + u.f(); + u.g(); //~ ERROR private +} + +mod pathological { + pub(bad::path) mod m1 {} //~ ERROR failed to resolve module path + pub(foo) mod m2 {} //~ ERROR visibilities can only be restricted to ancestor modules +}