diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:03 +0000 |
commit | 64d98f8ee037282c35007b64c2649055c56af1db (patch) | |
tree | 5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/privacy/restricted | |
parent | Adding debian version 1.67.1+dfsg1-1. (diff) | |
download | rustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/privacy/restricted')
-rw-r--r-- | tests/ui/privacy/restricted/auxiliary/pub_restricted.rs | 14 | ||||
-rw-r--r-- | tests/ui/privacy/restricted/lookup-ignores-private.rs | 34 | ||||
-rw-r--r-- | tests/ui/privacy/restricted/private-in-public.rs | 11 | ||||
-rw-r--r-- | tests/ui/privacy/restricted/private-in-public.stderr | 21 | ||||
-rw-r--r-- | tests/ui/privacy/restricted/relative-2018.rs | 13 | ||||
-rw-r--r-- | tests/ui/privacy/restricted/relative-2018.stderr | 17 | ||||
-rw-r--r-- | tests/ui/privacy/restricted/struct-literal-field.rs | 19 | ||||
-rw-r--r-- | tests/ui/privacy/restricted/struct-literal-field.stderr | 9 | ||||
-rw-r--r-- | tests/ui/privacy/restricted/test.rs | 52 | ||||
-rw-r--r-- | tests/ui/privacy/restricted/test.stderr | 112 |
10 files changed, 302 insertions, 0 deletions
diff --git a/tests/ui/privacy/restricted/auxiliary/pub_restricted.rs b/tests/ui/privacy/restricted/auxiliary/pub_restricted.rs new file mode 100644 index 000000000..a4013e6ac --- /dev/null +++ b/tests/ui/privacy/restricted/auxiliary/pub_restricted.rs @@ -0,0 +1,14 @@ +pub(crate) struct Crate; + +#[derive(Default)] +pub struct Universe { + pub x: i32, + pub(crate) y: i32, + pub(crate) z: i32, +} + +impl Universe { + pub fn f(&self) {} + pub(crate) fn g(&self) {} + pub(crate) fn h(&self) {} +} diff --git a/tests/ui/privacy/restricted/lookup-ignores-private.rs b/tests/ui/privacy/restricted/lookup-ignores-private.rs new file mode 100644 index 000000000..240ce1e2b --- /dev/null +++ b/tests/ui/privacy/restricted/lookup-ignores-private.rs @@ -0,0 +1,34 @@ +// build-pass (FIXME(62277): could be check-pass?) +#![allow(warnings)] + +mod foo { + pub use foo::bar::S; + mod bar { + #[derive(Default)] + pub struct S { + pub(in foo) x: i32, + } + impl S { + pub(in 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!() } + } + } +} + + +fn main() { + let s = foo::S::default(); + let _: bool = s.x; + let _: bool = s.f(); +} diff --git a/tests/ui/privacy/restricted/private-in-public.rs b/tests/ui/privacy/restricted/private-in-public.rs new file mode 100644 index 000000000..1e3dbdf73 --- /dev/null +++ b/tests/ui/privacy/restricted/private-in-public.rs @@ -0,0 +1,11 @@ +mod foo { + struct Priv; + mod bar { + use foo::Priv; + pub(super) fn f(_: Priv) {} + pub(crate) fn g(_: Priv) {} //~ ERROR E0446 + pub(crate) fn h(_: Priv) {} //~ ERROR E0446 + } +} + +fn main() { } diff --git a/tests/ui/privacy/restricted/private-in-public.stderr b/tests/ui/privacy/restricted/private-in-public.stderr new file mode 100644 index 000000000..65d996f0f --- /dev/null +++ b/tests/ui/privacy/restricted/private-in-public.stderr @@ -0,0 +1,21 @@ +error[E0446]: private type `Priv` in public interface + --> $DIR/private-in-public.rs:6:9 + | +LL | struct Priv; + | ----------- `Priv` declared as private +... +LL | pub(crate) fn g(_: Priv) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `Priv` in public interface + --> $DIR/private-in-public.rs:7:9 + | +LL | struct Priv; + | ----------- `Priv` declared as private +... +LL | pub(crate) fn h(_: Priv) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0446`. diff --git a/tests/ui/privacy/restricted/relative-2018.rs b/tests/ui/privacy/restricted/relative-2018.rs new file mode 100644 index 000000000..954169a9f --- /dev/null +++ b/tests/ui/privacy/restricted/relative-2018.rs @@ -0,0 +1,13 @@ +// edition:2018 + +mod m { + pub(in crate) struct S1; // OK + pub(in super) struct S2; // OK + pub(in self) struct S3; // OK + pub(in ::core) struct S4; + //~^ ERROR visibilities can only be restricted to ancestor modules + pub(in a::b) struct S5; + //~^ ERROR relative paths are not supported in visibilities in 2018 edition or later +} + +fn main() {} diff --git a/tests/ui/privacy/restricted/relative-2018.stderr b/tests/ui/privacy/restricted/relative-2018.stderr new file mode 100644 index 000000000..dec0d5157 --- /dev/null +++ b/tests/ui/privacy/restricted/relative-2018.stderr @@ -0,0 +1,17 @@ +error[E0742]: visibilities can only be restricted to ancestor modules + --> $DIR/relative-2018.rs:7:12 + | +LL | pub(in ::core) struct S4; + | ^^^^^^ + +error: relative paths are not supported in visibilities in 2018 edition or later + --> $DIR/relative-2018.rs:9:12 + | +LL | pub(in a::b) struct S5; + | ^--- + | | + | help: try: `crate::a::b` + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0742`. diff --git a/tests/ui/privacy/restricted/struct-literal-field.rs b/tests/ui/privacy/restricted/struct-literal-field.rs new file mode 100644 index 000000000..9c6104755 --- /dev/null +++ b/tests/ui/privacy/restricted/struct-literal-field.rs @@ -0,0 +1,19 @@ +#![allow(warnings)] + +mod foo { + pub mod bar { + pub struct S { + pub(in 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/tests/ui/privacy/restricted/struct-literal-field.stderr b/tests/ui/privacy/restricted/struct-literal-field.stderr new file mode 100644 index 000000000..eee964f02 --- /dev/null +++ b/tests/ui/privacy/restricted/struct-literal-field.stderr @@ -0,0 +1,9 @@ +error[E0451]: field `x` of struct `S` is private + --> $DIR/struct-literal-field.rs:18:9 + | +LL | S { x: 0 }; + | ^^^^ private field + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0451`. diff --git a/tests/ui/privacy/restricted/test.rs b/tests/ui/privacy/restricted/test.rs new file mode 100644 index 000000000..a8c269378 --- /dev/null +++ b/tests/ui/privacy/restricted/test.rs @@ -0,0 +1,52 @@ +// aux-build:pub_restricted.rs + +#![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 re-exported + 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 + let _ = u.z; //~ ERROR private + u.f(); + u.g(); //~ ERROR private + u.h(); //~ ERROR private +} + +mod pathological { + pub(in bad::path) mod m1 {} //~ ERROR failed to resolve: maybe a missing crate `bad`? + pub(in foo) mod m2 {} //~ ERROR visibilities can only be restricted to ancestor modules +} diff --git a/tests/ui/privacy/restricted/test.stderr b/tests/ui/privacy/restricted/test.stderr new file mode 100644 index 000000000..1acd221b4 --- /dev/null +++ b/tests/ui/privacy/restricted/test.stderr @@ -0,0 +1,112 @@ +error[E0433]: failed to resolve: maybe a missing crate `bad`? + --> $DIR/test.rs:50:12 + | +LL | pub(in bad::path) mod m1 {} + | ^^^ maybe a missing crate `bad`? + | + = help: consider adding `extern crate bad` to use the `bad` crate + +error[E0742]: visibilities can only be restricted to ancestor modules + --> $DIR/test.rs:51:12 + | +LL | pub(in foo) mod m2 {} + | ^^^ + +error[E0364]: `f` is private, and cannot be re-exported + --> $DIR/test.rs:21:24 + | +LL | pub(super) use foo::bar::f as g; + | ^^^^^^^^^^^^^^^^ + | +note: consider marking `f` as `pub` in the imported module + --> $DIR/test.rs:21:24 + | +LL | pub(super) use foo::bar::f as g; + | ^^^^^^^^^^^^^^^^ + +error[E0603]: struct `Crate` is private + --> $DIR/test.rs:38:25 + | +LL | use pub_restricted::Crate; + | ^^^^^ private struct + | +note: the struct `Crate` is defined here + --> $DIR/auxiliary/pub_restricted.rs:1:1 + | +LL | pub(crate) struct Crate; + | ^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0603]: function `f` is private + --> $DIR/test.rs:30:19 + | +LL | use foo::bar::f; + | ^ private function + | +note: the function `f` is defined here + --> $DIR/test.rs:8:9 + | +LL | pub(super) fn f() {} + | ^^^^^^^^^^^^^^^^^ + +error[E0616]: field `x` of struct `S` is private + --> $DIR/test.rs:31:18 + | +LL | S::default().x; + | ^ private field + +error[E0624]: associated function `f` is private + --> $DIR/test.rs:32:18 + | +LL | pub(super) fn f(&self) {} + | ---------------------- private associated function defined here +... +LL | S::default().f(); + | ^ private associated function + +error[E0624]: associated function `g` is private + --> $DIR/test.rs:33:8 + | +LL | pub(super) fn g() {} + | ----------------- private associated function defined here +... +LL | S::g(); + | ^ private associated function + +error[E0616]: field `y` of struct `Universe` is private + --> $DIR/test.rs:42:15 + | +LL | let _ = u.y; + | ^ private field + +error[E0616]: field `z` of struct `Universe` is private + --> $DIR/test.rs:43:15 + | +LL | let _ = u.z; + | ^ private field + +error[E0624]: associated function `g` is private + --> $DIR/test.rs:45:7 + | +LL | u.g(); + | ^ private associated function + | + ::: $DIR/auxiliary/pub_restricted.rs:12:5 + | +LL | pub(crate) fn g(&self) {} + | ---------------------- private associated function defined here + +error[E0624]: associated function `h` is private + --> $DIR/test.rs:46:7 + | +LL | u.h(); + | ^ private associated function + | + ::: $DIR/auxiliary/pub_restricted.rs:13:5 + | +LL | pub(crate) fn h(&self) {} + | ---------------------- private associated function defined here + +error: aborting due to 12 previous errors + +Some errors have detailed explanations: E0364, E0433, E0603, E0616, E0624, E0742. +For more information about an error, try `rustc --explain E0364`. |