diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
commit | 218caa410aa38c29984be31a5229b9fa717560ee (patch) | |
tree | c54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/privacy | |
parent | Releasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/privacy')
172 files changed, 8216 insertions, 0 deletions
diff --git a/tests/ui/privacy/associated-item-privacy-inherent.rs b/tests/ui/privacy/associated-item-privacy-inherent.rs new file mode 100644 index 000000000..7b7c734a9 --- /dev/null +++ b/tests/ui/privacy/associated-item-privacy-inherent.rs @@ -0,0 +1,112 @@ +#![feature(decl_macro, associated_type_defaults)] +#![allow(unused, private_in_public)] + +mod priv_nominal { + pub struct Pub; + impl Pub { + fn method(&self) {} + const CONST: u8 = 0; + // type AssocTy = u8; + } + + pub macro mac() { + let value = Pub::method; + //~^ ERROR type `for<'a> fn(&'a priv_nominal::Pub) {priv_nominal::Pub::method}` is private + value; + //~^ ERROR type `for<'a> fn(&'a priv_nominal::Pub) {priv_nominal::Pub::method}` is private + Pub.method(); + //~^ ERROR type `for<'a> fn(&'a priv_nominal::Pub) {priv_nominal::Pub::method}` is private + Pub::CONST; + //~^ ERROR associated constant `CONST` is private + // let _: Pub::AssocTy; + // pub type InSignatureTy = Pub::AssocTy; + } +} +fn priv_nominal() { + priv_nominal::mac!(); +} + +mod priv_signature { + struct Priv; + pub struct Pub; + impl Pub { + pub fn method(&self, arg: Priv) {} + } + + pub macro mac() { + let value = Pub::method; + //~^ ERROR type `priv_signature::Priv` is private + value; + //~^ ERROR type `priv_signature::Priv` is private + Pub.method(loop {}); + //~^ ERROR type `priv_signature::Priv` is private + } +} +fn priv_signature() { + priv_signature::mac!(); +} + +mod priv_substs { + struct Priv; + pub struct Pub; + impl Pub { + pub fn method<T>(&self) {} + } + + pub macro mac() { + let value = Pub::method::<Priv>; + //~^ ERROR type `priv_substs::Priv` is private + value; + //~^ ERROR type `priv_substs::Priv` is private + Pub.method::<Priv>(); + //~^ ERROR type `priv_substs::Priv` is private + } +} +fn priv_substs() { + priv_substs::mac!(); +} + +mod priv_parent_substs { + struct Priv; + pub struct Pub<T = Priv>(T); + impl Pub<Priv> { + pub fn method(&self) {} + pub fn static_method() {} + pub const CONST: u8 = 0; + // pub type AssocTy = u8; + } + + pub macro mac() { + let value = <Pub>::method; + //~^ ERROR type `priv_parent_substs::Priv` is private + value; + //~^ ERROR type `priv_parent_substs::Priv` is private + let value = Pub::method; + //~^ ERROR type `priv_parent_substs::Priv` is private + value; + //~^ ERROR type `priv_parent_substs::Priv` is private + let value = <Pub>::static_method; + //~^ ERROR type `priv_parent_substs::Priv` is private + value; + //~^ ERROR type `priv_parent_substs::Priv` is private + let value = Pub::static_method; + //~^ ERROR type `priv_parent_substs::Priv` is private + value; + //~^ ERROR type `priv_parent_substs::Priv` is private + Pub(Priv).method(); + //~^ ERROR type `priv_parent_substs::Priv` is private + + <Pub>::CONST; + //~^ ERROR type `priv_parent_substs::Priv` is private + Pub::CONST; + //~^ ERROR type `priv_parent_substs::Priv` is private + + // let _: Pub::AssocTy; + // pub type InSignatureTy = Pub::AssocTy; + } +} +fn priv_parent_substs() { + priv_parent_substs::mac!(); +} + +fn main() {} diff --git a/tests/ui/privacy/associated-item-privacy-inherent.stderr b/tests/ui/privacy/associated-item-privacy-inherent.stderr new file mode 100644 index 000000000..f4d4ee459 --- /dev/null +++ b/tests/ui/privacy/associated-item-privacy-inherent.stderr @@ -0,0 +1,233 @@ +error: type `for<'a> fn(&'a priv_nominal::Pub) {priv_nominal::Pub::method}` is private + --> $DIR/associated-item-privacy-inherent.rs:13:21 + | +LL | let value = Pub::method; + | ^^^^^^^^^^^ private type +... +LL | priv_nominal::mac!(); + | -------------------- in this macro invocation + | + = note: this error originates in the macro `priv_nominal::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `for<'a> fn(&'a priv_nominal::Pub) {priv_nominal::Pub::method}` is private + --> $DIR/associated-item-privacy-inherent.rs:15:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_nominal::mac!(); + | -------------------- in this macro invocation + | + = note: this error originates in the macro `priv_nominal::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `for<'a> fn(&'a priv_nominal::Pub) {priv_nominal::Pub::method}` is private + --> $DIR/associated-item-privacy-inherent.rs:17:13 + | +LL | Pub.method(); + | ^^^^^^ private type +... +LL | priv_nominal::mac!(); + | -------------------- in this macro invocation + | + = note: this error originates in the macro `priv_nominal::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: associated constant `CONST` is private + --> $DIR/associated-item-privacy-inherent.rs:19:9 + | +LL | Pub::CONST; + | ^^^^^^^^^^ private associated constant +... +LL | priv_nominal::mac!(); + | -------------------- in this macro invocation + | + = note: this error originates in the macro `priv_nominal::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_signature::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:37:21 + | +LL | let value = Pub::method; + | ^^^^^^^^^^^ private type +... +LL | priv_signature::mac!(); + | ---------------------- in this macro invocation + | + = note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_signature::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:39:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_signature::mac!(); + | ---------------------- in this macro invocation + | + = note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_signature::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:41:13 + | +LL | Pub.method(loop {}); + | ^^^^^^ private type +... +LL | priv_signature::mac!(); + | ---------------------- in this macro invocation + | + = note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:57:21 + | +LL | let value = Pub::method::<Priv>; + | ^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_substs::mac!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:59:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_substs::mac!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:61:9 + | +LL | Pub.method::<Priv>(); + | ^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_substs::mac!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:80:21 + | +LL | let value = <Pub>::method; + | ^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:82:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:84:21 + | +LL | let value = Pub::method; + | ^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:86:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:88:21 + | +LL | let value = <Pub>::static_method; + | ^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:90:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:92:21 + | +LL | let value = Pub::static_method; + | ^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:94:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:96:19 + | +LL | Pub(Priv).method(); + | ^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:99:10 + | +LL | <Pub>::CONST; + | ^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-inherent.rs:101:9 + | +LL | Pub::CONST; + | ^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 21 previous errors + diff --git a/tests/ui/privacy/associated-item-privacy-trait.rs b/tests/ui/privacy/associated-item-privacy-trait.rs new file mode 100644 index 000000000..c686a2177 --- /dev/null +++ b/tests/ui/privacy/associated-item-privacy-trait.rs @@ -0,0 +1,134 @@ +#![feature(decl_macro, associated_type_defaults)] +#![allow(unused, private_in_public)] + +mod priv_trait { + trait PrivTr { + fn method(&self) {} + const CONST: u8 = 0; + type AssocTy = u8; + } + pub struct Pub; + impl PrivTr for Pub {} + pub trait PubTr: PrivTr {} + + pub macro mac() { + let value = <Pub as PrivTr>::method; + //~^ ERROR type `for<'a> fn(&'a priv_trait::Pub) {<priv_trait::Pub as PrivTr>::method}` is private + value; + //~^ ERROR type `for<'a> fn(&'a priv_trait::Pub) {<priv_trait::Pub as PrivTr>::method}` is private + Pub.method(); + //~^ ERROR type `for<'a> fn(&'a Self) {<Self as PrivTr>::method}` is private + <Pub as PrivTr>::CONST; + //~^ ERROR associated constant `PrivTr::CONST` is private + let _: <Pub as PrivTr>::AssocTy; + //~^ ERROR associated type `PrivTr::AssocTy` is private + pub type InSignatureTy = <Pub as PrivTr>::AssocTy; + //~^ ERROR trait `PrivTr` is private + pub trait InSignatureTr: PrivTr {} + //~^ ERROR trait `PrivTr` is private + impl PrivTr for u8 {} + //~^ ERROR trait `PrivTr` is private + } +} +fn priv_trait() { + priv_trait::mac!(); +} + +mod priv_signature { + pub trait PubTr { + fn method(&self, arg: Priv) {} + } + struct Priv; + pub struct Pub; + impl PubTr for Pub {} + + pub macro mac() { + let value = <Pub as PubTr>::method; + //~^ ERROR type `priv_signature::Priv` is private + value; + //~^ ERROR type `priv_signature::Priv` is private + Pub.method(loop {}); + //~^ ERROR type `priv_signature::Priv` is private + } +} +fn priv_signature() { + priv_signature::mac!(); +} + +mod priv_substs { + pub trait PubTr { + fn method<T>(&self) {} + } + struct Priv; + pub struct Pub; + impl PubTr for Pub {} + + pub macro mac() { + let value = <Pub as PubTr>::method::<Priv>; + //~^ ERROR type `priv_substs::Priv` is private + value; + //~^ ERROR type `priv_substs::Priv` is private + Pub.method::<Priv>(); + //~^ ERROR type `priv_substs::Priv` is private + } +} +fn priv_substs() { + priv_substs::mac!(); +} + +mod priv_parent_substs { + pub trait PubTr<T = Priv> { + fn method(&self) {} + const CONST: u8 = 0; + type AssocTy = u8; + } + struct Priv; + pub struct Pub; + impl PubTr<Priv> for Pub {} + impl PubTr<Pub> for Priv {} + + pub macro mac() { + let value = <Pub as PubTr>::method; + //~^ ERROR type `priv_parent_substs::Priv` is private + value; + //~^ ERROR type `priv_parent_substs::Priv` is private + let value = <Pub as PubTr<_>>::method; + //~^ ERROR type `priv_parent_substs::Priv` is private + value; + //~^ ERROR type `priv_parent_substs::Priv` is private + Pub.method(); + //~^ ERROR type `priv_parent_substs::Priv` is private + + let value = <Priv as PubTr<_>>::method; + //~^ ERROR type `priv_parent_substs::Priv` is private + value; + //~^ ERROR type `priv_parent_substs::Priv` is private + Priv.method(); + //~^ ERROR type `priv_parent_substs::Priv` is private + + <Pub as PubTr>::CONST; + //~^ ERROR type `priv_parent_substs::Priv` is private + <Pub as PubTr<_>>::CONST; + //~^ ERROR type `priv_parent_substs::Priv` is private + <Priv as PubTr<_>>::CONST; + //~^ ERROR type `priv_parent_substs::Priv` is private + + let _: <Pub as PubTr>::AssocTy; // FIXME no longer an error?! + let _: <Pub as PubTr<_>>::AssocTy; + //~^ ERROR type `priv_parent_substs::Priv` is private + let _: <Priv as PubTr<_>>::AssocTy; + //~^ ERROR type `priv_parent_substs::Priv` is private + + pub type InSignatureTy1 = <Pub as PubTr>::AssocTy; + //~^ ERROR type `priv_parent_substs::Priv` is private + pub type InSignatureTy2 = <Priv as PubTr<Pub>>::AssocTy; + //~^ ERROR type `priv_parent_substs::Priv` is private + impl PubTr for u8 {} + //~^ ERROR type `priv_parent_substs::Priv` is private + } +} +fn priv_parent_substs() { + priv_parent_substs::mac!(); +} + +fn main() {} diff --git a/tests/ui/privacy/associated-item-privacy-trait.stderr b/tests/ui/privacy/associated-item-privacy-trait.stderr new file mode 100644 index 000000000..eb905bf7e --- /dev/null +++ b/tests/ui/privacy/associated-item-privacy-trait.stderr @@ -0,0 +1,332 @@ +error: type `for<'a> fn(&'a priv_trait::Pub) {<priv_trait::Pub as PrivTr>::method}` is private + --> $DIR/associated-item-privacy-trait.rs:15:21 + | +LL | let value = <Pub as PrivTr>::method; + | ^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_trait::mac!(); + | ------------------ in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `for<'a> fn(&'a priv_trait::Pub) {<priv_trait::Pub as PrivTr>::method}` is private + --> $DIR/associated-item-privacy-trait.rs:17:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_trait::mac!(); + | ------------------ in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `for<'a> fn(&'a Self) {<Self as PrivTr>::method}` is private + --> $DIR/associated-item-privacy-trait.rs:19:13 + | +LL | Pub.method(); + | ^^^^^^ private type +... +LL | priv_trait::mac!(); + | ------------------ in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: associated constant `PrivTr::CONST` is private + --> $DIR/associated-item-privacy-trait.rs:21:9 + | +LL | <Pub as PrivTr>::CONST; + | ^^^^^^^^^^^^^^^^^^^^^^ private associated constant +... +LL | priv_trait::mac!(); + | ------------------ in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: associated type `PrivTr::AssocTy` is private + --> $DIR/associated-item-privacy-trait.rs:23:16 + | +LL | let _: <Pub as PrivTr>::AssocTy; + | ^^^^^^^^^^^^^^^^^^^^^^^^ private associated type +... +LL | priv_trait::mac!(); + | ------------------ in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-trait.rs:25:34 + | +LL | pub type InSignatureTy = <Pub as PrivTr>::AssocTy; + | ^^^^^^^^^^^^^^^^^^^^^^^^ private trait +... +LL | priv_trait::mac!(); + | ------------------ in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-trait.rs:27:34 + | +LL | pub trait InSignatureTr: PrivTr {} + | ^^^^^^ private trait +... +LL | priv_trait::mac!(); + | ------------------ in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-trait.rs:29:14 + | +LL | impl PrivTr for u8 {} + | ^^^^^^ private trait +... +LL | priv_trait::mac!(); + | ------------------ in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_signature::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:46:21 + | +LL | let value = <Pub as PubTr>::method; + | ^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_signature::mac!(); + | ---------------------- in this macro invocation + | + = note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_signature::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:48:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_signature::mac!(); + | ---------------------- in this macro invocation + | + = note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_signature::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:50:13 + | +LL | Pub.method(loop {}); + | ^^^^^^ private type +... +LL | priv_signature::mac!(); + | ---------------------- in this macro invocation + | + = note: this error originates in the macro `priv_signature::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:67:21 + | +LL | let value = <Pub as PubTr>::method::<Priv>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_substs::mac!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:69:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_substs::mac!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:71:9 + | +LL | Pub.method::<Priv>(); + | ^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_substs::mac!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:91:21 + | +LL | let value = <Pub as PubTr>::method; + | ^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:93:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:95:21 + | +LL | let value = <Pub as PubTr<_>>::method; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:97:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:99:9 + | +LL | Pub.method(); + | ^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:102:21 + | +LL | let value = <Priv as PubTr<_>>::method; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:104:9 + | +LL | value; + | ^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:106:9 + | +LL | Priv.method(); + | ^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:109:9 + | +LL | <Pub as PubTr>::CONST; + | ^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:111:9 + | +LL | <Pub as PubTr<_>>::CONST; + | ^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:113:9 + | +LL | <Priv as PubTr<_>>::CONST; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:117:30 + | +LL | let _: <Pub as PubTr<_>>::AssocTy; + | ^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:119:17 + | +LL | let _: <Priv as PubTr<_>>::AssocTy; + | ^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:122:35 + | +LL | pub type InSignatureTy1 = <Pub as PubTr>::AssocTy; + | ^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:124:35 + | +LL | pub type InSignatureTy2 = <Priv as PubTr<Pub>>::AssocTy; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `priv_parent_substs::Priv` is private + --> $DIR/associated-item-privacy-trait.rs:126:14 + | +LL | impl PubTr for u8 {} + | ^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 30 previous errors + diff --git a/tests/ui/privacy/associated-item-privacy-type-binding.rs b/tests/ui/privacy/associated-item-privacy-type-binding.rs new file mode 100644 index 000000000..9826b83a3 --- /dev/null +++ b/tests/ui/privacy/associated-item-privacy-type-binding.rs @@ -0,0 +1,64 @@ +#![feature(decl_macro, associated_type_defaults)] +#![allow(unused, private_in_public)] + +mod priv_trait { + trait PrivTr { + type AssocTy = u8; + } + pub trait PubTr: PrivTr {} + + pub macro mac1() { + let _: Box<dyn PubTr<AssocTy = u8>>; + //~^ ERROR trait `PrivTr` is private + //~| ERROR trait `PrivTr` is private + type InSignatureTy2 = Box<dyn PubTr<AssocTy = u8>>; + //~^ ERROR trait `PrivTr` is private + trait InSignatureTr2: PubTr<AssocTy = u8> {} + //~^ ERROR trait `PrivTr` is private + } + pub macro mac2() { + let _: Box<dyn PrivTr<AssocTy = u8>>; + //~^ ERROR trait `PrivTr` is private + //~| ERROR trait `PrivTr` is private + type InSignatureTy1 = Box<dyn PrivTr<AssocTy = u8>>; + //~^ ERROR trait `PrivTr` is private + trait InSignatureTr1: PrivTr<AssocTy = u8> {} + //~^ ERROR trait `PrivTr` is private + } +} +fn priv_trait1() { + priv_trait::mac1!(); +} +fn priv_trait2() { + priv_trait::mac2!(); +} + +mod priv_parent_substs { + pub trait PubTrWithParam<T = Priv> { + type AssocTy = u8; + } + struct Priv; + pub trait PubTr: PubTrWithParam<Priv> {} + + pub macro mac() { + let _: Box<dyn PubTrWithParam<AssocTy = u8>>; + //~^ ERROR type `Priv` is private + //~| ERROR type `Priv` is private + let _: Box<dyn PubTr<AssocTy = u8>>; + //~^ ERROR type `Priv` is private + //~| ERROR type `Priv` is private + pub type InSignatureTy1 = Box<dyn PubTrWithParam<AssocTy = u8>>; + //~^ ERROR type `Priv` is private + pub type InSignatureTy2 = Box<dyn PubTr<AssocTy = u8>>; + //~^ ERROR type `Priv` is private + trait InSignatureTr1: PubTrWithParam<AssocTy = u8> {} + //~^ ERROR type `Priv` is private + trait InSignatureTr2: PubTr<AssocTy = u8> {} + //~^ ERROR type `Priv` is private + } +} +fn priv_parent_substs() { + priv_parent_substs::mac!(); +} + +fn main() {} diff --git a/tests/ui/privacy/associated-item-privacy-type-binding.stderr b/tests/ui/privacy/associated-item-privacy-type-binding.stderr new file mode 100644 index 000000000..de9893816 --- /dev/null +++ b/tests/ui/privacy/associated-item-privacy-type-binding.stderr @@ -0,0 +1,178 @@ +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-type-binding.rs:11:13 + | +LL | let _: Box<dyn PubTr<AssocTy = u8>>; + | ^ private trait +... +LL | priv_trait::mac1!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac1` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-type-binding.rs:11:16 + | +LL | let _: Box<dyn PubTr<AssocTy = u8>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private trait +... +LL | priv_trait::mac1!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac1` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-type-binding.rs:14:31 + | +LL | type InSignatureTy2 = Box<dyn PubTr<AssocTy = u8>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private trait +... +LL | priv_trait::mac1!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac1` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-type-binding.rs:16:31 + | +LL | trait InSignatureTr2: PubTr<AssocTy = u8> {} + | ^^^^^^^^^^^^^^^^^^^ private trait +... +LL | priv_trait::mac1!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac1` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-type-binding.rs:20:13 + | +LL | let _: Box<dyn PrivTr<AssocTy = u8>>; + | ^ private trait +... +LL | priv_trait::mac2!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac2` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-type-binding.rs:20:16 + | +LL | let _: Box<dyn PrivTr<AssocTy = u8>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private trait +... +LL | priv_trait::mac2!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac2` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-type-binding.rs:23:31 + | +LL | type InSignatureTy1 = Box<dyn PrivTr<AssocTy = u8>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private trait +... +LL | priv_trait::mac2!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac2` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `PrivTr` is private + --> $DIR/associated-item-privacy-type-binding.rs:25:31 + | +LL | trait InSignatureTr1: PrivTr<AssocTy = u8> {} + | ^^^^^^^^^^^^^^^^^^^^ private trait +... +LL | priv_trait::mac2!(); + | ------------------- in this macro invocation + | + = note: this error originates in the macro `priv_trait::mac2` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `Priv` is private + --> $DIR/associated-item-privacy-type-binding.rs:44:13 + | +LL | let _: Box<dyn PubTrWithParam<AssocTy = u8>>; + | ^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `Priv` is private + --> $DIR/associated-item-privacy-type-binding.rs:44:16 + | +LL | let _: Box<dyn PubTrWithParam<AssocTy = u8>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `Priv` is private + --> $DIR/associated-item-privacy-type-binding.rs:47:13 + | +LL | let _: Box<dyn PubTr<AssocTy = u8>>; + | ^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `Priv` is private + --> $DIR/associated-item-privacy-type-binding.rs:47:16 + | +LL | let _: Box<dyn PubTr<AssocTy = u8>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `Priv` is private + --> $DIR/associated-item-privacy-type-binding.rs:50:35 + | +LL | pub type InSignatureTy1 = Box<dyn PubTrWithParam<AssocTy = u8>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `Priv` is private + --> $DIR/associated-item-privacy-type-binding.rs:52:35 + | +LL | pub type InSignatureTy2 = Box<dyn PubTr<AssocTy = u8>>; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `Priv` is private + --> $DIR/associated-item-privacy-type-binding.rs:54:31 + | +LL | trait InSignatureTr1: PubTrWithParam<AssocTy = u8> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `Priv` is private + --> $DIR/associated-item-privacy-type-binding.rs:56:31 + | +LL | trait InSignatureTr2: PubTr<AssocTy = u8> {} + | ^^^^^^^^^^^^^^^^^^^ private type +... +LL | priv_parent_substs::mac!(); + | -------------------------- in this macro invocation + | + = note: this error originates in the macro `priv_parent_substs::mac` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 16 previous errors + diff --git a/tests/ui/privacy/auxiliary/cci_class.rs b/tests/ui/privacy/auxiliary/cci_class.rs new file mode 100644 index 000000000..de2945d74 --- /dev/null +++ b/tests/ui/privacy/auxiliary/cci_class.rs @@ -0,0 +1,14 @@ +pub mod kitties { + pub struct cat { + meows : usize, + + pub how_hungry : isize, + } + + pub fn cat(in_x : usize, in_y : isize) -> cat { + cat { + meows: in_x, + how_hungry: in_y + } + } +} diff --git a/tests/ui/privacy/auxiliary/cci_class_5.rs b/tests/ui/privacy/auxiliary/cci_class_5.rs new file mode 100644 index 000000000..3aeaa83d3 --- /dev/null +++ b/tests/ui/privacy/auxiliary/cci_class_5.rs @@ -0,0 +1,17 @@ +pub mod kitties { + pub struct cat { + meows : usize, + pub how_hungry : isize, + } + + impl cat { + fn nap(&self) {} + } + + pub fn cat(in_x : usize, in_y : isize) -> cat { + cat { + meows: in_x, + how_hungry: in_y + } + } +} diff --git a/tests/ui/privacy/auxiliary/ctor_aux.rs b/tests/ui/privacy/auxiliary/ctor_aux.rs new file mode 100644 index 000000000..9c99cca9a --- /dev/null +++ b/tests/ui/privacy/auxiliary/ctor_aux.rs @@ -0,0 +1,25 @@ +// edition:2021 +//! Missing docs lint warns about undocumented exported items. +//! Use the lint to additionally verify that items are reachable +//! but not exported. +#![allow(non_camel_case_types)] +#![deny(missing_docs)] + +mod hidden { + pub struct s; + pub enum e { x, y, z } + pub use e::*; + impl s { + pub fn f(&self) {} + } + impl e { + pub fn g(&self) {} + } +} +// Hide all type definitions while reexporting their constructors: +mod e {} +mod x {} +mod y {} +mod z {} +mod s {} +pub use hidden::*; diff --git a/tests/ui/privacy/auxiliary/impl_privacy_xc_2.rs b/tests/ui/privacy/auxiliary/impl_privacy_xc_2.rs new file mode 100644 index 000000000..5f9c22681 --- /dev/null +++ b/tests/ui/privacy/auxiliary/impl_privacy_xc_2.rs @@ -0,0 +1,13 @@ +#![crate_type = "lib"] + +pub struct Fish { + pub x: isize +} + +mod unexported { + use super::Fish; + impl PartialEq for Fish { + fn eq(&self, _: &Fish) -> bool { true } + fn ne(&self, _: &Fish) -> bool { false } + } +} diff --git a/tests/ui/privacy/auxiliary/issue-17718-const-privacy.rs b/tests/ui/privacy/auxiliary/issue-17718-const-privacy.rs new file mode 100644 index 000000000..93cf4bf3e --- /dev/null +++ b/tests/ui/privacy/auxiliary/issue-17718-const-privacy.rs @@ -0,0 +1,8 @@ +pub use foo::FOO2; + +pub const FOO: usize = 3; +const BAR: usize = 3; + +mod foo { + pub const FOO2: usize = 3; +} diff --git a/tests/ui/privacy/auxiliary/issue-57264-1.rs b/tests/ui/privacy/auxiliary/issue-57264-1.rs new file mode 100644 index 000000000..9302fa0d9 --- /dev/null +++ b/tests/ui/privacy/auxiliary/issue-57264-1.rs @@ -0,0 +1,9 @@ +mod inner { + pub struct PubUnnameable; +} + +pub struct Pub<T>(T); + +impl Pub<inner::PubUnnameable> { + pub fn pub_method() {} +} diff --git a/tests/ui/privacy/auxiliary/issue-57264-2.rs b/tests/ui/privacy/auxiliary/issue-57264-2.rs new file mode 100644 index 000000000..416206b4f --- /dev/null +++ b/tests/ui/privacy/auxiliary/issue-57264-2.rs @@ -0,0 +1,10 @@ +mod inner { + pub struct PubUnnameable; + + impl PubUnnameable { + pub fn pub_method(self) {} + } +} + +pub trait PubTraitWithSingleImplementor {} +impl PubTraitWithSingleImplementor for Option<inner::PubUnnameable> {} diff --git a/tests/ui/privacy/auxiliary/issue-75907.rs b/tests/ui/privacy/auxiliary/issue-75907.rs new file mode 100644 index 000000000..389c9c351 --- /dev/null +++ b/tests/ui/privacy/auxiliary/issue-75907.rs @@ -0,0 +1,17 @@ +pub struct Bar(pub u8, u8, u8); + +pub fn make_bar() -> Bar { + Bar(1, 12, 10) +} + +mod inner { + pub struct Foo(u8, pub u8, u8); + + impl Foo { + pub fn new() -> Foo { + Foo(1, 12, 10) + } + } +} + +pub use inner::Foo; diff --git a/tests/ui/privacy/auxiliary/issue-92755.rs b/tests/ui/privacy/auxiliary/issue-92755.rs new file mode 100644 index 000000000..6f8527346 --- /dev/null +++ b/tests/ui/privacy/auxiliary/issue-92755.rs @@ -0,0 +1,17 @@ +mod machine { + pub struct A { + pub b: B, + } + pub struct B {} + impl B { + pub fn f(&self) {} + } +} + +pub struct Context { + pub a: machine::A, +} + +pub fn ctx() -> Context { + todo!(); +} diff --git a/tests/ui/privacy/auxiliary/priv-impl-prim-ty.rs b/tests/ui/privacy/auxiliary/priv-impl-prim-ty.rs new file mode 100644 index 000000000..8ccbd3f12 --- /dev/null +++ b/tests/ui/privacy/auxiliary/priv-impl-prim-ty.rs @@ -0,0 +1,9 @@ +pub trait A { + fn frob(&self); +} + +impl A for isize { fn frob(&self) {} } + +pub fn frob<T:A>(t: T) { + t.frob(); +} diff --git a/tests/ui/privacy/auxiliary/privacy_reexport.rs b/tests/ui/privacy/auxiliary/privacy_reexport.rs new file mode 100644 index 000000000..6b72dbc92 --- /dev/null +++ b/tests/ui/privacy/auxiliary/privacy_reexport.rs @@ -0,0 +1,6 @@ +pub extern crate core; +pub use foo as bar; + +pub mod foo { + pub fn frob() {} +} diff --git a/tests/ui/privacy/auxiliary/privacy_tuple_struct.rs b/tests/ui/privacy/auxiliary/privacy_tuple_struct.rs new file mode 100644 index 000000000..223cda4b2 --- /dev/null +++ b/tests/ui/privacy/auxiliary/privacy_tuple_struct.rs @@ -0,0 +1,4 @@ +pub struct A(()); +pub struct B(isize); +pub struct C(pub isize, isize); +pub struct D(pub isize); diff --git a/tests/ui/privacy/auxiliary/private-inferred-type.rs b/tests/ui/privacy/auxiliary/private-inferred-type.rs new file mode 100644 index 000000000..7ac913f5b --- /dev/null +++ b/tests/ui/privacy/auxiliary/private-inferred-type.rs @@ -0,0 +1,36 @@ +#![feature(decl_macro)] + +fn priv_fn() {} +static PRIV_STATIC: u8 = 0; +enum PrivEnum { Variant } +pub enum PubEnum { Variant } +trait PrivTrait { fn method() {} } +impl PrivTrait for u8 {} +pub trait PubTrait { fn method() {} } +impl PubTrait for u8 {} +struct PrivTupleStruct(u8); +pub struct PubTupleStruct(u8); +impl PubTupleStruct { fn method() {} } + +struct Priv; +pub type Alias = Priv; +pub struct Pub<T = Alias>(pub T); + +impl Pub<Priv> { + pub fn static_method() {} +} +impl Pub<u8> { + fn priv_method(&self) {} +} + +pub macro m() { + priv_fn; + PRIV_STATIC; + PrivEnum::Variant; + PubEnum::Variant; + <u8 as PrivTrait>::method; + <u8 as PubTrait>::method; + PrivTupleStruct; + PubTupleStruct; + Pub(0u8).priv_method(); +} diff --git a/tests/ui/privacy/auxiliary/pub_use_mods_xcrate.rs b/tests/ui/privacy/auxiliary/pub_use_mods_xcrate.rs new file mode 100644 index 000000000..74d3504d5 --- /dev/null +++ b/tests/ui/privacy/auxiliary/pub_use_mods_xcrate.rs @@ -0,0 +1,10 @@ +pub mod a { + pub use a::b::c; + + pub mod b { + pub mod c { + fn f(){} + fn g(){} + } + } +} diff --git a/tests/ui/privacy/auxiliary/pub_use_xcrate1.rs b/tests/ui/privacy/auxiliary/pub_use_xcrate1.rs new file mode 100644 index 000000000..772c9627a --- /dev/null +++ b/tests/ui/privacy/auxiliary/pub_use_xcrate1.rs @@ -0,0 +1,3 @@ +pub struct Foo { + pub name: isize +} diff --git a/tests/ui/privacy/auxiliary/pub_use_xcrate2.rs b/tests/ui/privacy/auxiliary/pub_use_xcrate2.rs new file mode 100644 index 000000000..20d7066d3 --- /dev/null +++ b/tests/ui/privacy/auxiliary/pub_use_xcrate2.rs @@ -0,0 +1,3 @@ +extern crate pub_use_xcrate1; + +pub use pub_use_xcrate1::Foo; diff --git a/tests/ui/privacy/auxiliary/reachable-unnameable-items.rs b/tests/ui/privacy/auxiliary/reachable-unnameable-items.rs new file mode 100644 index 000000000..20f110b1a --- /dev/null +++ b/tests/ui/privacy/auxiliary/reachable-unnameable-items.rs @@ -0,0 +1,106 @@ +use inner_private_module::*; + +mod inner_private_module { + pub struct Unnameable1; + pub struct Unnameable2; + #[derive(Clone, Copy)] + pub struct Unnameable3; + pub struct Unnameable4; + pub struct Unnameable5; + pub struct Unnameable6; + pub struct Unnameable7; + #[derive(Default)] + pub struct Unnameable8; + pub enum UnnameableEnum { + NameableVariant + } + pub trait UnnameableTrait { + type Alias: Default; + } + + impl Unnameable1 { + pub fn method_of_unnameable_type1(&self) -> &'static str { + "Hello1" + } + } + impl Unnameable2 { + pub fn method_of_unnameable_type2(&self) -> &'static str { + "Hello2" + } + } + impl Unnameable3 { + pub fn method_of_unnameable_type3(&self) -> &'static str { + "Hello3" + } + } + impl Unnameable4 { + pub fn method_of_unnameable_type4(&self) -> &'static str { + "Hello4" + } + } + impl Unnameable5 { + pub fn method_of_unnameable_type5(&self) -> &'static str { + "Hello5" + } + } + impl Unnameable6 { + pub fn method_of_unnameable_type6(&self) -> &'static str { + "Hello6" + } + } + impl Unnameable7 { + pub fn method_of_unnameable_type7(&self) -> &'static str { + "Hello7" + } + } + impl Unnameable8 { + pub fn method_of_unnameable_type8(&self) -> &'static str { + "Hello8" + } + } + impl UnnameableEnum { + pub fn method_of_unnameable_enum(&self) -> &'static str { + "HelloEnum" + } + } +} + +pub fn function_returning_unnameable_type() -> Unnameable1 { + Unnameable1 +} + +pub const CONSTANT_OF_UNNAMEABLE_TYPE: Unnameable2 = + Unnameable2; + +pub fn function_accepting_unnameable_type(_: Option<Unnameable3>) {} + +pub type AliasOfUnnameableType = Unnameable4; + +impl Unnameable1 { + pub fn inherent_method_returning_unnameable_type(&self) -> Unnameable5 { + Unnameable5 + } +} + +pub trait Tr { + fn trait_method_returning_unnameable_type(&self) -> Unnameable6 { + Unnameable6 + } +} +impl Tr for Unnameable1 {} + +pub use inner_private_module::UnnameableEnum::NameableVariant; + +pub struct Struct { + pub field_of_unnameable_type: Unnameable7 +} + +pub static STATIC: Struct = Struct { field_of_unnameable_type: Unnameable7 } ; + +impl UnnameableTrait for AliasOfUnnameableType { + type Alias = Unnameable8; +} + +pub fn generic_function<T: UnnameableTrait>() -> T::Alias { + Default::default() +} diff --git a/tests/ui/privacy/crate-private-reexport.rs b/tests/ui/privacy/crate-private-reexport.rs new file mode 100644 index 000000000..fa4f88666 --- /dev/null +++ b/tests/ui/privacy/crate-private-reexport.rs @@ -0,0 +1,66 @@ +fn f1() {} +enum E1 { V } +struct S1 { + #[rustfmt::skip] + bar: i32, +} +mod m1 { + pub use ::f1; //~ ERROR `f1` is only public within the crate, and cannot be re-exported outside + pub use ::S1; //~ ERROR `S1` is only public within the crate, and cannot be re-exported outside + pub use ::E1; //~ ERROR `E1` is only public within the crate, and cannot be re-exported outside + pub use ::E1::V; //~ ERROR `V` is only public within the crate, and cannot be re-exported outside +} + +pub(crate) fn f2() {} +pub(crate) enum E2 { + V +} +pub(crate) struct S2 { + #[rustfmt::skip] + bar: i32, +} +mod m2 { + pub use ::f2; //~ ERROR `f2` is only public within the crate, and cannot be re-exported outside + pub use ::S2; //~ ERROR `S2` is only public within the crate, and cannot be re-exported outside + pub use ::E2; //~ ERROR `E2` is only public within the crate, and cannot be re-exported outside + pub use ::E2::V; //~ ERROR `V` is only public within the crate, and cannot be re-exported outside +} + +mod m3 { + pub(crate) fn f3() {} + pub(crate) enum E3 { + V + } + pub(crate) struct S3 { + #[rustfmt::skip] + bar: i32, + } +} +pub use m3::f3; //~ ERROR `f3` is only public within the crate, and cannot be re-exported outside +pub use m3::S3; //~ ERROR `S3` is only public within the crate, and cannot be re-exported outside +pub use m3::E3; //~ ERROR `E3` is only public within the crate, and cannot be re-exported outside +pub use m3::E3::V; //~ ERROR `V` is only public within the crate, and cannot be re-exported outside + +pub(self) fn f4() {} +pub use ::f4 as f5; //~ ERROR `f4` is only public within the crate, and cannot be re-exported outside + +pub mod m10 { + pub mod m { + pub(super) fn f6() {} + pub(crate) fn f7() {} + pub(in crate::m10) fn f8() {} + } + pub use self::m::f6; //~ ERROR `f6` is private, and cannot be re-exported + pub use self::m::f7; //~ ERROR `f7` is only public within the crate, and cannot be re-exported outside + pub use self::m::f8; //~ ERROR `f8` is private, and cannot be re-exported +} +pub use m10::m::f6; //~ ERROR function `f6` is private +pub use m10::m::f7; //~ ERROR `f7` is only public within the crate, and cannot be re-exported outside +pub use m10::m::f8; //~ ERROR function `f8` is private + +pub mod m11 { + pub(self) fn f9() {} +} +pub use m11::f9; //~ ERROR function `f9` is private + +fn main() {} diff --git a/tests/ui/privacy/crate-private-reexport.stderr b/tests/ui/privacy/crate-private-reexport.stderr new file mode 100644 index 000000000..66e11e821 --- /dev/null +++ b/tests/ui/privacy/crate-private-reexport.stderr @@ -0,0 +1,220 @@ +error[E0364]: `f1` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:8:13 + | +LL | pub use ::f1; + | ^^^^ + | +note: consider marking `f1` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:8:13 + | +LL | pub use ::f1; + | ^^^^ + +error[E0365]: `S1` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:9:13 + | +LL | pub use ::S1; + | ^^^^ re-export of crate public `S1` + | + = note: consider declaring type or module `S1` with `pub` + +error[E0365]: `E1` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:10:13 + | +LL | pub use ::E1; + | ^^^^ re-export of crate public `E1` + | + = note: consider declaring type or module `E1` with `pub` + +error[E0364]: `V` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:11:13 + | +LL | pub use ::E1::V; + | ^^^^^^^ + | +note: consider marking `V` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:11:13 + | +LL | pub use ::E1::V; + | ^^^^^^^ + +error[E0364]: `f2` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:23:13 + | +LL | pub use ::f2; + | ^^^^ + | +note: consider marking `f2` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:23:13 + | +LL | pub use ::f2; + | ^^^^ + +error[E0365]: `S2` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:24:13 + | +LL | pub use ::S2; + | ^^^^ re-export of crate public `S2` + | + = note: consider declaring type or module `S2` with `pub` + +error[E0365]: `E2` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:25:13 + | +LL | pub use ::E2; + | ^^^^ re-export of crate public `E2` + | + = note: consider declaring type or module `E2` with `pub` + +error[E0364]: `V` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:26:13 + | +LL | pub use ::E2::V; + | ^^^^^^^ + | +note: consider marking `V` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:26:13 + | +LL | pub use ::E2::V; + | ^^^^^^^ + +error[E0364]: `f3` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:39:9 + | +LL | pub use m3::f3; + | ^^^^^^ + | +note: consider marking `f3` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:39:9 + | +LL | pub use m3::f3; + | ^^^^^^ + +error[E0365]: `S3` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:40:9 + | +LL | pub use m3::S3; + | ^^^^^^ re-export of crate public `S3` + | + = note: consider declaring type or module `S3` with `pub` + +error[E0365]: `E3` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:41:9 + | +LL | pub use m3::E3; + | ^^^^^^ re-export of crate public `E3` + | + = note: consider declaring type or module `E3` with `pub` + +error[E0364]: `V` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:42:9 + | +LL | pub use m3::E3::V; + | ^^^^^^^^^ + | +note: consider marking `V` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:42:9 + | +LL | pub use m3::E3::V; + | ^^^^^^^^^ + +error[E0364]: `f4` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:45:9 + | +LL | pub use ::f4 as f5; + | ^^^^^^^^^^ + | +note: consider marking `f4` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:45:9 + | +LL | pub use ::f4 as f5; + | ^^^^^^^^^^ + +error[E0364]: `f6` is private, and cannot be re-exported + --> $DIR/crate-private-reexport.rs:53:13 + | +LL | pub use self::m::f6; + | ^^^^^^^^^^^ + | +note: consider marking `f6` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:53:13 + | +LL | pub use self::m::f6; + | ^^^^^^^^^^^ + +error[E0364]: `f7` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:54:13 + | +LL | pub use self::m::f7; + | ^^^^^^^^^^^ + | +note: consider marking `f7` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:54:13 + | +LL | pub use self::m::f7; + | ^^^^^^^^^^^ + +error[E0364]: `f8` is private, and cannot be re-exported + --> $DIR/crate-private-reexport.rs:55:13 + | +LL | pub use self::m::f8; + | ^^^^^^^^^^^ + | +note: consider marking `f8` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:55:13 + | +LL | pub use self::m::f8; + | ^^^^^^^^^^^ + +error[E0364]: `f7` is only public within the crate, and cannot be re-exported outside + --> $DIR/crate-private-reexport.rs:58:9 + | +LL | pub use m10::m::f7; + | ^^^^^^^^^^ + | +note: consider marking `f7` as `pub` in the imported module + --> $DIR/crate-private-reexport.rs:58:9 + | +LL | pub use m10::m::f7; + | ^^^^^^^^^^ + +error[E0603]: function `f6` is private + --> $DIR/crate-private-reexport.rs:57:17 + | +LL | pub use m10::m::f6; + | ^^ private function + | +note: the function `f6` is defined here + --> $DIR/crate-private-reexport.rs:49:9 + | +LL | pub(super) fn f6() {} + | ^^^^^^^^^^^^^^^^^^ + +error[E0603]: function `f8` is private + --> $DIR/crate-private-reexport.rs:59:17 + | +LL | pub use m10::m::f8; + | ^^ private function + | +note: the function `f8` is defined here + --> $DIR/crate-private-reexport.rs:51:9 + | +LL | pub(in crate::m10) fn f8() {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0603]: function `f9` is private + --> $DIR/crate-private-reexport.rs:64:14 + | +LL | pub use m11::f9; + | ^^ private function + | +note: the function `f9` is defined here + --> $DIR/crate-private-reexport.rs:62:5 + | +LL | pub(self) fn f9() {} + | ^^^^^^^^^^^^^^^^^ + +error: aborting due to 20 previous errors + +Some errors have detailed explanations: E0364, E0365, E0603. +For more information about an error, try `rustc --explain E0364`. diff --git a/tests/ui/privacy/ctor.rs b/tests/ui/privacy/ctor.rs new file mode 100644 index 000000000..0ec15d68e --- /dev/null +++ b/tests/ui/privacy/ctor.rs @@ -0,0 +1,16 @@ +// Verify that a type is considered reachable when its constructor is +// reachable. The auxiliary library is constructed so that all types are +// shadowed and cannot be named directly, while their constructors are +// reexported. Regression test for issue #96934. +// +// aux-build:ctor_aux.rs +// edition:2021 +// build-pass + +extern crate ctor_aux; + +fn main() { + ctor_aux::s.f(); + ctor_aux::x.g(); + ctor_aux::y.g(); +} diff --git a/tests/ui/privacy/decl-macro.rs b/tests/ui/privacy/decl-macro.rs new file mode 100644 index 000000000..1eb49bd53 --- /dev/null +++ b/tests/ui/privacy/decl-macro.rs @@ -0,0 +1,9 @@ +#![feature(decl_macro)] + +mod m { + macro mac() {} +} + +fn main() { + m::mac!(); //~ ERROR macro `mac` is private +} diff --git a/tests/ui/privacy/decl-macro.stderr b/tests/ui/privacy/decl-macro.stderr new file mode 100644 index 000000000..5bc6f07ff --- /dev/null +++ b/tests/ui/privacy/decl-macro.stderr @@ -0,0 +1,15 @@ +error[E0603]: macro `mac` is private + --> $DIR/decl-macro.rs:8:8 + | +LL | m::mac!(); + | ^^^ private macro + | +note: the macro `mac` is defined here + --> $DIR/decl-macro.rs:4:5 + | +LL | macro mac() {} + | ^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/effective_visibilities.rs b/tests/ui/privacy/effective_visibilities.rs new file mode 100644 index 000000000..ff20e20d3 --- /dev/null +++ b/tests/ui/privacy/effective_visibilities.rs @@ -0,0 +1,76 @@ +#![feature(rustc_attrs)] + +#[rustc_effective_visibility] +mod outer { //~ ERROR Direct: pub(crate), Reexported: pub(crate), Reachable: pub(crate), ReachableThroughImplTrait: pub(crate) + #[rustc_effective_visibility] + pub mod inner1 { //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + + #[rustc_effective_visibility] + extern "C" {} //~ ERROR not in the table + + #[rustc_effective_visibility] + pub trait PubTrait { //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + #[rustc_effective_visibility] + const A: i32; //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + #[rustc_effective_visibility] + type B; //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + } + + #[rustc_effective_visibility] + struct PrivStruct; //~ ERROR Direct: pub(self), Reexported: pub(self), Reachable: pub(self), ReachableThroughImplTrait: pub(self) + //~| ERROR Direct: pub(self), Reexported: pub(self), Reachable: pub(self), ReachableThroughImplTrait: pub(self) + + #[rustc_effective_visibility] + pub union PubUnion { //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + #[rustc_effective_visibility] + a: u8, //~ ERROR Direct: pub(self), Reexported: pub(self), Reachable: pub(self), ReachableThroughImplTrait: pub(self) + #[rustc_effective_visibility] + pub b: u8, //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + } + + #[rustc_effective_visibility] + pub enum Enum { //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + #[rustc_effective_visibility] + A( //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + //~| ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + #[rustc_effective_visibility] + PubUnion, //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + ), + } + } + + #[rustc_effective_visibility] + macro_rules! none_macro { //~ ERROR not in the table + () => {}; + } + + #[macro_export] + #[rustc_effective_visibility] + macro_rules! public_macro { //~ ERROR Direct: pub(self), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + () => {}; + } + + #[rustc_effective_visibility] + pub struct ReachableStruct { //~ ERROR Direct: pub(crate), Reexported: pub(crate), Reachable: pub, ReachableThroughImplTrait: pub + #[rustc_effective_visibility] + pub a: u8, //~ ERROR Direct: pub(crate), Reexported: pub(crate), Reachable: pub, ReachableThroughImplTrait: pub + } +} + +#[rustc_effective_visibility] +pub use outer::inner1; //~ ERROR Direct: pub, Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + +pub fn foo() -> outer::ReachableStruct { outer::ReachableStruct {a: 0} } + +mod half_public_import { + #[rustc_effective_visibility] + pub type HalfPublicImport = u8; //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + #[rustc_effective_visibility] + #[allow(non_upper_case_globals)] + pub(crate) const HalfPublicImport: u8 = 0; //~ ERROR Direct: pub(crate), Reexported: pub(crate), Reachable: pub(crate), ReachableThroughImplTrait: pub(crate) +} + +#[rustc_effective_visibility] +pub use half_public_import::HalfPublicImport; //~ ERROR Direct: pub, Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + +fn main() {} diff --git a/tests/ui/privacy/effective_visibilities.stderr b/tests/ui/privacy/effective_visibilities.stderr new file mode 100644 index 000000000..046b6095f --- /dev/null +++ b/tests/ui/privacy/effective_visibilities.stderr @@ -0,0 +1,140 @@ +error: Direct: pub(crate), Reexported: pub(crate), Reachable: pub(crate), ReachableThroughImplTrait: pub(crate) + --> $DIR/effective_visibilities.rs:4:1 + | +LL | mod outer { + | ^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:6:5 + | +LL | pub mod inner1 { + | ^^^^^^^^^^^^^^ + +error: not in the table + --> $DIR/effective_visibilities.rs:9:9 + | +LL | extern "C" {} + | ^^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:12:9 + | +LL | pub trait PubTrait { + | ^^^^^^^^^^^^^^^^^^ + +error: Direct: pub(self), Reexported: pub(self), Reachable: pub(self), ReachableThroughImplTrait: pub(self) + --> $DIR/effective_visibilities.rs:20:9 + | +LL | struct PrivStruct; + | ^^^^^^^^^^^^^^^^^ + +error: Direct: pub(self), Reexported: pub(self), Reachable: pub(self), ReachableThroughImplTrait: pub(self) + --> $DIR/effective_visibilities.rs:20:9 + | +LL | struct PrivStruct; + | ^^^^^^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:24:9 + | +LL | pub union PubUnion { + | ^^^^^^^^^^^^^^^^^^ + +error: Direct: pub(self), Reexported: pub(self), Reachable: pub(self), ReachableThroughImplTrait: pub(self) + --> $DIR/effective_visibilities.rs:26:13 + | +LL | a: u8, + | ^^^^^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:28:13 + | +LL | pub b: u8, + | ^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:32:9 + | +LL | pub enum Enum { + | ^^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:34:13 + | +LL | A( + | ^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:34:13 + | +LL | A( + | ^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:37:17 + | +LL | PubUnion, + | ^^^^^^^^ + +error: not in the table + --> $DIR/effective_visibilities.rs:43:5 + | +LL | macro_rules! none_macro { + | ^^^^^^^^^^^^^^^^^^^^^^^ + +error: Direct: pub(self), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:49:5 + | +LL | macro_rules! public_macro { + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub(crate), Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:54:5 + | +LL | pub struct ReachableStruct { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub(crate), Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:56:9 + | +LL | pub a: u8, + | ^^^^^^^^^ + +error: Direct: pub, Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:61:9 + | +LL | pub use outer::inner1; + | ^^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:67:5 + | +LL | pub type HalfPublicImport = u8; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub(crate), Reachable: pub(crate), ReachableThroughImplTrait: pub(crate) + --> $DIR/effective_visibilities.rs:70:5 + | +LL | pub(crate) const HalfPublicImport: u8 = 0; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: Direct: pub, Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:74:9 + | +LL | pub use half_public_import::HalfPublicImport; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:14:13 + | +LL | const A: i32; + | ^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities.rs:16:13 + | +LL | type B; + | ^^^^^^ + +error: aborting due to 23 previous errors + diff --git a/tests/ui/privacy/effective_visibilities_glob.rs b/tests/ui/privacy/effective_visibilities_glob.rs new file mode 100644 index 000000000..eb9dcd6cd --- /dev/null +++ b/tests/ui/privacy/effective_visibilities_glob.rs @@ -0,0 +1,21 @@ +// Effective visibility tracking for imports is fine-grained, so `S2` is not fully exported +// even if its parent import (`m::*`) is fully exported as a `use` item. + +#![feature(rustc_attrs)] + +mod m { + #[rustc_effective_visibility] + pub struct S1 {} //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + #[rustc_effective_visibility] + pub struct S2 {} //~ ERROR Direct: pub(crate), Reexported: pub(crate), Reachable: pub(crate), ReachableThroughImplTrait: pub(crate) +} + +mod glob { + #[rustc_effective_visibility] + pub use crate::m::*; //~ ERROR Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub +} + +#[rustc_effective_visibility] +pub use glob::S1; //~ ERROR Direct: pub, Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + +fn main() {} diff --git a/tests/ui/privacy/effective_visibilities_glob.stderr b/tests/ui/privacy/effective_visibilities_glob.stderr new file mode 100644 index 000000000..0496cd5df --- /dev/null +++ b/tests/ui/privacy/effective_visibilities_glob.stderr @@ -0,0 +1,26 @@ +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities_glob.rs:8:5 + | +LL | pub struct S1 {} + | ^^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub(crate), Reachable: pub(crate), ReachableThroughImplTrait: pub(crate) + --> $DIR/effective_visibilities_glob.rs:10:5 + | +LL | pub struct S2 {} + | ^^^^^^^^^^^^^ + +error: Direct: pub(crate), Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities_glob.rs:15:13 + | +LL | pub use crate::m::*; + | ^^^^^^^^ + +error: Direct: pub, Reexported: pub, Reachable: pub, ReachableThroughImplTrait: pub + --> $DIR/effective_visibilities_glob.rs:19:9 + | +LL | pub use glob::S1; + | ^^^^^^^^ + +error: aborting due to 4 previous errors + diff --git a/tests/ui/privacy/effective_visibilities_invariants.rs b/tests/ui/privacy/effective_visibilities_invariants.rs new file mode 100644 index 000000000..af5a2bed6 --- /dev/null +++ b/tests/ui/privacy/effective_visibilities_invariants.rs @@ -0,0 +1,12 @@ +// Invariant checking doesn't ICE in some cases with errors (issue #104249). + +#![feature(staged_api)] //~ ERROR module has missing stability attribute + +pub mod m {} //~ ERROR module has missing stability attribute + +pub mod m { //~ ERROR the name `m` is defined multiple times + mod inner {} + type Inner = u8; +} + +fn main() {} diff --git a/tests/ui/privacy/effective_visibilities_invariants.stderr b/tests/ui/privacy/effective_visibilities_invariants.stderr new file mode 100644 index 000000000..fd205f405 --- /dev/null +++ b/tests/ui/privacy/effective_visibilities_invariants.stderr @@ -0,0 +1,32 @@ +error[E0428]: the name `m` is defined multiple times + --> $DIR/effective_visibilities_invariants.rs:7:1 + | +LL | pub mod m {} + | --------- previous definition of the module `m` here +LL | +LL | pub mod m { + | ^^^^^^^^^ `m` redefined here + | + = note: `m` must be defined only once in the type namespace of this module + +error: module has missing stability attribute + --> $DIR/effective_visibilities_invariants.rs:3:1 + | +LL | / #![feature(staged_api)] +LL | | +LL | | pub mod m {} +LL | | +... | +LL | | +LL | | fn main() {} + | |____________^ + +error: module has missing stability attribute + --> $DIR/effective_visibilities_invariants.rs:5:1 + | +LL | pub mod m {} + | ^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0428`. diff --git a/tests/ui/privacy/export-tag-variant.rs b/tests/ui/privacy/export-tag-variant.rs new file mode 100644 index 000000000..46c10067c --- /dev/null +++ b/tests/ui/privacy/export-tag-variant.rs @@ -0,0 +1,7 @@ +mod foo { + pub fn x() { } + + enum Y { Y1 } +} + +fn main() { let z = foo::Y::Y1; } //~ ERROR: enum `Y` is private diff --git a/tests/ui/privacy/export-tag-variant.stderr b/tests/ui/privacy/export-tag-variant.stderr new file mode 100644 index 000000000..f73bd454d --- /dev/null +++ b/tests/ui/privacy/export-tag-variant.stderr @@ -0,0 +1,15 @@ +error[E0603]: enum `Y` is private + --> $DIR/export-tag-variant.rs:7:26 + | +LL | fn main() { let z = foo::Y::Y1; } + | ^ private enum + | +note: the enum `Y` is defined here + --> $DIR/export-tag-variant.rs:4:5 + | +LL | enum Y { Y1 } + | ^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/impl-privacy-xc-2.rs b/tests/ui/privacy/impl-privacy-xc-2.rs new file mode 100644 index 000000000..390764588 --- /dev/null +++ b/tests/ui/privacy/impl-privacy-xc-2.rs @@ -0,0 +1,10 @@ +// run-pass +// aux-build:impl_privacy_xc_2.rs + +extern crate impl_privacy_xc_2; + +pub fn main() { + let fish1 = impl_privacy_xc_2::Fish { x: 1 }; + let fish2 = impl_privacy_xc_2::Fish { x: 2 }; + if fish1.eq(&fish2) { println!("yes") } else { println!("no") }; +} diff --git a/tests/ui/privacy/issue-13641.rs b/tests/ui/privacy/issue-13641.rs new file mode 100644 index 000000000..198cea428 --- /dev/null +++ b/tests/ui/privacy/issue-13641.rs @@ -0,0 +1,13 @@ +mod a { + struct Foo; + impl Foo { pub fn new() {} } + enum Bar {} + impl Bar { pub fn new() {} } +} + +fn main() { + a::Foo::new(); + //~^ ERROR: struct `Foo` is private + a::Bar::new(); + //~^ ERROR: enum `Bar` is private +} diff --git a/tests/ui/privacy/issue-13641.stderr b/tests/ui/privacy/issue-13641.stderr new file mode 100644 index 000000000..cdd0772d3 --- /dev/null +++ b/tests/ui/privacy/issue-13641.stderr @@ -0,0 +1,27 @@ +error[E0603]: struct `Foo` is private + --> $DIR/issue-13641.rs:9:8 + | +LL | a::Foo::new(); + | ^^^ private struct + | +note: the struct `Foo` is defined here + --> $DIR/issue-13641.rs:2:5 + | +LL | struct Foo; + | ^^^^^^^^^^^ + +error[E0603]: enum `Bar` is private + --> $DIR/issue-13641.rs:11:8 + | +LL | a::Bar::new(); + | ^^^ private enum + | +note: the enum `Bar` is defined here + --> $DIR/issue-13641.rs:4:5 + | +LL | enum Bar {} + | ^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/issue-17718-const-privacy.rs b/tests/ui/privacy/issue-17718-const-privacy.rs new file mode 100644 index 000000000..6ab3a60df --- /dev/null +++ b/tests/ui/privacy/issue-17718-const-privacy.rs @@ -0,0 +1,16 @@ +// aux-build:issue-17718-const-privacy.rs + +extern crate issue_17718_const_privacy as other; + +use a::B; //~ ERROR: constant `B` is private +use other::{ + FOO, + BAR, //~ ERROR: constant `BAR` is private + FOO2, +}; + +mod a { + const B: usize = 3; +} + +fn main() {} diff --git a/tests/ui/privacy/issue-17718-const-privacy.stderr b/tests/ui/privacy/issue-17718-const-privacy.stderr new file mode 100644 index 000000000..133a6360b --- /dev/null +++ b/tests/ui/privacy/issue-17718-const-privacy.stderr @@ -0,0 +1,27 @@ +error[E0603]: constant `B` is private + --> $DIR/issue-17718-const-privacy.rs:5:8 + | +LL | use a::B; + | ^ private constant + | +note: the constant `B` is defined here + --> $DIR/issue-17718-const-privacy.rs:13:5 + | +LL | const B: usize = 3; + | ^^^^^^^^^^^^^^^^^^^ + +error[E0603]: constant `BAR` is private + --> $DIR/issue-17718-const-privacy.rs:8:5 + | +LL | BAR, + | ^^^ private constant + | +note: the constant `BAR` is defined here + --> $DIR/auxiliary/issue-17718-const-privacy.rs:4:1 + | +LL | const BAR: usize = 3; + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/issue-29161.rs b/tests/ui/privacy/issue-29161.rs new file mode 100644 index 000000000..d80405aa8 --- /dev/null +++ b/tests/ui/privacy/issue-29161.rs @@ -0,0 +1,15 @@ +mod a { + struct A; + + impl Default for A { + pub fn default() -> A { //~ ERROR unnecessary visibility qualifier + A + } + } +} + + +fn main() { + a::A::default(); + //~^ ERROR struct `A` is private + } diff --git a/tests/ui/privacy/issue-29161.stderr b/tests/ui/privacy/issue-29161.stderr new file mode 100644 index 000000000..7ce95e4b0 --- /dev/null +++ b/tests/ui/privacy/issue-29161.stderr @@ -0,0 +1,22 @@ +error[E0449]: unnecessary visibility qualifier + --> $DIR/issue-29161.rs:5:9 + | +LL | pub fn default() -> A { + | ^^^ `pub` not permitted here because it's implied + +error[E0603]: struct `A` is private + --> $DIR/issue-29161.rs:13:8 + | +LL | a::A::default(); + | ^ private struct + | +note: the struct `A` is defined here + --> $DIR/issue-29161.rs:2:5 + | +LL | struct A; + | ^^^^^^^^^ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0449, E0603. +For more information about an error, try `rustc --explain E0449`. diff --git a/tests/ui/privacy/issue-30079.rs b/tests/ui/privacy/issue-30079.rs new file mode 100644 index 000000000..a02a932d0 --- /dev/null +++ b/tests/ui/privacy/issue-30079.rs @@ -0,0 +1,39 @@ +struct SemiPriv; + +mod m1 { + struct Priv; + impl ::SemiPriv { + pub fn f(_: Priv) {} //~ WARN private type `m1::Priv` in public interface + //~^ WARNING hard error + } + + impl Priv { + pub fn f(_: Priv) {} // ok + } +} + +mod m2 { + struct Priv; + impl ::std::ops::Deref for ::SemiPriv { + type Target = Priv; //~ ERROR private type `m2::Priv` in public interface + fn deref(&self) -> &Self::Target { unimplemented!() } + } + + impl ::std::ops::Deref for Priv { + type Target = Priv; // ok + fn deref(&self) -> &Self::Target { unimplemented!() } + } +} + +trait SemiPrivTrait { + type Assoc; +} + +mod m3 { + struct Priv; + impl ::SemiPrivTrait for () { + type Assoc = Priv; //~ ERROR private type `m3::Priv` in public interface + } +} + +fn main() {} diff --git a/tests/ui/privacy/issue-30079.stderr b/tests/ui/privacy/issue-30079.stderr new file mode 100644 index 000000000..9179ff339 --- /dev/null +++ b/tests/ui/privacy/issue-30079.stderr @@ -0,0 +1,31 @@ +warning: private type `m1::Priv` in public interface (error E0446) + --> $DIR/issue-30079.rs:6:9 + | +LL | pub fn f(_: Priv) {} + | ^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + = note: `#[warn(private_in_public)]` on by default + +error[E0446]: private type `m2::Priv` in public interface + --> $DIR/issue-30079.rs:18:9 + | +LL | struct Priv; + | ----------- `m2::Priv` declared as private +LL | impl ::std::ops::Deref for ::SemiPriv { +LL | type Target = Priv; + | ^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `m3::Priv` in public interface + --> $DIR/issue-30079.rs:35:9 + | +LL | struct Priv; + | ----------- `m3::Priv` declared as private +LL | impl ::SemiPrivTrait for () { +LL | type Assoc = Priv; + | ^^^^^^^^^^ can't leak private type + +error: aborting due to 2 previous errors; 1 warning emitted + +For more information about this error, try `rustc --explain E0446`. diff --git a/tests/ui/privacy/issue-46209-private-enum-variant-reexport.rs b/tests/ui/privacy/issue-46209-private-enum-variant-reexport.rs new file mode 100644 index 000000000..6f115e78e --- /dev/null +++ b/tests/ui/privacy/issue-46209-private-enum-variant-reexport.rs @@ -0,0 +1,40 @@ +#[deny(unused_imports)] +mod rank { + pub use self::Professor::*; + //~^ ERROR glob import doesn't reexport anything + pub use self::Lieutenant::{JuniorGrade, Full}; + //~^ ERROR `JuniorGrade` is private, and cannot be re-exported + //~| ERROR `Full` is private, and cannot be re-exported + pub use self::PettyOfficer::*; + //~^ ERROR glob import doesn't reexport anything + pub use self::Crewman::*; + //~^ ERROR glob import doesn't reexport anything + + enum Professor { + Adjunct, + Assistant, + Associate, + Full + } + + enum Lieutenant { + JuniorGrade, + Full, + } + + pub(in rank) enum PettyOfficer { + SecondClass, + FirstClass, + Chief, + MasterChief + } + + pub(crate) enum Crewman { + Recruit, + Apprentice, + Full + } + +} + +fn main() {} diff --git a/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr b/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr new file mode 100644 index 000000000..59b181fab --- /dev/null +++ b/tests/ui/privacy/issue-46209-private-enum-variant-reexport.stderr @@ -0,0 +1,51 @@ +error[E0364]: `JuniorGrade` is private, and cannot be re-exported + --> $DIR/issue-46209-private-enum-variant-reexport.rs:5:32 + | +LL | pub use self::Lieutenant::{JuniorGrade, Full}; + | ^^^^^^^^^^^ + | +note: consider marking `JuniorGrade` as `pub` in the imported module + --> $DIR/issue-46209-private-enum-variant-reexport.rs:5:32 + | +LL | pub use self::Lieutenant::{JuniorGrade, Full}; + | ^^^^^^^^^^^ + +error[E0364]: `Full` is private, and cannot be re-exported + --> $DIR/issue-46209-private-enum-variant-reexport.rs:5:45 + | +LL | pub use self::Lieutenant::{JuniorGrade, Full}; + | ^^^^ + | +note: consider marking `Full` as `pub` in the imported module + --> $DIR/issue-46209-private-enum-variant-reexport.rs:5:45 + | +LL | pub use self::Lieutenant::{JuniorGrade, Full}; + | ^^^^ + +error: glob import doesn't reexport anything because no candidate is public enough + --> $DIR/issue-46209-private-enum-variant-reexport.rs:3:13 + | +LL | pub use self::Professor::*; + | ^^^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/issue-46209-private-enum-variant-reexport.rs:1:8 + | +LL | #[deny(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: glob import doesn't reexport anything because no candidate is public enough + --> $DIR/issue-46209-private-enum-variant-reexport.rs:8:13 + | +LL | pub use self::PettyOfficer::*; + | ^^^^^^^^^^^^^^^^^^^^^ + +error: glob import doesn't reexport anything because no candidate is public enough + --> $DIR/issue-46209-private-enum-variant-reexport.rs:10:13 + | +LL | pub use self::Crewman::*; + | ^^^^^^^^^^^^^^^^ + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0364`. diff --git a/tests/ui/privacy/issue-57264-1.rs b/tests/ui/privacy/issue-57264-1.rs new file mode 100644 index 000000000..59ebc4f54 --- /dev/null +++ b/tests/ui/privacy/issue-57264-1.rs @@ -0,0 +1,8 @@ +// check-pass +// aux-build:issue-57264-1.rs + +extern crate issue_57264_1; + +fn main() { + issue_57264_1::Pub::pub_method(); +} diff --git a/tests/ui/privacy/issue-57264-2.rs b/tests/ui/privacy/issue-57264-2.rs new file mode 100644 index 000000000..36ce5fd3b --- /dev/null +++ b/tests/ui/privacy/issue-57264-2.rs @@ -0,0 +1,10 @@ +// check-pass +// aux-build:issue-57264-2.rs + +extern crate issue_57264_2; + +fn infer<T: issue_57264_2::PubTraitWithSingleImplementor>(arg: T) -> T { arg } + +fn main() { + infer(None).unwrap().pub_method(); +} diff --git a/tests/ui/privacy/issue-75062-fieldless-tuple-struct.rs b/tests/ui/privacy/issue-75062-fieldless-tuple-struct.rs new file mode 100644 index 000000000..4601a3d47 --- /dev/null +++ b/tests/ui/privacy/issue-75062-fieldless-tuple-struct.rs @@ -0,0 +1,10 @@ +// Regression test for issue #75062 +// Tests that we don't ICE on a privacy error for a fieldless tuple struct. + +mod foo { + struct Bar(); +} + +fn main() { + foo::Bar(); //~ ERROR tuple struct +} diff --git a/tests/ui/privacy/issue-75062-fieldless-tuple-struct.stderr b/tests/ui/privacy/issue-75062-fieldless-tuple-struct.stderr new file mode 100644 index 000000000..14a12003e --- /dev/null +++ b/tests/ui/privacy/issue-75062-fieldless-tuple-struct.stderr @@ -0,0 +1,15 @@ +error[E0603]: tuple struct `Bar` is private + --> $DIR/issue-75062-fieldless-tuple-struct.rs:9:10 + | +LL | foo::Bar(); + | ^^^ private tuple struct + | +note: the tuple struct `Bar` is defined here + --> $DIR/issue-75062-fieldless-tuple-struct.rs:5:5 + | +LL | struct Bar(); + | ^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/issue-75906.rs b/tests/ui/privacy/issue-75906.rs new file mode 100644 index 000000000..710039d79 --- /dev/null +++ b/tests/ui/privacy/issue-75906.rs @@ -0,0 +1,13 @@ +mod m { + pub struct Foo { x: u8 } + + pub struct Bar(u8); +} + +use m::{Foo, Bar}; + +fn main() { + let x = Foo { x: 12 }; + let y = Bar(12); + //~^ ERROR cannot initialize a tuple struct which contains private fields [E0423] +} diff --git a/tests/ui/privacy/issue-75906.stderr b/tests/ui/privacy/issue-75906.stderr new file mode 100644 index 000000000..600dc7c87 --- /dev/null +++ b/tests/ui/privacy/issue-75906.stderr @@ -0,0 +1,19 @@ +error[E0423]: cannot initialize a tuple struct which contains private fields + --> $DIR/issue-75906.rs:11:13 + | +LL | let y = Bar(12); + | ^^^ + | +note: constructor is not visible here due to private fields + --> $DIR/issue-75906.rs:4:20 + | +LL | pub struct Bar(u8); + | ^^ private field +help: consider making the field publicly accessible + | +LL | pub struct Bar(pub u8); + | +++ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0423`. diff --git a/tests/ui/privacy/issue-75907.rs b/tests/ui/privacy/issue-75907.rs new file mode 100644 index 000000000..3bed841d1 --- /dev/null +++ b/tests/ui/privacy/issue-75907.rs @@ -0,0 +1,18 @@ +// Test for diagnostic improvement issue #75907 + +mod foo { + pub(crate) struct Foo(u8); + pub(crate) struct Bar(pub u8, pub(in crate::foo) u8, Foo); + + pub(crate) fn make_bar() -> Bar { + Bar(1, 12, Foo(10)) + } +} + +use foo::{make_bar, Bar, Foo}; + +fn main() { + let Bar(x, y, Foo(z)) = make_bar(); + //~^ ERROR cannot match against a tuple struct which contains private fields + //~| ERROR cannot match against a tuple struct which contains private fields +} diff --git a/tests/ui/privacy/issue-75907.stderr b/tests/ui/privacy/issue-75907.stderr new file mode 100644 index 000000000..f7cb874c2 --- /dev/null +++ b/tests/ui/privacy/issue-75907.stderr @@ -0,0 +1,37 @@ +error[E0532]: cannot match against a tuple struct which contains private fields + --> $DIR/issue-75907.rs:15:9 + | +LL | let Bar(x, y, Foo(z)) = make_bar(); + | ^^^ + | +note: constructor is not visible here due to private fields + --> $DIR/issue-75907.rs:15:16 + | +LL | let Bar(x, y, Foo(z)) = make_bar(); + | ^ ^^^^^^ private field + | | + | private field +help: consider making the fields publicly accessible + | +LL | pub(crate) struct Bar(pub u8, pub u8, pub Foo); + | ~~~ ~~~ +++ + +error[E0532]: cannot match against a tuple struct which contains private fields + --> $DIR/issue-75907.rs:15:19 + | +LL | let Bar(x, y, Foo(z)) = make_bar(); + | ^^^ + | +note: constructor is not visible here due to private fields + --> $DIR/issue-75907.rs:15:23 + | +LL | let Bar(x, y, Foo(z)) = make_bar(); + | ^ private field +help: consider making the field publicly accessible + | +LL | pub(crate) struct Foo(pub u8); + | +++ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0532`. diff --git a/tests/ui/privacy/issue-75907_b.rs b/tests/ui/privacy/issue-75907_b.rs new file mode 100644 index 000000000..fdfc5907c --- /dev/null +++ b/tests/ui/privacy/issue-75907_b.rs @@ -0,0 +1,14 @@ +// Test for diagnostic improvement issue #75907, extern crate +// aux-build:issue-75907.rs + +extern crate issue_75907 as a; + +use a::{make_bar, Bar, Foo}; + +fn main() { + let Bar(x, y, z) = make_bar(); + //~^ ERROR cannot match against a tuple struct which contains private fields + + let Foo(x, y, z) = Foo::new(); + //~^ ERROR cannot match against a tuple struct which contains private fields +} diff --git a/tests/ui/privacy/issue-75907_b.stderr b/tests/ui/privacy/issue-75907_b.stderr new file mode 100644 index 000000000..b82d08473 --- /dev/null +++ b/tests/ui/privacy/issue-75907_b.stderr @@ -0,0 +1,31 @@ +error[E0532]: cannot match against a tuple struct which contains private fields + --> $DIR/issue-75907_b.rs:9:9 + | +LL | let Bar(x, y, z) = make_bar(); + | ^^^ + | +note: constructor is not visible here due to private fields + --> $DIR/issue-75907_b.rs:9:16 + | +LL | let Bar(x, y, z) = make_bar(); + | ^ ^ private field + | | + | private field + +error[E0532]: cannot match against a tuple struct which contains private fields + --> $DIR/issue-75907_b.rs:12:9 + | +LL | let Foo(x, y, z) = Foo::new(); + | ^^^ + | +note: constructor is not visible here due to private fields + --> $DIR/issue-75907_b.rs:12:13 + | +LL | let Foo(x, y, z) = Foo::new(); + | ^ ^ private field + | | + | private field + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0532`. diff --git a/tests/ui/privacy/issue-79593.rs b/tests/ui/privacy/issue-79593.rs new file mode 100644 index 000000000..39c222f7c --- /dev/null +++ b/tests/ui/privacy/issue-79593.rs @@ -0,0 +1,29 @@ +mod foo { + pub struct Pub { private: () } + + pub enum Enum { + Variant { x: (), y: () }, + Other + } + + fn correct() { + Pub {}; + //~^ ERROR missing field `private` in initializer of `Pub` + Enum::Variant { x: () }; + //~^ ERROR missing field `y` in initializer of `Enum` + } +} + +fn correct() { + foo::Pub {}; + //~^ ERROR cannot construct `Pub` with struct literal syntax due to private fields +} + +fn wrong() { + foo::Enum::Variant { x: () }; + //~^ ERROR missing field `y` in initializer of `Enum` + foo::Enum::Variant { }; + //~^ ERROR missing fields `x` and `y` in initializer of `Enum` +} + +fn main() {} diff --git a/tests/ui/privacy/issue-79593.stderr b/tests/ui/privacy/issue-79593.stderr new file mode 100644 index 000000000..21ba760ad --- /dev/null +++ b/tests/ui/privacy/issue-79593.stderr @@ -0,0 +1,35 @@ +error[E0063]: missing field `private` in initializer of `Pub` + --> $DIR/issue-79593.rs:10:9 + | +LL | Pub {}; + | ^^^ missing `private` + +error[E0063]: missing field `y` in initializer of `Enum` + --> $DIR/issue-79593.rs:12:9 + | +LL | Enum::Variant { x: () }; + | ^^^^^^^^^^^^^ missing `y` + +error: cannot construct `Pub` with struct literal syntax due to private fields + --> $DIR/issue-79593.rs:18:5 + | +LL | foo::Pub {}; + | ^^^^^^^^ + | + = note: ... and other private field `private` that was not provided + +error[E0063]: missing field `y` in initializer of `Enum` + --> $DIR/issue-79593.rs:23:5 + | +LL | foo::Enum::Variant { x: () }; + | ^^^^^^^^^^^^^^^^^^ missing `y` + +error[E0063]: missing fields `x` and `y` in initializer of `Enum` + --> $DIR/issue-79593.rs:25:5 + | +LL | foo::Enum::Variant { }; + | ^^^^^^^^^^^^^^^^^^ missing `x` and `y` + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0063`. diff --git a/tests/ui/privacy/issue-92755.rs b/tests/ui/privacy/issue-92755.rs new file mode 100644 index 000000000..49559152b --- /dev/null +++ b/tests/ui/privacy/issue-92755.rs @@ -0,0 +1,10 @@ +// aux-build:issue-92755.rs +// build-pass + +// Thank you @tmiasko for providing the content of this test! + +extern crate issue_92755; + +fn main() { + issue_92755::ctx().a.b.f(); +} diff --git a/tests/ui/privacy/legacy-ctor-visibility.rs b/tests/ui/privacy/legacy-ctor-visibility.rs new file mode 100644 index 000000000..5732b6446 --- /dev/null +++ b/tests/ui/privacy/legacy-ctor-visibility.rs @@ -0,0 +1,15 @@ +use m::S; + +mod m { + pub struct S(u8); + + mod n { + use S; + fn f() { + S(10); + //~^ ERROR expected function, tuple struct or tuple variant, found struct `S` + } + } +} + +fn main() {} diff --git a/tests/ui/privacy/legacy-ctor-visibility.stderr b/tests/ui/privacy/legacy-ctor-visibility.stderr new file mode 100644 index 000000000..c8057d85e --- /dev/null +++ b/tests/ui/privacy/legacy-ctor-visibility.stderr @@ -0,0 +1,9 @@ +error[E0423]: expected function, tuple struct or tuple variant, found struct `S` + --> $DIR/legacy-ctor-visibility.rs:9:13 + | +LL | S(10); + | ^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0423`. diff --git a/tests/ui/privacy/macro-private-reexport.rs b/tests/ui/privacy/macro-private-reexport.rs new file mode 100644 index 000000000..d0aab528e --- /dev/null +++ b/tests/ui/privacy/macro-private-reexport.rs @@ -0,0 +1,17 @@ +// edition:2021 + +#![feature(decl_macro)] + +mod foo { + macro_rules! bar { + () => {}; + } + + pub use bar as _; //~ ERROR `bar` is only public within the crate, and cannot be re-exported outside + + macro baz() {} + + pub use baz as _; //~ ERROR `baz` is private, and cannot be re-exported +} + +fn main() {} diff --git a/tests/ui/privacy/macro-private-reexport.stderr b/tests/ui/privacy/macro-private-reexport.stderr new file mode 100644 index 000000000..b8768f361 --- /dev/null +++ b/tests/ui/privacy/macro-private-reexport.stderr @@ -0,0 +1,29 @@ +error[E0364]: `bar` is only public within the crate, and cannot be re-exported outside + --> $DIR/macro-private-reexport.rs:10:13 + | +LL | pub use bar as _; + | ^^^^^^^^ + | +help: consider adding a `#[macro_export]` to the macro in the imported module + --> $DIR/macro-private-reexport.rs:6:5 + | +LL | / macro_rules! bar { +LL | | () => {}; +LL | | } + | |_____^ + +error[E0364]: `baz` is private, and cannot be re-exported + --> $DIR/macro-private-reexport.rs:14:13 + | +LL | pub use baz as _; + | ^^^^^^^^ + | +note: consider marking `baz` as `pub` in the imported module + --> $DIR/macro-private-reexport.rs:14:13 + | +LL | pub use baz as _; + | ^^^^^^^^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0364`. diff --git a/tests/ui/privacy/priv-impl-prim-ty.rs b/tests/ui/privacy/priv-impl-prim-ty.rs new file mode 100644 index 000000000..5d6a6b64e --- /dev/null +++ b/tests/ui/privacy/priv-impl-prim-ty.rs @@ -0,0 +1,11 @@ +// run-pass +// aux-build:priv-impl-prim-ty.rs + +// pretty-expanded FIXME #23616 + +extern crate priv_impl_prim_ty as bar; + +pub fn main() { + bar::frob(1); + +} diff --git a/tests/ui/privacy/priv-in-bad-locations.rs b/tests/ui/privacy/priv-in-bad-locations.rs new file mode 100644 index 000000000..76af8c6cd --- /dev/null +++ b/tests/ui/privacy/priv-in-bad-locations.rs @@ -0,0 +1,17 @@ +pub extern "C" { //~ ERROR unnecessary visibility qualifier + pub fn bar(); +} + +trait A { + fn foo(&self) {} +} + +struct B; + +pub impl B {} //~ ERROR unnecessary visibility qualifier + +pub impl A for B { //~ ERROR unnecessary visibility qualifier + pub fn foo(&self) {} //~ ERROR unnecessary visibility qualifier +} + +pub fn main() {} diff --git a/tests/ui/privacy/priv-in-bad-locations.stderr b/tests/ui/privacy/priv-in-bad-locations.stderr new file mode 100644 index 000000000..75bd2fe47 --- /dev/null +++ b/tests/ui/privacy/priv-in-bad-locations.stderr @@ -0,0 +1,31 @@ +error[E0449]: unnecessary visibility qualifier + --> $DIR/priv-in-bad-locations.rs:1:1 + | +LL | pub extern "C" { + | ^^^ `pub` not permitted here because it's implied + | + = note: place qualifiers on individual foreign items instead + +error[E0449]: unnecessary visibility qualifier + --> $DIR/priv-in-bad-locations.rs:11:1 + | +LL | pub impl B {} + | ^^^ `pub` not permitted here because it's implied + | + = note: place qualifiers on individual impl items instead + +error[E0449]: unnecessary visibility qualifier + --> $DIR/priv-in-bad-locations.rs:13:1 + | +LL | pub impl A for B { + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/priv-in-bad-locations.rs:14:5 + | +LL | pub fn foo(&self) {} + | ^^^ `pub` not permitted here because it's implied + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0449`. diff --git a/tests/ui/privacy/privacy-in-paths.rs b/tests/ui/privacy/privacy-in-paths.rs new file mode 100644 index 000000000..9cee2b89d --- /dev/null +++ b/tests/ui/privacy/privacy-in-paths.rs @@ -0,0 +1,30 @@ +mod foo { + pub use self::bar::S; + mod bar { + pub struct S; + pub use baz; + } + + trait T { + type Assoc; + } + impl T for () { + type Assoc = S; + } +} + +impl foo::S { + fn f() {} +} + +pub mod baz { + fn f() {} + + fn g() { + ::foo::bar::baz::f(); //~ERROR module `bar` is private + ::foo::bar::S::f(); //~ERROR module `bar` is private + <() as ::foo::T>::Assoc::f(); //~ERROR trait `T` is private + } +} + +fn main() {} diff --git a/tests/ui/privacy/privacy-in-paths.stderr b/tests/ui/privacy/privacy-in-paths.stderr new file mode 100644 index 000000000..2eb3ebb51 --- /dev/null +++ b/tests/ui/privacy/privacy-in-paths.stderr @@ -0,0 +1,39 @@ +error[E0603]: module `bar` is private + --> $DIR/privacy-in-paths.rs:24:16 + | +LL | ::foo::bar::baz::f(); + | ^^^ private module + | +note: the module `bar` is defined here + --> $DIR/privacy-in-paths.rs:3:5 + | +LL | mod bar { + | ^^^^^^^ + +error[E0603]: module `bar` is private + --> $DIR/privacy-in-paths.rs:25:16 + | +LL | ::foo::bar::S::f(); + | ^^^ private module + | +note: the module `bar` is defined here + --> $DIR/privacy-in-paths.rs:3:5 + | +LL | mod bar { + | ^^^^^^^ + +error[E0603]: trait `T` is private + --> $DIR/privacy-in-paths.rs:26:23 + | +LL | <() as ::foo::T>::Assoc::f(); + | ^ private trait + | +note: the trait `T` is defined here + --> $DIR/privacy-in-paths.rs:8:5 + | +LL | trait T { + | ^^^^^^^ + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/privacy-ns.rs b/tests/ui/privacy/privacy-ns.rs new file mode 100644 index 000000000..c32e3f178 --- /dev/null +++ b/tests/ui/privacy/privacy-ns.rs @@ -0,0 +1,114 @@ +// run-pass +#![allow(non_snake_case)] + + +// Check we do the correct privacy checks when we import a name and there is an +// item with that name in both the value and type namespaces. + +// pretty-expanded FIXME #23616 + +#![allow(dead_code)] +#![allow(unused_imports)] + + +// public type, private value +pub mod foo1 { + pub trait Bar { + fn dummy(&self) { } + } + pub struct Baz; + + fn Bar() { } +} + +fn test_unused1() { + use foo1::*; +} + +fn test_single1() { + use foo1::Bar; + + let _x: Box<dyn Bar>; +} + +fn test_list1() { + use foo1::{Bar,Baz}; + + let _x: Box<dyn Bar>; +} + +fn test_glob1() { + use foo1::*; + + let _x: Box<dyn Bar>; +} + +// private type, public value +pub mod foo2 { + trait Bar { + fn dummy(&self) { } + } + pub struct Baz; + + pub fn Bar() { } +} + +fn test_unused2() { + use foo2::*; +} + +fn test_single2() { + use foo2::Bar; + + Bar(); +} + +fn test_list2() { + use foo2::{Bar,Baz}; + + Bar(); +} + +fn test_glob2() { + use foo2::*; + + Bar(); +} + +// public type, public value +pub mod foo3 { + pub trait Bar { + fn dummy(&self) { } + } + pub struct Baz; + + pub fn Bar() { } +} + +fn test_unused3() { + use foo3::*; +} + +fn test_single3() { + use foo3::Bar; + + Bar(); + let _x: Box<dyn Bar>; +} + +fn test_list3() { + use foo3::{Bar,Baz}; + + Bar(); + let _x: Box<dyn Bar>; +} + +fn test_glob3() { + use foo3::*; + + Bar(); + let _x: Box<dyn Bar>; +} + +fn main() { +} diff --git a/tests/ui/privacy/privacy-ns1.rs b/tests/ui/privacy/privacy-ns1.rs new file mode 100644 index 000000000..1af5b857e --- /dev/null +++ b/tests/ui/privacy/privacy-ns1.rs @@ -0,0 +1,56 @@ +// Check we do the correct privacy checks when we import a name and there is an +// item with that name in both the value and type namespaces. + +#![allow(dead_code)] +#![allow(unused_imports)] + + +// public type, private value +pub mod foo1 { + pub trait Bar { + } + pub struct Baz; + + fn Bar() { } +} + +fn test_glob1() { + use foo1::*; + + Bar(); //~ ERROR expected function, tuple struct or tuple variant, found trait `Bar` +} + +// private type, public value +pub mod foo2 { + trait Bar { + } + pub struct Baz; + + pub fn Bar() { } +} + +fn test_glob2() { + use foo2::*; + + let _x: Box<Bar>; + //~^ ERROR constant provided when a type was expected +} + +// neither public +pub mod foo3 { + trait Bar { + } + pub struct Baz; + + fn Bar() { } +} + +fn test_glob3() { + use foo3::*; + + Bar(); //~ ERROR cannot find function, tuple struct or tuple variant `Bar` in this scope + let _x: Box<Bar>; //~ ERROR cannot find type `Bar` in this scope +} + +fn main() { +} diff --git a/tests/ui/privacy/privacy-ns1.stderr b/tests/ui/privacy/privacy-ns1.stderr new file mode 100644 index 000000000..91bc84e70 --- /dev/null +++ b/tests/ui/privacy/privacy-ns1.stderr @@ -0,0 +1,67 @@ +error[E0423]: expected function, tuple struct or tuple variant, found trait `Bar` + --> $DIR/privacy-ns1.rs:20:5 + | +LL | pub struct Baz; + | --------------- similarly named unit struct `Baz` defined here +... +LL | Bar(); + | ^^^ + | +help: a unit struct with a similar name exists + | +LL | Baz(); + | ~~~ +help: consider importing this function instead + | +LL | use foo2::Bar; + | + +error[E0425]: cannot find function, tuple struct or tuple variant `Bar` in this scope + --> $DIR/privacy-ns1.rs:51:5 + | +LL | pub struct Baz; + | --------------- similarly named unit struct `Baz` defined here +... +LL | Bar(); + | ^^^ + | +help: a unit struct with a similar name exists + | +LL | Baz(); + | ~~~ +help: consider importing this function + | +LL | use foo2::Bar; + | + +error[E0412]: cannot find type `Bar` in this scope + --> $DIR/privacy-ns1.rs:52:17 + | +LL | pub struct Baz; + | --------------- similarly named struct `Baz` defined here +... +LL | let _x: Box<Bar>; + | ^^^ + | +help: a struct with a similar name exists + | +LL | let _x: Box<Baz>; + | ~~~ +help: consider importing this trait + | +LL | use foo1::Bar; + | + +error[E0747]: constant provided when a type was expected + --> $DIR/privacy-ns1.rs:35:17 + | +LL | let _x: Box<Bar>; + | ^^^ + | + = help: `Bar` is a function item, not a type + = help: function item types cannot be named directly + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0412, E0423, E0425, E0747. +For more information about an error, try `rustc --explain E0412`. diff --git a/tests/ui/privacy/privacy-ns2.rs b/tests/ui/privacy/privacy-ns2.rs new file mode 100644 index 000000000..47035ef3a --- /dev/null +++ b/tests/ui/privacy/privacy-ns2.rs @@ -0,0 +1,79 @@ +// Check we do the correct privacy checks when we import a name and there is an +// item with that name in both the value and type namespaces. + +#![allow(dead_code)] +#![allow(unused_imports)] + + +// public type, private value +pub mod foo1 { + pub trait Bar { + } + pub struct Baz; + + fn Bar() { } +} + +fn test_single1() { + use foo1::Bar; + + Bar(); //~ ERROR expected function, tuple struct or tuple variant, found trait `Bar` +} + +fn test_list1() { + use foo1::{Bar,Baz}; + + Bar(); //~ ERROR expected function, tuple struct or tuple variant, found trait `Bar` +} + +// private type, public value +pub mod foo2 { + trait Bar { + } + pub struct Baz; + + pub fn Bar() { } +} + +fn test_single2() { + use foo2::Bar; + + let _x : Box<Bar>; //~ ERROR constant provided when a type was expected + let _x : Bar(); //~ ERROR expected type, found function `Bar` +} + +fn test_list2() { + use foo2::{Bar,Baz}; + + let _x: Box<Bar>; //~ ERROR constant provided when a type was expected +} + +// neither public +pub mod foo3 { + trait Bar { + } + pub struct Baz; + + fn Bar() { } +} + +fn test_unused3() { + use foo3::Bar; //~ ERROR `Bar` is private +} + +fn test_single3() { + use foo3::Bar; //~ ERROR `Bar` is private + + Bar(); + let _x: Box<Bar>; +} + +fn test_list3() { + use foo3::{Bar,Baz}; //~ ERROR `Bar` is private + + Bar(); + let _x: Box<Bar>; +} + +fn main() { +} diff --git a/tests/ui/privacy/privacy-ns2.stderr b/tests/ui/privacy/privacy-ns2.stderr new file mode 100644 index 000000000..904e9013f --- /dev/null +++ b/tests/ui/privacy/privacy-ns2.stderr @@ -0,0 +1,102 @@ +error[E0423]: expected function, tuple struct or tuple variant, found trait `Bar` + --> $DIR/privacy-ns2.rs:20:5 + | +LL | Bar(); + | ^^^ not a function, tuple struct or tuple variant + | +help: consider importing this function instead + | +LL | use foo2::Bar; + | + +error[E0423]: expected function, tuple struct or tuple variant, found trait `Bar` + --> $DIR/privacy-ns2.rs:26:5 + | +LL | pub struct Baz; + | --------------- similarly named unit struct `Baz` defined here +... +LL | Bar(); + | ^^^ + | +help: a unit struct with a similar name exists + | +LL | Baz(); + | ~~~ +help: consider importing this function instead + | +LL | use foo2::Bar; + | + +error[E0573]: expected type, found function `Bar` + --> $DIR/privacy-ns2.rs:42:14 + | +LL | let _x : Bar(); + | ^^^^^ not a type + | +help: use `=` if you meant to assign + | +LL | let _x = Bar(); + | ~ +help: consider importing this trait instead + | +LL | use foo1::Bar; + | + +error[E0603]: trait `Bar` is private + --> $DIR/privacy-ns2.rs:61:15 + | +LL | use foo3::Bar; + | ^^^ private trait + | +note: the trait `Bar` is defined here + --> $DIR/privacy-ns2.rs:53:5 + | +LL | trait Bar { + | ^^^^^^^^^ + +error[E0603]: trait `Bar` is private + --> $DIR/privacy-ns2.rs:65:15 + | +LL | use foo3::Bar; + | ^^^ private trait + | +note: the trait `Bar` is defined here + --> $DIR/privacy-ns2.rs:53:5 + | +LL | trait Bar { + | ^^^^^^^^^ + +error[E0603]: trait `Bar` is private + --> $DIR/privacy-ns2.rs:72:16 + | +LL | use foo3::{Bar,Baz}; + | ^^^ private trait + | +note: the trait `Bar` is defined here + --> $DIR/privacy-ns2.rs:53:5 + | +LL | trait Bar { + | ^^^^^^^^^ + +error[E0747]: constant provided when a type was expected + --> $DIR/privacy-ns2.rs:41:18 + | +LL | let _x : Box<Bar>; + | ^^^ + | + = help: `Bar` is a function item, not a type + = help: function item types cannot be named directly + +error[E0747]: constant provided when a type was expected + --> $DIR/privacy-ns2.rs:48:17 + | +LL | let _x: Box<Bar>; + | ^^^ + | + = help: `Bar` is a function item, not a type + = help: function item types cannot be named directly + +error: aborting due to 8 previous errors + +Some errors have detailed explanations: E0423, E0573, E0603, E0747. +For more information about an error, try `rustc --explain E0423`. diff --git a/tests/ui/privacy/privacy-reexport.rs b/tests/ui/privacy/privacy-reexport.rs new file mode 100644 index 000000000..b3ec3af04 --- /dev/null +++ b/tests/ui/privacy/privacy-reexport.rs @@ -0,0 +1,13 @@ +// run-pass +// aux-build:privacy_reexport.rs + +// pretty-expanded FIXME #23616 + +extern crate privacy_reexport; + +pub fn main() { + // Check that public extern crates are visible to outside crates + privacy_reexport::core::cell::Cell::new(0); + + privacy_reexport::bar::frob(); +} diff --git a/tests/ui/privacy/privacy-sanity.rs b/tests/ui/privacy/privacy-sanity.rs new file mode 100644 index 000000000..8bbf1ab5d --- /dev/null +++ b/tests/ui/privacy/privacy-sanity.rs @@ -0,0 +1,82 @@ +#![feature(negative_impls)] + +pub trait Tr { + fn f(); + const C: u8; + type T; +} +pub struct S { + pub a: u8 +} +struct Ts(pub u8); + +pub impl Tr for S { //~ ERROR unnecessary visibility qualifier + pub fn f() {} //~ ERROR unnecessary visibility qualifier + pub const C: u8 = 0; //~ ERROR unnecessary visibility qualifier + pub type T = u8; //~ ERROR unnecessary visibility qualifier +} +pub impl S { //~ ERROR unnecessary visibility qualifier + pub fn f() {} + pub const C: u8 = 0; + // pub type T = u8; +} +pub extern "C" { //~ ERROR unnecessary visibility qualifier + pub fn f(); + pub static St: u8; +} + +const MAIN: u8 = { + pub trait Tr { + fn f(); + const C: u8; + type T; + } + pub struct S { + pub a: u8 + } + struct Ts(pub u8); + + pub impl Tr for S { //~ ERROR unnecessary visibility qualifier + pub fn f() {} //~ ERROR unnecessary visibility qualifier + pub const C: u8 = 0; //~ ERROR unnecessary visibility qualifier + pub type T = u8; //~ ERROR unnecessary visibility qualifier + } + pub impl S { //~ ERROR unnecessary visibility qualifier + pub fn f() {} + pub const C: u8 = 0; + // pub type T = u8; + } + pub extern "C" { //~ ERROR unnecessary visibility qualifier + pub fn f(); + pub static St: u8; + } + + 0 +}; + +fn main() { + pub trait Tr { + fn f(); + const C: u8; + type T; + } + pub struct S { + pub a: u8 + } + struct Ts(pub u8); + + pub impl Tr for S { //~ ERROR unnecessary visibility qualifier + pub fn f() {} //~ ERROR unnecessary visibility qualifier + pub const C: u8 = 0; //~ ERROR unnecessary visibility qualifier + pub type T = u8; //~ ERROR unnecessary visibility qualifier + } + pub impl S { //~ ERROR unnecessary visibility qualifier + pub fn f() {} + pub const C: u8 = 0; + // pub type T = u8; + } + pub extern "C" { //~ ERROR unnecessary visibility qualifier + pub fn f(); + pub static St: u8; + } +} diff --git a/tests/ui/privacy/privacy-sanity.stderr b/tests/ui/privacy/privacy-sanity.stderr new file mode 100644 index 000000000..c92553fd1 --- /dev/null +++ b/tests/ui/privacy/privacy-sanity.stderr @@ -0,0 +1,123 @@ +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:13:1 + | +LL | pub impl Tr for S { + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:14:5 + | +LL | pub fn f() {} + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:15:5 + | +LL | pub const C: u8 = 0; + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:16:5 + | +LL | pub type T = u8; + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:18:1 + | +LL | pub impl S { + | ^^^ `pub` not permitted here because it's implied + | + = note: place qualifiers on individual impl items instead + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:23:1 + | +LL | pub extern "C" { + | ^^^ `pub` not permitted here because it's implied + | + = note: place qualifiers on individual foreign items instead + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:39:5 + | +LL | pub impl Tr for S { + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:40:9 + | +LL | pub fn f() {} + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:41:9 + | +LL | pub const C: u8 = 0; + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:42:9 + | +LL | pub type T = u8; + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:44:5 + | +LL | pub impl S { + | ^^^ `pub` not permitted here because it's implied + | + = note: place qualifiers on individual impl items instead + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:49:5 + | +LL | pub extern "C" { + | ^^^ `pub` not permitted here because it's implied + | + = note: place qualifiers on individual foreign items instead + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:68:5 + | +LL | pub impl Tr for S { + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:69:9 + | +LL | pub fn f() {} + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:70:9 + | +LL | pub const C: u8 = 0; + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:71:9 + | +LL | pub type T = u8; + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:73:5 + | +LL | pub impl S { + | ^^^ `pub` not permitted here because it's implied + | + = note: place qualifiers on individual impl items instead + +error[E0449]: unnecessary visibility qualifier + --> $DIR/privacy-sanity.rs:78:5 + | +LL | pub extern "C" { + | ^^^ `pub` not permitted here because it's implied + | + = note: place qualifiers on individual foreign items instead + +error: aborting due to 18 previous errors + +For more information about this error, try `rustc --explain E0449`. diff --git a/tests/ui/privacy/privacy-ufcs.rs b/tests/ui/privacy/privacy-ufcs.rs new file mode 100644 index 000000000..fec7f4134 --- /dev/null +++ b/tests/ui/privacy/privacy-ufcs.rs @@ -0,0 +1,13 @@ +// Test to ensure private traits are inaccessible with UFCS angle-bracket syntax. + +mod foo { + trait Bar { + fn baz() {} + } + + impl Bar for i32 {} +} + +fn main() { + <i32 as ::foo::Bar>::baz(); //~ERROR trait `Bar` is private +} diff --git a/tests/ui/privacy/privacy-ufcs.stderr b/tests/ui/privacy/privacy-ufcs.stderr new file mode 100644 index 000000000..e93a458ce --- /dev/null +++ b/tests/ui/privacy/privacy-ufcs.stderr @@ -0,0 +1,15 @@ +error[E0603]: trait `Bar` is private + --> $DIR/privacy-ufcs.rs:12:20 + | +LL | <i32 as ::foo::Bar>::baz(); + | ^^^ private trait + | +note: the trait `Bar` is defined here + --> $DIR/privacy-ufcs.rs:4:5 + | +LL | trait Bar { + | ^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/privacy1-rpass.rs b/tests/ui/privacy/privacy1-rpass.rs new file mode 100644 index 000000000..4e54780da --- /dev/null +++ b/tests/ui/privacy/privacy1-rpass.rs @@ -0,0 +1,23 @@ +// run-pass +#![allow(dead_code)] +// pretty-expanded FIXME #23616 + +pub mod test2 { + // This used to generate an ICE (make sure that default functions are + // parented to their trait to find the first private thing as the trait). + + struct B; + trait A { fn foo(&self) {} } + impl A for B {} + + mod tests { + use super::A; + fn foo() { + let a = super::B; + a.foo(); + } + } +} + + +pub fn main() {} diff --git a/tests/ui/privacy/privacy1.rs b/tests/ui/privacy/privacy1.rs new file mode 100644 index 000000000..3c9fa983d --- /dev/null +++ b/tests/ui/privacy/privacy1.rs @@ -0,0 +1,176 @@ +#![feature(lang_items, start, no_core)] +#![no_core] // makes debugging this test *a lot* easier (during resolve) + +#[lang="sized"] +pub trait Sized {} + +#[lang="copy"] +pub trait Copy {} + +#[lang="deref"] +pub trait Deref { + type Target; +} + +#[lang="receiver"] +pub trait Receiver: Deref {} + +impl<'a, T> Deref for &'a T { + type Target = T; +} + +impl<'a, T> Receiver for &'a T {} + +mod bar { + // shouldn't bring in too much + pub use self::glob::*; + + // can't publicly re-export private items + pub use self::baz::{foo, bar}; + + pub struct A; + impl A { + pub fn foo() {} + fn bar() {} + + pub fn foo2(&self) {} + fn bar2(&self) {} + } + + trait B { + fn foo() -> Self; + } + + impl B for isize { fn foo() -> isize { 3 } } + + pub enum Enum { + Pub + } + + mod baz { + pub struct A; + impl A { + pub fn foo() {} + fn bar() {} + + pub fn foo2(&self) {} + fn bar2(&self) {} + } + + pub fn foo() {} + pub fn bar() {} + } + + extern "C" { + fn epriv(); + pub fn epub(); + } + + fn test() { + self::Enum::Pub; + unsafe { + epriv(); + epub(); + } + self::baz::A; + self::baz::A::foo(); + self::baz::A::bar(); //~ ERROR: associated function `bar` is private + self::baz::A.foo2(); + + // this used to cause an ICE in privacy traversal. + super::gpub(); + } + + mod glob { + pub fn gpub() {} + fn gpriv() {} + } +} + +pub fn gpub() {} + +fn lol() { + bar::A; + bar::A::foo(); + bar::A::bar(); //~ ERROR: associated function `bar` is private + bar::A.foo2(); +} + +mod foo { + fn test() { + ::bar::A::foo(); + ::bar::A::bar(); //~ ERROR: associated function `bar` is private + ::bar::A.foo2(); + ::bar::baz::A::foo(); //~ ERROR: module `baz` is private + ::bar::baz::A::bar(); //~ ERROR: module `baz` is private + //~^ ERROR: associated function `bar` is private + ::bar::baz::A.foo2(); //~ ERROR: module `baz` is private + ::bar::baz::A.bar2(); //~ ERROR: module `baz` is private + //~^ ERROR: associated function `bar2` is private + + let _: isize = + ::bar::B::foo(); //~ ERROR: trait `B` is private + ::lol(); + + ::bar::Enum::Pub; + + unsafe { + ::bar::epriv(); //~ ERROR: function `epriv` is private + ::bar::epub(); + } + + ::bar::foo(); + ::bar::bar(); + + ::bar::gpub(); + + ::bar::baz::foo(); //~ ERROR: module `baz` is private + ::bar::baz::bar(); //~ ERROR: module `baz` is private + } + + fn test2() { + use bar::baz::{foo, bar}; + //~^ ERROR: module `baz` is private + //~| ERROR: module `baz` is private + + foo(); + bar(); + } + + fn test3() { + use bar::baz; + //~^ ERROR: module `baz` is private + } + + fn test4() { + use bar::{foo, bar}; + foo(); + bar(); + } + + fn test5() { + use bar; + bar::foo(); + bar::bar(); + } + + impl ::bar::B for f32 { fn foo() -> f32 { 1.0 } } + //~^ ERROR: trait `B` is private +} + +pub mod mytest { + // Even though the inner `A` struct is a publicly exported item (usable from + // external crates through `foo::foo`, it should not be accessible through + // its definition path (which has the private `i` module). + use self::foo::i::A; //~ ERROR: module `i` is private + + pub mod foo { + pub use self::i::A as foo; + + mod i { + pub struct A; + } + } +} + +#[start] fn main(_: isize, _: *const *const u8) -> isize { 3 } diff --git a/tests/ui/privacy/privacy1.stderr b/tests/ui/privacy/privacy1.stderr new file mode 100644 index 000000000..70e6fcb7a --- /dev/null +++ b/tests/ui/privacy/privacy1.stderr @@ -0,0 +1,205 @@ +error[E0603]: module `baz` is private + --> $DIR/privacy1.rs:132:18 + | +LL | use bar::baz::{foo, bar}; + | ^^^ private module + | +note: the module `baz` is defined here + --> $DIR/privacy1.rs:50:5 + | +LL | mod baz { + | ^^^^^^^ + +error[E0603]: module `baz` is private + --> $DIR/privacy1.rs:132:18 + | +LL | use bar::baz::{foo, bar}; + | ^^^ private module + | +note: the module `baz` is defined here + --> $DIR/privacy1.rs:50:5 + | +LL | mod baz { + | ^^^^^^^ + +error[E0603]: module `baz` is private + --> $DIR/privacy1.rs:141:18 + | +LL | use bar::baz; + | ^^^ private module + | +note: the module `baz` is defined here + --> $DIR/privacy1.rs:50:5 + | +LL | mod baz { + | ^^^^^^^ + +error[E0603]: module `i` is private + --> $DIR/privacy1.rs:165:20 + | +LL | use self::foo::i::A; + | ^ private module + | +note: the module `i` is defined here + --> $DIR/privacy1.rs:170:9 + | +LL | mod i { + | ^^^^^ + +error[E0603]: module `baz` is private + --> $DIR/privacy1.rs:104:16 + | +LL | ::bar::baz::A::foo(); + | ^^^ private module + | +note: the module `baz` is defined here + --> $DIR/privacy1.rs:50:5 + | +LL | mod baz { + | ^^^^^^^ + +error[E0603]: module `baz` is private + --> $DIR/privacy1.rs:105:16 + | +LL | ::bar::baz::A::bar(); + | ^^^ private module + | +note: the module `baz` is defined here + --> $DIR/privacy1.rs:50:5 + | +LL | mod baz { + | ^^^^^^^ + +error[E0603]: module `baz` is private + --> $DIR/privacy1.rs:107:16 + | +LL | ::bar::baz::A.foo2(); + | ^^^ private module + | +note: the module `baz` is defined here + --> $DIR/privacy1.rs:50:5 + | +LL | mod baz { + | ^^^^^^^ + +error[E0603]: module `baz` is private + --> $DIR/privacy1.rs:108:16 + | +LL | ::bar::baz::A.bar2(); + | ^^^ private module + | +note: the module `baz` is defined here + --> $DIR/privacy1.rs:50:5 + | +LL | mod baz { + | ^^^^^^^ + +error[E0603]: trait `B` is private + --> $DIR/privacy1.rs:112:16 + | +LL | ::bar::B::foo(); + | ^ private trait + | +note: the trait `B` is defined here + --> $DIR/privacy1.rs:40:5 + | +LL | trait B { + | ^^^^^^^ + +error[E0603]: function `epriv` is private + --> $DIR/privacy1.rs:118:20 + | +LL | ::bar::epriv(); + | ^^^^^ private function + | +note: the function `epriv` is defined here + --> $DIR/privacy1.rs:65:9 + | +LL | fn epriv(); + | ^^^^^^^^^^^ + +error[E0603]: module `baz` is private + --> $DIR/privacy1.rs:127:16 + | +LL | ::bar::baz::foo(); + | ^^^ private module + | +note: the module `baz` is defined here + --> $DIR/privacy1.rs:50:5 + | +LL | mod baz { + | ^^^^^^^ + +error[E0603]: module `baz` is private + --> $DIR/privacy1.rs:128:16 + | +LL | ::bar::baz::bar(); + | ^^^ private module + | +note: the module `baz` is defined here + --> $DIR/privacy1.rs:50:5 + | +LL | mod baz { + | ^^^^^^^ + +error[E0603]: trait `B` is private + --> $DIR/privacy1.rs:157:17 + | +LL | impl ::bar::B for f32 { fn foo() -> f32 { 1.0 } } + | ^ private trait + | +note: the trait `B` is defined here + --> $DIR/privacy1.rs:40:5 + | +LL | trait B { + | ^^^^^^^ + +error[E0624]: associated function `bar` is private + --> $DIR/privacy1.rs:77:23 + | +LL | fn bar() {} + | -------- private associated function defined here +... +LL | self::baz::A::bar(); + | ^^^ private associated function + +error[E0624]: associated function `bar` is private + --> $DIR/privacy1.rs:95:13 + | +LL | fn bar() {} + | -------- private associated function defined here +... +LL | bar::A::bar(); + | ^^^ private associated function + +error[E0624]: associated function `bar` is private + --> $DIR/privacy1.rs:102:19 + | +LL | fn bar() {} + | -------- private associated function defined here +... +LL | ::bar::A::bar(); + | ^^^ private associated function + +error[E0624]: associated function `bar` is private + --> $DIR/privacy1.rs:105:24 + | +LL | fn bar() {} + | -------- private associated function defined here +... +LL | ::bar::baz::A::bar(); + | ^^^ private associated function + +error[E0624]: associated function `bar2` is private + --> $DIR/privacy1.rs:108:23 + | +LL | fn bar2(&self) {} + | -------------- private associated function defined here +... +LL | ::bar::baz::A.bar2(); + | ^^^^ private associated function + +error: aborting due to 18 previous errors + +Some errors have detailed explanations: E0603, E0624. +For more information about an error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/privacy2.rs b/tests/ui/privacy/privacy2.rs new file mode 100644 index 000000000..c8fa436bd --- /dev/null +++ b/tests/ui/privacy/privacy2.rs @@ -0,0 +1,27 @@ +#![feature(start, no_core)] +#![no_core] // makes debugging this test *a lot* easier (during resolve) + +// Test to make sure that globs don't leak in regular `use` statements. + +mod bar { + pub use self::glob::*; + + pub mod glob { + use foo; + } +} + +pub fn foo() {} + +fn test1() { + use bar::foo; + //~^ ERROR unresolved import `bar::foo` [E0432] + //~| no `foo` in `bar` +} + +fn test2() { + use bar::glob::foo; + //~^ ERROR `foo` is private +} + +#[start] fn main(_: isize, _: *const *const u8) -> isize { 3 } diff --git a/tests/ui/privacy/privacy2.stderr b/tests/ui/privacy/privacy2.stderr new file mode 100644 index 000000000..882f31465 --- /dev/null +++ b/tests/ui/privacy/privacy2.stderr @@ -0,0 +1,35 @@ +error[E0432]: unresolved import `bar::foo` + --> $DIR/privacy2.rs:17:9 + | +LL | use bar::foo; + | ^^^^^^^^ no `foo` in `bar` + +error[E0603]: function import `foo` is private + --> $DIR/privacy2.rs:23:20 + | +LL | use bar::glob::foo; + | ^^^ private function import + | +note: the function import `foo` is defined here... + --> $DIR/privacy2.rs:10:13 + | +LL | use foo; + | ^^^ +note: ...and refers to the function `foo` which is defined here + --> $DIR/privacy2.rs:14:1 + | +LL | pub fn foo() {} + | ^^^^^^^^^^^^ consider importing it directly + +error: requires `sized` lang_item + +error: requires `sized` lang_item + +error: requires `sized` lang_item + +error: requires `sized` lang_item + +error: aborting due to 6 previous errors + +Some errors have detailed explanations: E0432, E0603. +For more information about an error, try `rustc --explain E0432`. diff --git a/tests/ui/privacy/privacy3.rs b/tests/ui/privacy/privacy3.rs new file mode 100644 index 000000000..5a7cd76a9 --- /dev/null +++ b/tests/ui/privacy/privacy3.rs @@ -0,0 +1,27 @@ +#![feature(start, no_core)] +#![no_core] // makes debugging this test *a lot* easier (during resolve) + +// Test to make sure that private items imported through globs remain private +// when they're used. + +mod bar { + pub use self::glob::*; + + mod glob { + fn gpriv() {} + } +} + +pub fn foo() {} + +fn test1() { + use bar::gpriv; + //~^ ERROR unresolved import `bar::gpriv` [E0432] + //~| no `gpriv` in `bar` + + // This should pass because the compiler will insert a fake name binding + // for `gpriv` + gpriv(); +} + +#[start] fn main(_: isize, _: *const *const u8) -> isize { 3 } diff --git a/tests/ui/privacy/privacy3.stderr b/tests/ui/privacy/privacy3.stderr new file mode 100644 index 000000000..42ce456d9 --- /dev/null +++ b/tests/ui/privacy/privacy3.stderr @@ -0,0 +1,17 @@ +error[E0432]: unresolved import `bar::gpriv` + --> $DIR/privacy3.rs:18:9 + | +LL | use bar::gpriv; + | ^^^^^^^^^^ no `gpriv` in `bar` + +error: requires `sized` lang_item + +error: requires `sized` lang_item + +error: requires `sized` lang_item + +error: requires `sized` lang_item + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0432`. diff --git a/tests/ui/privacy/privacy4.rs b/tests/ui/privacy/privacy4.rs new file mode 100644 index 000000000..fa257b800 --- /dev/null +++ b/tests/ui/privacy/privacy4.rs @@ -0,0 +1,25 @@ +#![feature(lang_items, start, no_core)] +#![no_core] // makes debugging this test *a lot* easier (during resolve) + +#[lang = "sized"] pub trait Sized {} +#[lang="copy"] pub trait Copy {} + +// Test to make sure that private items imported through globs remain private +// when they're used. + +mod bar { + pub use self::glob::*; + + mod glob { + fn gpriv() {} + } +} + +pub fn foo() {} + +fn test2() { + use bar::glob::gpriv; //~ ERROR: module `glob` is private + gpriv(); +} + +#[start] fn main(_: isize, _: *const *const u8) -> isize { 3 } diff --git a/tests/ui/privacy/privacy4.stderr b/tests/ui/privacy/privacy4.stderr new file mode 100644 index 000000000..7552fa71a --- /dev/null +++ b/tests/ui/privacy/privacy4.stderr @@ -0,0 +1,15 @@ +error[E0603]: module `glob` is private + --> $DIR/privacy4.rs:21:14 + | +LL | use bar::glob::gpriv; + | ^^^^ private module + | +note: the module `glob` is defined here + --> $DIR/privacy4.rs:13:5 + | +LL | mod glob { + | ^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/privacy5.rs b/tests/ui/privacy/privacy5.rs new file mode 100644 index 000000000..3dc26b195 --- /dev/null +++ b/tests/ui/privacy/privacy5.rs @@ -0,0 +1,128 @@ +// aux-build:privacy_tuple_struct.rs + +extern crate privacy_tuple_struct as other; + +mod a { + pub struct A(()); + pub struct B(isize); + pub struct C(pub isize, isize); + pub struct D(pub isize); + + fn test() { + let a = A(()); + let b = B(2); + let c = C(2, 3); + let d = D(4); + + let A(()) = a; + let A(_) = a; + match a { A(()) => {} } + match a { A(_) => {} } + + let B(_) = b; + let B(_b) = b; + match b { B(_) => {} } + match b { B(_b) => {} } + match b { B(1) => {} B(_) => {} } + + let C(_, _) = c; + let C(_a, _) = c; + let C(_, _b) = c; + let C(_a, _b) = c; + match c { C(_, _) => {} } + match c { C(_a, _) => {} } + match c { C(_, _b) => {} } + match c { C(_a, _b) => {} } + + let D(_) = d; + let D(_d) = d; + match d { D(_) => {} } + match d { D(_d) => {} } + match d { D(1) => {} D(_) => {} } + + let a2 = A; + let b2 = B; + let c2 = C; + let d2 = D; + } +} + +fn this_crate() { + let a = a::A(()); //~ ERROR tuple struct constructor `A` is private + let b = a::B(2); //~ ERROR tuple struct constructor `B` is private + let c = a::C(2, 3); //~ ERROR tuple struct constructor `C` is private + let d = a::D(4); + + let a::A(()) = a; //~ ERROR tuple struct constructor `A` is private + let a::A(_) = a; //~ ERROR tuple struct constructor `A` is private + match a { a::A(()) => {} } //~ ERROR tuple struct constructor `A` is private + match a { a::A(_) => {} } //~ ERROR tuple struct constructor `A` is private + + let a::B(_) = b; //~ ERROR tuple struct constructor `B` is private + let a::B(_b) = b; //~ ERROR tuple struct constructor `B` is private + match b { a::B(_) => {} } //~ ERROR tuple struct constructor `B` is private + match b { a::B(_b) => {} } //~ ERROR tuple struct constructor `B` is private + match b { a::B(1) => {} a::B(_) => {} } //~ ERROR tuple struct constructor `B` is private + //~^ ERROR tuple struct constructor `B` is private + + let a::C(_, _) = c; //~ ERROR tuple struct constructor `C` is private + let a::C(_a, _) = c; //~ ERROR tuple struct constructor `C` is private + let a::C(_, _b) = c; //~ ERROR tuple struct constructor `C` is private + let a::C(_a, _b) = c; //~ ERROR tuple struct constructor `C` is private + match c { a::C(_, _) => {} } //~ ERROR tuple struct constructor `C` is private + match c { a::C(_a, _) => {} } //~ ERROR tuple struct constructor `C` is private + match c { a::C(_, _b) => {} } //~ ERROR tuple struct constructor `C` is private + match c { a::C(_a, _b) => {} } //~ ERROR tuple struct constructor `C` is private + + let a::D(_) = d; + let a::D(_d) = d; + match d { a::D(_) => {} } + match d { a::D(_d) => {} } + match d { a::D(1) => {} a::D(_) => {} } + + let a2 = a::A; //~ ERROR tuple struct constructor `A` is private + let b2 = a::B; //~ ERROR tuple struct constructor `B` is private + let c2 = a::C; //~ ERROR tuple struct constructor `C` is private + let d2 = a::D; +} + +fn xcrate() { + let a = other::A(()); //~ ERROR tuple struct constructor `A` is private + let b = other::B(2); //~ ERROR tuple struct constructor `B` is private + let c = other::C(2, 3); //~ ERROR tuple struct constructor `C` is private + let d = other::D(4); + + let other::A(()) = a; //~ ERROR tuple struct constructor `A` is private + let other::A(_) = a; //~ ERROR tuple struct constructor `A` is private + match a { other::A(()) => {} } //~ ERROR tuple struct constructor `A` is private + match a { other::A(_) => {} } //~ ERROR tuple struct constructor `A` is private + + let other::B(_) = b; //~ ERROR tuple struct constructor `B` is private + let other::B(_b) = b; //~ ERROR tuple struct constructor `B` is private + match b { other::B(_) => {} } //~ ERROR tuple struct constructor `B` is private + match b { other::B(_b) => {} } //~ ERROR tuple struct constructor `B` is private + match b { other::B(1) => {}//~ ERROR tuple struct constructor `B` is private + other::B(_) => {} } //~ ERROR tuple struct constructor `B` is private + + let other::C(_, _) = c; //~ ERROR tuple struct constructor `C` is private + let other::C(_a, _) = c; //~ ERROR tuple struct constructor `C` is private + let other::C(_, _b) = c; //~ ERROR tuple struct constructor `C` is private + let other::C(_a, _b) = c; //~ ERROR tuple struct constructor `C` is private + match c { other::C(_, _) => {} } //~ ERROR tuple struct constructor `C` is private + match c { other::C(_a, _) => {} } //~ ERROR tuple struct constructor `C` is private + match c { other::C(_, _b) => {} } //~ ERROR tuple struct constructor `C` is private + match c { other::C(_a, _b) => {} } //~ ERROR tuple struct constructor `C` is private + + let other::D(_) = d; + let other::D(_d) = d; + match d { other::D(_) => {} } + match d { other::D(_d) => {} } + match d { other::D(1) => {} other::D(_) => {} } + + let a2 = other::A; //~ ERROR tuple struct constructor `A` is private + let b2 = other::B; //~ ERROR tuple struct constructor `B` is private + let c2 = other::C; //~ ERROR tuple struct constructor `C` is private + let d2 = other::D; +} + +fn main() {} diff --git a/tests/ui/privacy/privacy5.stderr b/tests/ui/privacy/privacy5.stderr new file mode 100644 index 000000000..615b0af27 --- /dev/null +++ b/tests/ui/privacy/privacy5.stderr @@ -0,0 +1,867 @@ +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:51:16 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private +... +LL | let a = a::A(()); + | ^ private tuple struct constructor + | +note: the tuple struct constructor `A` is defined here + --> $DIR/privacy5.rs:6:5 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct A(pub ()); + | +++ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:52:16 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private +... +LL | let b = a::B(2); + | ^ private tuple struct constructor + | +note: the tuple struct constructor `B` is defined here + --> $DIR/privacy5.rs:7:5 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct B(pub isize); + | +++ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:53:16 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private +... +LL | let c = a::C(2, 3); + | ^ private tuple struct constructor + | +note: the tuple struct constructor `C` is defined here + --> $DIR/privacy5.rs:8:5 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the fields publicly accessible + | +LL | pub struct C(pub isize, pub isize); + | ~~~ +++ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:56:12 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private +... +LL | let a::A(()) = a; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `A` is defined here + --> $DIR/privacy5.rs:6:5 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct A(pub ()); + | +++ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:57:12 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private +... +LL | let a::A(_) = a; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `A` is defined here + --> $DIR/privacy5.rs:6:5 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct A(pub ()); + | +++ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:58:18 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private +... +LL | match a { a::A(()) => {} } + | ^ private tuple struct constructor + | +note: the tuple struct constructor `A` is defined here + --> $DIR/privacy5.rs:6:5 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct A(pub ()); + | +++ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:59:18 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private +... +LL | match a { a::A(_) => {} } + | ^ private tuple struct constructor + | +note: the tuple struct constructor `A` is defined here + --> $DIR/privacy5.rs:6:5 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct A(pub ()); + | +++ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:61:12 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private +... +LL | let a::B(_) = b; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `B` is defined here + --> $DIR/privacy5.rs:7:5 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct B(pub isize); + | +++ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:62:12 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private +... +LL | let a::B(_b) = b; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `B` is defined here + --> $DIR/privacy5.rs:7:5 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct B(pub isize); + | +++ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:63:18 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private +... +LL | match b { a::B(_) => {} } + | ^ private tuple struct constructor + | +note: the tuple struct constructor `B` is defined here + --> $DIR/privacy5.rs:7:5 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct B(pub isize); + | +++ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:64:18 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private +... +LL | match b { a::B(_b) => {} } + | ^ private tuple struct constructor + | +note: the tuple struct constructor `B` is defined here + --> $DIR/privacy5.rs:7:5 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct B(pub isize); + | +++ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:65:18 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private +... +LL | match b { a::B(1) => {} a::B(_) => {} } + | ^ private tuple struct constructor + | +note: the tuple struct constructor `B` is defined here + --> $DIR/privacy5.rs:7:5 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct B(pub isize); + | +++ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:65:32 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private +... +LL | match b { a::B(1) => {} a::B(_) => {} } + | ^ private tuple struct constructor + | +note: the tuple struct constructor `B` is defined here + --> $DIR/privacy5.rs:7:5 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct B(pub isize); + | +++ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:68:12 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private +... +LL | let a::C(_, _) = c; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `C` is defined here + --> $DIR/privacy5.rs:8:5 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the fields publicly accessible + | +LL | pub struct C(pub isize, pub isize); + | ~~~ +++ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:69:12 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private +... +LL | let a::C(_a, _) = c; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `C` is defined here + --> $DIR/privacy5.rs:8:5 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the fields publicly accessible + | +LL | pub struct C(pub isize, pub isize); + | ~~~ +++ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:70:12 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private +... +LL | let a::C(_, _b) = c; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `C` is defined here + --> $DIR/privacy5.rs:8:5 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the fields publicly accessible + | +LL | pub struct C(pub isize, pub isize); + | ~~~ +++ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:71:12 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private +... +LL | let a::C(_a, _b) = c; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `C` is defined here + --> $DIR/privacy5.rs:8:5 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the fields publicly accessible + | +LL | pub struct C(pub isize, pub isize); + | ~~~ +++ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:72:18 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private +... +LL | match c { a::C(_, _) => {} } + | ^ private tuple struct constructor + | +note: the tuple struct constructor `C` is defined here + --> $DIR/privacy5.rs:8:5 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the fields publicly accessible + | +LL | pub struct C(pub isize, pub isize); + | ~~~ +++ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:73:18 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private +... +LL | match c { a::C(_a, _) => {} } + | ^ private tuple struct constructor + | +note: the tuple struct constructor `C` is defined here + --> $DIR/privacy5.rs:8:5 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the fields publicly accessible + | +LL | pub struct C(pub isize, pub isize); + | ~~~ +++ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:74:18 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private +... +LL | match c { a::C(_, _b) => {} } + | ^ private tuple struct constructor + | +note: the tuple struct constructor `C` is defined here + --> $DIR/privacy5.rs:8:5 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the fields publicly accessible + | +LL | pub struct C(pub isize, pub isize); + | ~~~ +++ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:75:18 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private +... +LL | match c { a::C(_a, _b) => {} } + | ^ private tuple struct constructor + | +note: the tuple struct constructor `C` is defined here + --> $DIR/privacy5.rs:8:5 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the fields publicly accessible + | +LL | pub struct C(pub isize, pub isize); + | ~~~ +++ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:83:17 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private +... +LL | let a2 = a::A; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `A` is defined here + --> $DIR/privacy5.rs:6:5 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct A(pub ()); + | +++ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:84:17 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private +... +LL | let b2 = a::B; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `B` is defined here + --> $DIR/privacy5.rs:7:5 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct B(pub isize); + | +++ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:85:17 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private +... +LL | let c2 = a::C; + | ^ private tuple struct constructor + | +note: the tuple struct constructor `C` is defined here + --> $DIR/privacy5.rs:8:5 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the fields publicly accessible + | +LL | pub struct C(pub isize, pub isize); + | ~~~ +++ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:90:20 + | +LL | let a = other::A(()); + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:1:14 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `A` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:1:1 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:91:20 + | +LL | let b = other::B(2); + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:2:14 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `B` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:2:1 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:92:20 + | +LL | let c = other::C(2, 3); + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:3:14 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `C` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:3:1 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:95:16 + | +LL | let other::A(()) = a; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:1:14 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `A` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:1:1 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:96:16 + | +LL | let other::A(_) = a; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:1:14 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `A` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:1:1 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:97:22 + | +LL | match a { other::A(()) => {} } + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:1:14 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `A` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:1:1 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:98:22 + | +LL | match a { other::A(_) => {} } + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:1:14 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `A` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:1:1 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:100:16 + | +LL | let other::B(_) = b; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:2:14 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `B` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:2:1 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:101:16 + | +LL | let other::B(_b) = b; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:2:14 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `B` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:2:1 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:102:22 + | +LL | match b { other::B(_) => {} } + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:2:14 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `B` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:2:1 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:103:22 + | +LL | match b { other::B(_b) => {} } + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:2:14 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `B` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:2:1 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:104:22 + | +LL | match b { other::B(1) => {} + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:2:14 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `B` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:2:1 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:105:16 + | +LL | other::B(_) => {} } + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:2:14 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `B` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:2:1 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:107:16 + | +LL | let other::C(_, _) = c; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:3:14 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `C` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:3:1 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:108:16 + | +LL | let other::C(_a, _) = c; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:3:14 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `C` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:3:1 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:109:16 + | +LL | let other::C(_, _b) = c; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:3:14 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `C` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:3:1 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:110:16 + | +LL | let other::C(_a, _b) = c; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:3:14 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `C` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:3:1 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:111:22 + | +LL | match c { other::C(_, _) => {} } + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:3:14 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `C` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:3:1 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:112:22 + | +LL | match c { other::C(_a, _) => {} } + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:3:14 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `C` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:3:1 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:113:22 + | +LL | match c { other::C(_, _b) => {} } + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:3:14 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `C` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:3:1 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:114:22 + | +LL | match c { other::C(_a, _b) => {} } + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:3:14 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `C` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:3:1 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `A` is private + --> $DIR/privacy5.rs:122:21 + | +LL | let a2 = other::A; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:1:14 + | +LL | pub struct A(()); + | -- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `A` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:1:1 + | +LL | pub struct A(()); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `B` is private + --> $DIR/privacy5.rs:123:21 + | +LL | let b2 = other::B; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:2:14 + | +LL | pub struct B(isize); + | ----- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `B` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:2:1 + | +LL | pub struct B(isize); + | ^^^^^^^^^^^^ + +error[E0603]: tuple struct constructor `C` is private + --> $DIR/privacy5.rs:124:21 + | +LL | let c2 = other::C; + | ^ private tuple struct constructor + | + ::: $DIR/auxiliary/privacy_tuple_struct.rs:3:14 + | +LL | pub struct C(pub isize, isize); + | ---------------- a constructor is private if any of the fields is private + | +note: the tuple struct constructor `C` is defined here + --> $DIR/auxiliary/privacy_tuple_struct.rs:3:1 + | +LL | pub struct C(pub isize, isize); + | ^^^^^^^^^^^^ + +error: aborting due to 48 previous errors + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/private-class-field.rs b/tests/ui/privacy/private-class-field.rs new file mode 100644 index 000000000..98e32ee07 --- /dev/null +++ b/tests/ui/privacy/private-class-field.rs @@ -0,0 +1,26 @@ +// run-pass +#![allow(dead_code)] +#![allow(non_camel_case_types)] + + +struct cat { + meows : usize, + + how_hungry : isize, +} + +impl cat { + pub fn meow_count(&mut self) -> usize { self.meows } +} + +fn cat(in_x : usize, in_y : isize) -> cat { + cat { + meows: in_x, + how_hungry: in_y + } +} + +pub fn main() { + let mut nyan : cat = cat(52, 99); + assert_eq!(nyan.meow_count(), 52); +} diff --git a/tests/ui/privacy/private-field-ty-err.rs b/tests/ui/privacy/private-field-ty-err.rs new file mode 100644 index 000000000..10db60695 --- /dev/null +++ b/tests/ui/privacy/private-field-ty-err.rs @@ -0,0 +1,20 @@ +fn main() { + let x = foo::Foo::default(); + if x.len { + //~^ ERROR field `len` of struct `Foo` is private + println!("foo"); + } +} + +mod foo { + #[derive(Default)] + pub struct Foo { + len: String, + } + + impl Foo { + pub fn len(&self) -> usize { + 42 + } + } +} diff --git a/tests/ui/privacy/private-field-ty-err.stderr b/tests/ui/privacy/private-field-ty-err.stderr new file mode 100644 index 000000000..98ba7856e --- /dev/null +++ b/tests/ui/privacy/private-field-ty-err.stderr @@ -0,0 +1,9 @@ +error[E0616]: field `len` of struct `Foo` is private + --> $DIR/private-field-ty-err.rs:3:10 + | +LL | if x.len { + | ^^^ private field + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0616`. diff --git a/tests/ui/privacy/private-impl-method.rs b/tests/ui/privacy/private-impl-method.rs new file mode 100644 index 000000000..f7be6726c --- /dev/null +++ b/tests/ui/privacy/private-impl-method.rs @@ -0,0 +1,21 @@ +mod a { + pub struct Foo { + pub x: isize + } + + impl Foo { + fn foo(&self) {} + } +} + +fn f() { + impl a::Foo { + fn bar(&self) {} // This should be visible outside `f` + } +} + +fn main() { + let s = a::Foo { x: 1 }; + s.bar(); + s.foo(); //~ ERROR associated function `foo` is private +} diff --git a/tests/ui/privacy/private-impl-method.stderr b/tests/ui/privacy/private-impl-method.stderr new file mode 100644 index 000000000..bb54dce7e --- /dev/null +++ b/tests/ui/privacy/private-impl-method.stderr @@ -0,0 +1,12 @@ +error[E0624]: associated function `foo` is private + --> $DIR/private-impl-method.rs:20:7 + | +LL | fn foo(&self) {} + | ------------- private associated function defined here +... +LL | s.foo(); + | ^^^ private associated function + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0624`. diff --git a/tests/ui/privacy/private-in-public-assoc-ty.rs b/tests/ui/privacy/private-in-public-assoc-ty.rs new file mode 100644 index 000000000..fba72c131 --- /dev/null +++ b/tests/ui/privacy/private-in-public-assoc-ty.rs @@ -0,0 +1,52 @@ +// Private types and traits are not allowed in interfaces of associated types. +// This test also ensures that the checks are performed even inside private modules. + +#![feature(associated_type_defaults)] +#![feature(type_alias_impl_trait)] + +mod m { + struct Priv; + trait PrivTr {} + impl PrivTr for Priv {} + pub trait PubTrAux1<T> {} + pub trait PubTrAux2 { + type A; + } + impl<T> PubTrAux1<T> for u8 {} + impl PubTrAux2 for u8 { + type A = Priv; + //~^ ERROR private type `Priv` in public interface + } + + // "Private-in-public in associated types is hard error" in RFC 2145 + // applies only to the aliased types, not bounds. + pub trait PubTr { + type Alias1: PrivTr; + //~^ WARN private trait `PrivTr` in public interface + //~| WARN this was previously accepted + type Alias2: PubTrAux1<Priv> = u8; + //~^ WARN private type `Priv` in public interface + //~| WARN this was previously accepted + type Alias3: PubTrAux2<A = Priv> = u8; + //~^ WARN private type `Priv` in public interface + //~| WARN this was previously accepted + + type Alias4 = Priv; + //~^ ERROR private type `Priv` in public interface + + type Exist; + fn infer_exist() -> Self::Exist; + } + impl PubTr for u8 { + type Alias1 = Priv; + //~^ ERROR private type `Priv` in public interface + + type Exist = impl PrivTr; + //~^ ERROR private trait `PrivTr` in public interface + fn infer_exist() -> Self::Exist { + Priv + } + } +} + +fn main() {} diff --git a/tests/ui/privacy/private-in-public-assoc-ty.stderr b/tests/ui/privacy/private-in-public-assoc-ty.stderr new file mode 100644 index 000000000..a59027d81 --- /dev/null +++ b/tests/ui/privacy/private-in-public-assoc-ty.stderr @@ -0,0 +1,68 @@ +error[E0446]: private type `Priv` in public interface + --> $DIR/private-in-public-assoc-ty.rs:17:9 + | +LL | struct Priv; + | ----------- `Priv` declared as private +... +LL | type A = Priv; + | ^^^^^^ can't leak private type + +warning: private trait `PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-assoc-ty.rs:24:9 + | +LL | type Alias1: PrivTr; + | ^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + = note: `#[warn(private_in_public)]` on by default + +warning: private type `Priv` in public interface (error E0446) + --> $DIR/private-in-public-assoc-ty.rs:27:9 + | +LL | type Alias2: PubTrAux1<Priv> = u8; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +warning: private type `Priv` in public interface (error E0446) + --> $DIR/private-in-public-assoc-ty.rs:30:9 + | +LL | type Alias3: PubTrAux2<A = Priv> = u8; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error[E0446]: private type `Priv` in public interface + --> $DIR/private-in-public-assoc-ty.rs:34:9 + | +LL | struct Priv; + | ----------- `Priv` declared as private +... +LL | type Alias4 = Priv; + | ^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `Priv` in public interface + --> $DIR/private-in-public-assoc-ty.rs:41:9 + | +LL | struct Priv; + | ----------- `Priv` declared as private +... +LL | type Alias1 = Priv; + | ^^^^^^^^^^^ can't leak private type + +error[E0445]: private trait `PrivTr` in public interface + --> $DIR/private-in-public-assoc-ty.rs:44:9 + | +LL | trait PrivTr {} + | ------------ `PrivTr` declared as private +... +LL | type Exist = impl PrivTr; + | ^^^^^^^^^^ can't leak private trait + +error: aborting due to 4 previous errors; 3 warnings emitted + +Some errors have detailed explanations: E0445, E0446. +For more information about an error, try `rustc --explain E0445`. diff --git a/tests/ui/privacy/private-in-public-expr-pat.rs b/tests/ui/privacy/private-in-public-expr-pat.rs new file mode 100644 index 000000000..5c9ecd13b --- /dev/null +++ b/tests/ui/privacy/private-in-public-expr-pat.rs @@ -0,0 +1,13 @@ +// Patterns and expressions are not interface parts and don't produce private-in-public errors. + +// build-pass (FIXME(62277): could be check-pass?) + +struct Priv1(usize); +struct Priv2; + +pub struct Pub(Priv2); + +pub fn public_expr(_: [u8; Priv1(0).0]) {} // OK +pub fn public_pat(Pub(Priv2): Pub) {} // OK + +fn main() {} diff --git a/tests/ui/privacy/private-in-public-ill-formed.rs b/tests/ui/privacy/private-in-public-ill-formed.rs new file mode 100644 index 000000000..031e2874a --- /dev/null +++ b/tests/ui/privacy/private-in-public-ill-formed.rs @@ -0,0 +1,37 @@ +mod aliases_pub { + struct Priv; + mod m { + pub struct Pub3; + } + + trait PrivTr { + type AssocAlias; + } + impl PrivTr for Priv { + type AssocAlias = m::Pub3; + } + + impl <Priv as PrivTr>::AssocAlias { + //~^ ERROR no nominal type found for inherent implementation + pub fn f(arg: Priv) {} // private type `aliases_pub::Priv` in public interface + } +} + +mod aliases_priv { + struct Priv; + struct Priv3; + + trait PrivTr { + type AssocAlias; + } + impl PrivTr for Priv { + type AssocAlias = Priv3; + } + + impl <Priv as PrivTr>::AssocAlias { + //~^ ERROR no nominal type found for inherent implementation + pub fn f(arg: Priv) {} // OK + } +} + +fn main() {} diff --git a/tests/ui/privacy/private-in-public-ill-formed.stderr b/tests/ui/privacy/private-in-public-ill-formed.stderr new file mode 100644 index 000000000..e7c94bc30 --- /dev/null +++ b/tests/ui/privacy/private-in-public-ill-formed.stderr @@ -0,0 +1,19 @@ +error[E0118]: no nominal type found for inherent implementation + --> $DIR/private-in-public-ill-formed.rs:14:10 + | +LL | impl <Priv as PrivTr>::AssocAlias { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type + | + = note: either implement a trait on it or create a newtype to wrap it instead + +error[E0118]: no nominal type found for inherent implementation + --> $DIR/private-in-public-ill-formed.rs:31:10 + | +LL | impl <Priv as PrivTr>::AssocAlias { + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type + | + = note: either implement a trait on it or create a newtype to wrap it instead + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0118`. diff --git a/tests/ui/privacy/private-in-public-lint.rs b/tests/ui/privacy/private-in-public-lint.rs new file mode 100644 index 000000000..8b6e43601 --- /dev/null +++ b/tests/ui/privacy/private-in-public-lint.rs @@ -0,0 +1,19 @@ +mod m1 { + pub struct Pub; + struct Priv; + + impl Pub { + pub fn f() -> Priv {Priv} //~ ERROR private type `m1::Priv` in public interface + } +} + +mod m2 { + pub struct Pub; + struct Priv; + + impl Pub { + pub fn f() -> Priv {Priv} //~ ERROR private type `m2::Priv` in public interface + } +} + +fn main() {} diff --git a/tests/ui/privacy/private-in-public-lint.stderr b/tests/ui/privacy/private-in-public-lint.stderr new file mode 100644 index 000000000..1e98e3bed --- /dev/null +++ b/tests/ui/privacy/private-in-public-lint.stderr @@ -0,0 +1,21 @@ +error[E0446]: private type `m1::Priv` in public interface + --> $DIR/private-in-public-lint.rs:6:9 + | +LL | struct Priv; + | ----------- `m1::Priv` declared as private +... +LL | pub fn f() -> Priv {Priv} + | ^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `m2::Priv` in public interface + --> $DIR/private-in-public-lint.rs:15:9 + | +LL | struct Priv; + | ----------- `m2::Priv` declared as private +... +LL | pub fn f() -> Priv {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/private-in-public-non-principal-2.rs b/tests/ui/privacy/private-in-public-non-principal-2.rs new file mode 100644 index 000000000..db451d334 --- /dev/null +++ b/tests/ui/privacy/private-in-public-non-principal-2.rs @@ -0,0 +1,14 @@ +#![feature(auto_traits)] +#![feature(negative_impls)] + +#[allow(private_in_public)] +mod m { + pub trait PubPrincipal {} + auto trait PrivNonPrincipal {} + pub fn leak_dyn_nonprincipal() -> Box<dyn PubPrincipal + PrivNonPrincipal> { loop {} } +} + +fn main() { + m::leak_dyn_nonprincipal(); + //~^ ERROR trait `PrivNonPrincipal` is private +} diff --git a/tests/ui/privacy/private-in-public-non-principal-2.stderr b/tests/ui/privacy/private-in-public-non-principal-2.stderr new file mode 100644 index 000000000..7cc8bf0de --- /dev/null +++ b/tests/ui/privacy/private-in-public-non-principal-2.stderr @@ -0,0 +1,8 @@ +error: trait `PrivNonPrincipal` is private + --> $DIR/private-in-public-non-principal-2.rs:12:5 + | +LL | m::leak_dyn_nonprincipal(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ private trait + +error: aborting due to previous error + diff --git a/tests/ui/privacy/private-in-public-non-principal.rs b/tests/ui/privacy/private-in-public-non-principal.rs new file mode 100644 index 000000000..ac1d5a9e6 --- /dev/null +++ b/tests/ui/privacy/private-in-public-non-principal.rs @@ -0,0 +1,21 @@ +#![feature(auto_traits)] +#![feature(negative_impls)] + +pub trait PubPrincipal {} +auto trait PrivNonPrincipal {} + +pub fn leak_dyn_nonprincipal() -> Box<dyn PubPrincipal + PrivNonPrincipal> { loop {} } +//~^ WARN private trait `PrivNonPrincipal` in public interface +//~| WARN this was previously accepted + +#[deny(missing_docs)] +fn container() { + impl dyn PubPrincipal { + pub fn check_doc_lint() {} //~ ERROR missing documentation for an associated function + } + impl dyn PubPrincipal + PrivNonPrincipal { + pub fn check_doc_lint() {} // OK, no missing doc lint + } +} + +fn main() {} diff --git a/tests/ui/privacy/private-in-public-non-principal.stderr b/tests/ui/privacy/private-in-public-non-principal.stderr new file mode 100644 index 000000000..de20cada4 --- /dev/null +++ b/tests/ui/privacy/private-in-public-non-principal.stderr @@ -0,0 +1,24 @@ +warning: private trait `PrivNonPrincipal` in public interface (error E0445) + --> $DIR/private-in-public-non-principal.rs:7:1 + | +LL | pub fn leak_dyn_nonprincipal() -> Box<dyn PubPrincipal + PrivNonPrincipal> { loop {} } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + = note: `#[warn(private_in_public)]` on by default + +error: missing documentation for an associated function + --> $DIR/private-in-public-non-principal.rs:14:9 + | +LL | pub fn check_doc_lint() {} + | ^^^^^^^^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/private-in-public-non-principal.rs:11:8 + | +LL | #[deny(missing_docs)] + | ^^^^^^^^^^^^ + +error: aborting due to previous error; 1 warning emitted + diff --git a/tests/ui/privacy/private-in-public-type-alias-impl-trait.rs b/tests/ui/privacy/private-in-public-type-alias-impl-trait.rs new file mode 100644 index 000000000..c7df31529 --- /dev/null +++ b/tests/ui/privacy/private-in-public-type-alias-impl-trait.rs @@ -0,0 +1,27 @@ +// build-pass (FIXME(62277): could be check-pass?) + +#![feature(type_alias_impl_trait)] +#![deny(private_in_public)] + +pub type Pub = impl Default; + +#[derive(Default)] +struct Priv; + +fn check() -> Pub { + Priv +} + +pub trait Trait { + type Pub: Default; + fn method() -> Self::Pub; +} + +impl Trait for u8 { + type Pub = impl Default; + fn method() -> Self::Pub { + Priv + } +} + +fn main() {} diff --git a/tests/ui/privacy/private-in-public-warn.rs b/tests/ui/privacy/private-in-public-warn.rs new file mode 100644 index 000000000..0fa1de975 --- /dev/null +++ b/tests/ui/privacy/private-in-public-warn.rs @@ -0,0 +1,280 @@ +// Private types and traits are not allowed in public interfaces. +// This test also ensures that the checks are performed even inside private modules. + +#![feature(associated_type_defaults)] +#![deny(private_in_public)] +#![allow(improper_ctypes)] + +mod types { + struct Priv; + pub struct Pub; + pub trait PubTr { + type Alias; + } + + pub type Alias = Priv; //~ ERROR private type `types::Priv` in public interface + //~^ WARNING hard error + pub enum E { + V1(Priv), //~ ERROR private type `types::Priv` in public interface + //~^ WARNING hard error + V2 { field: Priv }, //~ ERROR private type `types::Priv` in public interface + //~^ WARNING hard error + } + pub trait Tr { + const C: Priv = Priv; //~ ERROR private type `types::Priv` in public interface + //~^ WARNING hard error + type Alias = Priv; //~ ERROR private type `types::Priv` in public interface + fn f1(arg: Priv) {} //~ ERROR private type `types::Priv` in public interface + //~^ WARNING hard error + fn f2() -> Priv { panic!() } //~ ERROR private type `types::Priv` in public interface + //~^ WARNING hard error + } + extern "C" { + pub static ES: Priv; //~ ERROR private type `types::Priv` in public interface + //~^ WARNING hard error + pub fn ef1(arg: Priv); //~ ERROR private type `types::Priv` in public interface + //~^ WARNING hard error + pub fn ef2() -> Priv; //~ ERROR private type `types::Priv` in public interface + //~^ WARNING hard error + } + impl PubTr for Pub { + type Alias = Priv; //~ ERROR private type `types::Priv` in public interface + } +} + +mod traits { + trait PrivTr {} + pub struct Pub<T>(T); + pub trait PubTr {} + + pub type Alias<T: PrivTr> = T; //~ ERROR private trait `traits::PrivTr` in public interface + //~| WARNING hard error + //~| WARNING bounds on generic parameters are not enforced in type aliases + pub trait Tr1: PrivTr {} //~ ERROR private trait `traits::PrivTr` in public interface + //~^ WARNING hard error + pub trait Tr2<T: PrivTr> {} //~ ERROR private trait `traits::PrivTr` in public interface + //~^ WARNING hard error + pub trait Tr3 { + type Alias: PrivTr; + //~^ ERROR private trait `traits::PrivTr` in public interface + //~| WARNING hard error + fn f<T: PrivTr>(arg: T) {} //~ ERROR private trait `traits::PrivTr` in public interface + //~^ WARNING hard error + } + impl<T: PrivTr> Pub<T> {} //~ ERROR private trait `traits::PrivTr` in public interface + //~^ WARNING hard error + impl<T: PrivTr> PubTr for Pub<T> {} // OK, trait impl predicates +} + +mod traits_where { + trait PrivTr {} + pub struct Pub<T>(T); + pub trait PubTr {} + + pub type Alias<T> where T: PrivTr = T; + //~^ ERROR private trait `traits_where::PrivTr` in public interface + //~| WARNING hard error + //~| WARNING where clauses are not enforced in type aliases + pub trait Tr2<T> where T: PrivTr {} + //~^ ERROR private trait `traits_where::PrivTr` in public interface + //~| WARNING hard error + pub trait Tr3 { + fn f<T>(arg: T) where T: PrivTr {} + //~^ ERROR private trait `traits_where::PrivTr` in public interface + //~| WARNING hard error + } + impl<T> Pub<T> where T: PrivTr {} + //~^ ERROR private trait `traits_where::PrivTr` in public interface + //~| WARNING hard error + impl<T> PubTr for Pub<T> where T: PrivTr {} // OK, trait impl predicates +} + +mod generics { + struct Priv<T = u8>(T); + pub struct Pub<T = u8>(T); + trait PrivTr<T> {} + pub trait PubTr<T> {} + + pub trait Tr1: PrivTr<Pub> {} + //~^ ERROR private trait `generics::PrivTr<generics::Pub>` in public interface + //~| WARNING hard error + pub trait Tr2: PubTr<Priv> {} //~ ERROR private type `generics::Priv` in public interface + //~^ WARNING hard error + pub trait Tr3: PubTr<[Priv; 1]> {} //~ ERROR private type `generics::Priv` in public interface + //~^ WARNING hard error + pub trait Tr4: PubTr<Pub<Priv>> {} //~ ERROR private type `generics::Priv` in public interface + //~^ WARNING hard error +} + +mod impls { + struct Priv; + pub struct Pub; + trait PrivTr { + type Alias; + } + pub trait PubTr { + type Alias; + } + + impl Priv { + pub fn f(arg: Priv) {} // OK + } + impl PrivTr for Priv { + type Alias = Priv; // OK + } + impl PubTr for Priv { + type Alias = Priv; // OK + } + impl PrivTr for Pub { + type Alias = Priv; // OK + } + impl PubTr for Pub { + type Alias = Priv; //~ ERROR private type `impls::Priv` in public interface + } +} + +mod impls_generics { + struct Priv<T = u8>(T); + pub struct Pub<T = u8>(T); + trait PrivTr<T = u8> { + type Alias; + } + pub trait PubTr<T = u8> { + type Alias; + } + + impl Priv<Pub> { + pub fn f(arg: Priv) {} // OK + } + impl Pub<Priv> { + pub fn f(arg: Priv) {} // OK + } + impl PrivTr<Pub> for Priv { + type Alias = Priv; // OK + } + impl PubTr<Priv> for Priv { + type Alias = Priv; // OK + } + impl PubTr for Priv<Pub> { + type Alias = Priv; // OK + } + impl PubTr for [Priv; 1] { + type Alias = Priv; // OK + } + impl PubTr for Pub<Priv> { + type Alias = Priv; // OK + } + impl PrivTr<Pub> for Pub { + type Alias = Priv; // OK + } + impl PubTr<Priv> for Pub { + type Alias = Priv; // OK + } +} + +mod aliases_pub { + struct Priv; + mod m { + pub struct Pub1; + pub struct Pub2; + pub struct Pub3; + pub trait PubTr<T = u8> { + type Check = u8; + } + } + + use self::m::Pub1 as PrivUseAlias; + use self::m::PubTr as PrivUseAliasTr; + type PrivAlias = m::Pub2; + trait PrivTr { + type AssocAlias; + } + impl PrivTr for Priv { + type AssocAlias = m::Pub3; + } + + pub fn f1(arg: PrivUseAlias) {} // OK + pub fn f2(arg: PrivAlias) {} // OK + + pub trait Tr1: PrivUseAliasTr {} // OK + pub trait Tr2: PrivUseAliasTr<PrivAlias> {} // OK + + impl PrivAlias { + pub fn f(arg: Priv) {} //~ ERROR private type `aliases_pub::Priv` in public interface + //~^ WARNING hard error + } + impl PrivUseAliasTr for PrivUseAlias { + type Check = Priv; //~ ERROR private type `aliases_pub::Priv` in public interface + } + impl PrivUseAliasTr for PrivAlias { + type Check = Priv; //~ ERROR private type `aliases_pub::Priv` in public interface + } + impl PrivUseAliasTr for <Priv as PrivTr>::AssocAlias { + type Check = Priv; //~ ERROR private type `aliases_pub::Priv` in public interface + } + impl PrivUseAliasTr for Option<<Priv as PrivTr>::AssocAlias> { + type Check = Priv; //~ ERROR private type `aliases_pub::Priv` in public interface + } + impl PrivUseAliasTr for (<Priv as PrivTr>::AssocAlias, Priv) { + type Check = Priv; // OK + } + impl PrivUseAliasTr for Option<(<Priv as PrivTr>::AssocAlias, Priv)> { + type Check = Priv; // OK + } +} + +mod aliases_priv { + struct Priv; + + struct Priv1; + struct Priv2; + struct Priv3; + trait PrivTr1<T = u8> { + type Check = u8; + } + + use self::Priv1 as PrivUseAlias; + use self::PrivTr1 as PrivUseAliasTr; + type PrivAlias = Priv2; + trait PrivTr { + type AssocAlias; + } + impl PrivTr for Priv { + type AssocAlias = Priv3; + } + + pub trait Tr1: PrivUseAliasTr {} + //~^ ERROR private trait `PrivTr1` in public interface + //~| WARNING hard error + pub trait Tr2: PrivUseAliasTr<PrivAlias> {} + //~^ ERROR private trait `PrivTr1<Priv2>` in public interface + //~| WARNING hard error + //~| ERROR private type `Priv2` in public interface + //~| WARNING hard error + + impl PrivUseAlias { + pub fn f(arg: Priv) {} // OK + } + impl PrivAlias { + pub fn f(arg: Priv) {} // OK + } + impl PrivUseAliasTr for PrivUseAlias { + type Check = Priv; // OK + } + impl PrivUseAliasTr for PrivAlias { + type Check = Priv; // OK + } + impl PrivUseAliasTr for <Priv as PrivTr>::AssocAlias { + type Check = Priv; // OK + } +} + +mod aliases_params { + struct Priv; + type PrivAliasGeneric<T = Priv> = T; + type Result<T> = ::std::result::Result<T, Priv>; + + pub fn f1(arg: PrivAliasGeneric<u8>) {} // OK, not an error +} + +fn main() {} diff --git a/tests/ui/privacy/private-in-public-warn.stderr b/tests/ui/privacy/private-in-public-warn.stderr new file mode 100644 index 000000000..66f91ce6f --- /dev/null +++ b/tests/ui/privacy/private-in-public-warn.stderr @@ -0,0 +1,339 @@ +error: private type `types::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:15:5 + | +LL | pub type Alias = Priv; + | ^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> +note: the lint level is defined here + --> $DIR/private-in-public-warn.rs:5:9 + | +LL | #![deny(private_in_public)] + | ^^^^^^^^^^^^^^^^^ + +error: private type `types::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:18:12 + | +LL | V1(Priv), + | ^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private type `types::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:20:14 + | +LL | V2 { field: Priv }, + | ^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private type `types::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:24:9 + | +LL | const C: Priv = Priv; + | ^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public-warn.rs:26:9 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | type Alias = Priv; + | ^^^^^^^^^^ can't leak private type + +error: private type `types::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:27:9 + | +LL | fn f1(arg: Priv) {} + | ^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private type `types::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:29:9 + | +LL | fn f2() -> Priv { panic!() } + | ^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private type `types::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:33:9 + | +LL | pub static ES: Priv; + | ^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private type `types::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:35:9 + | +LL | pub fn ef1(arg: Priv); + | ^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private type `types::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:37:9 + | +LL | pub fn ef2() -> Priv; + | ^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public-warn.rs:41:9 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | type Alias = Priv; + | ^^^^^^^^^^ can't leak private type + +error: private trait `traits::PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:50:5 + | +LL | pub type Alias<T: PrivTr> = T; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `traits::PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:53:5 + | +LL | pub trait Tr1: PrivTr {} + | ^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `traits::PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:55:5 + | +LL | pub trait Tr2<T: PrivTr> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `traits::PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:58:9 + | +LL | type Alias: PrivTr; + | ^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `traits::PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:61:9 + | +LL | fn f<T: PrivTr>(arg: T) {} + | ^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `traits::PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:64:5 + | +LL | impl<T: PrivTr> Pub<T> {} + | ^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `traits_where::PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:74:5 + | +LL | pub type Alias<T> where T: PrivTr = T; + | ^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `traits_where::PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:78:5 + | +LL | pub trait Tr2<T> where T: PrivTr {} + | ^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `traits_where::PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:82:9 + | +LL | fn f<T>(arg: T) where T: PrivTr {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `traits_where::PrivTr` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:86:5 + | +LL | impl<T> Pub<T> where T: PrivTr {} + | ^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `generics::PrivTr<generics::Pub>` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:98:5 + | +LL | pub trait Tr1: PrivTr<Pub> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private type `generics::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:101:5 + | +LL | pub trait Tr2: PubTr<Priv> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private type `generics::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:103:5 + | +LL | pub trait Tr3: PubTr<[Priv; 1]> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private type `generics::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:105:5 + | +LL | pub trait Tr4: PubTr<Pub<Priv>> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error[E0446]: private type `impls::Priv` in public interface + --> $DIR/private-in-public-warn.rs:132:9 + | +LL | struct Priv; + | ----------- `impls::Priv` declared as private +... +LL | type Alias = Priv; + | ^^^^^^^^^^ can't leak private type + +error: private type `aliases_pub::Priv` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:203:9 + | +LL | pub fn f(arg: Priv) {} + | ^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error[E0446]: private type `aliases_pub::Priv` in public interface + --> $DIR/private-in-public-warn.rs:207:9 + | +LL | struct Priv; + | ----------- `aliases_pub::Priv` declared as private +... +LL | type Check = Priv; + | ^^^^^^^^^^ can't leak private type + +error[E0446]: private type `aliases_pub::Priv` in public interface + --> $DIR/private-in-public-warn.rs:210:9 + | +LL | struct Priv; + | ----------- `aliases_pub::Priv` declared as private +... +LL | type Check = Priv; + | ^^^^^^^^^^ can't leak private type + +error[E0446]: private type `aliases_pub::Priv` in public interface + --> $DIR/private-in-public-warn.rs:213:9 + | +LL | struct Priv; + | ----------- `aliases_pub::Priv` declared as private +... +LL | type Check = Priv; + | ^^^^^^^^^^ can't leak private type + +error[E0446]: private type `aliases_pub::Priv` in public interface + --> $DIR/private-in-public-warn.rs:216:9 + | +LL | struct Priv; + | ----------- `aliases_pub::Priv` declared as private +... +LL | type Check = Priv; + | ^^^^^^^^^^ can't leak private type + +error: private trait `PrivTr1` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:246:5 + | +LL | pub trait Tr1: PrivUseAliasTr {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private trait `PrivTr1<Priv2>` in public interface (error E0445) + --> $DIR/private-in-public-warn.rs:249:5 + | +LL | pub trait Tr2: PrivUseAliasTr<PrivAlias> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error: private type `Priv2` in public interface (error E0446) + --> $DIR/private-in-public-warn.rs:249:5 + | +LL | pub trait Tr2: PrivUseAliasTr<PrivAlias> {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +warning: bounds on generic parameters are not enforced in type aliases + --> $DIR/private-in-public-warn.rs:50:23 + | +LL | pub type Alias<T: PrivTr> = T; + | ^^^^^^ + | + = note: `#[warn(type_alias_bounds)]` on by default +help: the bound will not be checked when the type alias is used, and should be removed + | +LL - pub type Alias<T: PrivTr> = T; +LL + pub type Alias<T> = T; + | + +warning: where clauses are not enforced in type aliases + --> $DIR/private-in-public-warn.rs:74:29 + | +LL | pub type Alias<T> where T: PrivTr = T; + | ^^^^^^^^^ + | +help: the clause will not be checked when the type alias is used, and should be removed + | +LL - pub type Alias<T> where T: PrivTr = T; +LL + pub type Alias<T> = T; + | + +error: aborting due to 34 previous errors; 2 warnings emitted + +For more information about this error, try `rustc --explain E0446`. diff --git a/tests/ui/privacy/private-in-public.rs b/tests/ui/privacy/private-in-public.rs new file mode 100644 index 000000000..dbd1c483f --- /dev/null +++ b/tests/ui/privacy/private-in-public.rs @@ -0,0 +1,148 @@ +// Private types and traits are not allowed in public interfaces. +// This test also ensures that the checks are performed even inside private modules. + +#![feature(associated_type_defaults)] + +mod types { + struct Priv; + pub struct Pub; + pub trait PubTr { + type Alias; + } + + pub const C: Priv = Priv; //~ ERROR private type `types::Priv` in public interface + pub static S: Priv = Priv; //~ ERROR private type `types::Priv` in public interface + pub fn f1(arg: Priv) {} //~ ERROR private type `types::Priv` in public interface + pub fn f2() -> Priv { panic!() } //~ ERROR private type `types::Priv` in public interface + pub struct S1(pub Priv); //~ ERROR private type `types::Priv` in public interface + pub struct S2 { pub field: Priv } //~ ERROR private type `types::Priv` in public interface + impl Pub { + pub const C: Priv = Priv; //~ ERROR private type `types::Priv` in public interface + pub fn f1(arg: Priv) {} //~ ERROR private type `types::Priv` in public interface + pub fn f2() -> Priv { panic!() } //~ ERROR private type `types::Priv` in public interface + } +} + +mod traits { + trait PrivTr {} + pub struct Pub<T>(T); + pub trait PubTr {} + + pub enum E<T: PrivTr> { V(T) } //~ ERROR private trait `traits::PrivTr` in public interface + pub fn f<T: PrivTr>(arg: T) {} //~ ERROR private trait `traits::PrivTr` in public interface + pub struct S1<T: PrivTr>(T); //~ ERROR private trait `traits::PrivTr` in public interface + impl<T: PrivTr> Pub<T> { //~ ERROR private trait `traits::PrivTr` in public interface + pub fn f<U: PrivTr>(arg: U) {} //~ ERROR private trait `traits::PrivTr` in public interface + } +} + +mod traits_where { + trait PrivTr {} + pub struct Pub<T>(T); + pub trait PubTr {} + + pub enum E<T> where T: PrivTr { V(T) } + //~^ ERROR private trait `traits_where::PrivTr` in public interface + pub fn f<T>(arg: T) where T: PrivTr {} + //~^ ERROR private trait `traits_where::PrivTr` in public interface + pub struct S1<T>(T) where T: PrivTr; + //~^ ERROR private trait `traits_where::PrivTr` in public interface + impl<T> Pub<T> where T: PrivTr { + //~^ ERROR private trait `traits_where::PrivTr` in public interface + pub fn f<U>(arg: U) where U: PrivTr {} + //~^ ERROR private trait `traits_where::PrivTr` in public interface + } +} + +mod generics { + struct Priv<T = u8>(T); + pub struct Pub<T = u8>(T); + trait PrivTr<T> {} + pub trait PubTr<T> {} + + pub fn f1(arg: [Priv; 1]) {} //~ ERROR private type `generics::Priv` in public interface + pub fn f2(arg: Pub<Priv>) {} //~ ERROR private type `generics::Priv` in public interface + pub fn f3(arg: Priv<Pub>) {} + //~^ ERROR private type `generics::Priv<generics::Pub>` in public interface +} + +mod impls { + struct Priv; + pub struct Pub; + trait PrivTr { + type Alias; + } + pub trait PubTr { + type Alias; + } + + impl Pub { + pub fn f(arg: Priv) {} //~ ERROR private type `impls::Priv` in public interface + } +} + +mod aliases_pub { + struct Priv; + mod m { + pub struct Pub1; + pub struct Pub2; + pub struct Pub3; + pub trait PubTr<T = u8> { + type Check = u8; + } + } + + use self::m::Pub1 as PrivUseAlias; + use self::m::PubTr as PrivUseAliasTr; + type PrivAlias = m::Pub2; + trait PrivTr { + type Assoc = m::Pub3; + } + impl PrivTr for Priv {} + + // This should be OK, but associated type aliases are not substituted yet + pub fn f3(arg: <Priv as PrivTr>::Assoc) {} + //~^ ERROR private trait `aliases_pub::PrivTr` in public interface + //~| ERROR private type `aliases_pub::Priv` in public interface + + impl PrivUseAlias { + pub fn f(arg: Priv) {} //~ ERROR private type `aliases_pub::Priv` in public interface + } +} + +mod aliases_priv { + struct Priv; + + struct Priv1; + struct Priv2; + struct Priv3; + trait PrivTr1<T = u8> { + type Check = u8; + } + + use self::Priv1 as PrivUseAlias; + use self::PrivTr1 as PrivUseAliasTr; + type PrivAlias = Priv2; + trait PrivTr { + type Assoc = Priv3; + } + impl PrivTr for Priv {} + + pub fn f1(arg: PrivUseAlias) {} //~ ERROR private type `Priv1` in public interface + pub fn f2(arg: PrivAlias) {} //~ ERROR private type `Priv2` in public interface + pub fn f3(arg: <Priv as PrivTr>::Assoc) {} + //~^ ERROR private trait `aliases_priv::PrivTr` in public interface + //~| ERROR private type `aliases_priv::Priv` in public interface +} + +mod aliases_params { + struct Priv; + type PrivAliasGeneric<T = Priv> = T; + type Result<T> = ::std::result::Result<T, Priv>; + + pub fn f2(arg: PrivAliasGeneric) {} + //~^ ERROR private type `aliases_params::Priv` in public interface + pub fn f3(arg: Result<u8>) {} //~ ERROR private type `aliases_params::Priv` in public interface +} + +fn main() {} diff --git a/tests/ui/privacy/private-in-public.stderr b/tests/ui/privacy/private-in-public.stderr new file mode 100644 index 000000000..887eebf53 --- /dev/null +++ b/tests/ui/privacy/private-in-public.stderr @@ -0,0 +1,292 @@ +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public.rs:13:5 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | pub const C: Priv = Priv; + | ^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public.rs:14:5 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | pub static S: Priv = Priv; + | ^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public.rs:15:5 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | pub fn f1(arg: Priv) {} + | ^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public.rs:16:5 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | pub fn f2() -> Priv { panic!() } + | ^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public.rs:17:19 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | pub struct S1(pub Priv); + | ^^^^^^^^ can't leak private type + +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public.rs:18:21 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | pub struct S2 { pub field: Priv } + | ^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public.rs:20:9 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | pub const C: Priv = Priv; + | ^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public.rs:21:9 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | pub fn f1(arg: Priv) {} + | ^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `types::Priv` in public interface + --> $DIR/private-in-public.rs:22:9 + | +LL | struct Priv; + | ----------- `types::Priv` declared as private +... +LL | pub fn f2() -> Priv { panic!() } + | ^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0445]: private trait `traits::PrivTr` in public interface + --> $DIR/private-in-public.rs:31:5 + | +LL | trait PrivTr {} + | ------------ `traits::PrivTr` declared as private +... +LL | pub enum E<T: PrivTr> { V(T) } + | ^^^^^^^^^^^^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `traits::PrivTr` in public interface + --> $DIR/private-in-public.rs:32:5 + | +LL | trait PrivTr {} + | ------------ `traits::PrivTr` declared as private +... +LL | pub fn f<T: PrivTr>(arg: T) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `traits::PrivTr` in public interface + --> $DIR/private-in-public.rs:33:5 + | +LL | trait PrivTr {} + | ------------ `traits::PrivTr` declared as private +... +LL | pub struct S1<T: PrivTr>(T); + | ^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `traits::PrivTr` in public interface + --> $DIR/private-in-public.rs:34:5 + | +LL | trait PrivTr {} + | ------------ `traits::PrivTr` declared as private +... +LL | impl<T: PrivTr> Pub<T> { + | ^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `traits::PrivTr` in public interface + --> $DIR/private-in-public.rs:35:9 + | +LL | trait PrivTr {} + | ------------ `traits::PrivTr` declared as private +... +LL | pub fn f<U: PrivTr>(arg: U) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `traits_where::PrivTr` in public interface + --> $DIR/private-in-public.rs:44:5 + | +LL | trait PrivTr {} + | ------------ `traits_where::PrivTr` declared as private +... +LL | pub enum E<T> where T: PrivTr { V(T) } + | ^^^^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `traits_where::PrivTr` in public interface + --> $DIR/private-in-public.rs:46:5 + | +LL | trait PrivTr {} + | ------------ `traits_where::PrivTr` declared as private +... +LL | pub fn f<T>(arg: T) where T: PrivTr {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `traits_where::PrivTr` in public interface + --> $DIR/private-in-public.rs:48:5 + | +LL | trait PrivTr {} + | ------------ `traits_where::PrivTr` declared as private +... +LL | pub struct S1<T>(T) where T: PrivTr; + | ^^^^^^^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `traits_where::PrivTr` in public interface + --> $DIR/private-in-public.rs:50:5 + | +LL | trait PrivTr {} + | ------------ `traits_where::PrivTr` declared as private +... +LL | impl<T> Pub<T> where T: PrivTr { + | ^^^^^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `traits_where::PrivTr` in public interface + --> $DIR/private-in-public.rs:52:9 + | +LL | trait PrivTr {} + | ------------ `traits_where::PrivTr` declared as private +... +LL | pub fn f<U>(arg: U) where U: PrivTr {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait + +error[E0446]: private type `generics::Priv` in public interface + --> $DIR/private-in-public.rs:63:5 + | +LL | struct Priv<T = u8>(T); + | ------------------- `generics::Priv` declared as private +... +LL | pub fn f1(arg: [Priv; 1]) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `generics::Priv` in public interface + --> $DIR/private-in-public.rs:64:5 + | +LL | struct Priv<T = u8>(T); + | ------------------- `generics::Priv` declared as private +... +LL | pub fn f2(arg: Pub<Priv>) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `generics::Priv<generics::Pub>` in public interface + --> $DIR/private-in-public.rs:65:5 + | +LL | struct Priv<T = u8>(T); + | ------------------- `generics::Priv<generics::Pub>` declared as private +... +LL | pub fn f3(arg: Priv<Pub>) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `impls::Priv` in public interface + --> $DIR/private-in-public.rs:80:9 + | +LL | struct Priv; + | ----------- `impls::Priv` declared as private +... +LL | pub fn f(arg: Priv) {} + | ^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0445]: private trait `aliases_pub::PrivTr` in public interface + --> $DIR/private-in-public.rs:104:5 + | +LL | trait PrivTr { + | ------------ `aliases_pub::PrivTr` declared as private +... +LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait + +error[E0446]: private type `aliases_pub::Priv` in public interface + --> $DIR/private-in-public.rs:104:5 + | +LL | struct Priv; + | ----------- `aliases_pub::Priv` declared as private +... +LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `aliases_pub::Priv` in public interface + --> $DIR/private-in-public.rs:109:9 + | +LL | struct Priv; + | ----------- `aliases_pub::Priv` declared as private +... +LL | pub fn f(arg: Priv) {} + | ^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `Priv1` in public interface + --> $DIR/private-in-public.rs:131:5 + | +LL | struct Priv1; + | ------------ `Priv1` declared as private +... +LL | pub fn f1(arg: PrivUseAlias) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `Priv2` in public interface + --> $DIR/private-in-public.rs:132:5 + | +LL | struct Priv2; + | ------------ `Priv2` declared as private +... +LL | pub fn f2(arg: PrivAlias) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0445]: private trait `aliases_priv::PrivTr` in public interface + --> $DIR/private-in-public.rs:133:5 + | +LL | trait PrivTr { + | ------------ `aliases_priv::PrivTr` declared as private +... +LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private trait + +error[E0446]: private type `aliases_priv::Priv` in public interface + --> $DIR/private-in-public.rs:133:5 + | +LL | struct Priv; + | ----------- `aliases_priv::Priv` declared as private +... +LL | pub fn f3(arg: <Priv as PrivTr>::Assoc) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `aliases_params::Priv` in public interface + --> $DIR/private-in-public.rs:143:5 + | +LL | struct Priv; + | ----------- `aliases_params::Priv` declared as private +... +LL | pub fn f2(arg: PrivAliasGeneric) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `aliases_params::Priv` in public interface + --> $DIR/private-in-public.rs:145:5 + | +LL | struct Priv; + | ----------- `aliases_params::Priv` declared as private +... +LL | pub fn f3(arg: Result<u8>) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^ can't leak private type + +error: aborting due to 32 previous errors + +Some errors have detailed explanations: E0445, E0446. +For more information about an error, try `rustc --explain E0445`. diff --git a/tests/ui/privacy/private-inferred-type-1.rs b/tests/ui/privacy/private-inferred-type-1.rs new file mode 100644 index 000000000..d633189e3 --- /dev/null +++ b/tests/ui/privacy/private-inferred-type-1.rs @@ -0,0 +1,18 @@ +trait Arr0 { + fn arr0_secret(&self); +} +trait TyParam { + fn ty_param_secret(&self); +} + +mod m { + struct Priv; + + impl ::Arr0 for [Priv; 0] { fn arr0_secret(&self) {} } + impl ::TyParam for Option<Priv> { fn ty_param_secret(&self) {} } +} + +fn main() { + [].arr0_secret(); //~ ERROR type `Priv` is private + None.ty_param_secret(); //~ ERROR type `Priv` is private +} diff --git a/tests/ui/privacy/private-inferred-type-1.stderr b/tests/ui/privacy/private-inferred-type-1.stderr new file mode 100644 index 000000000..245789f43 --- /dev/null +++ b/tests/ui/privacy/private-inferred-type-1.stderr @@ -0,0 +1,14 @@ +error: type `Priv` is private + --> $DIR/private-inferred-type-1.rs:16:5 + | +LL | [].arr0_secret(); + | ^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type-1.rs:17:5 + | +LL | None.ty_param_secret(); + | ^^^^^^^^^^^^^^^^^^^^^^ private type + +error: aborting due to 2 previous errors + diff --git a/tests/ui/privacy/private-inferred-type-2.rs b/tests/ui/privacy/private-inferred-type-2.rs new file mode 100644 index 000000000..15b263b38 --- /dev/null +++ b/tests/ui/privacy/private-inferred-type-2.rs @@ -0,0 +1,19 @@ +// aux-build:private-inferred-type.rs + +extern crate private_inferred_type as ext; + +mod m { + struct Priv; + pub struct Pub<T>(pub T); + + impl Pub<Priv> { + pub fn get_priv() -> Priv { Priv } + pub fn static_method() {} + } +} + +fn main() { + m::Pub::get_priv; //~ ERROR type `Priv` is private + m::Pub::static_method; //~ ERROR type `Priv` is private + ext::Pub::static_method; //~ ERROR type `ext::Priv` is private +} diff --git a/tests/ui/privacy/private-inferred-type-2.stderr b/tests/ui/privacy/private-inferred-type-2.stderr new file mode 100644 index 000000000..3a0fc03b4 --- /dev/null +++ b/tests/ui/privacy/private-inferred-type-2.stderr @@ -0,0 +1,20 @@ +error: type `Priv` is private + --> $DIR/private-inferred-type-2.rs:16:5 + | +LL | m::Pub::get_priv; + | ^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type-2.rs:17:5 + | +LL | m::Pub::static_method; + | ^^^^^^^^^^^^^^^^^^^^^ private type + +error: type `ext::Priv` is private + --> $DIR/private-inferred-type-2.rs:18:5 + | +LL | ext::Pub::static_method; + | ^^^^^^^^^^^^^^^^^^^^^^^ private type + +error: aborting due to 3 previous errors + diff --git a/tests/ui/privacy/private-inferred-type-3.rs b/tests/ui/privacy/private-inferred-type-3.rs new file mode 100644 index 000000000..cdbdcf60b --- /dev/null +++ b/tests/ui/privacy/private-inferred-type-3.rs @@ -0,0 +1,17 @@ +// aux-build:private-inferred-type.rs + +// error-pattern:type `fn() {ext::priv_fn}` is private +// error-pattern:static `ext::PRIV_STATIC` is private +// error-pattern:type `ext::PrivEnum` is private +// error-pattern:type `fn() {<u8 as ext::PrivTrait>::method}` is private +// error-pattern:type `fn(u8) -> ext::PrivTupleStruct {ext::PrivTupleStruct}` is private +// error-pattern:type `fn(u8) -> PubTupleStruct {PubTupleStruct}` is private +// error-pattern:type `for<'a> fn(&'a Pub<u8>) {Pub::<u8>::priv_method}` is private + +#![feature(decl_macro)] + +extern crate private_inferred_type as ext; + +fn main() { + ext::m!(); +} diff --git a/tests/ui/privacy/private-inferred-type-3.stderr b/tests/ui/privacy/private-inferred-type-3.stderr new file mode 100644 index 000000000..42faeb4bf --- /dev/null +++ b/tests/ui/privacy/private-inferred-type-3.stderr @@ -0,0 +1,58 @@ +error: type `fn() {ext::priv_fn}` is private + --> $DIR/private-inferred-type-3.rs:16:5 + | +LL | ext::m!(); + | ^^^^^^^^^ private type + | + = note: this error originates in the macro `ext::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: static `ext::PRIV_STATIC` is private + --> $DIR/private-inferred-type-3.rs:16:5 + | +LL | ext::m!(); + | ^^^^^^^^^ private static + | + = note: this error originates in the macro `ext::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `ext::PrivEnum` is private + --> $DIR/private-inferred-type-3.rs:16:5 + | +LL | ext::m!(); + | ^^^^^^^^^ private type + | + = note: this error originates in the macro `ext::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `fn() {<u8 as ext::PrivTrait>::method}` is private + --> $DIR/private-inferred-type-3.rs:16:5 + | +LL | ext::m!(); + | ^^^^^^^^^ private type + | + = note: this error originates in the macro `ext::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `fn(u8) -> ext::PrivTupleStruct {ext::PrivTupleStruct}` is private + --> $DIR/private-inferred-type-3.rs:16:5 + | +LL | ext::m!(); + | ^^^^^^^^^ private type + | + = note: this error originates in the macro `ext::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `fn(u8) -> PubTupleStruct {PubTupleStruct}` is private + --> $DIR/private-inferred-type-3.rs:16:5 + | +LL | ext::m!(); + | ^^^^^^^^^ private type + | + = note: this error originates in the macro `ext::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `for<'a> fn(&'a Pub<u8>) {Pub::<u8>::priv_method}` is private + --> $DIR/private-inferred-type-3.rs:16:5 + | +LL | ext::m!(); + | ^^^^^^^^^ private type + | + = note: this error originates in the macro `ext::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 7 previous errors + diff --git a/tests/ui/privacy/private-inferred-type.rs b/tests/ui/privacy/private-inferred-type.rs new file mode 100644 index 000000000..e8743dd96 --- /dev/null +++ b/tests/ui/privacy/private-inferred-type.rs @@ -0,0 +1,133 @@ +#![feature(decl_macro)] +#![allow(private_in_public)] + +mod m { + fn priv_fn() {} + static PRIV_STATIC: u8 = 0; + enum PrivEnum { Variant } + pub enum PubEnum { Variant } + trait PrivTrait { fn method() {} } + impl PrivTrait for u8 {} + pub trait PubTrait { fn method() {} } + impl PubTrait for u8 {} + struct PrivTupleStruct(u8); + pub struct PubTupleStruct(u8); + impl PubTupleStruct { fn method() {} } + + #[derive(Clone, Copy)] + struct Priv; + pub type Alias = Priv; + pub struct Pub<T = Alias>(pub T); + + impl Pub<Priv> { + pub fn static_method() {} + pub const INHERENT_ASSOC_CONST: u8 = 0; + } + impl<T> Pub<T> { + pub fn static_method_generic_self() {} + pub const INHERENT_ASSOC_CONST_GENERIC_SELF: u8 = 0; + } + impl Pub<u8> { + fn priv_method(&self) {} + pub fn method_with_substs<T>(&self) {} + pub fn method_with_priv_params(&self, _: Priv) {} + } + impl TraitWithAssocConst for Priv {} + impl TraitWithAssocTy for Priv { type AssocTy = u8; } + + pub macro m() { + priv_fn; //~ ERROR type `fn() {priv_fn}` is private + PRIV_STATIC; // OK, not cross-crate + PrivEnum::Variant; //~ ERROR type `PrivEnum` is private + PubEnum::Variant; // OK + <u8 as PrivTrait>::method; //~ ERROR type `fn() {<u8 as PrivTrait>::method}` is private + <u8 as PubTrait>::method; // OK + PrivTupleStruct; + //~^ ERROR type `fn(u8) -> PrivTupleStruct {PrivTupleStruct}` is private + PubTupleStruct; + //~^ ERROR type `fn(u8) -> PubTupleStruct {PubTupleStruct}` is private + Pub(0u8).priv_method(); + //~^ ERROR type `for<'a> fn(&'a Pub<u8>) {Pub::<u8>::priv_method}` is private + } + + trait Trait {} + pub trait TraitWithTyParam<T> {} + pub trait TraitWithTyParam2<T> { fn pub_method() {} } + pub trait TraitWithAssocTy { type AssocTy; } + pub trait TraitWithAssocConst { const TRAIT_ASSOC_CONST: u8 = 0; } + impl Trait for u8 {} + impl<T> TraitWithTyParam<T> for u8 {} + impl TraitWithTyParam2<Priv> for u8 {} + impl TraitWithAssocTy for u8 { type AssocTy = Priv; } + //~^ ERROR private type `Priv` in public interface + + pub fn leak_anon1() -> impl Trait + 'static { 0 } + pub fn leak_anon2() -> impl TraitWithTyParam<Alias> { 0 } + pub fn leak_anon3() -> impl TraitWithAssocTy<AssocTy = Alias> { 0 } + + pub fn leak_dyn1() -> Box<dyn Trait + 'static> { Box::new(0) } + pub fn leak_dyn2() -> Box<dyn TraitWithTyParam<Alias>> { Box::new(0) } + pub fn leak_dyn3() -> Box<dyn TraitWithAssocTy<AssocTy = Alias>> { Box::new(0) } +} + +mod adjust { + // Construct a chain of derefs with a private type in the middle + use std::ops::Deref; + + pub struct S1; + struct S2; + pub type S2Alias = S2; + pub struct S3; + + impl Deref for S1 { + type Target = S2Alias; //~ ERROR private type `S2` in public interface + fn deref(&self) -> &Self::Target { loop {} } + } + impl Deref for S2 { + type Target = S3; + fn deref(&self) -> &Self::Target { loop {} } + } + + impl S3 { + pub fn method_s3(&self) {} + } +} + +fn main() { + let _: m::Alias; //~ ERROR type `Priv` is private + //~^ ERROR type `Priv` is private + let _: <m::Alias as m::TraitWithAssocTy>::AssocTy; //~ ERROR type `Priv` is private + m::Alias {}; //~ ERROR type `Priv` is private + m::Pub { 0: m::Alias {} }; //~ ERROR type `Priv` is private + m::Pub { 0: loop {} }; // OK, `m::Pub` is in value context, so it means Pub<_>, not Pub<Priv> + m::Pub::static_method; //~ ERROR type `Priv` is private + m::Pub::INHERENT_ASSOC_CONST; //~ ERROR type `Priv` is private + m::Pub(0u8).method_with_substs::<m::Alias>(); //~ ERROR type `Priv` is private + m::Pub(0u8).method_with_priv_params(loop{}); //~ ERROR type `Priv` is private + <m::Alias as m::TraitWithAssocConst>::TRAIT_ASSOC_CONST; //~ ERROR type `Priv` is private + <m::Pub<m::Alias>>::INHERENT_ASSOC_CONST; //~ ERROR type `Priv` is private + <m::Pub<m::Alias>>::INHERENT_ASSOC_CONST_GENERIC_SELF; //~ ERROR type `Priv` is private + <m::Pub<m::Alias>>::static_method_generic_self; //~ ERROR type `Priv` is private + use m::TraitWithTyParam2; + u8::pub_method; //~ ERROR type `Priv` is private + + adjust::S1.method_s3(); //~ ERROR type `S2` is private + + m::m!(); + + m::leak_anon1(); //~ ERROR trait `Trait` is private + m::leak_anon2(); //~ ERROR type `Priv` is private + m::leak_anon3(); //~ ERROR type `Priv` is private + + m::leak_dyn1(); //~ ERROR trait `Trait` is private + m::leak_dyn2(); //~ ERROR type `Priv` is private + m::leak_dyn3(); //~ ERROR type `Priv` is private + + // Check that messages are not duplicated for various kinds of assignments + let a = m::Alias {}; //~ ERROR type `Priv` is private + let mut b = a; //~ ERROR type `Priv` is private + b = a; //~ ERROR type `Priv` is private + match a { //~ ERROR type `Priv` is private + _ => {} + } +} diff --git a/tests/ui/privacy/private-inferred-type.stderr b/tests/ui/privacy/private-inferred-type.stderr new file mode 100644 index 000000000..fc3f9ab62 --- /dev/null +++ b/tests/ui/privacy/private-inferred-type.stderr @@ -0,0 +1,237 @@ +error[E0446]: private type `Priv` in public interface + --> $DIR/private-inferred-type.rs:61:36 + | +LL | struct Priv; + | ----------- `Priv` declared as private +... +LL | impl TraitWithAssocTy for u8 { type AssocTy = Priv; } + | ^^^^^^^^^^^^ can't leak private type + +error[E0446]: private type `S2` in public interface + --> $DIR/private-inferred-type.rs:83:9 + | +LL | struct S2; + | --------- `S2` declared as private +... +LL | type Target = S2Alias; + | ^^^^^^^^^^^ can't leak private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:97:9 + | +LL | let _: m::Alias; + | ^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:97:12 + | +LL | let _: m::Alias; + | ^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:99:13 + | +LL | let _: <m::Alias as m::TraitWithAssocTy>::AssocTy; + | ^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:100:5 + | +LL | m::Alias {}; + | ^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:101:5 + | +LL | m::Pub { 0: m::Alias {} }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:103:5 + | +LL | m::Pub::static_method; + | ^^^^^^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:104:5 + | +LL | m::Pub::INHERENT_ASSOC_CONST; + | ^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:105:5 + | +LL | m::Pub(0u8).method_with_substs::<m::Alias>(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:106:17 + | +LL | m::Pub(0u8).method_with_priv_params(loop{}); + | ^^^^^^^^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:107:5 + | +LL | <m::Alias as m::TraitWithAssocConst>::TRAIT_ASSOC_CONST; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:108:6 + | +LL | <m::Pub<m::Alias>>::INHERENT_ASSOC_CONST; + | ^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:109:5 + | +LL | <m::Pub<m::Alias>>::INHERENT_ASSOC_CONST_GENERIC_SELF; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:110:5 + | +LL | <m::Pub<m::Alias>>::static_method_generic_self; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:112:5 + | +LL | u8::pub_method; + | ^^^^^^^^^^^^^^ private type + +error: type `S2` is private + --> $DIR/private-inferred-type.rs:114:5 + | +LL | adjust::S1.method_s3(); + | ^^^^^^^^^^ private type + +error: type `fn() {priv_fn}` is private + --> $DIR/private-inferred-type.rs:39:9 + | +LL | priv_fn; + | ^^^^^^^ private type +... +LL | m::m!(); + | ------- in this macro invocation + | + = note: this error originates in the macro `m::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `PrivEnum` is private + --> $DIR/private-inferred-type.rs:41:9 + | +LL | PrivEnum::Variant; + | ^^^^^^^^^^^^^^^^^ private type +... +LL | m::m!(); + | ------- in this macro invocation + | + = note: this error originates in the macro `m::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `fn() {<u8 as PrivTrait>::method}` is private + --> $DIR/private-inferred-type.rs:43:9 + | +LL | <u8 as PrivTrait>::method; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ private type +... +LL | m::m!(); + | ------- in this macro invocation + | + = note: this error originates in the macro `m::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `fn(u8) -> PrivTupleStruct {PrivTupleStruct}` is private + --> $DIR/private-inferred-type.rs:45:9 + | +LL | PrivTupleStruct; + | ^^^^^^^^^^^^^^^ private type +... +LL | m::m!(); + | ------- in this macro invocation + | + = note: this error originates in the macro `m::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `fn(u8) -> PubTupleStruct {PubTupleStruct}` is private + --> $DIR/private-inferred-type.rs:47:9 + | +LL | PubTupleStruct; + | ^^^^^^^^^^^^^^ private type +... +LL | m::m!(); + | ------- in this macro invocation + | + = note: this error originates in the macro `m::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: type `for<'a> fn(&'a Pub<u8>) {Pub::<u8>::priv_method}` is private + --> $DIR/private-inferred-type.rs:49:18 + | +LL | Pub(0u8).priv_method(); + | ^^^^^^^^^^^ private type +... +LL | m::m!(); + | ------- in this macro invocation + | + = note: this error originates in the macro `m::m` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: trait `Trait` is private + --> $DIR/private-inferred-type.rs:118:5 + | +LL | m::leak_anon1(); + | ^^^^^^^^^^^^^^^ private trait + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:119:5 + | +LL | m::leak_anon2(); + | ^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:120:5 + | +LL | m::leak_anon3(); + | ^^^^^^^^^^^^^^^ private type + +error: trait `Trait` is private + --> $DIR/private-inferred-type.rs:122:5 + | +LL | m::leak_dyn1(); + | ^^^^^^^^^^^^^^ private trait + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:123:5 + | +LL | m::leak_dyn2(); + | ^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:124:5 + | +LL | m::leak_dyn3(); + | ^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:127:13 + | +LL | let a = m::Alias {}; + | ^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:128:17 + | +LL | let mut b = a; + | ^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:129:9 + | +LL | b = a; + | ^ private type + +error: type `Priv` is private + --> $DIR/private-inferred-type.rs:130:11 + | +LL | match a { + | ^ private type + +error: aborting due to 33 previous errors + +For more information about this error, try `rustc --explain E0446`. diff --git a/tests/ui/privacy/private-item-simple.rs b/tests/ui/privacy/private-item-simple.rs new file mode 100644 index 000000000..1f718ed92 --- /dev/null +++ b/tests/ui/privacy/private-item-simple.rs @@ -0,0 +1,7 @@ +mod a { + fn f() {} +} + +fn main() { + a::f(); //~ ERROR function `f` is private +} diff --git a/tests/ui/privacy/private-item-simple.stderr b/tests/ui/privacy/private-item-simple.stderr new file mode 100644 index 000000000..e3d90150e --- /dev/null +++ b/tests/ui/privacy/private-item-simple.stderr @@ -0,0 +1,15 @@ +error[E0603]: function `f` is private + --> $DIR/private-item-simple.rs:6:8 + | +LL | a::f(); + | ^ private function + | +note: the function `f` is defined here + --> $DIR/private-item-simple.rs:2:5 + | +LL | fn f() {} + | ^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0603`. diff --git a/tests/ui/privacy/private-method-cross-crate.rs b/tests/ui/privacy/private-method-cross-crate.rs new file mode 100644 index 000000000..ab3bbdfe4 --- /dev/null +++ b/tests/ui/privacy/private-method-cross-crate.rs @@ -0,0 +1,8 @@ +// aux-build:cci_class_5.rs +extern crate cci_class_5; +use cci_class_5::kitties::cat; + +fn main() { + let nyan : cat = cat(52, 99); + nyan.nap(); //~ ERROR associated function `nap` is private +} diff --git a/tests/ui/privacy/private-method-cross-crate.stderr b/tests/ui/privacy/private-method-cross-crate.stderr new file mode 100644 index 000000000..93f6a7f2f --- /dev/null +++ b/tests/ui/privacy/private-method-cross-crate.stderr @@ -0,0 +1,14 @@ +error[E0624]: associated function `nap` is private + --> $DIR/private-method-cross-crate.rs:7:8 + | +LL | nyan.nap(); + | ^^^ private associated function + | + ::: $DIR/auxiliary/cci_class_5.rs:8:9 + | +LL | fn nap(&self) {} + | ------------- private associated function defined here + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0624`. diff --git a/tests/ui/privacy/private-method-inherited.rs b/tests/ui/privacy/private-method-inherited.rs new file mode 100644 index 000000000..2f6454288 --- /dev/null +++ b/tests/ui/privacy/private-method-inherited.rs @@ -0,0 +1,14 @@ +// Tests that inherited visibility applies to methods. + +mod a { + pub struct Foo; + + impl Foo { + fn f(self) {} + } +} + +fn main() { + let x = a::Foo; + x.f(); //~ ERROR associated function `f` is private +} diff --git a/tests/ui/privacy/private-method-inherited.stderr b/tests/ui/privacy/private-method-inherited.stderr new file mode 100644 index 000000000..011a7fee4 --- /dev/null +++ b/tests/ui/privacy/private-method-inherited.stderr @@ -0,0 +1,12 @@ +error[E0624]: associated function `f` is private + --> $DIR/private-method-inherited.rs:13:7 + | +LL | fn f(self) {} + | ---------- private associated function defined here +... +LL | x.f(); + | ^ private associated function + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0624`. diff --git a/tests/ui/privacy/private-method-rpass.rs b/tests/ui/privacy/private-method-rpass.rs new file mode 100644 index 000000000..726944fb2 --- /dev/null +++ b/tests/ui/privacy/private-method-rpass.rs @@ -0,0 +1,34 @@ +// run-pass +#![allow(dead_code)] +#![allow(non_camel_case_types)] + +// pretty-expanded FIXME #23616 + +struct cat { + meows : usize, + + how_hungry : isize, +} + +impl cat { + pub fn play(&mut self) { + self.meows += 1_usize; + self.nap(); + } +} + +impl cat { + fn nap(&mut self) { for _ in 1_usize..10_usize { } } +} + +fn cat(in_x : usize, in_y : isize) -> cat { + cat { + meows: in_x, + how_hungry: in_y + } +} + +pub fn main() { + let mut nyan : cat = cat(52_usize, 99); + nyan.play(); +} diff --git a/tests/ui/privacy/private-method.rs b/tests/ui/privacy/private-method.rs new file mode 100644 index 000000000..76a642cde --- /dev/null +++ b/tests/ui/privacy/private-method.rs @@ -0,0 +1,23 @@ +mod kitties { + pub struct Cat { + meows : usize, + + how_hungry : isize, + } + + impl Cat { + fn nap(&self) {} + } + + pub fn cat(in_x : usize, in_y : isize) -> Cat { + Cat { + meows: in_x, + how_hungry: in_y + } + } +} + +fn main() { + let nyan : kitties::Cat = kitties::cat(52, 99); + nyan.nap(); //~ ERROR associated function `nap` is private +} diff --git a/tests/ui/privacy/private-method.stderr b/tests/ui/privacy/private-method.stderr new file mode 100644 index 000000000..17c7179dc --- /dev/null +++ b/tests/ui/privacy/private-method.stderr @@ -0,0 +1,12 @@ +error[E0624]: associated function `nap` is private + --> $DIR/private-method.rs:22:8 + | +LL | fn nap(&self) {} + | ------------- private associated function defined here +... +LL | nyan.nap(); + | ^^^ private associated function + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0624`. diff --git a/tests/ui/privacy/private-struct-field-cross-crate.rs b/tests/ui/privacy/private-struct-field-cross-crate.rs new file mode 100644 index 000000000..301cd37b7 --- /dev/null +++ b/tests/ui/privacy/private-struct-field-cross-crate.rs @@ -0,0 +1,9 @@ +// aux-build:cci_class.rs +extern crate cci_class; +use cci_class::kitties::cat; + +fn main() { + let nyan : cat = cat(52, 99); + assert_eq!(nyan.meows, 52); + //~^ ERROR field `meows` of struct `cat` is private +} diff --git a/tests/ui/privacy/private-struct-field-cross-crate.stderr b/tests/ui/privacy/private-struct-field-cross-crate.stderr new file mode 100644 index 000000000..40cf3448d --- /dev/null +++ b/tests/ui/privacy/private-struct-field-cross-crate.stderr @@ -0,0 +1,9 @@ +error[E0616]: field `meows` of struct `cat` is private + --> $DIR/private-struct-field-cross-crate.rs:7:19 + | +LL | assert_eq!(nyan.meows, 52); + | ^^^^^ private field + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0616`. diff --git a/tests/ui/privacy/private-struct-field-ctor.rs b/tests/ui/privacy/private-struct-field-ctor.rs new file mode 100644 index 000000000..56e84a751 --- /dev/null +++ b/tests/ui/privacy/private-struct-field-ctor.rs @@ -0,0 +1,9 @@ +mod a { + pub struct Foo { + x: isize + } +} + +fn main() { + let s = a::Foo { x: 1 }; //~ ERROR field `x` of struct `Foo` is private +} diff --git a/tests/ui/privacy/private-struct-field-ctor.stderr b/tests/ui/privacy/private-struct-field-ctor.stderr new file mode 100644 index 000000000..9dc9db0ea --- /dev/null +++ b/tests/ui/privacy/private-struct-field-ctor.stderr @@ -0,0 +1,9 @@ +error[E0451]: field `x` of struct `Foo` is private + --> $DIR/private-struct-field-ctor.rs:8:22 + | +LL | let s = a::Foo { x: 1 }; + | ^^^^ private field + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0451`. diff --git a/tests/ui/privacy/private-struct-field-pattern.rs b/tests/ui/privacy/private-struct-field-pattern.rs new file mode 100644 index 000000000..4a766500e --- /dev/null +++ b/tests/ui/privacy/private-struct-field-pattern.rs @@ -0,0 +1,17 @@ +use a::Foo; + +mod a { + pub struct Foo { + x: isize + } + + pub fn make() -> Foo { + Foo { x: 3 } + } +} + +fn main() { + match a::make() { + Foo { x: _ } => {} //~ ERROR field `x` of struct `Foo` is private + } +} diff --git a/tests/ui/privacy/private-struct-field-pattern.stderr b/tests/ui/privacy/private-struct-field-pattern.stderr new file mode 100644 index 000000000..630553036 --- /dev/null +++ b/tests/ui/privacy/private-struct-field-pattern.stderr @@ -0,0 +1,9 @@ +error[E0451]: field `x` of struct `Foo` is private + --> $DIR/private-struct-field-pattern.rs:15:15 + | +LL | Foo { x: _ } => {} + | ^^^^ private field + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0451`. diff --git a/tests/ui/privacy/private-struct-field.rs b/tests/ui/privacy/private-struct-field.rs new file mode 100644 index 000000000..94cee4eff --- /dev/null +++ b/tests/ui/privacy/private-struct-field.rs @@ -0,0 +1,14 @@ +mod cat { + pub struct Cat { + meows: usize + } + + pub fn new_cat() -> Cat { + Cat { meows: 52 } + } +} + +fn main() { + let nyan = cat::new_cat(); + assert_eq!(nyan.meows, 52); //~ ERROR field `meows` of struct `Cat` is private +} diff --git a/tests/ui/privacy/private-struct-field.stderr b/tests/ui/privacy/private-struct-field.stderr new file mode 100644 index 000000000..facf4e82f --- /dev/null +++ b/tests/ui/privacy/private-struct-field.stderr @@ -0,0 +1,9 @@ +error[E0616]: field `meows` of struct `Cat` is private + --> $DIR/private-struct-field.rs:13:21 + | +LL | assert_eq!(nyan.meows, 52); + | ^^^^^ private field + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0616`. diff --git a/tests/ui/privacy/private-type-in-interface.rs b/tests/ui/privacy/private-type-in-interface.rs new file mode 100644 index 000000000..7fbdbaf5f --- /dev/null +++ b/tests/ui/privacy/private-type-in-interface.rs @@ -0,0 +1,30 @@ +// aux-build:private-inferred-type.rs + +#![allow(warnings)] + +extern crate private_inferred_type as ext; + +mod m { + struct Priv; + pub type Alias = Priv; + + pub trait Trait { type X; } + impl Trait for Priv { type X = u8; } +} + +fn f(_: m::Alias) {} //~ ERROR type `Priv` is private + //~^ ERROR type `Priv` is private +fn f_ext(_: ext::Alias) {} //~ ERROR type `ext::Priv` is private + //~^ ERROR type `ext::Priv` is private + +trait Tr1 {} +impl m::Alias {} //~ ERROR type `Priv` is private +impl Tr1 for ext::Alias {} //~ ERROR type `ext::Priv` is private +type A = <m::Alias as m::Trait>::X; //~ ERROR type `Priv` is private + +trait Tr2<T> {} +impl<T> Tr2<T> for u8 {} +fn g() -> impl Tr2<m::Alias> { 0 } //~ ERROR type `Priv` is private +fn g_ext() -> impl Tr2<ext::Alias> { 0 } //~ ERROR type `ext::Priv` is private + +fn main() {} diff --git a/tests/ui/privacy/private-type-in-interface.stderr b/tests/ui/privacy/private-type-in-interface.stderr new file mode 100644 index 000000000..4e87caa34 --- /dev/null +++ b/tests/ui/privacy/private-type-in-interface.stderr @@ -0,0 +1,56 @@ +error: type `Priv` is private + --> $DIR/private-type-in-interface.rs:15:9 + | +LL | fn f(_: m::Alias) {} + | ^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-type-in-interface.rs:15:6 + | +LL | fn f(_: m::Alias) {} + | ^ private type + +error: type `ext::Priv` is private + --> $DIR/private-type-in-interface.rs:17:13 + | +LL | fn f_ext(_: ext::Alias) {} + | ^^^^^^^^^^ private type + +error: type `ext::Priv` is private + --> $DIR/private-type-in-interface.rs:17:10 + | +LL | fn f_ext(_: ext::Alias) {} + | ^ private type + +error: type `Priv` is private + --> $DIR/private-type-in-interface.rs:21:6 + | +LL | impl m::Alias {} + | ^^^^^^^^ private type + +error: type `ext::Priv` is private + --> $DIR/private-type-in-interface.rs:22:14 + | +LL | impl Tr1 for ext::Alias {} + | ^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-type-in-interface.rs:23:10 + | +LL | type A = <m::Alias as m::Trait>::X; + | ^^^^^^^^^^^^^^^^^^^^^^^^^ private type + +error: type `Priv` is private + --> $DIR/private-type-in-interface.rs:27:11 + | +LL | fn g() -> impl Tr2<m::Alias> { 0 } + | ^^^^^^^^^^^^^^^^^^ private type + +error: type `ext::Priv` is private + --> $DIR/private-type-in-interface.rs:28:15 + | +LL | fn g_ext() -> impl Tr2<ext::Alias> { 0 } + | ^^^^^^^^^^^^^^^^^^^^ private type + +error: aborting due to 9 previous errors + diff --git a/tests/ui/privacy/private-variant-reexport.rs b/tests/ui/privacy/private-variant-reexport.rs new file mode 100644 index 000000000..688284460 --- /dev/null +++ b/tests/ui/privacy/private-variant-reexport.rs @@ -0,0 +1,20 @@ +mod m1 { + pub use ::E::V; //~ ERROR `V` is only public within the crate, and cannot be re-exported outside +} + +mod m2 { + pub use ::E::{V}; //~ ERROR `V` is only public within the crate, and cannot be re-exported outside +} + +mod m3 { + pub use ::E::V::{self}; //~ ERROR `V` is only public within the crate, and cannot be re-exported outside +} + +#[deny(unused_imports)] +mod m4 { + pub use ::E::*; //~ ERROR glob import doesn't reexport anything +} + +enum E { V } + +fn main() {} diff --git a/tests/ui/privacy/private-variant-reexport.stderr b/tests/ui/privacy/private-variant-reexport.stderr new file mode 100644 index 000000000..78771ee30 --- /dev/null +++ b/tests/ui/privacy/private-variant-reexport.stderr @@ -0,0 +1,48 @@ +error[E0364]: `V` is only public within the crate, and cannot be re-exported outside + --> $DIR/private-variant-reexport.rs:2:13 + | +LL | pub use ::E::V; + | ^^^^^^ + | +note: consider marking `V` as `pub` in the imported module + --> $DIR/private-variant-reexport.rs:2:13 + | +LL | pub use ::E::V; + | ^^^^^^ + +error[E0364]: `V` is only public within the crate, and cannot be re-exported outside + --> $DIR/private-variant-reexport.rs:6:19 + | +LL | pub use ::E::{V}; + | ^ + | +note: consider marking `V` as `pub` in the imported module + --> $DIR/private-variant-reexport.rs:6:19 + | +LL | pub use ::E::{V}; + | ^ + +error[E0365]: `V` is only public within the crate, and cannot be re-exported outside + --> $DIR/private-variant-reexport.rs:10:22 + | +LL | pub use ::E::V::{self}; + | ^^^^ re-export of crate public `V` + | + = note: consider declaring type or module `V` with `pub` + +error: glob import doesn't reexport anything because no candidate is public enough + --> $DIR/private-variant-reexport.rs:15:13 + | +LL | pub use ::E::*; + | ^^^^^^ + | +note: the lint level is defined here + --> $DIR/private-variant-reexport.rs:13:8 + | +LL | #[deny(unused_imports)] + | ^^^^^^^^^^^^^^ + +error: aborting due to 4 previous errors + +Some errors have detailed explanations: E0364, E0365. +For more information about an error, try `rustc --explain E0364`. diff --git a/tests/ui/privacy/pub-extern-privacy.rs b/tests/ui/privacy/pub-extern-privacy.rs new file mode 100644 index 000000000..dbbbe4e3b --- /dev/null +++ b/tests/ui/privacy/pub-extern-privacy.rs @@ -0,0 +1,18 @@ +// run-pass +// ignore-wasm32-bare no libc to test ffi with + +// pretty-expanded FIXME #23616 + +use std::mem::transmute; + +mod a { + extern "C" { + pub fn free(x: *const u8); + } +} + +pub fn main() { + unsafe { + a::free(transmute(0_usize)); + } +} diff --git a/tests/ui/privacy/pub-priv-dep/auxiliary/priv_dep.rs b/tests/ui/privacy/pub-priv-dep/auxiliary/priv_dep.rs new file mode 100644 index 000000000..e7afeb84f --- /dev/null +++ b/tests/ui/privacy/pub-priv-dep/auxiliary/priv_dep.rs @@ -0,0 +1,2 @@ +pub struct OtherType; +pub trait OtherTrait {} diff --git a/tests/ui/privacy/pub-priv-dep/auxiliary/pub_dep.rs b/tests/ui/privacy/pub-priv-dep/auxiliary/pub_dep.rs new file mode 100644 index 000000000..3ebafd953 --- /dev/null +++ b/tests/ui/privacy/pub-priv-dep/auxiliary/pub_dep.rs @@ -0,0 +1 @@ +pub struct PubType; diff --git a/tests/ui/privacy/pub-priv-dep/pub-priv1.rs b/tests/ui/privacy/pub-priv-dep/pub-priv1.rs new file mode 100644 index 000000000..ec8666f93 --- /dev/null +++ b/tests/ui/privacy/pub-priv-dep/pub-priv1.rs @@ -0,0 +1,44 @@ +// aux-crate:priv:priv_dep=priv_dep.rs +// aux-build:pub_dep.rs +// compile-flags: -Zunstable-options +#![deny(exported_private_dependencies)] + +// This crate is a private dependency +extern crate priv_dep; +// This crate is a public dependency +extern crate pub_dep; + +use priv_dep::{OtherTrait, OtherType}; +use pub_dep::PubType; + +// Type from private dependency used in private +// type - this is fine +struct PrivateType { + field: OtherType, +} + +pub struct PublicType { + pub field: OtherType, + //~^ ERROR type `OtherType` from private dependency 'priv_dep' in public interface + priv_field: OtherType, // Private field - this is fine + pub other_field: PubType, // Type from public dependency - this is fine +} + +impl PublicType { + pub fn pub_fn(param: OtherType) {} + //~^ ERROR type `OtherType` from private dependency 'priv_dep' in public interface + + fn priv_fn(param: OtherType) {} +} + +pub trait MyPubTrait { + type Foo: OtherTrait; +} +//~^^ ERROR trait `OtherTrait` from private dependency 'priv_dep' in public interface + +pub struct AllowedPrivType { + #[allow(exported_private_dependencies)] + pub allowed: OtherType, +} + +fn main() {} diff --git a/tests/ui/privacy/pub-priv-dep/pub-priv1.stderr b/tests/ui/privacy/pub-priv-dep/pub-priv1.stderr new file mode 100644 index 000000000..e62a440d8 --- /dev/null +++ b/tests/ui/privacy/pub-priv-dep/pub-priv1.stderr @@ -0,0 +1,26 @@ +error: type `OtherType` from private dependency 'priv_dep' in public interface + --> $DIR/pub-priv1.rs:21:5 + | +LL | pub field: OtherType, + | ^^^^^^^^^^^^^^^^^^^^ + | +note: the lint level is defined here + --> $DIR/pub-priv1.rs:4:9 + | +LL | #![deny(exported_private_dependencies)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: type `OtherType` from private dependency 'priv_dep' in public interface + --> $DIR/pub-priv1.rs:28:5 + | +LL | pub fn pub_fn(param: OtherType) {} + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: trait `OtherTrait` from private dependency 'priv_dep' in public interface + --> $DIR/pub-priv1.rs:35:5 + | +LL | type Foo: OtherTrait; + | ^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 3 previous errors + diff --git a/tests/ui/privacy/pub-priv-dep/std-pub.rs b/tests/ui/privacy/pub-priv-dep/std-pub.rs new file mode 100644 index 000000000..e25aa93a0 --- /dev/null +++ b/tests/ui/privacy/pub-priv-dep/std-pub.rs @@ -0,0 +1,12 @@ +// The 'std' crates should always be implicitly public, +// without having to pass any compiler arguments + +// run-pass + +#![deny(exported_private_dependencies)] + +pub struct PublicType { + pub field: Option<u8> +} + +fn main() {} diff --git a/tests/ui/privacy/pub-use-xcrate.rs b/tests/ui/privacy/pub-use-xcrate.rs new file mode 100644 index 000000000..e8a6e8cf1 --- /dev/null +++ b/tests/ui/privacy/pub-use-xcrate.rs @@ -0,0 +1,15 @@ +// run-pass +// aux-build:pub_use_xcrate1.rs +// aux-build:pub_use_xcrate2.rs + +// pretty-expanded FIXME #23616 + +extern crate pub_use_xcrate2; + +use pub_use_xcrate2::Foo; + +pub fn main() { + let _foo: Foo = Foo { + name: 0 + }; +} diff --git a/tests/ui/privacy/pub_use_mods_xcrate_exe.rs b/tests/ui/privacy/pub_use_mods_xcrate_exe.rs new file mode 100644 index 000000000..f163619e7 --- /dev/null +++ b/tests/ui/privacy/pub_use_mods_xcrate_exe.rs @@ -0,0 +1,11 @@ +// run-pass +// aux-build:pub_use_mods_xcrate.rs + +// pretty-expanded FIXME #23616 + +#![allow(unused_imports)] + +extern crate pub_use_mods_xcrate; +use pub_use_mods_xcrate::a::c; + +pub fn main(){} diff --git a/tests/ui/privacy/reachable-unnameable-items.rs b/tests/ui/privacy/reachable-unnameable-items.rs new file mode 100644 index 000000000..1babe0119 --- /dev/null +++ b/tests/ui/privacy/reachable-unnameable-items.rs @@ -0,0 +1,31 @@ +// run-pass +// needs-unwind +// aux-build:reachable-unnameable-items.rs + +extern crate reachable_unnameable_items; +use reachable_unnameable_items::*; + +fn main() { + let res1 = function_returning_unnameable_type().method_of_unnameable_type1(); + let res2 = CONSTANT_OF_UNNAMEABLE_TYPE.method_of_unnameable_type2(); + let res4 = AliasOfUnnameableType{}.method_of_unnameable_type4(); + let res5 = function_returning_unnameable_type().inherent_method_returning_unnameable_type(). + method_of_unnameable_type5(); + let res6 = function_returning_unnameable_type().trait_method_returning_unnameable_type(). + method_of_unnameable_type6(); + let res7 = STATIC.field_of_unnameable_type.method_of_unnameable_type7(); + let res8 = generic_function::<AliasOfUnnameableType>().method_of_unnameable_type8(); + let res_enum = NameableVariant.method_of_unnameable_enum(); + assert_eq!(res1, "Hello1"); + assert_eq!(res2, "Hello2"); + assert_eq!(res4, "Hello4"); + assert_eq!(res5, "Hello5"); + assert_eq!(res6, "Hello6"); + assert_eq!(res7, "Hello7"); + assert_eq!(res8, "Hello8"); + assert_eq!(res_enum, "HelloEnum"); + + let none = None; + function_accepting_unnameable_type(none); + let _guard = std::panic::catch_unwind(|| none.unwrap().method_of_unnameable_type3()); +} 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`. diff --git a/tests/ui/privacy/suggest-making-field-public.fixed b/tests/ui/privacy/suggest-making-field-public.fixed new file mode 100644 index 000000000..78e335b3d --- /dev/null +++ b/tests/ui/privacy/suggest-making-field-public.fixed @@ -0,0 +1,15 @@ +// run-rustfix +mod a { + pub struct A(pub String); +} + +mod b { + use crate::a::A; + pub fn x() { + A("".into()); //~ ERROR cannot initialize a tuple struct which contains private fields + } +} +fn main() { + a::A("a".into()); //~ ERROR tuple struct constructor `A` is private + b::x(); +} diff --git a/tests/ui/privacy/suggest-making-field-public.rs b/tests/ui/privacy/suggest-making-field-public.rs new file mode 100644 index 000000000..b65c801d1 --- /dev/null +++ b/tests/ui/privacy/suggest-making-field-public.rs @@ -0,0 +1,15 @@ +// run-rustfix +mod a { + pub struct A(pub(self)String); +} + +mod b { + use crate::a::A; + pub fn x() { + A("".into()); //~ ERROR cannot initialize a tuple struct which contains private fields + } +} +fn main() { + a::A("a".into()); //~ ERROR tuple struct constructor `A` is private + b::x(); +} diff --git a/tests/ui/privacy/suggest-making-field-public.stderr b/tests/ui/privacy/suggest-making-field-public.stderr new file mode 100644 index 000000000..e92e9aae3 --- /dev/null +++ b/tests/ui/privacy/suggest-making-field-public.stderr @@ -0,0 +1,39 @@ +error[E0603]: tuple struct constructor `A` is private + --> $DIR/suggest-making-field-public.rs:13:8 + | +LL | pub struct A(pub(self)String); + | --------------- a constructor is private if any of the fields is private +... +LL | a::A("a".into()); + | ^ private tuple struct constructor + | +note: the tuple struct constructor `A` is defined here + --> $DIR/suggest-making-field-public.rs:3:5 + | +LL | pub struct A(pub(self)String); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider making the field publicly accessible + | +LL | pub struct A(pub String); + | ~~~ + +error[E0423]: cannot initialize a tuple struct which contains private fields + --> $DIR/suggest-making-field-public.rs:9:9 + | +LL | A("".into()); + | ^ + | +note: constructor is not visible here due to private fields + --> $DIR/suggest-making-field-public.rs:3:18 + | +LL | pub struct A(pub(self)String); + | ^^^^^^^^^^^^^^^ private field +help: consider making the field publicly accessible + | +LL | pub struct A(pub String); + | ~~~ + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0423, E0603. +For more information about an error, try `rustc --explain E0423`. diff --git a/tests/ui/privacy/union-field-privacy-1.rs b/tests/ui/privacy/union-field-privacy-1.rs new file mode 100644 index 000000000..8a84bd86a --- /dev/null +++ b/tests/ui/privacy/union-field-privacy-1.rs @@ -0,0 +1,17 @@ +mod m { + pub union U { + pub a: u8, + pub(super) b: u8, + c: u8, + } +} + +fn main() { unsafe { + let u = m::U { a: 0 }; // OK + let u = m::U { b: 0 }; // OK + let u = m::U { c: 0 }; //~ ERROR field `c` of union `U` is private + + let m::U { a } = u; // OK + let m::U { b } = u; // OK + let m::U { c } = u; //~ ERROR field `c` of union `U` is private +}} diff --git a/tests/ui/privacy/union-field-privacy-1.stderr b/tests/ui/privacy/union-field-privacy-1.stderr new file mode 100644 index 000000000..b1f0b785e --- /dev/null +++ b/tests/ui/privacy/union-field-privacy-1.stderr @@ -0,0 +1,15 @@ +error[E0451]: field `c` of union `U` is private + --> $DIR/union-field-privacy-1.rs:12:20 + | +LL | let u = m::U { c: 0 }; + | ^^^^ private field + +error[E0451]: field `c` of union `U` is private + --> $DIR/union-field-privacy-1.rs:16:16 + | +LL | let m::U { c } = u; + | ^ private field + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0451`. diff --git a/tests/ui/privacy/union-field-privacy-2.rs b/tests/ui/privacy/union-field-privacy-2.rs new file mode 100644 index 000000000..f02e0f8a9 --- /dev/null +++ b/tests/ui/privacy/union-field-privacy-2.rs @@ -0,0 +1,15 @@ +mod m { + pub union U { + pub a: u8, + pub(super) b: u8, + c: u8, + } +} + +fn main() { + let u = m::U { a: 10 }; + + let a = u.a; // OK + let b = u.b; // OK + let c = u.c; //~ ERROR field `c` of union `U` is private +} diff --git a/tests/ui/privacy/union-field-privacy-2.stderr b/tests/ui/privacy/union-field-privacy-2.stderr new file mode 100644 index 000000000..bf6a2b625 --- /dev/null +++ b/tests/ui/privacy/union-field-privacy-2.stderr @@ -0,0 +1,9 @@ +error[E0616]: field `c` of union `U` is private + --> $DIR/union-field-privacy-2.rs:14:15 + | +LL | let c = u.c; + | ^ private field + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0616`. diff --git a/tests/ui/privacy/useless-pub.rs b/tests/ui/privacy/useless-pub.rs new file mode 100644 index 000000000..fde7cd5d8 --- /dev/null +++ b/tests/ui/privacy/useless-pub.rs @@ -0,0 +1,16 @@ +struct A { pub i: isize } + +pub trait E { + fn foo(&self); +} + +impl E for A { + pub fn foo(&self) {} //~ ERROR: unnecessary visibility qualifier +} + +enum Foo { + V1 { pub f: i32 }, //~ ERROR unnecessary visibility qualifier + V2(pub i32), //~ ERROR unnecessary visibility qualifier +} + +fn main() {} diff --git a/tests/ui/privacy/useless-pub.stderr b/tests/ui/privacy/useless-pub.stderr new file mode 100644 index 000000000..14c4983ae --- /dev/null +++ b/tests/ui/privacy/useless-pub.stderr @@ -0,0 +1,21 @@ +error[E0449]: unnecessary visibility qualifier + --> $DIR/useless-pub.rs:8:5 + | +LL | pub fn foo(&self) {} + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/useless-pub.rs:12:10 + | +LL | V1 { pub f: i32 }, + | ^^^ `pub` not permitted here because it's implied + +error[E0449]: unnecessary visibility qualifier + --> $DIR/useless-pub.rs:13:8 + | +LL | V2(pub i32), + | ^^^ `pub` not permitted here because it's implied + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0449`. diff --git a/tests/ui/privacy/where-priv-type.rs b/tests/ui/privacy/where-priv-type.rs new file mode 100644 index 000000000..66ee9c4bb --- /dev/null +++ b/tests/ui/privacy/where-priv-type.rs @@ -0,0 +1,90 @@ +// priv-in-pub lint tests where the private type appears in the +// `where` clause of a public item + +#![crate_type = "lib"] +#![feature(generic_const_exprs)] +#![allow(incomplete_features)] + + +struct PrivTy; +trait PrivTr {} +pub struct PubTy; +pub struct PubTyGeneric<T>(T); +pub trait PubTr {} +impl PubTr for PrivTy {} +pub trait PubTrWithAssocTy { type AssocTy; } +impl PubTrWithAssocTy for PrivTy { type AssocTy = PrivTy; } + + +pub struct S +//~^ WARNING private type `PrivTy` in public interface +//~| WARNING hard error +where + PrivTy: +{} + + +pub enum E +//~^ WARNING private type `PrivTy` in public interface +//~| WARNING hard error +where + PrivTy: +{} + + +pub fn f() +//~^ WARNING private type `PrivTy` in public interface +//~| WARNING hard error +where + PrivTy: +{} + + +impl S +//~^ ERROR private type `PrivTy` in public interface +where + PrivTy: +{ + pub fn f() + //~^ WARNING private type `PrivTy` in public interface + //~| WARNING hard error + where + PrivTy: + {} +} + + +impl PubTr for PubTy +where + PrivTy: +{} + + +impl<T> PubTr for PubTyGeneric<T> +where + T: PubTrWithAssocTy<AssocTy=PrivTy> +{} + + +pub struct Const<const U: u8>; + +pub trait Trait { + type AssocTy; + fn assoc_fn() -> Self::AssocTy; +} + +impl<const U: u8> Trait for Const<U> +where + Const<{ my_const_fn(U) }>: , +{ + type AssocTy = Const<{ my_const_fn(U) }>; + //~^ ERROR private type + fn assoc_fn() -> Self::AssocTy { + Const + } +} + +const fn my_const_fn(val: u8) -> u8 { + // body of this function doesn't matter + val +} diff --git a/tests/ui/privacy/where-priv-type.stderr b/tests/ui/privacy/where-priv-type.stderr new file mode 100644 index 000000000..c5fb2cdb0 --- /dev/null +++ b/tests/ui/privacy/where-priv-type.stderr @@ -0,0 +1,66 @@ +warning: private type `PrivTy` in public interface (error E0446) + --> $DIR/where-priv-type.rs:19:1 + | +LL | pub struct S + | ^^^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + = note: `#[warn(private_in_public)]` on by default + +warning: private type `PrivTy` in public interface (error E0446) + --> $DIR/where-priv-type.rs:27:1 + | +LL | pub enum E + | ^^^^^^^^^^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +warning: private type `PrivTy` in public interface (error E0446) + --> $DIR/where-priv-type.rs:35:1 + | +LL | / pub fn f() +LL | | +LL | | +LL | | where +LL | | PrivTy: + | |___________^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error[E0446]: private type `PrivTy` in public interface + --> $DIR/where-priv-type.rs:43:1 + | +LL | struct PrivTy; + | ------------- `PrivTy` declared as private +... +LL | impl S + | ^^^^^^ can't leak private type + +warning: private type `PrivTy` in public interface (error E0446) + --> $DIR/where-priv-type.rs:48:5 + | +LL | / pub fn f() +LL | | +LL | | +LL | | where +LL | | PrivTy: + | |_______________^ + | + = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release! + = note: for more information, see issue #34537 <https://github.com/rust-lang/rust/issues/34537> + +error[E0446]: private type `fn(u8) -> u8 {my_const_fn}` in public interface + --> $DIR/where-priv-type.rs:80:5 + | +LL | type AssocTy = Const<{ my_const_fn(U) }>; + | ^^^^^^^^^^^^ can't leak private type +... +LL | const fn my_const_fn(val: u8) -> u8 { + | ----------------------------------- `fn(u8) -> u8 {my_const_fn}` declared as private + +error: aborting due to 2 previous errors; 4 warnings emitted + +For more information about this error, try `rustc --explain E0446`. diff --git a/tests/ui/privacy/where-pub-type-impls-priv-trait.rs b/tests/ui/privacy/where-pub-type-impls-priv-trait.rs new file mode 100644 index 000000000..87c211df1 --- /dev/null +++ b/tests/ui/privacy/where-pub-type-impls-priv-trait.rs @@ -0,0 +1,56 @@ +// priv-in-pub lint tests where the private trait bounds a public type + +#![crate_type = "lib"] +#![feature(generic_const_exprs)] +#![allow(incomplete_features)] + + +struct PrivTy; +trait PrivTr {} +pub struct PubTy; +pub struct PubTyGeneric<T>(T); +pub trait PubTr {} +impl PubTr for PrivTy {} +impl PrivTr for PubTy {} +pub trait PubTrWithAssocTy { type AssocTy; } +impl PubTrWithAssocTy for PrivTy { type AssocTy = PrivTy; } + + +pub struct S +//~^ ERROR private trait `PrivTr` in public interface +where + PubTy: PrivTr +{} + + +pub enum E +//~^ ERROR private trait `PrivTr` in public interface +where + PubTy: PrivTr +{} + + +pub fn f() +//~^ ERROR private trait `PrivTr` in public interface +where + PubTy: PrivTr +{} + + +impl S +//~^ ERROR private trait `PrivTr` in public interface +where + PubTy: PrivTr +{ + pub fn f() + //~^ ERROR private trait `PrivTr` in public interface + where + PubTy: PrivTr + {} +} + + +impl PubTr for PubTy +where + PubTy: PrivTr +{} diff --git a/tests/ui/privacy/where-pub-type-impls-priv-trait.stderr b/tests/ui/privacy/where-pub-type-impls-priv-trait.stderr new file mode 100644 index 000000000..a433cebbb --- /dev/null +++ b/tests/ui/privacy/where-pub-type-impls-priv-trait.stderr @@ -0,0 +1,54 @@ +error[E0445]: private trait `PrivTr` in public interface + --> $DIR/where-pub-type-impls-priv-trait.rs:19:1 + | +LL | trait PrivTr {} + | ------------ `PrivTr` declared as private +... +LL | pub struct S + | ^^^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `PrivTr` in public interface + --> $DIR/where-pub-type-impls-priv-trait.rs:26:1 + | +LL | trait PrivTr {} + | ------------ `PrivTr` declared as private +... +LL | pub enum E + | ^^^^^^^^^^ can't leak private trait + +error[E0445]: private trait `PrivTr` in public interface + --> $DIR/where-pub-type-impls-priv-trait.rs:33:1 + | +LL | trait PrivTr {} + | ------------ `PrivTr` declared as private +... +LL | / pub fn f() +LL | | +LL | | where +LL | | PubTy: PrivTr + | |_________________^ can't leak private trait + +error[E0445]: private trait `PrivTr` in public interface + --> $DIR/where-pub-type-impls-priv-trait.rs:40:1 + | +LL | trait PrivTr {} + | ------------ `PrivTr` declared as private +... +LL | impl S + | ^^^^^^ can't leak private trait + +error[E0445]: private trait `PrivTr` in public interface + --> $DIR/where-pub-type-impls-priv-trait.rs:45:5 + | +LL | trait PrivTr {} + | ------------ `PrivTr` declared as private +... +LL | / pub fn f() +LL | | +LL | | where +LL | | PubTy: PrivTr + | |_____________________^ can't leak private trait + +error: aborting due to 5 previous errors + +For more information about this error, try `rustc --explain E0445`. |